예제 #1
0
        public static Schema FromJSON(JSONDataMap map, bool readOnly = false)
        {
            if (map == null || map.Count == 0)
            {
                throw new CRUDException(StringConsts.ARGUMENT_ERROR + "Schema.FromJSON(map==null|empty)");
            }

            var name = map["Name"].AsString();

            if (name.IsNullOrWhiteSpace())
            {
                throw new CRUDException(StringConsts.ARGUMENT_ERROR + "Schema.FromJSON(map.Name=null|empty)");
            }

            var adefs = map["FieldDefs"] as JSONDataArray;

            if (adefs == null || adefs.Count == 0)
            {
                throw new CRUDException(StringConsts.ARGUMENT_ERROR + "Schema.FromJSON(map.FieldDefs=null|empty)");
            }

            var defs = new List <Schema.FieldDef>();

            foreach (var mdef in adefs.Cast <JSONDataMap>())
            {
                var fname = mdef["Name"].AsString();
                if (fname.IsNullOrWhiteSpace())
                {
                    throw new CRUDException(StringConsts.ARGUMENT_ERROR + "Schema.FromJSON(map.FierldDefs[name=null|empty])");
                }
                var req      = mdef["IsRequired"].AsBool();
                var key      = mdef["IsKey"].AsBool();
                var vis      = mdef["Visible"].AsBool();
                var desc     = mdef["Description"].AsString();
                var minLen   = mdef["MinLen"].AsInt();
                var maxLen   = mdef["MaxLen"].AsInt();
                var valList  = mdef["ValueList"].AsString();
                var strKind  = mdef["Kind"].AsString();
                var dataKind = strKind == null ? DataKind.Text : (DataKind)Enum.Parse(typeof(DataKind), strKind);
                var strCase  = mdef["CharCase"].AsString();
                var chrCase  = strCase == null ? CharCase.AsIs : (CharCase)Enum.Parse(typeof(CharCase), strCase);

                var tp  = mdef["Type"].AsString(string.Empty).ToLowerInvariant().Trim();
                var tpn = mdef["Nullable"].AsBool();

                var type = JSONMappings.MapJSONTypeToCLR(tp, tpn);

                var atr = new FieldAttribute(required: req, key: key, visible: vis, description: desc,
                                             minLength: minLen, maxLength: maxLen,
                                             valueList: valList, kind: dataKind, charCase: chrCase);
                var def = new Schema.FieldDef(fname, type, atr);
                defs.Add(def);
            }

            return(new Schema(name, readOnly, defs));
        }
예제 #2
0
        public static Schema FromJSON(JSONDataMap map, bool readOnly = false)
        {
            if (map == null || map.Count == 0)
            {
                throw new CRUDException(StringConsts.ARGUMENT_ERROR + "Schema.FromJSON(map==null|empty)");
            }

            var name = map["Name"].AsString();

            if (name.IsNullOrWhiteSpace())
            {
                throw new CRUDException(StringConsts.ARGUMENT_ERROR + "Schema.FromJSON(map.Name=null|empty)");
            }

            var adefs = map["FieldDefs"] as JSONDataArray;

            if (adefs == null || adefs.Count == 0)
            {
                throw new CRUDException(StringConsts.ARGUMENT_ERROR + "Schema.FromJSON(map.FieldDefs=null|empty)");
            }

            var defs = new List <Schema.FieldDef>();

            foreach (var mdef in adefs.Cast <JSONDataMap>())
            {
                var fname = mdef["Name"].AsString();
                if (fname.IsNullOrWhiteSpace())
                {
                    throw new CRUDException(StringConsts.ARGUMENT_ERROR + "Schema.FromJSON(map.FierldDefs[name=null|empty])");
                }
                var req = mdef["IsRequired"].AsBool();
                var key = mdef["IsKey"].AsBool();
                var vis = mdef["Visible"].AsBool();

                var tp  = mdef["Type"].AsString(string.Empty).ToLowerInvariant().Trim();
                var tpn = mdef["Nullable"].AsBool();

                var type = JSONMappings.MapJSONTypeToCLR(tp, tpn);

                var atr = new FieldAttribute(required: req, key: key, visible: vis);
                var def = new Schema.FieldDef(fname, type, atr);
                defs.Add(def);
            }

            return(new Schema(name, readOnly, defs));
        }
예제 #3
0
            /// <summary>
            /// Writes fielddef as JSON. Do not call this method directly, instead call rowset.ToJSON() or use JSONWriter class
            /// </summary>
            public void WriteAsJSON(System.IO.TextWriter wri, int nestingLevel, JSONWritingOptions options = null)
            {
                var attr = this[null];

                if (attr != null && attr.NonUI)
                {
                    wri.Write("{}");
                    return;  //nothing to write for NONUI
                }

                bool   typeIsNullable;
                string tp = JSONMappings.MapCLRTypeToJSON(m_Type, out typeIsNullable);

                var map = new Dictionary <string, object>
                {
                    { "Name", m_Name },
                    { "Order", m_Order },
                    { "Type", tp },
                    { "Nullable", typeIsNullable }
                };

                if (attr != null)
                {
                    map.Add("IsKey", attr.Key);
                    map.Add("IsRequired", attr.Required);
                    map.Add("Visible", attr.Visible);
                    if (attr.Default != null)
                    {
                        map.Add("Default", attr.Default);
                    }
                    if (attr.CharCase != CharCase.AsIs)
                    {
                        map.Add("CharCase", attr.CharCase);
                    }
                    if (attr.Kind != DataKind.Text)
                    {
                        map.Add("Kind", attr.Kind);
                    }
                    if (attr.MinLength != 0)
                    {
                        map.Add("MinLen", attr.MinLength);
                    }
                    if (attr.MaxLength != 0)
                    {
                        map.Add("MaxLen", attr.MaxLength);
                    }
                    if (attr.Min != null)
                    {
                        map.Add("Min", attr.Min);
                    }
                    if (attr.Max != null)
                    {
                        map.Add("Max", attr.Max);
                    }
                    if (attr.ValueList != null)
                    {
                        map.Add("ValueList", attr.ValueList);
                    }
                    if (attr.Description != null)
                    {
                        map.Add("Description", attr.Description);
                    }
                    //metadata content is in the internal format and not dumped
                }

                JSONWriter.WriteMap(wri, map, nestingLevel, options);
            }