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 static Array ReadArray(ref JsonReader jsonReader) { Array jsonArray = null; List<Value> jsonValues = null; while (jsonReader.Read()) { switch (jsonReader.TokenType) { case JsonReader.JsonTokenType.ObjectStart: break; case JsonReader.JsonTokenType.ObjectEnd: 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}; return jsonArray; } break; case JsonReader.JsonTokenType.Property: break; case JsonReader.JsonTokenType.Value: if (jsonValues != null) jsonValues.Add(GetValue(ref jsonReader)); break; default: throw new ArgumentOutOfRangeException(); } } throw new FormatException("Json array was started but never ended."); }
private string OutputArray(Array array) { var str = ""; if (array == null || array.Elements == null) return str; foreach (var element in array.Elements) { str += "["; str += OutputElements(element); str += "]"; } return str; }
private string OutputArray(Array array) { var strBuilder = new StringBuilder(); if (array == null || array.Values == null) return ""; strBuilder.Append("["); for (var i = 0; i < array.Values.Count; i++) { strBuilder.Append(OutputValue(array.Values[i])); if (i < array.Values.Count - 1) { strBuilder.Append(","); } } strBuilder.Append("]"); return strBuilder.ToString(); }
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; }