Esempio n. 1
0
        public static IJsonSchemaValidator Create(ListTreeNode <JsonValue> value)
        {
            foreach (var x in value.ArrayItems())
            {
                if (x.IsInteger() || x.IsFloat())
                {
                    return(JsonIntEnumValidator.Create(value.ArrayItems()
                                                       .Where(y => y.IsInteger() || y.IsFloat())
                                                       .Select(y => y.GetInt32())
                                                       ));
                }
                else if (x.IsString())
                {
                    return(JsonStringEnumValidator.Create(value.ArrayItems()
                                                          .Where(y => y.IsString())
                                                          .Select(y => y.GetString())
                                                          , EnumSerializationType.AsString
                                                          ));
                }
                else
                {
                }
            }

            throw new NotImplementedException();
        }
Esempio n. 2
0
            public static void Deserialize(ListTreeNode <T> src, ref U t)
            {
                if (s_d == null)
                {
                    if (typeof(U).IsEnum)
                    {
                        // enum from string
                        var mi = typeof(Enum).GetMethods(BindingFlags.Static | BindingFlags.Public).First(
                            x => x.Name == "Parse" && x.GetParameters().Length == 3
                            );

                        var enumParse = GenericInvokeCallFactory.StaticFunc <Type, string, bool, object>(mi);
                        s_d = x =>
                        {
                            var enumValue = enumParse(typeof(U), x.GetString(), true);
                            return(GenericCast <object, U> .Cast(enumValue));
                        };
                    }
                    else
                    {
                        s_d = x => GenericCast <string, U> .Cast(x.GetString());
                    }
                }

                t = s_d(src);
            }
Esempio n. 3
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));
        }
            public static U DeserializeField <U>(JsonSchema prop, ListTreeNode <S> s)
            {
                var u = default(U);

                prop.Validator.Deserialize(s, ref u);
                return(u);
            }
        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);
                    }
                }
            }
        }
 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);
 }
Esempio n. 7
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());
            }
        }
Esempio n. 8
0
        public bool FromJsonSchema(IFileSystemAccessor fs, string key, ListTreeNode <JsonValue> value)
        {
            switch (key)
            {
            case "multipleOf":
                MultipleOf = value.GetInt32();
                return(true);

            case "maximum":
                Maximum = value.GetInt32();
                return(true);

            case "exclusiveMaximum":
                ExclusiveMaximum = value.GetBoolean();
                return(true);

            case "minimum":
                Minimum = value.GetInt32();
                return(true);

            case "exclusiveMinimum":
                ExclusiveMinimum = value.GetBoolean();
                return(true);
            }

            return(false);
        }
Esempio n. 9
0
 public static void Deserialize(ListTreeNode <T> src, ref U dst)
 {
     if (s_d == null)
     {
         s_d = s => GenericCast <int, U> .Cast(s.GetInt32());
     }
     dst = s_d(src);
 }
Esempio n. 10
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())
     });
 }
        public bool FromJsonSchema(IFileSystemAccessor fs, string key, ListTreeNode <JsonValue> value)
        {
            switch (key)
            {
            case "maxProperties":
                MaxProperties = value.GetInt32();
                return(true);

            case "minProperties":
                MinProperties = value.GetInt32();
                return(true);

            case "required":
            {
                foreach (var req in value.ArrayItems())
                {
                    m_required.Add(req.GetString());
                }
            }
                return(true);

            case "properties":
            {
                foreach (var prop in value.ObjectItems())
                {
                    AddProperty(fs, prop.Key.GetString(), prop.Value);
                }
            }
                return(true);

            case "patternProperties":
                PatternProperties = value.GetString();
                return(true);

            case "additionalProperties":
            {
                var sub = new JsonSchema();
                sub.Parse(fs, value, "additionalProperties");
                AdditionalProperties = sub;
            }
                return(true);

            case "dependencies":
            {
                foreach (var kv in value.ObjectItems())
                {
                    Dependencies.Add(kv.Key.GetString(),
                                     kv.Value.ArrayItems().Select(x => x.GetString()).ToArray());
                }
            }
                return(true);

            case "propertyNames":
                return(true);
            }

            return(false);
        }
            public static void Deserialize(ListTreeNode <S> src, ref T dst, Dictionary <string, JsonSchema> props)
            {
                if (s_d == null)
                {
                    var target = typeof(T);

                    var fields             = target.GetFields(BindingFlags.Instance | BindingFlags.Public);
                    var fieldDeserializers = fields.ToDictionary(x => Utf8String.From(x.Name), x =>
                    {
                        /*
                         * var mi = typeof(GenericDeserializer<T>).GetMethod("GetFieldDeserializer",
                         *  BindingFlags.Static | BindingFlags.NonPublic);
                         * var g = mi.MakeGenericMethod(x.FieldType);
                         * return (FieldSetter)g.Invoke(null, new object[] { x });
                         */
                        JsonSchema prop;
                        if (!props.TryGetValue(x.Name, out prop))
                        {
                            return(null);
                        }

                        var mi = typeof(GenericDeserializer <S, T>).GetMethod("DeserializeField",
                                                                              BindingFlags.Static | BindingFlags.Public);
                        var g = mi.MakeGenericMethod(x.FieldType);

                        return((FieldSetter)((s, o) =>
                        {
                            var f = g.Invoke(null, new object[] { prop, s });
                            x.SetValue(o, f);
                        }));
                    });

                    s_d = (ListTreeNode <S> s) =>
                    {
                        // boxing
                        var t = (object)Activator.CreateInstance <T>();
                        if (s.IsMap())
                        {
                            foreach (var kv in s.ObjectItems())
                            {
                                FieldSetter setter;
                                if (fieldDeserializers.TryGetValue(kv.Key.GetUtf8String(), out setter))
                                {
                                    if (setter != null)
                                    {
                                        setter(kv.Value, t);
                                    }
                                }
                            }
                        }

                        return((T)t);
                    };
                }

                dst = s_d(src);
            }
 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());
 }
Esempio n. 14
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);
 }
 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);
 }
Esempio n. 16
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,
     });
 }
Esempio n. 17
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);
        }
Esempio n. 18
0
        public U Create(ListTreeNode <T> src)
        {
            if (s_creator == null)
            {
                var d = GetCreator();
                s_creator = new Creator(d);
            }

            return(s_creator(src));
        }
Esempio n. 19
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);
        }
Esempio n. 20
0
        private 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]);
        }
Esempio n. 21
0
 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));
     }
 }
Esempio n. 22
0
 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);
 }
Esempio n. 23
0
 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
     }
 }
Esempio n. 24
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();
 }
Esempio n. 25
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();
        }
Esempio n. 26
0
        private 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);
            }
        }
Esempio n. 27
0
        private static ListTreeNode <MsgPackValue> _Parse(ListTreeNode <MsgPackValue> tree, ArraySegment <Byte> bytes)
        {
            var 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);
            }
        }
Esempio n. 28
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);
        }
        public static ListTreeNode <T> GetArrrayItem <T>(this ListTreeNode <T> self, int index)
            where T : IListTreeItem, IValue <T>
        {
            var i = 0;

            foreach (var v in self.ArrayItems())
            {
                if (i++ == index)
                {
                    return(v);
                }
            }
            throw new KeyNotFoundException();
        }
Esempio n. 30
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()];
            var i = 0;

            foreach (var x in s.ArrayItems())
            {
                x.Deserialize(ref u[i++]);
            }
            return(u);
        }