Пример #1
0
        ProxyObject DecodeObject()
        {
            var proxy = new ProxyObject();

            // Ditch opening brace.
            json.Read();

            // {
            while (true)
            {
                // ReSharper disable once SwitchStatementMissingSomeCases
                switch (NextToken)
                {
                case Token.None:
                    return(null);

                case Token.Comma:
                    continue;

                case Token.CloseBrace:
                    return(proxy);

                default:
                    // Key
                    string key = DecodeString();
                    if (key == null)
                    {
                        return(null);
                    }

                    // :
                    if (NextToken != Token.Colon)
                    {
                        return(null);
                    }

                    json.Read();

                    // Value
                    proxy.Add(key, DecodeValue());
                    break;
                }
            }
        }
Пример #2
0
        ProxyObject DecodeObject()
        {
            ProxyObject obj = new ProxyObject();

            // ditch opening brace
            json.Read();

            // {
            while (true)
            {
                switch (NextToken)
                {
                case TOKEN.NONE:
                    return(null);

                case TOKEN.COMMA:
                    continue;

                case TOKEN.CURLY_CLOSE:
                    return(obj);

                default:
                    // name
                    string name = DecodeString();
                    if (name == null)
                    {
                        return(null);
                    }

                    // :
                    if (NextToken != TOKEN.COLON)
                    {
                        return(null);
                    }
                    json.Read();

                    obj.Add(name, DecodeValue());
                    break;
                }
            }
        }
Пример #3
0
        private static Variant FindIndex(ProxyArray array, int index)
        {
            for (int i = 0; i < array.Count; i++)
            {
                if (array[i] is ProxyObject)
                {
                    ProxyObject proxy = array[i] as ProxyObject;

                    if (proxy.ContainsKey(ProxyArray.CombineHintName))
                    {
                        int @Index = proxy[ProxyArray.CombineHintName];

                        if (@Index == index)
                        {
                            return(proxy);
                        }
                    }
                }
            }
            return(null);
        }
Пример #4
0
        void EncodeProxyObject(ProxyObject value)
        {
            if (value.Count == 0)
            {
                builder.Append("{}");
            }
            else
            {
                AppendOpenBrace();

                var firstItem = true;
                foreach (var e in value.Keys)
                {
                    AppendComma(firstItem);
                    EncodeString(e);
                    AppendColon();
                    EncodeValue(value[e], false);
                    firstItem = false;
                }

                AppendCloseBrace();
            }
        }
Пример #5
0
        protected static Variant CombineVariant(Variant startingVariant, Variant combineWith)
        {
            Type type = null;

            if (startingVariant != null)
            {
                type = startingVariant.GetType();
            }
            else if (combineWith != null)
            {
                type = combineWith.GetType();
            }
            else
            {
                throw new ArgumentException("Both values are null for combine");
            }

            if (type == typeof(ProxyObject))
            {
                ProxyObject combineObject  = combineWith as ProxyObject;
                ProxyObject startingObject = startingVariant as ProxyObject;

                foreach (var item in combineObject)
                {
                    if (startingObject.ContainsKey(item.Key))
                    {
                        //It had the value so we have to loop over it.
                        startingObject[item.Key] = CombineVariant(startingObject[item.Key], item.Value);
                    }
                    else
                    {
                        //It did not have a value so we can just copy the whole tree
                        startingObject[item.Key] = item.Value;
                    }
                }
            }
            else if (type == typeof(ProxyArray))
            {
                ProxyArray combineArray  = combineWith as ProxyArray;
                ProxyArray startingArray = startingVariant as ProxyArray;

                for (int i = 0; i < combineArray.Count; i++)
                {
                    if (combineArray[i] is ProxyObject)
                    {
                        ProxyObject arrayObject = combineArray[i] as ProxyObject;

                        if (arrayObject != null)
                        {
                            if (arrayObject.ContainsKey(ProxyArray.CombineHintName))
                            {
                                int @Index = arrayObject[ProxyArray.CombineHintName];

                                Variant indexVariant = FindIndex(startingArray, @Index);

                                if (indexVariant == null)
                                {
                                    startingArray.Add(arrayObject);
                                }
                                else
                                {
                                    indexVariant = CombineVariant(indexVariant, arrayObject);
                                }
                            }
                        }
                    }
                }
            }
            else if (type == typeof(ProxyNumber))
            {
                startingVariant = combineWith;
            }
            else if (type == typeof(ProxyString))
            {
                startingVariant = combineWith;
            }

            return(startingVariant);
        }