private static Object ReadObject(ref JsonReader jsonReader) { var jsonObject = new Object(); var jsonMembersList = new List<Members>(); List<Pair> jsonPairs = null; while (jsonReader.Read()) { switch (jsonReader.TokenType) { case JsonReader.JsonTokenType.ObjectStart: jsonPairs = new List<Pair>(); break; case JsonReader.JsonTokenType.ObjectEnd: if (jsonPairs != null) { var jsonMembers = new Members {Pairs = jsonPairs}; jsonMembersList.Add(jsonMembers); jsonObject.Members = jsonMembersList; return jsonObject; } break; case JsonReader.JsonTokenType.ArrayStart: break; case JsonReader.JsonTokenType.ArrayEnd: break; case JsonReader.JsonTokenType.Property: var pair = new Pair { Name = (string) jsonReader.GetName(), Value = GetValue(ref jsonReader) }; if (jsonPairs != null) jsonPairs.Add(pair); break; case JsonReader.JsonTokenType.Value: break; default: throw new ArgumentOutOfRangeException(); } } throw new FormatException("Json object was started but never ended."); }
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 void ReadJsonHelper(JsonReader jsonReader, ICollection<Members> jsonMembersMain) { Array jsonArray = null; List<Pair> jsonPairs = null; List<Value> jsonValues = null; while (jsonReader.Read()) { switch (jsonReader.TokenType) { case JsonReader.JsonTokenType.ObjectStart: jsonPairs = new List<Pair>(); break; case JsonReader.JsonTokenType.ObjectEnd: if (jsonPairs != null) { var jsonMembers = new Members {Pairs = jsonPairs}; jsonMembersMain.Add(jsonMembers); } break; case JsonReader.JsonTokenType.ArrayStart: jsonArray = new Array(); jsonValues = new List<Value>(); break; case JsonReader.JsonTokenType.ArrayEnd: if (jsonArray != null) { var jsonElements = new Elements {Values = jsonValues}; jsonArray.Elements = new List<Elements> {jsonElements}; } break; case JsonReader.JsonTokenType.Property: var pair = new Pair { Name = (string) jsonReader.GetName(), Value = GetValue(ref jsonReader) }; if (jsonPairs != null) jsonPairs.Add(pair); break; case JsonReader.JsonTokenType.Value: if (jsonValues != null) jsonValues.Add(GetValue(ref jsonReader)); break; default: throw new ArgumentOutOfRangeException(); } } }
private string OutputPair(Pair pair) { var str = ""; if (pair == null) return str; str += "\"" + pair.Name + "\":"; str += OutputValue(pair.Value); return str; }
private static void ReadJsonHelper(JsonReader jsonReader, List<Value> jsonElementsMain) { Object jsonObject = null; List<Pair> jsonPairs = null; List<Value> jsonValues = null; while (jsonReader.Read()) { switch (jsonReader.TokenType) { case JsonReader.JsonTokenType.ObjectStart: jsonObject = new Object(); jsonPairs = new List<Pair>(); break; case JsonReader.JsonTokenType.ObjectEnd: if (jsonObject != null) { jsonObject.Pairs = jsonPairs; } break; case JsonReader.JsonTokenType.ArrayStart: jsonValues = new List<Value>(); break; case JsonReader.JsonTokenType.ArrayEnd: if (jsonValues != null) { jsonElementsMain.AddRange(jsonValues); } break; case JsonReader.JsonTokenType.Property: var pair = new Pair { Name = (string)jsonReader.GetName(), Value = GetValue(ref jsonReader) }; if (jsonPairs != null) jsonPairs.Add(pair); break; case JsonReader.JsonTokenType.Value: if (jsonValues != null) jsonValues.Add(GetValue(ref jsonReader)); break; default: throw new ArgumentOutOfRangeException(); } } }
private static TestDom 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 arrInner = new Array { Values = values }; 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 objInner = new Object { Pairs = pairsInner }; 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 obj = new Object { Pairs = pairs }; var json = new TestDom { Object = obj }; return(json); }
private static void ReadJsonHelper(JsonReader jsonReader, ICollection <Members> jsonMembersMain) { Array jsonArray = null; List <Pair> jsonPairs = null; List <Value> jsonValues = null; while (jsonReader.Read()) { switch (jsonReader.TokenType) { case JsonReader.JsonTokenType.ObjectStart: jsonPairs = new List <Pair>(); break; case JsonReader.JsonTokenType.ObjectEnd: if (jsonPairs != null) { var jsonMembers = new Members { Pairs = jsonPairs }; jsonMembersMain.Add(jsonMembers); } break; case JsonReader.JsonTokenType.ArrayStart: jsonArray = new Array(); jsonValues = new List <Value>(); break; case JsonReader.JsonTokenType.ArrayEnd: if (jsonArray != null) { var jsonElements = new Elements { Values = jsonValues }; jsonArray.Elements = new List <Elements> { jsonElements }; } break; case JsonReader.JsonTokenType.Property: var pair = new Pair { Name = (string)jsonReader.GetName(), Value = GetValue(ref jsonReader) }; if (jsonPairs != null) { jsonPairs.Add(pair); } break; case JsonReader.JsonTokenType.Value: if (jsonValues != null) { jsonValues.Add(GetValue(ref jsonReader)); } break; default: throw new ArgumentOutOfRangeException(); } } }