public override int parse()
        {
            string value   = "";
            int    cur_pos = SkipGarbage(_data, _data_start, _data_boundary);

            if (_data[cur_pos] != '"')
            {
                throw new System.Exception(FormatError("double quote was expected", cur_pos));
            }
            cur_pos++;
            int     str_start = cur_pos;
            Tjstate state     = Tjstate.JWAITFORSTRINGVALUE;

            while (cur_pos < _data_boundary)
            {
                char sti = _data[cur_pos];
                switch (state)
                {
                case Tjstate.JWAITFORSTRINGVALUE:
                    if (sti == '\\')
                    {
                        value    += _data.Substring(str_start, cur_pos - str_start);
                        str_start = cur_pos;
                        state     = Tjstate.JWAITFORSTRINGVALUE_ESC;
                    }
                    else if (sti == '"')
                    {
                        value       += _data.Substring(str_start, cur_pos - str_start);
                        _stringValue = value;
                        _data_len    = (cur_pos + 1) - _data_start;
                        return(_data_len);
                    }
                    else if (sti < 32)
                    {
                        throw new System.Exception(FormatError("invalid char: was expecting string value", cur_pos));
                    }
                    break;

                case Tjstate.JWAITFORSTRINGVALUE_ESC:
                    value    += sti;
                    state     = Tjstate.JWAITFORSTRINGVALUE;
                    str_start = cur_pos + 1;
                    break;

                default:
                    throw new System.Exception(FormatError("invalid state for YJSONObject", cur_pos));
                }
                cur_pos++;
            }
            throw new System.Exception(FormatError("unexpected end of data", cur_pos));
        }
        public override int parse()
        {
            string current_name = "";
            int    name_start   = _data_start;
            int    cur_pos      = SkipGarbage(_data, _data_start, _data_boundary);

            if (_data.Length <= cur_pos || _data[cur_pos] != '{')
            {
                throw new System.Exception(FormatError("Opening braces was expected", cur_pos));
            }
            cur_pos++;
            Tjstate state = Tjstate.JWAITFORNAME;


            while (cur_pos < _data_boundary)
            {
                char sti = _data[cur_pos];
                switch (state)
                {
                case Tjstate.JWAITFORNAME:
                    if (sti == '"')
                    {
                        state      = Tjstate.JWAITFORENDOFNAME;
                        name_start = cur_pos + 1;
                    }
                    else if (sti == '}')
                    {
                        _data_len = cur_pos + 1 - _data_start;
                        return(_data_len);
                    }
                    else
                    {
                        if (sti != ' ' && sti != '\n' && sti != '\r')
                        {
                            throw new System.Exception(FormatError("invalid char: was expecting \"", cur_pos));
                        }
                    }
                    break;

                case Tjstate.JWAITFORENDOFNAME:
                    if (sti == '"')
                    {
                        current_name = _data.Substring(name_start, cur_pos - name_start);
                        state        = Tjstate.JWAITFORCOLON;
                    }
                    else
                    {
                        if (sti < 32)
                        {
                            throw new System.Exception(FormatError("invalid char: was expecting an identifier compliant char", cur_pos));
                        }
                    }
                    break;

                case Tjstate.JWAITFORCOLON:
                    if (sti == ':')
                    {
                        state = Tjstate.JWAITFORDATA;
                    }
                    else
                    {
                        if (sti != ' ' && sti != '\n' && sti != '\r')
                        {
                            throw new System.Exception(
                                      FormatError("invalid char: was expecting \"", cur_pos));
                        }
                    }
                    break;

                case Tjstate.JWAITFORDATA:
                    if (sti == '{')
                    {
                        YJSONObject jobj = new YJSONObject(_data, cur_pos, _data_boundary);
                        int         len  = jobj.parse();
                        cur_pos += len;
                        parsed.Add(current_name, jobj);
                        _keys.Add(current_name);
                        state = Tjstate.JWAITFORNEXTSTRUCTMEMBER;
                        //cur_pos is already incremented
                        continue;
                    }
                    else if (sti == '[')
                    {
                        YJSONArray jobj = new YJSONArray(_data, cur_pos, _data_boundary);
                        int        len  = jobj.parse();
                        cur_pos += len;
                        parsed.Add(current_name, jobj);
                        _keys.Add(current_name);
                        state = Tjstate.JWAITFORNEXTSTRUCTMEMBER;
                        //cur_pos is already incremented
                        continue;
                    }
                    else if (sti == '"')
                    {
                        YJSONString jobj = new YJSONString(_data, cur_pos, _data_boundary);
                        int         len  = jobj.parse();
                        cur_pos += len;
                        parsed.Add(current_name, jobj);
                        _keys.Add(current_name);
                        state = Tjstate.JWAITFORNEXTSTRUCTMEMBER;
                        //cur_pos is already incremented
                        continue;
                    }
                    else if (sti == '-' || (sti >= '0' && sti <= '9'))
                    {
                        YJSONNumber jobj = new YJSONNumber(_data, cur_pos, _data_boundary);
                        int         len  = jobj.parse();
                        cur_pos += len;
                        parsed.Add(current_name, jobj);
                        _keys.Add(current_name);
                        state = Tjstate.JWAITFORNEXTSTRUCTMEMBER;
                        //cur_pos is already incremented
                        continue;
                    }
                    else if (sti != ' ' && sti != '\n' && sti != '\r')
                    {
                        throw new System.Exception(FormatError("invalid char: was expecting  \",0..9,t or f", cur_pos));
                    }
                    break;

                case Tjstate.JWAITFORNEXTSTRUCTMEMBER:
                    if (sti == ',')
                    {
                        state      = Tjstate.JWAITFORNAME;
                        name_start = cur_pos + 1;
                    }
                    else if (sti == '}')
                    {
                        _data_len = cur_pos + 1 - _data_start;
                        return(_data_len);
                    }
                    else
                    {
                        if (sti != ' ' && sti != '\n' && sti != '\r')
                        {
                            throw new System.Exception(FormatError("invalid char: was expecting ,", cur_pos));
                        }
                    }
                    break;

                case Tjstate.JWAITFORNEXTARRAYITEM:
                case Tjstate.JWAITFORSTRINGVALUE:
                case Tjstate.JWAITFORINTVALUE:
                case Tjstate.JWAITFORBOOLVALUE:
                    throw new System.Exception(FormatError("invalid state for YJSONObject", cur_pos));
                }
                cur_pos++;
            }
            throw new System.Exception(FormatError("unexpected end of data", cur_pos));
        }
        public override int parse()
        {
            int cur_pos = SkipGarbage(_data, _data_start, _data_boundary);

            if (_data[cur_pos] != '[')
            {
                throw new System.Exception(FormatError("Opening braces was expected", cur_pos));
            }
            cur_pos++;
            Tjstate state = Tjstate.JWAITFORDATA;

            while (cur_pos < _data_boundary)
            {
                char sti = _data[cur_pos];
                switch (state)
                {
                case Tjstate.JWAITFORDATA:
                    if (sti == '{')
                    {
                        YJSONObject jobj = new YJSONObject(_data, cur_pos, _data_boundary);
                        int         len  = jobj.parse();
                        cur_pos += len;
                        _arrayValue.Add(jobj);
                        state = Tjstate.JWAITFORNEXTARRAYITEM;
                        //cur_pos is already incremented
                        continue;
                    }
                    else if (sti == '[')
                    {
                        YJSONArray jobj = new YJSONArray(_data, cur_pos, _data_boundary);
                        int        len  = jobj.parse();
                        cur_pos += len;
                        _arrayValue.Add(jobj);
                        state = Tjstate.JWAITFORNEXTARRAYITEM;
                        //cur_pos is already incremented
                        continue;
                    }
                    else if (sti == '"')
                    {
                        YJSONString jobj = new YJSONString(_data, cur_pos, _data_boundary);
                        int         len  = jobj.parse();
                        cur_pos += len;
                        _arrayValue.Add(jobj);
                        state = Tjstate.JWAITFORNEXTARRAYITEM;
                        //cur_pos is already incremented
                        continue;
                    }
                    else if (sti == '-' || (sti >= '0' && sti <= '9'))
                    {
                        YJSONNumber jobj = new YJSONNumber(_data, cur_pos, _data_boundary);
                        int         len  = jobj.parse();
                        cur_pos += len;
                        _arrayValue.Add(jobj);
                        state = Tjstate.JWAITFORNEXTARRAYITEM;
                        //cur_pos is already incremented
                        continue;
                    }
                    else if (sti == ']')
                    {
                        _data_len = cur_pos + 1 - _data_start;
                        return(_data_len);
                    }
                    else if (sti != ' ' && sti != '\n' && sti != '\r')
                    {
                        throw new System.Exception(FormatError("invalid char: was expecting  \",0..9,t or f", cur_pos));
                    }
                    break;

                case Tjstate.JWAITFORNEXTARRAYITEM:
                    if (sti == ',')
                    {
                        state = Tjstate.JWAITFORDATA;
                    }
                    else if (sti == ']')
                    {
                        _data_len = cur_pos + 1 - _data_start;
                        return(_data_len);
                    }
                    else
                    {
                        if (sti != ' ' && sti != '\n' && sti != '\r')
                        {
                            throw new System.Exception(FormatError("invalid char: was expecting ,", cur_pos));
                        }
                    }
                    break;

                default:
                    throw new System.Exception(FormatError("invalid state for YJSONObject", cur_pos));
                }
                cur_pos++;
            }
            throw new System.Exception(FormatError("unexpected end of data", cur_pos));
        }
예제 #4
0
        private Nullable<TJSONRECORD> ParseEx(Tjstate initialstate, string defaultname, ref string st, ref int i)
        {
            Nullable<TJSONRECORD> functionReturnValue = default(Nullable<TJSONRECORD>);
              TJSONRECORD res = default(TJSONRECORD);
              TJSONRECORD value = default(TJSONRECORD);
              Tjstate state = default(Tjstate);
              string svalue = "";
              int ivalue = 0;
              int isign = 0;
              char sti = '\0';

              string name = null;

              name = defaultname;
              state = initialstate;
              isign = 1;

              ivalue = 0;

              while (i < st.Length)
              {
            sti = st[i];
            switch (state)
            {
              case Tjstate.JWAITFORNAME:
            if (sti == '"')
            {
              state = Tjstate.JWAITFORENDOFNAME;
            }
            else
            {
              if (sti != ' ' & sti != '\n' & sti != ' ')
                ParseError(ref st, i, "invalid char: was expecting \"");
            }

            break;
              case Tjstate.JWAITFORENDOFNAME:
            if (sti == '"')
            {
              state = Tjstate.JWAITFORCOLON;
            }
            else
            {
              if (sti >= 32)
                name = name + sti;
              else
                ParseError(ref st, i, "invalid char: was expecting an identifier compliant char");
            }

            break;
              case Tjstate.JWAITFORCOLON:
            if (sti == ':')
            {
              state = Tjstate.JWAITFORDATA;
            }
            else
            {
              if (sti != ' ' & sti != '\n' & sti != ' ')
                ParseError(ref st, i, "invalid char: was expecting \"");
            }
            break;
              case Tjstate.JWAITFORDATA:
            if (sti == '{')
            {
              res = createStructRecord(name);
              state = Tjstate.JWAITFORNEXTSTRUCTMEMBER;
            }
            else if (sti == '[')
            {
              res = createArrayRecord(name);
              state = Tjstate.JWAITFORNEXTARRAYITEM;
            }
            else if (sti == '"')
            {
              svalue = "";
              state = Tjstate.JWAITFORSTRINGVALUE;
            }
            else if (sti >= '0' & sti <= '9')
            {
              state = Tjstate.JWAITFORINTVALUE;
              ivalue = sti - 48;
              isign = 1;
            }
            else if (sti == '-')
            {
              state = Tjstate.JWAITFORINTVALUE;
              ivalue = 0;
              isign = -1;
            }
            else if (sti == 't' || sti == 'f' || sti == 'T' || sti == 'F')
            {
              svalue = sti.ToString().ToUpper();
              state = Tjstate.JWAITFORBOOLVALUE;
            }
            else if (sti != ' ' & sti != '\n' & sti != ' ')
            {
              ParseError(ref st, i, "invalid char: was expecting  \",0..9,t or f");
            }
            break;
              case Tjstate.JWAITFORSTRINGVALUE:
            if (sti == '"')
            {
              state = Tjstate.JSCOMPLETED;
              res = createStrRecord(name, svalue);
            }
            else if (sti < 32)
            {
              ParseError(ref st, i, "invalid char: was expecting string value");
            }
            else
            {
              svalue = svalue + sti;
            }
            break;
              case Tjstate.JWAITFORINTVALUE:
            if (sti >= '0' & sti <= '9')
            {
              ivalue = (ivalue * 10) + sti - 48;
            }
            else
            {
              res = createIntRecord(name, isign * ivalue);
              state = Tjstate.JSCOMPLETED;
              i = i - 1;
            }
            break;
              case Tjstate.JWAITFORBOOLVALUE:
            if (sti < 'A' | sti > 'Z')
            {
              if (svalue != "TRUE" & svalue != "FALSE")
                ParseError(ref st, i, "unexpected value, was expecting \"true\" or \"false\"");
              if (svalue == "TRUE")
                res = createBoolRecord(name, true);
              else
                res = createBoolRecord(name, false);
              state = Tjstate.JSCOMPLETED;
              i = i - 1;
            }
            else
            {
              svalue = svalue + sti.ToString().ToUpper();
            }
            break;
              case Tjstate.JWAITFORNEXTSTRUCTMEMBER:
            sti = Skipgarbage(ref st, ref i);
            if (i < st.Length)
            {
              if (sti == '}')
              {
                functionReturnValue = res;
                i = i + 1;
                return functionReturnValue;
              }
              else
              {
                value = (TJSONRECORD)ParseEx(Tjstate.JWAITFORNAME, "", ref st, ref i);
                add2StructRecord(ref res, ref value);
                sti = Skipgarbage(ref st, ref i);
                if (i < st.Length)
                {
                  if (sti == '}' & i < st.Length)
                  {
                    i = i - 1;
                  }
                  else if (sti != ' ' & sti != '\n' & sti != ' ' & sti != ',')
                  {
                    ParseError(ref st, i, "invalid char: vas expecting , or }");
                  }
                }
              }

            }
            break;
              case Tjstate.JWAITFORNEXTARRAYITEM:
            sti = Skipgarbage(ref st, ref i);
            if (i < st.Length)
            {
              if (sti == ']')
              {
                functionReturnValue = res;
                i = i + 1;
                return functionReturnValue;
              }
              else
              {
                value = (TJSONRECORD)ParseEx(Tjstate.JWAITFORDATA, res.itemcount.ToString(), ref st, ref i);
                add2ArrayRecord(ref res, ref value);
                sti = Skipgarbage(ref st, ref i);
                if (i < st.Length)
                {
                  if (sti == ']' & i < st.Length)
                  {
                    i = i - 1;
                  }
                  else if (sti != ' ' & sti != '\n' & sti != ' ' & sti != ',')
                  {
                    ParseError(ref st, i, "invalid char: vas expecting , or ]");
                  }
                }
              }
            }
            break;
              case Tjstate.JSCOMPLETED:
            functionReturnValue = res;
            return functionReturnValue;
            }
            i++;
              }
              ParseError(ref st, i, "unexpected end of data");
              functionReturnValue = null;
              return functionReturnValue;
        }