Example #1
0
        /// <summary>
        /// The table data is read from the JSON token and then inserted
        /// in the table struct.
        /// </summary>
        /// <param name="tableData"></param>
        /// <param name="tableStruct"></param>
        /// <returns></returns>
        private static Table ParseData(JToken tableData, ref TableStruct tableStruct)
        {
            tableStruct.Table.id       = tableData["id"]?.ToObject <string>();
            tableStruct.Table.metadata = tableData["metadata"]?.ToObject <TableMetaData>();

            JArray entities = tableData["data"].Value <JArray>();

            // set the count of the amount of entities in the json
            tableStruct.Ujson.EntityCount = (uint)entities.Count;

            uint i = 0;

            // add ID if we dont have one
            // Debug.Log(rowProperties["id"]);
            // Debug.Log(rowProperties["id"] == null);
            // if (rowProperties["id"] == null)
            // {
            // result.Fields.Add(new TableRowInfo{ ColumnName = "id", ColumnID = i }, new TableField
            // {
            // Data = entityID
            // });
            // i++;
            // }

            // Loop through all the entities
            foreach (var el in entities)
            {
                var      rowParameters = el.Children <JProperty>();
                TableRow tblRow        = new TableRow
                {
                    RowId = i
                };

                if (i == 0)
                {
                    // Set the amount of columns existing in all the entities
                    tableStruct.Ujson.ColumnCount = (uint)rowParameters.Count();

                    // We need to story all the data in one large array
                    // _ujson.JsonColumnEntries.Capacity = (int)(_ujson.ColumnCount * _ujson.EntityCount);
                }

                uint j = 0;
                foreach (var entity in rowParameters)
                {
                    TableField field = new TableField();

                    switch (entity.Value.Type)
                    {
                    case JTokenType.Boolean:
                        field.Data = entity.ToObject <bool>();
                        // Debug.Log("Bool");
                        break;

                    case JTokenType.Integer:
                        field.Data = entity.Value.ToObject <double>();
                        // Debug.Log("Number");
                        break;

                    case JTokenType.String:
                        field.Data = entity.Value.ToObject <string>();
                        // Debug.Log("String");
                        break;

                    case JTokenType.Object:
                        field.Data = entity.Value.ToObject <JObject>();
                        // Debug.Log(field.Data.ToString());
                        break;
                    }

                    // From there we need store the entries
                    JsonEntry entry = new JsonEntry {
                        Name = entity.Name, Type = entity.Value.Type
                    };
                    tableStruct.Ujson.JsonColumnEntries.Add(entry);
                    tableStruct.Data.Add(field);

                    // Add the field to the table row as well.
                    tblRow.Fields.Add(new TableRowInfo {
                        ColumnName = entity.Name, ColumnID = j
                    }, field);
                    j++;
                }

                tableStruct.Table.Rows.Add(i, tblRow);
                i++;
            }

            return(tableStruct.Table);
        }
Example #2
0
        /// <summary>
        /// Import the JSON data and read from it.
        /// </summary>
        /// <param name="jsonTableData"></param>
        public void Import(JToken jsonTableData)
        {
            _listUpdated = true;

            // Get highest ID
            uint highestID = 0;

            if (jsonTableData["id"] == null || jsonTableData["data"] == null || jsonTableData["metadata"] == null)
            {
                throw new ArgumentException("JSON does not contains the property id, data or metadata");
            }

            if (m_ConfigFile == null)
            {
                throw new ArgumentException("Database Config file could not be found");
            }

            // The key-value data
            JObject entries = jsonTableData["data"].Value <JObject>();
            // The metadata from the json stream
            JObject metaData = jsonTableData["metadata"].Value <JObject>();

            // set the count of the amount of entities in the json
            _ujson.EntityCount = (uint)entries.Count;

            // {"Sellable":true,"created_at":1594979553,"deleted":false,"effectPrimaryValue":0,"effectTypeId":0,"name":"","sellValue":0,"updated_at":1594979553}

            // Check change in column
            // _UJSON.Entries = new List<JSONEntry>((int)_UJSON.EntityCount);

            JArray children = new JArray();

            int i = 0;

            foreach (var row in entries)
            {
                JObject entity = row.Value.ToObject <JObject>();
                // UInt32 j = UInt32.Parse(r.Key) * _UJSON.ColumnCount;

                highestID = (uint)Mathf.Max(highestID, uint.Parse(row.Key));

                foreach (var column in entity)
                {
                    TableField field = new TableField();

                    switch (column.Value.Type)
                    {
                    case JTokenType.Boolean:
                        field.Data = column.Value.ToObject <bool>();
                        // Debug.Log("Bool");
                        break;

                    case JTokenType.Integer:
                        field.Data = column.Value.ToObject <double>();
                        // Debug.Log("Number");
                        break;

                    case JTokenType.String:
                        field.Data = column.Value.ToObject <string>();
                        // Debug.Log("String");
                        break;

                    case JTokenType.Object:
                        field.Data = column.Value.ToObject <JObject>();
                        break;
                    }

                    if (i == 0)
                    {
                        // Set the amount of columns existing in all the entities
                        _ujson.ColumnCount = (uint)entity.Count;

                        JsonEntry entry = new JsonEntry {
                            Name = column.Key, Type = column.Value.Type
                        };
                        _ujson.JsonColumnEntries.Add(entry);

                        // We need to story all the data in one large array
                        // _ujson.JsonColumnEntries.Capacity = (int)(_ujson.ColumnCount * _ujson.EntityCount);
                    }
                }

                children.Add(row.Value);
                i++;
            }

            // Set data
            _ujson.EntityCount = (uint)Mathf.Max(
                _ujson.EntityCount,
                Mathf.Max((uint)entries.Count, highestID + 1)
                );

            Export(JObject.FromObject(new
            {
                id       = jsonTableData["id"],
                metadata = metaData,
                data     = children,
            }));
        }