Beispiel #1
0
        public void Parse(IFileSystemAccessor fs, JsonNode root, string Key)
        {
            m_context.Push(Key);

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

            foreach (var kv in root.ObjectItems)
            {
                //Console.WriteLine(kv.Key);
                switch (kv.Key)
                {
                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.Value.Segment;
                    break;
                    #endregion

                    #region Validation
                // http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.1
                case "type":
                    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, true);
                    foreach (var item in kv.Value.ArrayItems)
                    {
                        if (item.ContainsKey("$ref"))
                        {
                            var sub = JsonSchema.ParseFromPath(fs.Get(item["$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.Parse(fs, kv.Key, kv.Value))
                        {
                            continue;
                        }
                    }
                    throw new NotImplementedException(string.Format("unknown key: {0}", kv.Key));
                }
                }
            }
            m_context.Pop();

            if (Validator == null)
            {
                SkipComparison = true;
            }
        }