예제 #1
0
        public void AsDecimal_with_valid_decimal()
        {
            JsonValue jsonValue = JsonValue.NewString("123.456");
            decimal   result    = jsonValue.AsDecimal();

            Assert.AreEqual(123.456, result);
        }
예제 #2
0
        public static object ConvertJsonValueToObject(JsonValue value)
        {
            if (value.IsNull)
            {
                return(null);
            }

            if (value.IsBoolean)
            {
                return(value.AsBoolean());
            }

            if (value.IsFloat || value.IsNumber)
            {
                return(value.AsDecimal());
            }

            if (value.IsString)
            {
                return(value.AsString());
            }

            if (value.IsArray)
            {
                return(value.AsArray().Select(ConvertJsonValueToObject).ToArray());
            }

            if (!value.IsRecord)
            {
                throw new NotImplementedException($"There's not implementation for JsonValue tag {value.Tag}");
            }

            var record = (JsonValue.Record)value;

            return(record.properties.ToDictionary(kvPair => kvPair.Item1, kvPair => ConvertJsonValueToObject(kvPair.Item2)));
        }