コード例 #1
0
ファイル: JSONParser.cs プロジェクト: DominusX/prime-json
        private static JSONNode automatoObject(string str, ref int index)
        {
            JSONNode json   = new JSONObject();
            int      estado = 0;
            string   key    = "";

            while (index < str.Length)
            {
                if ((str[index] == '{' || str[index] == ',') && estado == 0)
                {
                    estado = 1;
                }
                else if (str[index] == ':' && estado == 0)
                {
                    estado = 2;
                }
                else if (str[index] == '"' && (estado == 1 || estado == 2))
                {
                    if (estado == 1)
                    {
                        key = JSONParser.automatoString(str, ref index);
                    }
                    else if (estado == 2)
                    {
                        json.Add(key, new JSONData(JSONParser.automatoString(str, ref index)));
                        key = "";
                    }
                    estado = 0;
                }
                else if (str[index] == 't' && estado == 2)
                {
                    json.Add(key, new JSONData(JSONParser.automatoTrue(str, ref index)));
                    key    = "";
                    estado = 0;
                }
                else if (str[index] == 'f' && estado == 2)
                {
                    json.Add(key, new JSONData(JSONParser.automatoFalse(str, ref index)));
                    key    = "";
                    estado = 0;
                }
                else if (str[index] == 'n' && estado == 2)
                {
                    json.Add(key, new JSONData(JSONParser.automatoNull(str, ref index)));
                    key    = "";
                    estado = 0;
                }
                else if (((Char.GetNumericValue(str[index]) >= 0 && Char.GetNumericValue(str[index]) <= 9) || str[index] == '-') && estado == 2)
                {
                    json.Add(key, new JSONData(JSONParser.automatoNumber(str, ref index)));
                    key    = "";
                    estado = 0;
                }
                else if (str[index] == '[' && estado == 2)
                {
                    json.Add(key, JSONParser.automatoArray(str, ref index));
                    key    = "";
                    estado = 0;
                }
                else if (str[index] == '{' && estado == 2)
                {
                    json.Add(key, JSONParser.automatoObject(str, ref index));
                    key    = "";
                    estado = 0;
                }
                else if (str[index] == '}' && estado == 0)
                {
                    break;
                }
                else if (str[index] == ' ')
                {
                }
                else
                {
                    throw new FormatException();
                }
                index++;
            }
            return(json);
        }
コード例 #2
0
ファイル: JSONParser.cs プロジェクト: DominusX/prime-json
        private static JSONNode automatoArray(string str, ref int index)
        {
            JSONNode json   = new JSONArray();
            int      estado = 0;

            while (index < str.Length)
            {
                if ((str[index] == '[' || str[index] == ',') && estado == 0)
                {
                    estado = 1;
                }
                else if (str[index] == '"' && estado == 1)
                {
                    json.Add(new JSONData(JSONParser.automatoString(str, ref index)));
                    estado = 0;
                }
                else if (str[index] == 't' && estado == 1)
                {
                    json.Add(new JSONData(JSONParser.automatoTrue(str, ref index)));
                    estado = 0;
                }
                else if (str[index] == 'f' && estado == 1)
                {
                    json.Add(new JSONData(JSONParser.automatoFalse(str, ref index)));
                    estado = 0;
                }
                else if (str[index] == 'n' && estado == 1)
                {
                    json.Add(new JSONData(automatoNull(str, ref index)));
                    estado = 0;
                }
                else if ((char.IsNumber(str[index]) || str[index] == '-') && estado == 1)
                {
                    json.Add(new JSONData(JSONParser.automatoNumber(str, ref index)));
                    estado = 0;
                }
                else if (str[index] == '[' && estado == 1)
                {
                    json.Add(JSONParser.automatoArray(str, ref index));
                    estado = 0;
                }
                else if (str[index] == '{' && estado == 1)
                {
                    json.Add(JSONParser.automatoObject(str, ref index));
                }
                else if (str[index] == ']' && estado == 0)
                {
                    break;
                }
                else if (str[index] == ' ')
                {
                }
                else
                {
                    throw new FormatException();
                }
                index++;
            }

            return(json);
        }