예제 #1
0
 /// <summary>Creates a default <see cref="JsonObjectModel"/> from the given schema. </summary>
 /// <param name="schema">The <see cref="JsonSchema4"/>. </param>
 /// <returns>The <see cref="JsonObjectModel"/>. </returns>
 public static JsonObjectModel FromSchema(JsonSchema4 schema)
 {
     var obj = new JsonObjectModel();
     foreach (var property in schema.Properties)
     {
         if (property.Value.Type.HasFlag(JsonObjectType.Object))
         {
             if (property.Value.IsRequired)
                 obj[property.Key] = FromSchema(property.Value);
             else
                 obj[property.Key] = null;
         }
         else if (property.Value.Type.HasFlag(JsonObjectType.Array))
         {
             if (property.Value.IsRequired)
                 obj[property.Key] = new ObservableCollection<JsonTokenModel>();
             else
                 obj[property.Key] = null;
         }
         else
             obj[property.Key] = GetDefaultValue(property);
     }
     obj.Schema = schema;
     return obj;
 }
예제 #2
0
        /// <summary>Creates a <see cref="JsonObjectModel"/> from the given <see cref="JObject"/> and <see cref="JsonSchema4"/>. </summary>
        /// <param name="obj">The <see cref="JObject"/>. </param>
        /// <param name="schema">The <see cref="JsonSchema4"/>. </param>
        /// <returns>The <see cref="JsonObjectModel"/>. </returns>
        public static JsonObjectModel FromJson(JObject obj, JsonSchema schema)
        {
            schema = schema.ActualSchema;

            var result = new JsonObjectModel();

            foreach (var property in schema.Properties)
            {
                var propertySchema = property.Value.ActualSchema;
                if (propertySchema.Type.HasFlag(JsonObjectType.Array))
                {
                    var propertyItemSchema = propertySchema.Item != null ? propertySchema.Item.ActualSchema : null;
                    if (obj[property.Key] != null)
                    {
                        var objects = obj[property.Key].Select(o => o is JObject ?
                                                               (JsonTokenModel)FromJson((JObject)o, propertyItemSchema) :
                                                               JsonValueModel.FromJson((JValue)o, propertyItemSchema));

                        var list = new ObservableCollection <JsonTokenModel>(objects);
                        foreach (var item in list)
                        {
                            item.ParentList = list;
                        }

                        result[property.Key] = list;
                    }
                    else
                    {
                        result[property.Key] = null;
                    }
                }
                else if (propertySchema.Type.HasFlag(JsonObjectType.Object) || propertySchema.Type == JsonObjectType.None)
                {
                    var token = obj[property.Key];
                    if (token is JObject)
                    {
                        result[property.Key] = FromJson((JObject)token, propertySchema);
                    }
                    else
                    {
                        result[property.Key] = null;
                    }
                }
                else
                {
                    JToken value;
                    if (obj.TryGetValue(property.Key, out value))
                    {
                        result[property.Key] = ((JValue)value).Value;
                    }
                    else
                    {
                        result[property.Key] = GetDefaultValue(property);
                    }
                }
            }
            result.Schema = schema;
            return(result);
        }
        /// <summary>Initializes a new instance of the <see cref="JsonPropertyModel"/> class. </summary>
        /// <param name="name">The name of the property. </param>
        /// <param name="parent">The parent object. </param>
        /// <param name="schema">The property type as schema object. </param>
        public JsonPropertyModel(string name, JsonObjectModel parent, JsonProperty schema)
        {
            Name = name;
            Parent = parent;
            Schema = schema;

            Parent.PropertyChanged += (sender, args) => RaisePropertyChanged(() => Value);
        }
예제 #4
0
        /// <summary>Initializes a new instance of the <see cref="JsonPropertyModel"/> class. </summary>
        /// <param name="name">The name of the property. </param>
        /// <param name="parent">The parent object. </param>
        /// <param name="schema">The property type as schema object. </param>
        public JsonPropertyModel(string name, JsonObjectModel parent, JsonSchemaProperty schema)
        {
            Name   = name;
            Parent = parent;
            Schema = schema;

            Parent.PropertyChanged += (sender, args) => RaisePropertyChanged(() => Value);
        }
예제 #5
0
        /// <summary>Creates a new <see cref="JsonDocumentModel"/> based on a given schema file path. </summary>
        /// <param name="schemaPath">The schema file path. </param>
        /// <param name="dispatcher">The UI dispatcher. </param>
        /// <returns>The <see cref="JsonDocumentModel"/>. </returns>
        public static Task <JsonDocumentModel> CreateAsync(string schemaPath, IDispatcher dispatcher)
        {
            return(Task.Run(() =>
            {
                var schema = JsonSchema4.FromFileAsync(schemaPath).GetAwaiter().GetResult();
                var data = JsonObjectModel.FromSchema(schema);

                var document = new JsonDocumentModel();
                document.Initialize(data, dispatcher);
                return document;
            }));
        }
예제 #6
0
        /// <summary>Creates a new <see cref="JsonDocumentModel"/> based on a given schema file path. </summary>
        /// <param name="schemaPath">The schema file path. </param>
        /// <param name="dispatcher">The UI dispatcher. </param>
        /// <returns>The <see cref="JsonDocumentModel"/>. </returns>
        public static Task <JsonDocumentModel> CreateAsync(string schemaPath, IDispatcher dispatcher)
        {
            return(Task.Run(() =>
            {
                var schema = JsonSchema4.FromJson(File.ReadAllText(schemaPath, Encoding.UTF8));
                var data = JsonObjectModel.FromSchema(schema);

                var document = new JsonDocumentModel();
                document.Initialize(data, dispatcher);
                return document;
            }));
        }
예제 #7
0
        /// <summary>Loads a <see cref="JsonDocumentModel"/> from a file path and schema path. </summary>
        /// <param name="filePath">The file path. </param>
        /// <param name="schemaPath">The schema path. </param>
        /// <param name="dispatcher">The UI dispatcher. </param>
        /// <returns>The <see cref="JsonDocumentModel"/>. </returns>
        public static Task <JsonDocumentModel> LoadAsync(string filePath, string schemaPath, IDispatcher dispatcher)
        {
            return(Task.Run(() =>
            {
                var schema = JsonSchema4.FromFileAsync(schemaPath).GetAwaiter().GetResult();
                var data = JsonObjectModel.FromJson(File.ReadAllText(filePath, Encoding.UTF8), schema);

                var document = new JsonDocumentModel();
                document.Initialize(data, dispatcher);
                document.FilePath = filePath;
                document.SchemaPath = schemaPath;
                return document;
            }));
        }
예제 #8
0
        /// <summary>Creates a default <see cref="JsonObjectModel"/> from the given schema. </summary>
        /// <param name="schema">The <see cref="JsonSchema4"/>. </param>
        /// <returns>The <see cref="JsonObjectModel"/>. </returns>
        public static JsonObjectModel FromSchema(JsonSchema schema)
        {
            schema = schema.ActualSchema;

            var obj = new JsonObjectModel();

            foreach (var property in schema.Properties)
            {
                var propertySchema = property.Value.ActualSchema;
                if (propertySchema.Type.HasFlag(JsonObjectType.Object))
                {
                    if (property.Value.IsRequired)
                    {
                        obj[property.Key] = FromSchema(propertySchema);
                    }
                    else
                    {
                        obj[property.Key] = null;
                    }
                }
                else if (propertySchema.Type.HasFlag(JsonObjectType.Array))
                {
                    if (property.Value.IsRequired)
                    {
                        obj[property.Key] = new ObservableCollection <JsonTokenModel>();
                    }
                    else
                    {
                        obj[property.Key] = null;
                    }
                }
                else
                {
                    obj[property.Key] = GetDefaultValue(property);
                }
            }
            obj.Schema = schema;
            return(obj);
        }
예제 #9
0
        /// <summary>Creates a <see cref="JsonObjectModel"/> from the given <see cref="JObject"/> and <see cref="JsonSchema4"/>. </summary>
        /// <param name="obj">The <see cref="JObject"/>. </param>
        /// <param name="schema">The <see cref="JsonSchema4"/>. </param>
        /// <returns>The <see cref="JsonObjectModel"/>. </returns>
        public static JsonObjectModel FromJson(JObject obj, JsonSchema4 schema)
        {
            var result = new JsonObjectModel();
            foreach (var property in schema.Properties)
            {
                if (property.Value.Type.HasFlag(JsonObjectType.Array))
                {
                    var propertySchema = property.Value.Item;
                    var objects = obj[property.Key].Select(o => o is JObject ?
                        (JsonTokenModel)FromJson((JObject)o, propertySchema) : JsonValueModel.FromJson((JValue)o, propertySchema));

                    var list = new ObservableCollection<JsonTokenModel>(objects);
                    foreach (var item in list)
                        item.ParentList = list;

                    result[property.Key] = list;
                }
                else if (property.Value.Type.HasFlag(JsonObjectType.Object))
                {
                    var token = obj[property.Key];
                    if (token is JObject)
                        result[property.Key] = FromJson((JObject)token, property.Value);
                    else
                        result[property.Key] = null;
                }
                else
                {
                    JToken value;
                    if (obj.TryGetValue(property.Key, out value))
                        result[property.Key] = ((JValue)value).Value;
                    else
                        result[property.Key] = GetDefaultValue(property);
                }
            }
            result.Schema = schema;
            return result;
        }
예제 #10
0
 /// <summary>Initializes the document. </summary>
 /// <param name="data">The JSON data. </param>
 /// <param name="dispatcher">The UI dispatcher. </param>
 public void Initialize(JsonObjectModel data, IDispatcher dispatcher)
 {
     UndoRedoManager = new UndoRedoManager(data, dispatcher);
     Data = data;
 }
예제 #11
0
 /// <summary>Initializes the document. </summary>
 /// <param name="data">The JSON data. </param>
 /// <param name="dispatcher">The UI dispatcher. </param>
 public void Initialize(JsonObjectModel data, IDispatcher dispatcher)
 {
     UndoRedoManager = new UndoRedoManager(data, dispatcher);
     Data            = data;
 }