Deserialize() public method

public Deserialize ( string Text ) : void
Text string
return void
示例#1
0
        public void AddChildObject(string Raw)
        {
            JSONObject child = new JSONObject();
            child.Parent = this;
            child.ValueType = JSONType.Object;
            if (NodeMenu != null) { child.NodeMenu = NodeMenu; }
            this.ChildObjects.Add(child);

            child.Deserialize(Raw);
        }
示例#2
0
        public void Deserialize(string Text)
        {
            this.Raw = Text;

            //this.Raw = this.Raw.RemoveIfFirst('{').RemoveIfLast('}');

            bool insideD = false;
            bool insideS = false;
            bool isAttribute = false;
            int bDepth = 0;
            int brDepth = 0;
            string buffer = "";

            for (int x = 0; x < this.Raw.Length; x++)
            {
                switch (this.Raw[x])
                {
                    case '"':
                        if (!insideS) { insideD = !insideD; }
                        buffer += '"';
                        break;
                    case '\'':
                        if (!insideD) { insideS = !insideS; }
                        buffer += '\'';
                        break;
                    case '[':
                        brDepth++;
                        buffer += '[';
                        break;
                    case ']':
                        brDepth--;
                        buffer += ']';
                        break;
                    case '{':
                        bDepth++;
                        buffer += '{';
                        break;
                    case '}':
                        bDepth--;
                        buffer += '}';
                        break;
                    case ':':
                        if (!insideD && !insideS && bDepth == 0 && brDepth == 0)
                        {
                            isAttribute = true;
                        }
                        buffer += ':';
                        break;
                    case ',':
                        if (!insideD && !insideS && bDepth == 0 && brDepth == 0)
                        {
                            if (isAttribute)
                            {
                                this.AddChildAttribute(buffer);
                            }
                            else
                            {
                                buffer = buffer.RemoveIfFirst('{').RemoveIfLast('}');
                                this.AddChildObject(buffer);
                            }
                            buffer = "";
                            isAttribute = false;
                        }
                        else
                        {
                            buffer += ',';
                        }
                        break;
                    case '\r':
                    case '\n':
                    case '\t':
                        if (insideD || insideS)
                        {
                            buffer += this.Raw[x];
                        }
                        break;
                    default:
                        buffer += this.Raw[x];
                        break;
                }
            }

            if (!string.IsNullOrEmpty(buffer))
            {
                if (isAttribute)
                {
                    this.AddChildAttribute(buffer);
                }
                else
                {
                    buffer = buffer.RemoveIfFirst('{').RemoveIfLast('}');
                    this.AddChildObject(buffer);
                }
            }

            if (this.GetChild("id") != null)
            {
                this.Name = this.GetChild("id").Value.ToString();
            }

            #region May not need
            int start = this.Raw.IndexOf('{') + 1;
            int end = -1;
            int bcount = 1;

            for (int x = start; x < this.Raw.Length; x++)
            {
                if (this.Raw[x] == '{') { bcount++; }
                if (this.Raw[x] == '}')
                {
                    bcount--;
                    if (bcount <= 1)
                    {
                        end = x;
                        break;
                    }
                }

                if (start > -1 && end > -1)
                {
                    JSONObject newNode = new JSONObject();
                    newNode.ValueType = JSONType.Object;
                    newNode.Parent = this;
                    newNode.Deserialize(this.Raw.Substring(start, end - start));
                }
            }
            #endregion
        }
示例#3
0
        public void AddChildAttribute(JSONObject child, string Raw)
        {
            child.Parent = this;
            child.Raw = Raw;

            int split = Raw.IndexOf(':');
            if (split == -1) { return; }
            string[] items = new string[2];
            items[0] = Raw.Substring(0, split);
            items[1] = Raw.Substring(split + 1);
            child.Name = items[0].Replace("\"", "").Trim();

            if (items[1].Trim().StartsWith("\""))
            {
                child.ValueType = JSONType.String;
                string v = items[1].Replace("\\\"", "|||||");
                v = v.Replace("\"", "");
                v = v.Replace("|||||", "\\\"");
                child.Value = v.Trim();
            }
            else if (items[1].Trim().StartsWith("'"))
            {
                child.ValueType = JSONType.String;
                child.Value = items[1].Replace("'", "").Trim();
            }
            else if (items[1].Trim().CanConvert(typeof(System.Int32)))
            {
                child.ValueType = JSONType.Number;
                child.Value = Convert.ToInt32(items[1]);
            }
            else if (items[1].Trim().CanConvert(typeof(bool)))
            {
                child.ValueType = JSONType.Boolean;
                child.Value = Convert.ToBoolean(items[1].Trim());
            }
            else if (items[1].FirstVisibleCharacter() == '[' && items[1].LastVisibleCharacter() == ']')
            {
                items[1] = items[1].RemoveIfFirst('[').RemoveIfLast(']');
                char next = items[1].FirstVisibleCharacter();
                if (next == '{') //We are dealing with Objects here.
                {
                    child.ValueType = JSONType.ObjectArray;
                    child.Deserialize(items[1]);
                }
                else  // Raw text
                {
                    child.ValueType = JSONType.Raw;
                    child.Value = items[1];
                }
            }
            else if (items[1].FirstVisibleCharacter() == '{')
            {
                child.ValueType = JSONType.Object;
                items[1] = items[1].RemoveIfFirst('{').RemoveIfLast('}');
                List<JSONObject> props = SplitObjects(items[1]);
                foreach (JSONObject prop in props)
                {
                    child.AddChildAttribute(prop, prop.Raw);
                }
            }
            else
            {
                child.ValueType = JSONType.Raw;
                child.Value = items[1].Trim();
            }

            this.ChildObjects.Add(child);
        }