Beispiel #1
0
        public TestDom 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 TestDom();
                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;
            }
        }
Beispiel #2
0
        private static TestDom ReadJson(string jsonString)
        {
            var json = new TestDom();

            if (string.IsNullOrEmpty(jsonString))
            {
                return(json);
            }

            var jsonReader = new JsonReader(jsonString.AsSpan().AsBytes(), SymbolTable.InvariantUtf16);

            jsonReader.Read();
            switch (jsonReader.TokenType)
            {
            case JsonTokenType.StartArray:
                json.Array = ReadArray(ref jsonReader);
                break;

            case JsonTokenType.StartObject:
                json.Object = ReadObject(ref jsonReader);
                break;

            default:
                Assert.True(false, "The test JSON does not start with an array or object token");
                break;
            }

            return(json);
        }
        private static TestDom ReadJson(string jsonString)
        {
            var json = new TestDom();

            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);
        }
Beispiel #4
0
        public TestDom this[string index]
        {
            get
            {
                if (Object == null)
                {
                    throw new NullReferenceException();
                }
                if (Object.Pairs == null)
                {
                    throw new NullReferenceException();
                }

                var json = new TestDom();
                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();
            }
        }
Beispiel #5
0
        public TestDom 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 TestDom();
                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);
            }
        }
Beispiel #6
0
        public TestDom this[string index]
        {
            get
            {
                if (Object == null) throw new NullReferenceException();
                if (Object.Pairs == null) throw new NullReferenceException();

                var json = new TestDom();
                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();
            }
        }
Beispiel #7
0
        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);
        }