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); }
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); } }
public void EmptyContainers() { JsonValue value = ParseUtility.ParseAndValidate(@"{ ""dict"": { ""array"": [{}] }, ""array"": [ [], {}, [], {} ] }"); Assert.Equal(1, value["dict"].Object.Count); Assert.Equal(4, value["array"].Array.Count); }
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); }
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); }
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); }
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); }
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)); }
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); }
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)); } }
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); }
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); } }
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); }
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); }
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)); }
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); }
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); }
public void MissingIndex() { dynamic value = ParseUtility.ParseAndValidate(@"{ ""foo"": [ 0, 1 ] }"); Assert.Throws <IndexOutOfRangeException>(() => value.foo[10]); }
public void MissingLookup() { dynamic value = ParseUtility.ParseAndValidate(@"{ ""foo"": ""bar"" }"); Assert.Throws <KeyNotFoundException>(() => value.bar); }