예제 #1
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);
                    }
                }
            }
        }
예제 #2
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);
 }
예제 #3
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);
        }
예제 #4
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);
            }
        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));
        }
예제 #6
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();
        }
 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 type   = Expression.Constant(typeof(U));
             var value  = Expression.Parameter(typeof(string), "value");
             var ic     = Expression.Constant(true);
             var call   = Expression.Call(mi, type, value, ic);
             var lambda = Expression.Lambda(call, value);
             var func   = (Func <string, object>)lambda.Compile();
             s_d = x => GenericCast <object, U> .Cast(func(x.GetString()));
         }
         else
         {
             s_d = x => GenericCast <string, U> .Cast(x.GetString());
         }
     }
     t = s_d(src);
 }
            static U DeserializeField <U>(JsonSchema prop, ListTreeNode <S> s)
            {
                var u = default(U);

                prop.Validator.Deserialize(s, ref u);
                return(u);
            }
예제 #9
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());
            }
        }
 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);
 }
예제 #11
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 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);
 }
 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());
 }
예제 #14
0
 public static void Deserialize(ListTreeNode <T> src, ref U dst)
 {
     if (s_d == null)
     {
         // enum from int
         s_d = s => GenericCast <int, U> .Cast(s.GetInt32());
     }
     dst = s_d(src);
 }
 public U Create(ListTreeNode <T> src)
 {
     if (s_creator == null)
     {
         var d = GetCreator();
         s_creator = new Creator(d);
     }
     return(s_creator(src));
 }
            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.NonPublic);
                        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) =>
                    {
                        if (!s.IsMap())
                        {
                            throw new ArgumentException(s.Value.ValueType.ToString());
                        }

                        // boxing
                        var t = (object)Activator.CreateInstance <T>();
                        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 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 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);
 }
예제 #19
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,
     });
 }
예제 #20
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);
        }
            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]);
            }
예제 #22
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));
     }
 }
예제 #23
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);
 }
예제 #24
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
     }
 }
 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 string GetObjectValueOrDefault <T>(this ListTreeNode <T> self, String key, string defualtValue)
     where T : IListTreeItem, IValue <T>
 {
     try
     {
         return(self[key].GetString());
     }
     catch (KeyNotFoundException)
     {
         return(defualtValue);
     }
 }
예제 #27
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);
            }
        }
예제 #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 void Deserialize(ListTreeNode <T> src, ref U dst)
 {
     if (s_d == null)
     {
         // enum from int
         var value  = Expression.Parameter(typeof(int), "value");
         var cast   = Expression.Convert(value, typeof(U));
         var lambda = Expression.Lambda(cast, value);
         var func   = (Func <int, U>)lambda.Compile();
         s_d = s => func(s.GetInt32());
     }
     dst = s_d(src);
 }
        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();
        }