Пример #1
0
 private static Value GetValue(ref JsonReader jsonReader)
 {
     var type = jsonReader.GetJsonValueType();
     var value = new Value {Type = (Value.ValueType) type};
     var obj = jsonReader.GetValue(type);
     switch (value.Type)
     {
         case Value.ValueType.String:
             value.StringValue = obj.ToString();
             break;
         case Value.ValueType.Number:
             value.NumberValue = Convert.ToDouble(obj.ToString());
             break;
         case Value.ValueType.True:
             break;
         case Value.ValueType.False:
             break;
         case Value.ValueType.Null:
             break;
         case Value.ValueType.Object:
             value.ObjectValue = ReadObject(ref jsonReader);
             break;
         case Value.ValueType.Array:
             value.ArrayValue = ReadArray(ref jsonReader);
             break;
         default:
             throw new ArgumentOutOfRangeException();
     }
     return value;
 }
Пример #2
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;
        }
Пример #3
0
        private string OutputValue(Value value)
        {
            var str = "";

            if (value == null) return str;

            var type = value.Type;
            switch (type)
            {
                case Value.ValueType.String:
                    str += "\"" + value.StringValue + "\"";
                    break;
                case Value.ValueType.Number:
                    str += value.NumberValue;
                    break;
                case Value.ValueType.Object:
                    str += OutputObject(value.ObjectValue);
                    break;
                case Value.ValueType.Array:
                    str += OutputArray(value.ArrayValue);
                    break;
                case Value.ValueType.True:
                    str += value.TrueValue.ToString().ToLower();
                    break;
                case Value.ValueType.False:
                    str += value.FalseValue.ToString().ToLower();
                    break;
                case Value.ValueType.Null:
                    str += value.NullValue.ToString().ToLower();
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
            return str;
        }