源码简读之Gson解析

来源:互联网 发布:何炅主持的网络节目 编辑:程序博客网 时间:2024/06/05 18:49

JsonReader

Gson.fromJson(String jsonString, Class class){    层层调用    JsonReader.peek()->doPeek();}int doPeek() throws IOException {    int peekStack = stack[stackSize - 1];    if (peekStack == JsonScope.EMPTY_ARRAY) {      stack[stackSize - 1] = JsonScope.NONEMPTY_ARRAY;    } else if (peekStack == JsonScope.NONEMPTY_ARRAY) {      // Look for a comma before the next element.      int c = nextNonWhitespace(true);      switch (c) {      case ']':        return peeked = PEEKED_END_ARRAY;      case ';':        checkLenient(); // fall-through      case ',':        break;      default:        throw syntaxError("Unterminated array");      }    } else if (peekStack == JsonScope.EMPTY_OBJECT || peekStack == JsonScope.NONEMPTY_OBJECT) {      stack[stackSize - 1] = JsonScope.DANGLING_NAME;      // Look for a comma before the next element.      if (peekStack == JsonScope.NONEMPTY_OBJECT) {        int c = nextNonWhitespace(true);        switch (c) {        case '}':          return peeked = PEEKED_END_OBJECT;        case ';':          checkLenient(); // fall-through        case ',':          break;        default:          throw syntaxError("Unterminated object");        }      }      int c = nextNonWhitespace(true);      switch (c) {      case '"':        return peeked = PEEKED_DOUBLE_QUOTED_NAME;      case '\'':        checkLenient();        return peeked = PEEKED_SINGLE_QUOTED_NAME;      case '}':        if (peekStack != JsonScope.NONEMPTY_OBJECT) {          return peeked = PEEKED_END_OBJECT;        } else {          throw syntaxError("Expected name");        }      default:        checkLenient();        pos--; // Don't consume the first character in an unquoted string.        if (isLiteral((char) c)) {          return peeked = PEEKED_UNQUOTED_NAME;        } else {          throw syntaxError("Expected name");        }      }    } else if (peekStack == JsonScope.DANGLING_NAME) {      stack[stackSize - 1] = JsonScope.NONEMPTY_OBJECT;      // Look for a colon before the value.      int c = nextNonWhitespace(true);      switch (c) {      case ':':        break;      case '=':        checkLenient();        if ((pos < limit || fillBuffer(1)) && buffer[pos] == '>') {          pos++;        }        break;      default:        throw syntaxError("Expected ':'");      }    } else if (peekStack == JsonScope.EMPTY_DOCUMENT) {      if (lenient) {        consumeNonExecutePrefix();      }      stack[stackSize - 1] = JsonScope.NONEMPTY_DOCUMENT;    } else if (peekStack == JsonScope.NONEMPTY_DOCUMENT) {      int c = nextNonWhitespace(false);      if (c == -1) {        return peeked = PEEKED_EOF;      } else {        checkLenient();        pos--;      }    } else if (peekStack == JsonScope.CLOSED) {      throw new IllegalStateException("JsonReader is closed");    }    int c = nextNonWhitespace(true);    switch (c) {    case ']':      if (peekStack == JsonScope.EMPTY_ARRAY) {        return peeked = PEEKED_END_ARRAY;      }      // fall-through to handle ",]"    case ';':    case ',':      // In lenient mode, a 0-length literal in an array means 'null'.      if (peekStack == JsonScope.EMPTY_ARRAY || peekStack == JsonScope.NONEMPTY_ARRAY) {        checkLenient();        pos--;        return peeked = PEEKED_NULL;      } else {        throw syntaxError("Unexpected value");      }    case '\'':      checkLenient();      return peeked = PEEKED_SINGLE_QUOTED;    case '"':      return peeked = PEEKED_DOUBLE_QUOTED;    case '[':      return peeked = PEEKED_BEGIN_ARRAY;    case '{':      return peeked = PEEKED_BEGIN_OBJECT;    default:      pos--; // Don't consume the first character in a literal value.    }    int result = peekKeyword();    if (result != PEEKED_NONE) {      return result;    }    result = peekNumber();    if (result != PEEKED_NONE) {      return result;    }    if (!isLiteral(buffer[pos])) {      throw syntaxError("Expected value");    }    checkLenient();    return peeked = PEEKED_UNQUOTED;  }
0 0
原创粉丝点击