示例#1
0
        public object ReadObject(ref int _index)
        {
            IDictionary _dictionary = new Dictionary <string, object>();
            bool        _done       = false;

            // Skip curls
            _index++;

            while (!_done)
            {
                eJSONToken _token = LookAhead(_index);

                if (_token == eJSONToken.NONE)
                {
                    Debug.LogError(string.Format("[JSON] Parse error at index ={0}", _index));
                    return(null);
                }
                else if (_token == eJSONToken.CURLY_CLOSE)
                {
                    NextToken(ref _index);

                    // Mark read dictionary object as finished
                    _done = true;
                }
                else
                {
                    string _key;
                    object _value;
                    int    _readStatus = ReadKeyValuePair(ref _index, out _key, out _value);

                    if (_readStatus != -1)
                    {
                        // Add dictionary entry
                        _dictionary[_key] = _value;

                        // Read next token
                        eJSONToken _nextToken = LookAhead(_index);

                        if (_nextToken == eJSONToken.COMMA)
                        {
                            NextToken(ref _index);
                        }
                        else if (_nextToken == eJSONToken.CURLY_CLOSE)
                        {
                            NextToken(ref _index);

                            // Mark read dictionary object as finished
                            _done = true;
                        }
                        else
                        {
                            Debug.LogError(string.Format("[JSON] Parse error at index ={0}", _index));
                            return(null);
                        }
                    }
                }
            }

            return(_dictionary);
        }
示例#2
0
        public object ReadArray(ref int _index)
        {
            IList _arraylist = new List <object>();
            bool  _done      = false;

            // Skip square bracket
            _index++;

            while (!_done)
            {
                eJSONToken _token = LookAhead(_index);

                if (_token == eJSONToken.NONE)
                {
                    Debug.LogError(string.Format("[JSON] Parse error at index ={0}", _index));
                    return(null);
                }
                else if (_token == eJSONToken.SQUARED_CLOSE)
                {
                    NextToken(ref _index);

                    // Mark read array object as finished
                    _done = true;
                }
                else
                {
                    // Read array element
                    object _arrayElement;

                    ReadArrayElement(ref _index, out _arrayElement);
                    _arraylist.Add(_arrayElement);

                    // Read next token
                    eJSONToken _nextToken = LookAhead(_index);

                    if (_nextToken == eJSONToken.COMMA)
                    {
                        NextToken(ref _index);
                    }
                    else if (_nextToken == eJSONToken.SQUARED_CLOSE)
                    {
                        NextToken(ref _index);

                        // Mark read array object as finished
                        _done = true;
                    }
                    else
                    {
                        Debug.LogError(string.Format("[JSON] Parse error at index ={0}", _index));
                        return(null);
                    }
                }
            }

            return(_arraylist);
        }
示例#3
0
        public object ReadValue(ref int _index)
        {
            // Remove white spaces
            RemoveWhiteSpace(ref _index);

            // Look ahead
            eJSONToken _token = LookAhead(_index);

            switch (_token)
            {
            case eJSONToken.CURLY_OPEN:
                return(ReadObject(ref _index));

            case eJSONToken.SQUARED_OPEN:
                return(ReadArray(ref _index));

            case eJSONToken.STRING:
                return(ReadString(ref _index));

            case eJSONToken.NUMBER:
                return(ReadNumber(ref _index));

            case eJSONToken.NULL:
                _index += 4;
                return(null);

            case eJSONToken.TRUE:
                _index += 4;
                return(true);

            case eJSONToken.FALSE:
                _index += 5;
                return(false);

            default:
                Debug.LogError(string.Format("[JSON] Parse error at index ={0}", _index));
                break;
            }

            return(null);
        }