Ejemplo n.º 1
0
        public void SimpleLookupTypes()
        {
            dynamic value = ParseUtility.ParseAndValidate(
                @"{
    ""string"": ""bar"",
    ""int"": 32,
    ""double"": 32.5,
    ""bool"": true,
    ""null"": null,
    ""array"": [ 0, 1, 2 ],
    ""dict"": { ""array"": [ 0, 1, 2 ] }
}");

            string stringValue = value.@string;
            int    intValue    = value.@int;
            double doubleValue = value.@double;
            bool   boolValue   = value.@bool;
            object nullValue   = value.@null;

            object[] arrayValue = value.array;
            IDictionary <string, object> dictValue = value.dict;

            Assert.Equal("bar", stringValue);
            Assert.Equal(32, intValue);
            Assert.Equal(32.5, doubleValue);
            Assert.True(boolValue);
            Assert.Null(nullValue);
            Assert.Equal(3, arrayValue.Length);
            Assert.Equal(1, dictValue.Count);
        }
Ejemplo n.º 2
0
        public void ArrayEnum()
        {
            dynamic           value   = ParseUtility.ParseAndValidate(@"{ ""array"": [ 0, 1, 2, 3, 4 ] }");
            dynamic           darray  = value.array;
            dynamic           darray2 = value["array"];
            IEnumerable <int> iarray  = darray;
            IEnumerable <int> iarray2 = darray2;

            Assert.Equal(iarray, iarray2);

            object[] array = darray;
            List <KeyValuePair <string, int> > array1 = value.array;
            List <KeyValuePair <int, int> >    array2 = value.array;

            int i = 0;

            foreach (decimal child in array)
            {
                int h = (int)child;
                Assert.Equal(h, int.Parse(array1[i].Key));
                Assert.Equal(h, array1[i].Value);
                Assert.Equal(h, array2[i].Key);
                Assert.Equal(h, array2[i].Value);
                Assert.Equal(h, (int)darray[i]);
                Assert.Equal(h, i++);
            }

            for (i = 0; i < array.Length; i++)
            {
                int h = (int)(decimal)array[i];
                Assert.Equal(h, i);
            }
        }
Ejemplo n.º 3
0
        public void EmptyContainers()
        {
            JsonValue value = ParseUtility.ParseAndValidate(@"{ ""dict"": { ""array"": [{}] }, ""array"": [ [], {}, [], {} ] }");

            Assert.Equal(1, value["dict"].Object.Count);
            Assert.Equal(4, value["array"].Array.Count);
        }
Ejemplo n.º 4
0
        public void ToDateSuccess()
        {
            DateTime date       = new DateTime(1970, 7, 4, 12, 0, 30, 500, DateTimeKind.Local);
            dynamic  value      = ParseUtility.ParseAndValidate($@"{{ ""date"": ""{date.ToString("O", CultureInfo.InvariantCulture)}"" }}");
            DateTime parsedDate = value.date;

            Assert.Equal(date, parsedDate);
        }
Ejemplo n.º 5
0
        public void ToDateFailed()
        {
            DateTime      date  = new DateTime(1970, 7, 4, 12, 0, 30, 500, DateTimeKind.Local);
            dynamic       value = ParseUtility.ParseAndValidate($@"{{ ""date"": ""Foo{date.ToString("O", CultureInfo.InvariantCulture)}"" }}");
            JsonException ex    = Assert.Throws <JsonException>(() => (DateTime)value.date);

            Assert.IsType <FormatException>(ex.InnerException);
        }
Ejemplo n.º 6
0
        public void ExceptionArray()
        {
            JsonException ex = Assert.Throws <JsonException>(() => ParseUtility.ParseAndValidate(@"{ ""foo"": [ 1, 2, { ""3"": { ""array"": [[,,]] } ] } }"));

            Assert.True(ex.HasToken);
            Assert.Equal(0, ex.TokenLine);
            Assert.Equal(37, ex.TokenColumn);
            Assert.Equal(",", ex.TokenText);
        }
Ejemplo n.º 7
0
        public void ParseException()
        {
            JsonException ex = Assert.Throws <JsonException>(() => ParseUtility.ParseAndValidate(@"{ ""foo"":: bar }"));

            Assert.True(ex.HasToken);
            Assert.Equal(0, ex.TokenLine);
            Assert.Equal(8, ex.TokenColumn);
            Assert.Equal(":", ex.TokenText);
        }
Ejemplo n.º 8
0
        public void TokenException()
        {
            JsonException ex = Assert.Throws <JsonException>(() => ParseUtility.ParseAndValidate(@"{ foo: bar }"));

            Assert.True(ex.HasToken);
            Assert.Equal(0, ex.TokenLine);
            Assert.Equal(2, ex.TokenColumn);
            Assert.Equal("foo", ex.TokenText.Substring(0, 3));
        }
Ejemplo n.º 9
0
        public void ConvertArrayIntermediate2()
        {
            JsonValue value = ParseUtility.ParseAndValidate(@"[ [ 2, 4, 6, 8 ], [ 1, 3, 5 ] ]");

            int[][] ints = value.ToObject <int[][]>();

            Assert.Equal(2, ints.Length);
            Assert.Equal(new int[][] { new int[] { 2, 4, 6, 8 }, new int[] { 1, 3, 5 } }, ints);
        }
Ejemplo n.º 10
0
        public void DictionaryEnum()
        {
            dynamic value = ParseUtility.ParseAndValidate(@"{ ""0"": 0, ""1"": 1, ""2"": 2, ""3"": 3, ""4"": 4 }");

            foreach (KeyValuePair <string, object> pair in value)
            {
                int i = (int)(decimal)pair.Value;
                Assert.Equal(i, int.Parse(pair.Key));
            }
        }
Ejemplo n.º 11
0
        public void ConvertArrayIntermediate1()
        {
            JsonValue value = ParseUtility.ParseAndValidate(@"[ 1, 2, 3, 4 ]");

            int[]    ints    = value.ToObject <int[]>();
            object[] objects = value.ToObject <object[]>();

            Assert.Equal(4, objects.Length);
            Assert.Equal(4, ints.Length);
            Assert.Equal(new int[] { 1, 2, 3, 4 }, ints);
        }
Ejemplo n.º 12
0
        public void DictionaryEnum()
        {
            JsonValue value = ParseUtility.ParseAndValidate(@"{ ""0"": 0, ""1"": 1, ""2"": 2, ""3"": 3, ""4"": 4 }");

            foreach (KeyValuePair <string, JsonValue> pair in value.Object)
            {
                Assert.Equal(int.Parse(pair.Key), pair.Value.Number);
            }

            foreach (string key in value.Object.Keys)
            {
                Assert.True(value.Object.ContainsKey(key));
                Assert.Equal(int.Parse(key), value[key].Number);
            }
        }
Ejemplo n.º 13
0
        public void Comment()
        {
            JsonValue value = ParseUtility.ParseAndValidate(
                @"{
/*  Comment
    Comment
    Comment */
    ""foo"":  1,
// Comment
// Comment
    ""bar"": 2
}");

            Assert.Equal(1, value["foo"].Number);
            Assert.Equal(2, value["bar"].Number);
        }
Ejemplo n.º 14
0
        public void MissingLookup()
        {
            JsonValue value = ParseUtility.ParseAndValidate(@"{ ""foo"": ""bar"" }");

            JsonValue bar = value["bar"];

            Assert.True(!bar.IsValid);

            bar = bar["foo"];
            Assert.True(!bar.IsValid);

            bar = value[10];
            Assert.True(!bar.IsValid);

            bar = bar[10];
            Assert.True(!bar.IsValid);
        }
Ejemplo n.º 15
0
        public void LineColumn()
        {
            JsonException ex = Assert.Throws <JsonException>(() => ParseUtility.ParseAndValidate(
                                                                 @"{
/*  Comment
    Comment
    Comment */
    ""foo"":  1,
// Comment
// Comment
    ""bar"": zoo
}"));

            Assert.True(ex.HasToken);
            Assert.Equal(7, ex.TokenLine);
            Assert.Equal(11, ex.TokenColumn);
            Assert.Equal("zoo", ex.TokenText.Substring(0, 3));
        }
Ejemplo n.º 16
0
        public void SimpleLookupTypes()
        {
            JsonValue value = ParseUtility.ParseAndValidate(
                @"{
    ""string"": ""bar"",
    ""int"": 32,
    ""double"": 32.5,
    ""bool"": true,
    ""null"": null,
    ""array"": [ 0, 1, 2 ],
    ""dict"": { ""array"": [ 0, 1, 2 ] }
}");

            JsonValue stringValue = value["string"];
            JsonValue intValue    = value["int"];
            JsonValue doubleValue = value["double"];
            JsonValue boolValue   = value["bool"];
            JsonValue nullValue   = value["null"];
            JsonValue arrayValue  = value["array"];
            JsonValue dictValue   = value["dict"];

            Assert.True(stringValue.IsString);
            Assert.True(intValue.IsNumber);
            Assert.Equal(32, intValue.Number, 2);
            Assert.True(doubleValue.IsNumber);
            Assert.Equal((decimal)32.5, doubleValue.Number, 2);
            Assert.True(boolValue.IsBool);
            Assert.True(nullValue.IsNull);
            Assert.True(arrayValue.IsArray);
            Assert.True(dictValue.IsObject);

            JsonValue nestedIntValue   = value["array"][1];
            JsonValue nestedArrayValue = value["dict"]["array"];

            Assert.True(nestedIntValue.IsNumber);
            Assert.Equal(1, nestedIntValue.Number, 2);
            Assert.True(nestedArrayValue.IsArray);
            Assert.Equal(arrayValue.Array, nestedArrayValue.Array);
        }
Ejemplo n.º 17
0
        public void ArrayEnum()
        {
            JsonValue value = ParseUtility.ParseAndValidate(@"{ ""array"": [ 0, 1, 2, 3, 4 ] }");

            Assert.True(value.IsObject);

            JsonValue array = value["array"];

            Assert.True(array.IsArray);

            int i = 0;

            foreach (JsonValue child in array.Array)
            {
                Assert.Equal(i++, child.Number);
            }

            for (i = 0; i < array.Array.Count; i++)
            {
                Assert.Equal(i, array[i].Number);
            }

            Assert.False(array[i].IsValid);
        }
Ejemplo n.º 18
0
        public void MissingIndex()
        {
            dynamic value = ParseUtility.ParseAndValidate(@"{ ""foo"": [ 0, 1 ] }");

            Assert.Throws <IndexOutOfRangeException>(() => value.foo[10]);
        }
Ejemplo n.º 19
0
        public void MissingLookup()
        {
            dynamic value = ParseUtility.ParseAndValidate(@"{ ""foo"": ""bar"" }");

            Assert.Throws <KeyNotFoundException>(() => value.bar);
        }