Beispiel #1
0
        public static object DefaultDictionaryDeserializer(ListTreeNode <T> s)
        {
            switch (s.Value.ValueType)
            {
            case ValueNodeType.Object:
            {
                var u = new Dictionary <string, object>();
                foreach (var kv in s.ObjectItems())
                {
                    //var e = default(object);
                    //kv.Value.Deserialize(ref e);
                    u.Add(kv.Key.GetString(), DefaultDictionaryDeserializer(kv.Value));
                }
                return(u);
            }

            case ValueNodeType.Null:
                return(null);

            case ValueNodeType.Boolean:
                return(s.GetBoolean());

            case ValueNodeType.Integer:
                return(s.GetInt32());

            case ValueNodeType.Number:
                return(s.GetDouble());

            case ValueNodeType.String:
                return(s.GetString());

            default:
                throw new NotImplementedException(s.Value.ValueType.ToString());
            }
        }
Beispiel #2
0
        public static IEnumerable <ListTreeNode <T> > Traverse <T>(this ListTreeNode <T> self) where T : IListTreeItem, IValue <T>
        {
            yield return(self);

            if (self.IsArray())
            {
                foreach (var x in self.ArrayItems())
                {
                    foreach (var y in x.Traverse())
                    {
                        yield return(y);
                    }
                }
            }
            else if (self.IsMap())
            {
                foreach (var kv in self.ObjectItems())
                {
                    foreach (var y in kv.Value.Traverse())
                    {
                        yield return(y);
                    }
                }
            }
        }
Beispiel #3
0
 public static bool IsFloat <T>(this ListTreeNode <T> self) where T : IListTreeItem, IValue <T>
 {
     return(self.Value.ValueType == ValueNodeType.Number ||
            self.Value.ValueType == ValueNodeType.NaN ||
            self.Value.ValueType == ValueNodeType.Infinity ||
            self.Value.ValueType == ValueNodeType.MinusInfinity);
 }
Beispiel #4
0
        public static bool ContainsKey <T>(this ListTreeNode <T> self, String key)
            where T : IListTreeItem, IValue <T>
        {
            var ukey = Utf8String.From(key);

            return(self.ContainsKey(ukey));
        }
Beispiel #5
0
 public static JsonPointer Create <T>(ListTreeNode <T> node)
     where T : IListTreeItem,     IValue <T>
 {
     return(new JsonPointer
     {
         Path = new ArraySegment <Utf8String>(node.Path().Skip(1).Select(x => GetKeyFromParent(x)).ToArray())
     });
 }
Beispiel #6
0
 public static IEnumerable <ListTreeNode <T> > ArrayItems <T>(this ListTreeNode <T> self) where T : IListTreeItem, IValue <T>
 {
     if (!self.IsArray())
     {
         throw new DeserializationException("is not array");
     }
     return(self.Children);
 }
Beispiel #7
0
 public static int GetObjectCount <T>(this ListTreeNode <T> self)
     where T : IListTreeItem, IValue <T>
 {
     if (!self.IsMap())
     {
         throw new DeserializationException("is not object");
     }
     return(self.Children.Count() / 2);
 }
Beispiel #8
0
 public static int GetArrayCount <T>(this ListTreeNode <T> self)
     where T : IListTreeItem, IValue <T>
 {
     if (!self.IsArray())
     {
         throw new DeserializationException("is not array");
     }
     return(self.Children.Count());
 }
Beispiel #9
0
 public static void Deserialize(ListTreeNode <T> node, ref U value)
 {
     if (s_deserializer == null)
     {
         var d = GetDeserializer();
         s_deserializer = new Deserializer(d);
     }
     value = s_deserializer(node);
 }
Beispiel #10
0
 public U Create(ListTreeNode <T> src)
 {
     if (s_creator == null)
     {
         var d = GetCreator();
         s_creator = new Creator(d);
     }
     return(s_creator(src));
 }
Beispiel #11
0
 public static JsonDiff Create <T>(ListTreeNode <T> node, JsonDiffType diffType, string msg)
     where T : IListTreeItem, IValue <T>
 {
     return(new JsonDiff
     {
         Path = JsonPointer.Create(node),
         DiffType = diffType,
         Msg = msg,
     });
 }
Beispiel #12
0
        static V[] ArrayCreator <V>(ListTreeNode <T> src)
        {
            if (!src.IsArray())
            {
                throw new ArgumentException("value is not array");
            }
            var count = src.GetArrayCount();

            return(new V[count]);
        }
Beispiel #13
0
        public void Call(IRpc f, int id, string method, ListTreeNode <T> args)
        {
            Callback callback;

            if (!m_map.TryGetValue(method, out callback))
            {
                throw new KeyNotFoundException();
            }
            callback(id, args, f);
        }
 public static void SetValue <T>(this ListTreeNode <T> self,
                                 Utf8String jsonPointer, ArraySegment <Byte> bytes)
     where T : IListTreeItem, IValue <T>
 {
     foreach (var node in self.GetNodes(jsonPointer))
     {
         node.SetValue(default(T).New(
                           bytes,
                           ValueNodeType.Boolean,
                           node.Value.ParentIndex));
     }
 }
Beispiel #15
0
 public static Utf8String KeyOf <T>(this ListTreeNode <T> self, ListTreeNode <T> node)
     where T : IListTreeItem, IValue <T>
 {
     foreach (var kv in self.ObjectItems())
     {
         if (node.ValueIndex == kv.Value.ValueIndex)
         {
             return(kv.Key.GetUtf8String());
         }
     }
     throw new KeyNotFoundException();
 }
 public static IEnumerable <ListTreeNode <T> > Path <T>(this ListTreeNode <T> self)
     where T : IListTreeItem, IValue <T>
 {
     if (self.HasParent)
     {
         foreach (var x in self.Parent.Path())
         {
             yield return(x);
         }
     }
     yield return(self);
 }
 public static void RemoveValue <T>(this ListTreeNode <T> self, Utf8String jsonPointer)
     where T : IListTreeItem, IValue <T>
 {
     foreach (var node in self.GetNodes(new JsonPointer(jsonPointer)))
     {
         if (node.Parent.IsMap())
         {
             node.Prev.SetValue(default(T)); // remove key
         }
         node.SetValue(default(T));          // remove
     }
 }
Beispiel #18
0
        public static ListTreeNode <T> GetObjectItem <T>(this ListTreeNode <T> self, Utf8String key)
            where T : IListTreeItem, IValue <T>

        {
            foreach (var kv in self.ObjectItems())
            {
                if (kv.Key.GetUtf8String() == key)
                {
                    return(kv.Value);
                }
            }
            throw new KeyNotFoundException();
        }
Beispiel #19
0
        static ListTreeNode <JsonValue> ParseString(ListTreeNode <JsonValue> tree, Utf8String segment)
        {
            int pos;

            if (segment.TrySearchAscii((Byte)'"', 1, out pos))
            {
                return(tree.AddValue(segment.Subbytes(0, pos + 1).Bytes, ValueNodeType.String));
            }
            else
            {
                throw new ParserException("no close string: " + segment);
            }
        }
Beispiel #20
0
        /// <summary>
        /// Deserialize Dictionary only string key
        /// </summary>
        /// <typeparam name="V"></typeparam>
        /// <param name="s"></param>
        /// <returns></returns>
        public static Dictionary <string, V> DictionaryDeserializer <V>(ListTreeNode <T> s)
        {
            var d = new Dictionary <string, V>();

            foreach (var kv in s.ObjectItems())
            {
                var value = default(V);
                GenericDeserializer <T, V> .Deserialize(kv.Value, ref value);

                d.Add(kv.Key.GetString(), value);
            }
            return(d);
        }
Beispiel #21
0
 public static Nullable <V> GenericNullableDeserializer <V>(ListTreeNode <T> parsed) where V : struct
 {
     if (!parsed.IsNull())
     {
         var c = default(V);
         parsed.Deserialize(ref c);
         return(c);
     }
     else
     {
         return(default(V));
     }
 }
Beispiel #22
0
        static ListTreeNode <MsgPackValue> _Parse(ListTreeNode <MsgPackValue> tree, ArraySegment <Byte> bytes)
        {
            MsgPackType formatType = GetFormat(bytes);

            if (formatType.IsArray())
            {
                var array = tree.AddValue(bytes, ValueNodeType.Array);

                uint count;
                bytes = GetItemCount(bytes, formatType, out count);
                for (var i = 0; i < count; ++i)
                {
                    var child = _Parse(array, bytes);
                    bytes = bytes.Advance(child.Value.Bytes.Count);
                }

                array.SetValueBytesCount(bytes.Offset - array.Value.Bytes.Offset);

                return(array);
            }
            else if (formatType.IsMap())
            {
                var obj = tree.AddValue(bytes, ValueNodeType.Object);

                uint count;
                bytes = GetItemCount(bytes, formatType, out count);
                for (var i = 0; i < count; ++i)
                {
                    // key
                    var key = _Parse(obj, bytes);
                    bytes = bytes.Advance(key.Value.Bytes.Count);

                    // value
                    var value = _Parse(obj, bytes);
                    bytes = bytes.Advance(value.Value.Bytes.Count);
                }

                obj.SetValueBytesCount(bytes.Offset - obj.Value.Bytes.Offset);

                return(obj);
            }
            else
            {
                var body       = GetBody(bytes, formatType);
                var headerSize = body.Offset - bytes.Offset;
                var size       = headerSize + body.Count;

                var value = tree.AddValue(bytes.Take(size), ValueNodeType.Null);
                return(value);
            }
        }
Beispiel #23
0
        public static ListTreeNode <T> GetArrrayItem <T>(this ListTreeNode <T> self, int index)
            where T : IListTreeItem, IValue <T>
        {
            int i = 0;

            foreach (var v in self.ArrayItems())
            {
                if (i++ == index)
                {
                    return(v);
                }
            }
            throw new KeyNotFoundException();
        }
Beispiel #24
0
        public static V[] GenericArrayDeserializer <V>(ListTreeNode <T> s)
        {
            if (!s.IsArray())
            {
                throw new ArgumentException("not array: " + s.Value.ValueType);
            }
            var u = new V[s.GetArrayCount()];
            int i = 0;

            foreach (var x in s.ArrayItems())
            {
                x.Deserialize(ref u[i++]);
            }
            return(u);
        }
Beispiel #25
0
        public static int IndexOf <T>(this ListTreeNode <T> self, ListTreeNode <T> child)
            where T : IListTreeItem, IValue <T>
        {
            int i = 0;

            foreach (var v in self.ArrayItems())
            {
                if (v.ValueIndex == child.ValueIndex)
                {
                    return(i);
                }
                ++i;
            }
            throw new KeyNotFoundException();
        }
Beispiel #26
0
        public static List <V> GenericListDeserializer <V>(ListTreeNode <T> s)
        {
            if (!s.IsArray())
            {
                throw new ArgumentException("not array: " + s.Value.ValueType);
            }
            var u = new List <V>(s.GetArrayCount());

            foreach (var x in s.ArrayItems())
            {
                var e = default(V);
                x.Deserialize(ref e);
                u.Add(e);
            }
            return(u);
        }
Beispiel #27
0
        /// <summary>
        /// Expected null, boolean, integer, number
        /// </summary>
        /// <param name="segment"></param>
        /// <param name="valueType"></param>
        /// <param name="parentIndex"></param>
        /// <returns></returns>
        static ListTreeNode <JsonValue> ParsePrimitive(ListTreeNode <JsonValue> tree, Utf8String segment, ValueNodeType valueType)
        {
            int i = 1;

            for (; i < segment.ByteLength; ++i)
            {
                if (Char.IsWhiteSpace((char)segment[i]) ||
                    segment[i] == '}' ||
                    segment[i] == ']' ||
                    segment[i] == ',' ||
                    segment[i] == ':'
                    )
                {
                    break;
                }
            }
            return(tree.AddValue(segment.Subbytes(0, i).Bytes, valueType));
        }
Beispiel #28
0
        static Utf8String GetKeyFromParent <T>(ListTreeNode <T> json)
            where T : IListTreeItem, IValue <T>
        {
            var parent = json.Parent;

            if (parent.IsArray())
            {
                var index = parent.IndexOf(json);
                return(Utf8String.From(index));
            }
            else if (parent.IsMap())
            {
                return(parent.KeyOf(json));
            }
            else
            {
                throw new NotImplementedException();
            }
        }
Beispiel #29
0
        public static ListTreeNode <JsonValue> Parse(ListTreeNode <JsonValue> tree, Utf8String segment)
        {
            // skip white space
            int pos;

            if (!segment.TrySearchByte(x => !char.IsWhiteSpace((char)x), out pos))
            {
                throw new ParserException("only whitespace");
            }
            segment = segment.Subbytes(pos);

            var valueType = GetValueType(segment);

            switch (valueType)
            {
            case ValueNodeType.Boolean:
            case ValueNodeType.Integer:
            case ValueNodeType.Number:
            case ValueNodeType.Null:
            case ValueNodeType.NaN:
            case ValueNodeType.Infinity:
            case ValueNodeType.MinusInfinity:
                return(ParsePrimitive(tree, segment, valueType));

            case ValueNodeType.String:
                return(ParseString(tree, segment));

            case ValueNodeType.Array:     // fall through
                return(ParseArray(tree, segment));

            case ValueNodeType.Object:     // fall through
                return(ParseObject(tree, segment));

            default:
                throw new NotImplementedException();
            }
        }
Beispiel #30
0
        public static IEnumerable <KeyValuePair <ListTreeNode <T>, ListTreeNode <T> > > ObjectItems <T>(this ListTreeNode <T> self)
            where T : IListTreeItem, IValue <T>
        {
            if (!self.IsMap())
            {
                throw new DeserializationException("is not object");
            }
            var it = self.Children.GetEnumerator();

            while (it.MoveNext())
            {
                var key = it.Current;

                it.MoveNext();
                yield return(new KeyValuePair <ListTreeNode <T>, ListTreeNode <T> >(key, it.Current));
            }
        }