Example #1
0
        private object process(string Input)
        {
            object Item = "";

            //Convert JSONSourceText To JSONItem Class.
            char[]       CharArray    = Input.Replace("\r\n", "").ToCharArray();
            string       Temp_Name    = "";
            string       Temp_Value   = "";
            JSONItemType temp_type    = JSONItemType.Null;
            int          subitemcount = 0;
            bool         object_name  = false;
            bool         object_value = false;

            string[] boolean = new string[] { "true", "false" };
            char[]   number  = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'e', 'E', '-', '+', '.' };
            //
            object temp_out   = new object();
            bool   escape_str = false;
            bool   IsString   = false;

            foreach (char Char in CharArray)
            {
                switch (temp_type)
                {
                case (JSONItemType.Null):
                    if (Char != ' ')
                    {
                        switch (Char)
                        {
                        case ('{'):
                            temp_type = JSONItemType.Object;
                            temp_out  = new JSONObject();
                            break;

                        case ('['):
                            temp_type = JSONItemType.Array;
                            temp_out  = new JSONArray();
                            break;

                        case ('"'):
                            //Debug.Print(Input);
                            temp_type = JSONItemType.String;
                            temp_out  = "";
                            break;

                        default:

                            if (boolean.Contains(Input.ToLower()))
                            {
                                temp_type = JSONItemType.Bool;
                                Item      = Convert.ToBoolean(Input);
                                break;
                            }
                            else if (number.Contains(Char))
                            {
                                temp_type   = JSONItemType.Number;
                                temp_out    = 0;
                                Temp_Value  = "";
                                Temp_Value += Char;
                            }
                            else
                            {
                            }
                            break;
                        }
                    }
                    break;

                case (JSONItemType.Array):
                    //[value,value].......
                    if (Char == '"')
                    {
                        if (IsString)
                        {
                            if (!escape_str)
                            {
                                IsString = false;
                            }
                            else
                            {
                                escape_str = false;
                            }
                        }
                        else
                        {
                            IsString = true;
                        }
                    }
                    if (Char == ']' && subitemcount == 0)
                    {
                        ((JSONArray)temp_out).Add(new JSONItem(Temp_Value));
                        Temp_Value   = "";
                        Item         = ((JSONArray)temp_out);
                        object_name  = false;
                        object_value = false;
                        Temp_Name    = "";
                        Temp_Value   = "";
                        temp_out     = null;
                        break;
                    }
                    else if (Char != ',' || (Char == ',' && subitemcount > 0))
                    {
                        if (Char == '{' || Char == '[' && !IsString)
                        {
                            subitemcount++;
                        }
                        else if (Char == '}' || Char == ']' && !IsString)
                        {
                            subitemcount--;
                        }
                        Temp_Value += Char;
                    }
                    else if (Char == ',' && subitemcount == 0)
                    {
                        ((JSONArray)temp_out).Add(new JSONItem(Temp_Value));
                        Temp_Value = "";
                    }
                    if (Char == '\\' && !escape_str)
                    {
                        escape_str = true;
                    }
                    else if (escape_str)
                    {
                        escape_str = false;
                    }
                    break;

                case (JSONItemType.Object):
                    //{name(string):value,.......}
                    if (object_value)
                    {
                        if (Char == '"')
                        {
                            if (IsString)
                            {
                                if (!escape_str)
                                {
                                    IsString = false;
                                }
                                else
                                {
                                    escape_str = false;
                                }
                            }
                            else
                            {
                                IsString = true;
                            }
                        }
                        if (Char == '}' && subitemcount == 0)
                        {
                            ((JSONObject)temp_out).Add(new JSONSubObject()
                            {
                                Name = Temp_Name, Value = new JSONItem(Temp_Value)
                            });
                            Item         = ((JSONObject)temp_out);
                            object_name  = false;
                            object_value = false;
                            Temp_Name    = "";
                            Temp_Value   = "";
                            break;
                        }
                        else if ((!(Char == ',' && subitemcount == 0)))
                        {
                            if (Char == '{' || Char == '[' && !IsString)
                            {
                                subitemcount++;
                            }
                            else if (Char == '}' || Char == ']' && !IsString)
                            {
                                subitemcount--;
                            }
                            Temp_Value += Char;
                        }
                        else if (Char == ',' && subitemcount == 0 && !IsString)
                        {
                            ((JSONObject)temp_out).Add(new JSONSubObject()
                            {
                                Name = Temp_Name, Value = new JSONItem(Temp_Value)
                            });
                            Temp_Name    = "";
                            Temp_Value   = "";
                            object_value = false;
                            object_name  = false;
                        }
                        if (Char == '\\' && !escape_str)
                        {
                            escape_str = true;
                        }
                        else if (escape_str)
                        {
                            escape_str = false;
                        }
                    }
                    else if (object_name)
                    {
                        if (Char == '"')
                        {
                            object_name = false;
                        }
                        else
                        {
                            Temp_Name += Char;
                        }
                    }
                    else if (!object_name && !object_value)
                    {
                        if (Char == '"')
                        {
                            object_name = true;
                        }
                        else if (Char == ':')
                        {
                            object_value = true;
                        }
                    }


                    break;

                case (JSONItemType.String):
                    //""
                    if (!escape_str)
                    {
                        if (Char == '"')
                        {
                            Item = Temp_Value;
                            //Debug.Print("T:{0} I:{1}",Temp_Value,Input);
                            break;
                        }
                        else if (Char == '\\')
                        {
                            escape_str = true;
                        }
                        else
                        {
                            Temp_Value += Char;
                        }
                    }
                    else
                    {
                        switch (Char)
                        {
                        case '\\':
                            Temp_Value += '\\';
                            escape_str  = false;
                            break;

                        case 'r':
                            Temp_Value += '\r';
                            escape_str  = false;
                            break;

                        case 'n':
                            Temp_Value += '\n';
                            escape_str  = false;
                            break;

                        case '"':
                            Temp_Value += '"';
                            escape_str  = false;
                            break;

                        case 't':
                            Temp_Value += '\t';
                            escape_str  = false;
                            break;

                        case 'b':
                            Temp_Value += '\b';
                            escape_str  = false;
                            break;

                        case 'f':
                            Temp_Value += '\f';
                            escape_str  = false;
                            break;

                        case '\'':
                            Temp_Value += '\'';
                            escape_str  = false;
                            break;

                        default:
                            escape_str = false;
                            break;
                        }
                    }

                    break;

                case (JSONItemType.Bool):
                    //True and False

                    break;

                case (JSONItemType.Number):
                    //Number
                    if (number.Contains(Char))
                    {
                        Temp_Value += Char;
                    }

                    break;
                }
            }
            if (temp_type == JSONItemType.Number)
            {
                Item = Convert.ToInt64(Temp_Value, 10);
            }
            return(Item);
        }
Example #2
0
 public JSONItem(JSONArray source)
 {
     Item = source;
 }