示例#1
0
        public void TestSuite(TestCase t)
        {
            var reader = JReader.FromString(t.Input);

            foreach (var action in t.Actions)
            {
                action(ref reader);
            }
        }
示例#2
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);)
            {
            }
        }
        /// <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 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);
        }
示例#5
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);
            }
        }
示例#6
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);
            }
        }
示例#7
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);
            }
        }
示例#8
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);
        }
示例#10
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);
        }
示例#11
0
 public ReaderTestContext(string input)
 {
     Reader = JReader.FromString(input);
 }