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);
        }
示例#2
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> > 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);
                    }
                }
            }
        }
示例#4
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();
 }
示例#5
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();
        }
示例#6
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);
        }
示例#7
0
        public void Parse(IFileSystemAccessor fs, ListTreeNode <JsonValue> root, string Key)
        {
            m_context.Push(Key);

            var compositionType = default(CompositionType);
            var composition     = new List <JsonSchema>();

            foreach (var kv in root.ObjectItems())
            {
                switch (kv.Key.GetString())
                {
                case "$schema":
                    Schema = kv.Value.GetString();
                    break;

                case "$ref":
                {
                    var refFs = fs.Get(kv.Value.GetString());

                    // parse JSON
                    var json    = refFs.ReadAllText();
                    var refRoot = JsonParser.Parse(json);

                    Parse(refFs, refRoot, "$ref");
                }
                break;

                    #region Annotation

                case "title":
                    Title = kv.Value.GetString();
                    break;

                case "description":
                    Description = kv.Value.GetString();
                    break;

                case "default":
                    Default = kv.Value;
                    break;

                    #endregion

                    #region Validation

                // http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.1
                case "type":
                    if (Validator == null)
                    {
                        Validator = JsonSchemaValidatorFactory.Create(kv.Value.GetString());
                    }
                    break;

                case "enum":
                    Validator = JsonEnumValidator.Create(kv.Value);
                    break;

                case "const":
                    break;

                    #endregion

                    #region Composite

                // http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.7
                case "oneOf":
                    break;

                case "not":
                    break;

                case "anyOf":     // composition
                case "allOf":     // composition
                {
                    compositionType =
                        (CompositionType)Enum.Parse(typeof(CompositionType), kv.Key.GetString(), true);
                    foreach (var item in kv.Value.ArrayItems())
                    {
                        if (item.ContainsKey(s_ref))
                        {
                            var sub = ParseFromPath(fs.Get(item[s_ref].GetString()));
                            composition.Add(sub);
                        }
                        else
                        {
                            var sub = new JsonSchema();
                            sub.Parse(fs, item, compositionType.ToString());
                            composition.Add(sub);
                        }
                    }

                    Composite(compositionType, composition);
                }
                break;

                    #endregion

                // http://json-schema.org/latest/json-schema-validation.html#rfc.section.7
                case "format":
                    break;

                    #region Gltf

                case "gltf_detailedDescription":
                    break;

                case "gltf_webgl":
                    break;

                case "gltf_uriType":
                    break;

                    #endregion

                default:
                {
                    if (Validator != null)
                    {
                        if (Validator.FromJsonSchema(fs, kv.Key.GetString(), kv.Value))
                        {
                            continue;
                        }
                    }
                    throw new NotImplementedException(string.Format("unknown key: {0}", kv.Key));
                }
                }
            }
            m_context.Pop();

            if (Validator == null)
            {
                SkipComparison = true;
            }
        }
示例#8
0
        public static IEnumerable <ListTreeNode <T> > GetNodes <T>(this ListTreeNode <T> self,
                                                                   JsonPointer jsonPointer)
            where T : IListTreeItem, IValue <T>
        {
            if (jsonPointer.Path.Count == 0)
            {
                yield return(self);

                yield break;
            }

            if (self.IsArray())
            {
                // array
                if (jsonPointer[0][0] == '*')
                {
                    // wildcard
                    foreach (var child in self.ArrayItems())
                    {
                        foreach (var childChild in child.GetNodes(jsonPointer.Unshift()))
                        {
                            yield return(childChild);
                        }
                    }
                }
                else
                {
                    var index = jsonPointer[0].ToInt32();
                    var child = self.ArrayItems().Skip(index).First();
                    foreach (var childChild in child.GetNodes(jsonPointer.Unshift()))
                    {
                        yield return(childChild);
                    }
                }
            }
            else if (self.IsMap())
            {
                // object
                if (jsonPointer[0][0] == '*')
                {
                    // wildcard
                    foreach (var kv in self.ObjectItems())
                    {
                        foreach (var childChild in kv.Value.GetNodes(jsonPointer.Unshift()))
                        {
                            yield return(childChild);
                        }
                    }
                }
                else
                {
                    ListTreeNode <T> child;
                    try
                    {
                        child = self.ObjectItems().First(x => x.Key.GetUtf8String() == jsonPointer[0]).Value;
                    }
                    catch (Exception)
                    {
                        // key
                        self.AddKey(jsonPointer[0]);
                        // value
                        self.AddValue(default(ArraySegment <byte>), ValueNodeType.Object);

                        child = self.ObjectItems().First(x => x.Key.GetUtf8String() == jsonPointer[0]).Value;
                    }

                    foreach (var childChild in child.GetNodes(jsonPointer.Unshift()))
                    {
                        yield return(childChild);
                    }
                }
            }
            else
            {
                throw new NotImplementedException();
            }
        }
示例#9
0
 public static bool ContainsKey <T>(this ListTreeNode <T> self, Utf8String key)
     where T : IListTreeItem, IValue <T>
 {
     return(self.ObjectItems().Any(x => x.Key.GetUtf8String() == key));
 }
示例#10
0
        public IEnumerable <JsonDiff> Diff(ListTreeNode <T> rhs, JsonPointer path = default(JsonPointer))
        {
            switch (Value.ValueType)
            {
            case ValueNodeType.Null:
            case ValueNodeType.Boolean:
            case ValueNodeType.Number:
            case ValueNodeType.Integer:
            case ValueNodeType.String:
                if (!Equals(rhs))
                {
                    yield return(JsonDiff.Create(this, JsonDiffType.ValueChanged,
                                                 string.Format("{0} => {1}", Value, rhs.Value)));
                }
                yield break;
            }

            if (Value.ValueType != rhs.Value.ValueType)
            {
                yield return(JsonDiff.Create(this, JsonDiffType.ValueChanged,
                                             string.Format("{0} => {1}", Value.ValueType, rhs.Value)));

                yield break;
            }

            if (Value.ValueType == ValueNodeType.Object)
            {
                var l = this.ObjectItems().ToDictionary(x => x.Key, x => x.Value);
                var r = rhs.ObjectItems().ToDictionary(x => x.Key, x => x.Value);

                foreach (var kv in l)
                {
                    ListTreeNode <T> x;
                    if (r.TryGetValue(kv.Key, out x))
                    {
                        r.Remove(kv.Key);
                        // Found
                        foreach (var y in kv.Value.Diff(x))
                        {
                            yield return(y);
                        }
                    }
                    else
                    {
                        // Removed
                        yield return(JsonDiff.Create(kv.Value, JsonDiffType.KeyRemoved, kv.Value.Value.ToString()));
                    }
                }

                foreach (var kv in r)
                {
                    // Addded
                    yield return(JsonDiff.Create(kv.Value, JsonDiffType.KeyAdded, kv.Value.Value.ToString()));
                }
            }
            else if (Value.ValueType == ValueNodeType.Array)
            {
                var ll = this.ArrayItems().GetEnumerator();
                var rr = rhs.ArrayItems().GetEnumerator();
                while (true)
                {
                    var lll = ll.MoveNext();
                    var rrr = rr.MoveNext();
                    if (lll && rrr)
                    {
                        foreach (var y in ll.Current.Diff(rr.Current))
                        {
                            yield return(y);
                        }
                    }
                    else if (lll)
                    {
                        yield return(JsonDiff.Create(ll.Current, JsonDiffType.KeyRemoved, ll.Current.Value.ToString()));
                    }
                    else if (rrr)
                    {
                        yield return(JsonDiff.Create(rr.Current, JsonDiffType.KeyAdded, rr.Current.Value.ToString()));
                    }
                    else
                    {
                        break;
                    }
                }
            }
            else
            {
                throw new NotImplementedException();
            }
        }