Skybrud.Umbraco.GridData.GridSection.Parse C# (CSharp) Method

Parse() public static method

Parses a section from the specified obj.
public static Parse ( GridDataModel model, Newtonsoft.Json.Linq.JObject obj ) : GridSection
model GridDataModel The parent model of the section.
obj Newtonsoft.Json.Linq.JObject The instance of JObject to be parsed.
return GridSection
        public static GridSection Parse(GridDataModel model, JObject obj) {

            // Some input validation
            if (obj == null) throw new ArgumentNullException("obj");

            // Parse basic properties
            GridSection section = new GridSection(obj) {
                Model = model,
                Grid = obj.GetInt32("grid")
            };

            // Parse the rows
            section.Rows = obj.GetArray("rows", x => GridRow.Parse(section, x)) ?? new GridRow[0];

            // Update "PreviousRow" and "NextRow" properties
            for (int i = 1; i < section.Rows.Length; i++) {
                section.Rows[i - 1].NextRow = section.Rows[i];
                section.Rows[i].PreviousRow = section.Rows[i - 1];
            }

            // Return the section
            return section;

        }

Usage Example

        /// <summary>
        /// Deserializes the specified <paramref name="json"/> string into an instance of <see cref="GridDataModel"/>.
        /// </summary>
        /// <param name="json">The JSON string to be deserialized.</param>
        /// <param name="propertyTypeAlias">The alias of the property the Grid model is representing.</param>
        public static GridDataModel Deserialize(string json, string propertyTypeAlias)
        {
            // Validate the JSON
            if (json == null || !json.StartsWith("{") || !json.EndsWith("}"))
            {
                return(null);
            }

            // Deserialize the JSON
            JObject obj = JObject.Parse(json);

            // Parse basic properties
            GridDataModel model = new GridDataModel(obj)
            {
                Raw           = json,
                Name          = obj.GetString("name"),
                PropertyAlias = propertyTypeAlias
            };

            // Parse the sections
            model.Sections = obj.GetArray("sections", x => GridSection.Parse(model, x)) ?? new GridSection[0];

            // Return the model
            return(model);
        }
All Usage Examples Of Skybrud.Umbraco.GridData.GridSection::Parse