Exemple #1
0
 private void ReadNull(ref JReader r, ValueVariant variant)
 {
     if (variant == NullableBoolIsNull)
     {
         RequireNull(r.BoolOrNull());
     }
     else if (variant == NullableIntIsNull)
     {
         RequireNull(r.IntOrNull());
     }
     else if (variant == NullableLongIsNull)
     {
         RequireNull(r.LongOrNull());
     }
     else if (variant == NullableDoubleIsNull)
     {
         RequireNull(r.DoubleOrNull());
     }
     else if (variant == NullableStringIsNull)
     {
         Assert.True(r.StringOrNull() == null, "expected null");
     }
     else if (variant == NullableArrayIsNull)
     {
         Assert.False(r.ArrayOrNull().IsDefined, "expected null");
     }
     else if (variant == NullableObjectIsNull)
     {
         Assert.False(r.ObjectOrNull().IsDefined, "expected null");
     }
     else
     {
         r.Null();
     }
 }
Exemple #2
0
            private void ReadAnyValue(ref JReader r, TestValue <ReaderAction> value)
            {
                var av = r.Any();

                Assert.Equal(value.Type, av.Type);
                switch (value.Type)
                {
                case ValueType.Bool:
                    Assert.Equal(value.BoolValue, av.BoolValue);
                    break;

                case ValueType.Number:
                    Assert.Equal(value.NumberValue, av.NumberValue);
                    break;

                case ValueType.String:
                    Assert.Equal(value.StringValue, av.StringValue.ToString());
                    break;

                case ValueType.Array:
                    ReadArray(ref r, av.ArrayValue, value);
                    break;

                case ValueType.Object:
                    ReadObject(ref r, av.ObjectValue, value);
                    break;

                default:
                    break;
                }
            }
            public object ReadJson(ref JReader reader)
            {
                var value = reader.Any();

                switch (value.Type)
                {
                case ValueType.Bool:
                    return(value.BoolValue);

                case ValueType.Number:
                    return(value.NumberValue);

                case ValueType.String:
                    return(value.StringValue);

                case ValueType.Array:
                    var list = new List <object>();
                    for (var arr = value.ArrayValue; arr.Next(ref reader);)
                    {
                        list.Add(ReadJson(ref reader));
                    }
                    return(list);

                case ValueType.Object:
                    var dict = new Dictionary <string, object>();
                    for (var obj = value.ObjectValue; obj.Next(ref reader);)
                    {
                        dict[obj.Name.ToString()] = ReadJson(ref reader);
                    }
                    return(dict);

                default:
                    return(null);
                }
            }
Exemple #4
0
        public void TestSuite(TestCase t)
        {
            var reader = JReader.FromString(t.Input);

            foreach (var action in t.Actions)
            {
                action(ref reader);
            }
        }
Exemple #5
0
        public void RequiredPropertiesAreAllFound()
        {
            var r             = JReader.FromString(@"{""a"":1, ""b"":2, ""c"":3}");
            var requiredProps = new string[] { "c", "b", "a" };

            for (var obj = r.Object().WithRequiredProperties(requiredProps); obj.Next(ref r);)
            {
            }
        }
Exemple #6
0
 private void ReadArray(ref JReader r, ArrayReader arr, TestValue <ReaderAction> value)
 {
     Assert.True(arr.IsDefined);
     foreach (var e in value.ArrayValue)
     {
         Assert.True(arr.Next(ref r), "expected array item");
         e(ref r);
     }
     Assert.False(arr.Next(ref r), "expected end of array");
 }
Exemple #7
0
        public void ReadSimpleTypesFromYamlParser()
        {
            // The .NET SDK allows flag data to be read from a YAML file by first running it through a YAML
            // parser, which produces types like Dictionary, and then feeding that into JReader. So we'll
            // verify that a basic use case like that works, using YamlDotNet.
            var yamlContent = @"
---
flags:
  flag1:
    key: flag1
    ""on"": true
    version: 1
    fallthrough:
      variation: 2
    variations:
      - fall
      - ""off""
      - ""on""
flagValues:
  flag2: value2
segments:
  seg1:
    key: seg1
    version: 1
    include: [""user1""]
";
            var yaml        = new YamlDotNet.Serialization.DeserializerBuilder().Build();
            var dataIn      = yaml.Deserialize <object>(yamlContent);

            var r = JReader.FromAdapter(ReaderAdapters.FromSimpleTypes(dataIn));

            var dataOut = JsonStreamConvert.ConvertSimpleTypes.ReadJson(ref r);

            var jsonOut      = JsonStreamConvert.SerializeObject(dataOut, JsonStreamConvert.ConvertSimpleTypes);
            var expectedJson = @"{
""flags"": {
  ""flag1"": {
    ""key"": ""flag1"",
    ""on"": ""true"",
    ""version"": ""1"",
    ""fallthrough"": { ""variation"": ""2"" },
    ""variations"": [ ""fall"", ""off"", ""on"" ]
  }
},
""flagValues"": { ""flag2"": ""value2"" },
""segments"": {
  ""seg1"": { ""key"": ""seg1"", ""version"": ""1"", ""include"": [""user1""] }
}
}";

            // Note that YamlDotNet parses all the booleans and numbers as strings; that's why we provide a
            // type coercion option.
            TestUtil.AssertJsonEqual(expectedJson, jsonOut);
        }
        /// <summary>
        /// Decodes a value from a JSON representation using the specified converter.
        /// </summary>
        /// <param name="json">the JSON representation as a string</param>
        /// <param name="converter">a converter for the desired type</param>
        /// <returns>an instance of that type</returns>
        /// <exception cref="JsonReadException">if an error occurred in parsing
        /// the JSON or translating it to the desired type; see subclasses of
        /// <see cref="JsonReadException"/> for more specific errors</exception>
        public static object DeserializeObject(string json, IJsonStreamConverter converter)
        {
            var reader = JReader.FromString(json);

            try
            {
                return(converter.ReadJson(ref reader));
            }
            catch (Exception ex)
            {
                throw reader.TranslateException(ex);
            }
        }
 public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 {
     // Unfortunately we can't hook directly into an existing Utf8JsonReader, because JReader (like any
     // other type) isn't allowed to retain a reference to a ref struct outside of itself. So we have
     // to parse out the next JSON value or tree all at once, and then wrap it in a delegate object
     // that a JReader can read from. That's less efficient than reading directly from the original
     // reader, due to 1. preallocating the value struct(s) and 2. the overhead of JReader calling the
     // delegate, but it's still better than actually parsing the JSON twice.
     using (JsonDocument jsonDocument = JsonDocument.ParseValue(ref reader))
     {
         var readerWrapper = JReader.FromAdapter(ReaderAdapters.FromJsonElement(jsonDocument.RootElement));
         return((T)_jsonStreamConverter.ReadJson(ref readerWrapper));
     }
 }
Exemple #10
0
 private void ReadObject(ref JReader r, ObjectReader obj, TestValue <ReaderAction> value)
 {
     Assert.True(obj.IsDefined);
     foreach (var p in value.ObjectValue)
     {
         Assert.True(obj.Next(ref r), "expected object property");
         Assert.Equal(p.Name, obj.Name.ToString());
         foreach (var action in p.Actions)
         {
             action(ref r);
         }
     }
     Assert.False(obj.Next(ref r), "expected end of object");
 }
        public void ReadsValues()
        {
            var r      = JReader.FromString("[10, 20, 30]");
            var values = new List <int>();

            for (var arr = r.Array(); arr.Next(ref r);)
            {
                values.Add(r.Int());
            }

            Assert.Equal(new List <int> {
                10, 20, 30
            }, values);
        }
            public object ReadJson(ref JReader reader)
            {
                string value = null;

                for (var obj = reader.Object().WithRequiredProperties(new string[] { "value" }); obj.Next(ref reader);)
                {
                    if (obj.Name == "value")
                    {
                        value = reader.String();
                    }
                }
                return(new MyTestClass {
                    Value = value
                });
            }
Exemple #13
0
        public void SyntaxErrorsUseOurExceptionType()
        {
            var reader = JReader.FromString("{no");

            try
            {
                var obj = reader.Object();
                reader.Int();
                Assert.True(false, "expected exception");
            }
            catch (Exception ex)
            {
                var realEx = reader.TranslateException(ex);
                Assert.IsType <SyntaxException>(realEx);
            }
        }
Exemple #14
0
        public void RequiredPropertyIsNotFound()
        {
            var r             = JReader.FromString(@"{""a"":1, ""c"":3}");
            var requiredProps = new string[] { "c", "b", "a" };

            try
            {
                for (var obj = r.Object().WithRequiredProperties(requiredProps); obj.Next(ref r);)
                {
                }
                Assert.True(false, "expected RequiredPropertyException");
            }
            catch (RequiredPropertyException e)
            {
                Assert.Equal("b", e.Name);
            }
        }
Exemple #15
0
        public void TypeErrorsUseOurExceptionType()
        {
            var reader = JReader.FromString("3");

            try
            {
                reader.Bool();
                Assert.True(false, "expected exception");
            }
            catch (Exception ex)
            {
                var realEx = reader.TranslateException(ex);
                var te     = Assert.IsType <TypeException>(realEx);
                Assert.Equal(ValueType.Bool, te.ExpectedType);
                Assert.Equal(ValueType.Number, te.ActualType);
            }
        }
Exemple #16
0
        public void RecursivelySkipsUnusedValue()
        {
            var r = JReader.FromString(@"{""a"":1, ""ignore"":[false,false,false], ""b"":2}");
            int a = 0, b = 0;

            for (var obj = r.Object(); obj.Next(ref r);)
            {
                if (obj.Name == "a")
                {
                    a = r.Int();
                }
                else if (obj.Name == "b")
                {
                    b = r.Int();
                }
            }

            Assert.Equal(1, a);
            Assert.Equal(2, b);
        }
        public void RecursivelySkipsUnusedValue()
        {
            var r      = JReader.FromString(@"[10, {""ignore"": [false,false,false]}, 20]");
            var values = new List <int>();

            var arr = r.Array();

            Assert.True(arr.Next(ref r));
            values.Add(r.Int());

            Assert.True(arr.Next(ref r));

            Assert.True(arr.Next(ref r));
            values.Add(r.Int());

            Assert.False(arr.Next(ref r));

            Assert.Equal(new List <int> {
                10, 20
            }, values);
        }
Exemple #18
0
        public void ReadsProperties()
        {
            var r = JReader.FromString(@"{""a"":1, ""b"":2}");
            int a = 0, b = 0;

            for (var obj = r.Object(); obj.Next(ref r);)
            {
                switch (obj.Name)
                {
                case var n when n == "a":
                    a = r.Int();
                    break;

                case var n when n == "b":
                    b = r.Int();
                    break;
                }
            }

            Assert.Equal(1, a);
            Assert.Equal(2, b);
        }
 /// <summary>
 /// Advances to the next object property if any, and returns <see langword="true"/> if successful.
 /// </summary>
 /// <remarks>
 /// <para>
 /// It returns <see langword="false"/> if the <c>JReader</c> has reached the end of the object,
 /// or if the object was empty or null.
 /// </para>
 /// <para>
 /// If <c>Next</c> returns <see langword="true"/>, you can then use <see cref="Name"/> to check
 /// the name of the property, and use <see cref="JReader"/> methods such as <see cref="JReader.Bool"/>
 /// to read the element value. If you do not care about the value, simply calling <c>Next</c> again
 /// without calling a <c>JReader</c> method will discard the value.
 /// </para>
 /// </remarks>
 /// <returns><see langword="true"/> if there is a next object property</returns>
 public bool Next(ref JReader reader)
 {
     if (!_defined)
     {
         return(false);
     }
     _name       = reader.ObjectNext(!_afterFirst);
     _afterFirst = true;
     if (!_name.Empty)
     {
         if (_requiredProperties != null)
         {
             for (int i = 0; i < _requiredProperties.Length; i++)
             {
                 if (_name.Equals(_requiredProperties[i]))
                 {
                     _foundProperties[i] = true;
                     break;
                 }
             }
         }
         return(true);
     }
     if (_requiredProperties != null)
     {
         for (int i = 0; i < _requiredProperties.Length; i++)
         {
             if (!_foundProperties[i])
             {
                 throw new RequiredPropertyException(_requiredProperties[i],
                                                     reader.LastPos);
             }
         }
     }
     return(false);
 }
Exemple #20
0
 public ReaderTestContext(string input)
 {
     Reader = JReader.FromString(input);
 }
Exemple #21
0
        public void ReadFromSimpleTypes()
        {
            var value = new List <object>
            {
                null,
                true,
                1,
                (long)2,
                3,
                (long)4,
                (float)5.5,
                (double)6.5,
                "x",
                new Dictionary <string, object> {
                    { "a", 1 }
                },
                new Dictionary <object, object> {
                    { "b", 2 }
                }
            };
            var r = JReader.FromAdapter(ReaderAdapters.FromSimpleTypes(value));

            var arr = r.Array();

            Assert.True(arr.Next(ref r));
            r.Null();

            Assert.True(arr.Next(ref r));
            Assert.True(r.Bool());

            Assert.True(arr.Next(ref r));
            Assert.Equal(1, r.Int());

            Assert.True(arr.Next(ref r));
            Assert.Equal(2, r.Int());

            Assert.True(arr.Next(ref r));
            Assert.Equal(3, r.Long());

            Assert.True(arr.Next(ref r));
            Assert.Equal(4, r.Long());

            Assert.True(arr.Next(ref r));
            Assert.Equal(5.5, r.Double());

            Assert.True(arr.Next(ref r));
            Assert.Equal(6.5, r.Double());

            Assert.True(arr.Next(ref r));
            Assert.Equal("x", r.String());

            Assert.True(arr.Next(ref r));
            var obj1 = r.Object();

            Assert.True(obj1.Next(ref r));
            Assert.Equal("a", obj1.Name.ToString());
            Assert.Equal(1, r.Int());
            Assert.False(obj1.Next(ref r));

            Assert.True(arr.Next(ref r));
            var obj2 = r.Object();

            Assert.True(obj2.Next(ref r));
            Assert.Equal("b", obj2.Name.ToString());
            Assert.Equal(2, r.Int());
            Assert.False(obj2.Next(ref r));

            Assert.False(arr.Next(ref r));
            Assert.True(r.EOF);
        }
Exemple #22
0
            public object ReadJson(ref JReader reader)
            {
                var ret = new MyTestClass();

                for (var obj = reader.Object(); obj.Next(ref reader);)
                {
                    switch (obj.Name.ToString())
                    {
                    case "stringProp":
                        ret.StringProp = reader.String();
                        break;

                    case "boolProp":
                        ret.BoolProp = reader.Bool();
                        break;

                    case "intProp":
                        ret.IntProp = reader.Int();
                        break;

                    case "longProp":
                        ret.LongProp = reader.Long();
                        break;

                    case "doubleProp":
                        ret.DoubleProp = reader.Double();
                        break;

                    case "arrayOfInts":
                        ret.ArrayOfInts = new List <int>();
                        for (var arr = reader.Array(); arr.Next(ref reader);)
                        {
                            ret.ArrayOfInts.Add(reader.Int());
                        }
                        break;

                    case "objectOfInts":
                        ret.ObjectOfInts = new Dictionary <string, int>();
                        for (var subObj = reader.Object(); subObj.Next(ref reader);)
                        {
                            ret.ObjectOfInts[subObj.Name.ToString()] = reader.Int();
                        }
                        break;

                    case "nullableString1":
                        ret.NullableString1 = reader.StringOrNull();
                        break;

                    case "nullableBool1":
                        ret.NullableBool1 = reader.BoolOrNull();
                        break;

                    case "nullableInt1":
                        ret.NullableInt1 = reader.IntOrNull();
                        break;

                    case "nullableLong1":
                        ret.NullableLong1 = reader.LongOrNull();
                        break;

                    case "nullableDouble1":
                        ret.NullableDouble1 = reader.DoubleOrNull();
                        break;

                    case "nullableString2":
                        ret.NullableString2 = reader.StringOrNull();
                        break;

                    case "nullableBool2":
                        ret.NullableBool2 = reader.BoolOrNull();
                        break;

                    case "nullableInt2":
                        ret.NullableInt2 = reader.IntOrNull();
                        break;

                    case "nullableLong2":
                        ret.NullableLong2 = reader.LongOrNull();
                        break;

                    case "nullableDouble2":
                        ret.NullableDouble2 = reader.DoubleOrNull();
                        break;
                    }
                }
                return(ret);
            }
Exemple #23
0
        private string ParseString(IReaderAdapter adapter)
        {
            var r = JReader.FromAdapter(adapter);

            return(r.String());
        }
Exemple #24
0
        private double ParseDouble(IReaderAdapter adapter)
        {
            var r = JReader.FromAdapter(adapter);

            return(r.Double());
        }
Exemple #25
0
        private bool ParseBool(IReaderAdapter adapter)
        {
            var r = JReader.FromAdapter(adapter);

            return(r.Bool());
        }