Ejemplo n.º 1
0
        public JsonStructureInfo()
        {
            MappedData = new Dictionary <string, string>();
            Data       = new JsonElement {
                Key = "", Childs = new List <JsonElement>(), Value = "root"
            };

            MappedModel = new Dictionary <string, JsonStructure>();
            Model       = new JsonStructure {
                Key = "", Description = "Json", Childs = new List <JsonStructure>()
            };
        }
Ejemplo n.º 2
0
        private void CreaTableRecursiveOnModel(DataTable dt, JsonStructure jes)
        {
            if (jes.IsSelected && (jes.Childs == null || jes.Childs.Count == 0))
            {
                var dc = dt.Columns.Add(jes.Key, typeof(string));
                dc.AllowDBNull = true;
                dc.Caption     = jes.Description;
            }

            if (jes.Childs != null && jes.Childs.Any())
            {
                foreach (var c in jes.Childs)
                {
                    CreaTableRecursiveOnModel(dt, c);
                }
            }
        }
Ejemplo n.º 3
0
        public JsonStructureInfo ParseStructure(string json)
        {
            var structure = new JsonStructureInfo();
            var jt        = JToken.Parse(json);

            switch (jt.Type)
            {
            case JTokenType.Object:
                ParseJsonObject((JObject)jt, structure, structure.Data, structure.Model);
                break;

            case JTokenType.Array:
                ParseJsonArray((JArray)jt, structure, structure.Data, structure.Model);
                break;

            default:
                var keyNode      = jt.Path;
                var elementArray = new JsonElement
                {
                    Key    = keyNode,
                    Value  = jt.ToString(),
                    Childs = new List <JsonElement>(),
                };
                structure.MappedData.Add(keyNode, "result");
                structure.Data.Childs.Add(elementArray);

                var epuredKey = EpureKey(keyNode);
                if (!structure.MappedModel.ContainsKey(epuredKey))
                {
                    if (string.IsNullOrEmpty(epuredKey))
                    {
                        epuredKey = "result";
                    }
                    var structureElement = new JsonStructure {
                        Key = epuredKey, Description = "result", Childs = new List <JsonStructure>()
                    };
                    structure.MappedModel.Add(epuredKey, structureElement);
                    structure.Model.Childs.Add(structureElement);
                }
                break;
            }
            return(structure);
        }
Ejemplo n.º 4
0
        private void ParseJsonArray(JArray ja, JsonStructureInfo structureInfo, JsonElement parent, JsonStructure parentStructure, string optionlDesc = "")
        {
            var keyNodeArray = ja.Path;
            var elementArray = new JsonElement
            {
                Key     = keyNodeArray,
                Value   = string.Empty,
                Childs  = new List <JsonElement>(),
                IsArray = true
            };

            structureInfo.MappedData.Add(keyNodeArray, string.Empty);
            parent.Childs.Add(elementArray);

            var           epuredKeyArray = EpureKey(keyNodeArray);
            JsonStructure structureArray;

            if (!structureInfo.MappedModel.ContainsKey(epuredKeyArray))
            {
                structureArray = new JsonStructure {
                    Key = epuredKeyArray, Description = optionlDesc + "[]", Childs = new List <JsonStructure>()
                };
                structureInfo.MappedModel.Add(epuredKeyArray, structureArray);
                parentStructure.Childs.Add(structureArray);
            }
            else
            {
                structureArray = structureInfo.MappedModel[epuredKeyArray];
            }

            for (int i = 0; i < ja.Count; i++)
            {
                JToken jt = ja[i];

                switch (jt.Type)
                {
                case JTokenType.Object:
                    ParseJsonObject(((JObject)jt), structureInfo, elementArray, structureArray);
                    break;

                case JTokenType.Array:
                    ParseJsonArray((JArray)jt, structureInfo, elementArray, structureArray);
                    break;

                case JTokenType.None:
                case JTokenType.Constructor:
                case JTokenType.Comment:
                case JTokenType.Null:
                case JTokenType.Undefined:
                    break;

                case JTokenType.Property:
                case JTokenType.Integer:
                case JTokenType.Float:
                case JTokenType.String:
                case JTokenType.Boolean:
                case JTokenType.Date:
                case JTokenType.Raw:
                case JTokenType.Bytes:
                case JTokenType.Guid:
                case JTokenType.Uri:
                case JTokenType.TimeSpan:
                    var keyNode = jt.Path;
                    var element = new JsonElement
                    {
                        Key    = keyNode,
                        Value  = jt.ToString(),    //value
                        Childs = new List <JsonElement>()
                    };

                    structureInfo.MappedData.Add(keyNode, jt.ToString());    //value
                    elementArray.Childs.Add(element);

                    var epuredKey = EpureKey(keyNode);

                    if (!structureInfo.MappedModel.ContainsKey(epuredKey))
                    {
                        var jse = new JsonStructure {
                            Key = epuredKey, Description = "value", Childs = new List <JsonStructure>()
                        };
                        structureInfo.MappedModel.Add(epuredKey, jse);
                        structureArray.Childs.Add(jse);
                    }
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }
Ejemplo n.º 5
0
        private void ParseJsonObject(JObject ja, JsonStructureInfo structureInfo, JsonElement parent, JsonStructure structureParent)
        {
            foreach (JProperty prop in ja.Properties())
            {
                if (prop.Value.Type == JTokenType.Array)
                {
                    ParseJsonArray((JArray)prop.Value, structureInfo, parent, structureParent, prop.Name);
                }
                else if (prop.Value.Type == JTokenType.Object)
                {
                    var keyNode = prop.Value.Path;
                    var element = new JsonElement
                    {
                        Key    = keyNode,
                        Value  = string.Empty,
                        Childs = new List <JsonElement>()
                    };

                    structureInfo.MappedData.Add(keyNode, "");
                    parent.Childs.Add(element);

                    var           epuredKey = EpureKey(keyNode);
                    JsonStructure jse;
                    if (!structureInfo.MappedModel.ContainsKey(epuredKey))
                    {
                        jse = new JsonStructure {
                            Key = epuredKey, Description = prop.Name, Childs = new List <JsonStructure>()
                        };
                        structureInfo.MappedModel.Add(epuredKey, jse);
                        structureParent.Childs.Add(jse);
                    }
                    else
                    {
                        jse = structureInfo.MappedModel[epuredKey];
                    }

                    ParseJsonObject((JObject)prop.Value, structureInfo, element, jse);
                }
                else
                {
                    var keyNode = prop.Value.Path;
                    var element = new JsonElement
                    {
                        Key    = keyNode,
                        Value  = prop.Value.ToString(),
                        Childs = new List <JsonElement>()
                    };

                    structureInfo.MappedData.Add(keyNode, prop.Value.ToString());
                    parent.Childs.Add(element);

                    var epuredKey = EpureKey(keyNode);

                    if (!structureInfo.MappedModel.ContainsKey(epuredKey))
                    {
                        var jse = new JsonStructure {
                            Key = epuredKey, Description = prop.Name, Childs = new List <JsonStructure>()
                        };
                        structureInfo.MappedModel.Add(epuredKey, jse);
                        structureParent.Childs.Add(jse);
                    }
                }
            }
        }