public Json this[int index] { get { if (Array == null) throw new NullReferenceException(); if (Array.Values == null) throw new NullReferenceException(); List<Value> values = Array.Values; if (index < 0 || index >= values.Count) throw new IndexOutOfRangeException(); Value value = values[index]; var json = new Json(); switch (value.Type) { case Value.ValueType.Object: json.Object = value.ObjectValue; break; case Value.ValueType.Array: json.Array = value.ArrayValue; break; case Value.ValueType.String: case Value.ValueType.Number: case Value.ValueType.False: case Value.ValueType.True: case Value.ValueType.Null: json.Value = value; break; default: break; } return json; } }
public Json this[string index] { get { if (Object == null) throw new NullReferenceException(); if (Object.Pairs == null) throw new NullReferenceException(); var json = new Json(); foreach (var pair in Object.Pairs) { if (pair.Name == index) { switch (pair.Value.Type) { case Value.ValueType.Object: json.Object = pair.Value.ObjectValue; break; case Value.ValueType.Array: json.Array = pair.Value.ArrayValue; break; case Value.ValueType.String: case Value.ValueType.Number: case Value.ValueType.False: case Value.ValueType.True: case Value.ValueType.Null: json.Value = pair.Value; break; default: break; } return json; } } throw new KeyNotFoundException(); } }
private static Json CreateJson() { var valueAge = new Value { Type = Value.ValueType.Number, NumberValue = 30 }; var pairAge = new Pair { Name = "age", Value = valueAge }; var valueFirst = new Value { Type = Value.ValueType.String, StringValue = "John" }; var pairFirst = new Pair { Name = "first", Value = valueFirst }; var valueLast = new Value { Type = Value.ValueType.String, StringValue = "Smith" }; var pairLast = new Pair { Name = "last", Value = valueLast }; var value1 = new Value { Type = Value.ValueType.String, StringValue = "425-000-1212" }; var value2 = new Value { Type = Value.ValueType.String, StringValue = "425-000-1213" }; var values = new List<Value> {value1, value2}; var elementInner = new Elements {Values = values}; var elementsInner = new List<Elements> {elementInner}; var arrInner = new Array {Elements = elementsInner}; var valuePhone = new Value { Type = Value.ValueType.Array, ArrayValue = arrInner }; var pairPhone = new Pair { Name = "phoneNumbers", Value = valuePhone }; var valueStreet = new Value { Type = Value.ValueType.String, StringValue = "1 Microsoft Way" }; var pairStreet = new Pair { Name = "street", Value = valueStreet }; var valueCity = new Value { Type = Value.ValueType.String, StringValue = "Redmond" }; var pairCity = new Pair { Name = "city", Value = valueCity }; var valueZip = new Value { Type = Value.ValueType.Number, NumberValue = 98052 }; var pairZip = new Pair { Name = "zip", Value = valueZip }; var pairsInner = new List<Pair> {pairStreet, pairCity, pairZip}; var memberInner = new Members {Pairs = pairsInner}; var membersInner = new List<Members> {memberInner}; var objInner = new Object {Members = membersInner}; var valueAddress = new Value { Type = Value.ValueType.Object, ObjectValue = objInner }; var pairAddress = new Pair { Name = "address", Value = valueAddress }; var pairs = new List<Pair> {pairAge, pairFirst, pairLast, pairPhone, pairAddress}; var member = new Members {Pairs = pairs}; var members = new List<Members> {member}; var obj = new Object {Members = members}; var json = new Json {Object = obj}; return json; }
private static Json ReadJson(string jsonString) { var json = new Json(); if (string.IsNullOrEmpty(jsonString)) { return json; } var jsonReader = new JsonReader(jsonString); var jsonObjectMain = new Object(); var jsonMembersMain = new List<Members>(); ReadJsonHelper(jsonReader, jsonMembersMain); jsonObjectMain.Members = jsonMembersMain; json.Object = jsonObjectMain; return json; }
private static Json ReadJson(string jsonString) { var json = new Json(); if (string.IsNullOrEmpty(jsonString)) { return json; } var jsonReader = new JsonReader(jsonString); var jsonObjectMain = new Object(); var jsonMembersMain = new List<Pair>(); var jsonArrayMain = new Array(); var jsonElementsMain = new List<Value>(); if (jsonString.Trim().Substring(0, 1) == "[") { ReadJsonHelper(jsonReader, jsonElementsMain); jsonArrayMain.Values = jsonElementsMain; json.Array = jsonArrayMain; } else { ReadJsonHelper(jsonReader, jsonMembersMain); jsonObjectMain.Pairs = jsonMembersMain; json.Object = jsonObjectMain; } return json; }
private static Json CreateJson() { var valueAge = new Value { Type = Value.ValueType.Number, NumberValue = 30 }; var pairAge = new Pair { Name = "age", Value = valueAge }; var valueFirst = new Value { Type = Value.ValueType.String, StringValue = "John" }; var pairFirst = new Pair { Name = "first", Value = valueFirst }; var valueLast = new Value { Type = Value.ValueType.String, StringValue = "Smith" }; var pairLast = new Pair { Name = "last", Value = valueLast }; var value1 = new Value { Type = Value.ValueType.String, StringValue = "425-000-1212" }; var value2 = new Value { Type = Value.ValueType.String, StringValue = "425-000-1213" }; var values = new List <Value> { value1, value2 }; var elementInner = new Elements { Values = values }; var elementsInner = new List <Elements> { elementInner }; var arrInner = new Array { Elements = elementsInner }; var valuePhone = new Value { Type = Value.ValueType.Array, ArrayValue = arrInner }; var pairPhone = new Pair { Name = "phoneNumbers", Value = valuePhone }; var valueStreet = new Value { Type = Value.ValueType.String, StringValue = "1 Microsoft Way" }; var pairStreet = new Pair { Name = "street", Value = valueStreet }; var valueCity = new Value { Type = Value.ValueType.String, StringValue = "Redmond" }; var pairCity = new Pair { Name = "city", Value = valueCity }; var valueZip = new Value { Type = Value.ValueType.Number, NumberValue = 98052 }; var pairZip = new Pair { Name = "zip", Value = valueZip }; var pairsInner = new List <Pair> { pairStreet, pairCity, pairZip }; var memberInner = new Members { Pairs = pairsInner }; var membersInner = new List <Members> { memberInner }; var objInner = new Object { Members = membersInner }; var valueAddress = new Value { Type = Value.ValueType.Object, ObjectValue = objInner }; var pairAddress = new Pair { Name = "address", Value = valueAddress }; var pairs = new List <Pair> { pairAge, pairFirst, pairLast, pairPhone, pairAddress }; var member = new Members { Pairs = pairs }; var members = new List <Members> { member }; var obj = new Object { Members = members }; var json = new Json { Object = obj }; return(json); }