Exemplo n.º 1
0
        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;
        }
Exemplo n.º 2
0
        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();
                }
            }
        }
Exemplo n.º 3
0
        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.");
        }
Exemplo n.º 4
0
        private string OutputMembers(Members members)
        {
            var str = "";

            if (members == null || members.Pairs == null) return str;

            for (var i = 0; i < members.Pairs.Count; i++)
            {
                str += OutputPair(members.Pairs[i]);
                if (i < members.Pairs.Count - 1)
                {
                    str += ",";
                }
            }
            return str;
        }