Exemplo n.º 1
0
        /// <summary>
        /// Reads either Table or Rowset from JSON created by WriteAsJSON. Metadata must be present.
        /// allMatched==false when some data did not match schema (i.e. too little fields or extra fields supplied)
        /// </summary>
        public static RowsetBase FromJSON(JSONDataMap jsonMap,
                                          out bool allMatched,
                                          bool schemaOnly           = false,
                                          bool readOnlySchema       = false,
                                          SetFieldFunc setFieldFunc = null)
        {
            if (jsonMap == null || jsonMap.Count == 0)
            {
                throw new CRUDException(StringConsts.ARGUMENT_ERROR + "RowsetBase.FromJSON(jsonMap=null)");
            }

            var schMap = jsonMap["Schema"] as JSONDataMap;

            if (schMap == null)
            {
                throw new CRUDException(StringConsts.ARGUMENT_ERROR + "RowsetBase.FromJSON(jsonMap!schema)");
            }

            var schema  = Schema.FromJSON(schMap, readOnlySchema);
            var isTable = jsonMap["IsTable"].AsBool();

            allMatched = true;
            var result = isTable ? (RowsetBase) new Table(schema) : (RowsetBase) new Rowset(schema);

            if (schemaOnly)
            {
                return(result);
            }

            var rows = jsonMap["Rows"] as JSONDataArray;

            if (rows == null)
            {
                return(result);
            }


            foreach (var jrow in rows)
            {
                var jdo = jrow as IJSONDataObject;
                if (jdo == null)
                {
                    allMatched = false;
                    continue;
                }

                var row = new DynamicRow(schema);

                if (!Row.TryFillFromJSON(row, jdo, setFieldFunc))
                {
                    allMatched = false;
                }

                result.Add(row);
            }

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Reads either Table or Rowset from JSON created by WriteAsJSON.
        /// </summary>
        /// <returns>Total number of rows found in JSON. If this number is less than
        /// result.Count, then not all rows matched the schema of the resulting rowset.</returns>
        /// <remarks>
        /// The schema of "result" must match the schema of the typed row T.
        /// It's the responsibility of the caller to clear the "result" prior to
        /// calling this function - the function appends rows to existing rowset.
        /// </remarks>
        public static int FromJSON <T>(string json,
                                       ref RowsetBase result,
                                       SetFieldFunc setFieldFunc = null)
            where T : TypedRow, new()
        {
            var map = JSONReader.DeserializeDataObject(json) as JSONDataMap;

            return(FromJSON <T>(map, ref result, setFieldFunc));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Reads either Table or Rowset from JSON created by WriteAsJSON.
        /// </summary>
        /// <returns>Total number of rows found in JSON. If this number is less than
        /// result.Count, then not all rows matched the schema of the resulting rowset.</returns>
        /// <remarks>
        /// The schema of "result" must match the schema of the typed row T.
        /// It's the responsibility of the caller to clear the "result" prior to
        /// calling this function - the function appends rows to existing rowset.
        /// </remarks>
        public static int FromJSON <T>(JSONDataMap jsonMap,
                                       ref RowsetBase result,
                                       SetFieldFunc setFieldFunc = null)
            where T : TypedRow, new()
        {
            if (jsonMap == null || jsonMap.Count == 0)
            {
                throw new CRUDException(StringConsts.ARGUMENT_ERROR + "RowsetBase.FromJSON(jsonMap=null)");
            }
            if (result == null)
            {
                throw new CRUDException(StringConsts.ARGUMENT_ERROR + "RowsetBase.FromJSON(result=null)");
            }

            var typedRow = new T();

            if (result.Schema != typedRow.Schema)
            {
                throw new CRUDException(StringConsts.ARGUMENT_ERROR + "RowsetBase.FromJSON(): invalid result schema");
            }

            var rows = jsonMap["Rows"] as JSONDataArray;

            if (rows == null)
            {
                return(0);
            }

            foreach (var jrow in rows)
            {
                var jdo = jrow as IJSONDataObject;
                if (jdo == null)
                {
                    continue;
                }

                var row = new T();

                if (Row.TryFillFromJSON(row, jdo, setFieldFunc))
                {
                    result.Add(row);
                }
            }

            return(rows.Count);
        }
Exemplo n.º 4
0
Arquivo: Doc.cs Projeto: rstonkus/azos
        /// <summary>
        /// Tries to fill the document with data returning true if field count matched
        /// </summary>
        public static bool TryFillFromJSON(Doc doc, IJsonDataObject jsonData, SetFieldFunc setFieldFunc = null)
        {
            if (doc == null || jsonData == null)
            {
                return(false);
            }

            var allMatch = true;
            var map      = jsonData as JsonDataMap;

            if (map != null)
            {
                foreach (var kvp in map)
                {
                    var fdef = doc.Schema[kvp.Key];
                    if (fdef == null)
                    {
                        var ad = doc as IAmorphousData;
                        if (ad != null && ad.AmorphousDataEnabled)
                        {
                            ad.AmorphousData[kvp.Key] = kvp.Value;
                        }

                        allMatch = false;
                        continue;
                    }

                    if (setFieldFunc == null)
                    {
                        doc.SetFieldValue(fdef, kvp.Value);
                    }
                    else
                    {
                        var ok = setFieldFunc(doc, fdef, kvp.Value);
                        if (!ok)
                        {
                            allMatch = false;
                        }
                    }
                }
                if (map.Count != doc.Schema.FieldCount)
                {
                    allMatch = false;
                }
            }
            else
            {
                var arr = jsonData as JsonDataArray;
                if (arr == null)
                {
                    return(false);
                }

                for (var i = 0; i < doc.Schema.FieldCount; i++)
                {
                    if (i == arr.Count)
                    {
                        break;
                    }
                    var fdef = doc.Schema[i];

                    if (setFieldFunc == null)
                    {
                        doc.SetFieldValue(fdef, arr[i]);
                    }
                    else
                    {
                        var ok = setFieldFunc(doc, fdef, arr[i]);
                        if (!ok)
                        {
                            allMatch = false;
                        }
                    }
                }
                if (arr.Count != doc.Schema.FieldCount)
                {
                    allMatch = false;
                }
            }

            return(allMatch);
        }