예제 #1
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 : TypedDoc, new()
        {
            var map = JSONReader.DeserializeDataObject(json) as JSONDataMap;

            return(FromJSON <T>(map, ref result, setFieldFunc));
        }
예제 #2
0
파일: Rowset.cs 프로젝트: chadfords/azos
        /// <summary>
        /// Creates a shallow copy from another rowset, optionally applying a filter
        /// </summary>
        public Rowset(RowsetBase other, Func <Doc, bool> filter = null) : base(other.Schema)
        {
            if (filter == null)
            {
                m_List = new List <Doc>(other.m_List);
            }
            else
            {
                m_List = other.Where(filter).ToList();
            }

            m_SortFieldList = new List <string>();
        }
예제 #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 : TypedDoc, new()
        {
            if (jsonMap == null || jsonMap.Count == 0)
            {
                throw new DataException(StringConsts.ARGUMENT_ERROR + "RowsetBase.FromJSON(jsonMap=null)");
            }
            if (result == null)
            {
                throw new DataException(StringConsts.ARGUMENT_ERROR + "RowsetBase.FromJSON(result=null)");
            }

            var typedDoc = new T();

            if (result.Schema != typedDoc.Schema)
            {
                throw new DataException(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 doc = new T();

                if (Doc.TryFillFromJSON(doc, jdo, setFieldFunc))
                {
                    result.Add(doc);
                }
            }

            return(rows.Count);
        }