Package elemental.json

Examples of elemental.json.JsonException


  JsonObject parseObject() throws JsonException {
    final JsonObject object = jsonFactory.createObject();
    int c = nextNonWhitespace();
    if (c != '{') {
      throw new JsonException(
          "Payload does not begin with '{'.  Got " + c + "("
              + Character.valueOf((char) c) + ")");
    }

    while (true) {
      c = nextNonWhitespace();
      switch (c) {
        case '}':
          // We're done.
          return object;
        case '"':
        case '\'':
          back(c);
          // Ready to start a key.
          final String key = nextString(c);
          if (nextNonWhitespace() != ':') {
            throw new JsonException(
                "Invalid object: expecting \":\"");
          }
          // TODO(knorton): Make sure this key is not already set.
          object.put(key, (JsonValue)nextValue());
          switch (nextNonWhitespace()) {
            case ',':
              break;
            case '}':
              return object;
            default:
              throw new JsonException(
                  "Invalid object: expecting } or ,");
          }
          break;
        case ',':
          break;
        default:
          if (lenient && (Character.isDigit((char) c) || Character.isLetterOrDigit((char) c)))
        {
          StringBuilder keyBuffer = new StringBuilder();
          keyBuffer.append(c);
          while (true) {
            c = next();
            if (Character.isDigit((char) c) || Character.isLetterOrDigit((char) c)) {
              keyBuffer.append(c);
            } else {
              back(c);
              break;
            }
          }
          if (nextNonWhitespace() != ':') {
            throw new JsonException(
                "Invalid object: expecting \":\"");
          }
          // TODO(knorton): Make sure this key is not already set.
          object.put(keyBuffer.toString(), (JsonValue)nextValue());
          switch (nextNonWhitespace()) {
            case ',':
              break;
            case '}':
              return object;
            default:
              throw new JsonException(
                  "Invalid object: expecting } or ,");
          }

        }else{
          throw new JsonException("Invalid object: ");
        }
      }
    }
  }
View Full Code Here


  private JsonNumber getNumberForLiteral(String literal)
      throws JsonException {
    try {
      return jsonFactory.create(Double.parseDouble(literal));
    } catch (NumberFormatException e) {
      throw new JsonException("Invalid number literal: " + literal);
    }
  }
View Full Code Here

    }
  }

  private JsonValue getValueForLiteral(String literal) throws JsonException {
    if ("".equals(literal)) {
      throw new JsonException("Missing value");
    }

    if ("null".equals(literal) || "undefined".equals(literal)) {
      return jsonFactory.createNull();
    }

    if ("true".equals(literal)) {
      return jsonFactory.create(true);
    }

    if ("false".equals(literal)) {
      return jsonFactory.create(false);
    }

    final char c = literal.charAt(0);
    if (c == '-' || Character.isDigit(c)) {
      return getNumberForLiteral(literal);
    }

    throw new JsonException("Invalid literal: \"" + literal + "\"");
  }
View Full Code Here

      sb.append("null");
    }

    private void checkCycle(JsonValue value) {
      if (visited.contains(value)) {
        throw new JsonException("Cycled detected during stringify");
      } else {
        visited.add(value);
      }
    }
View Full Code Here

  @SuppressWarnings({"unchecked"})
  public <T extends JsonValue> T parse(String jsonString) throws JsonException {
    try {
      return parse0(jsonString);
    } catch(Exception e) {
      throw new JsonException("Can't parse " + jsonString);
    }
  }
View Full Code Here

    while ((pos < n) && ((len = read(buffer, pos, n - pos)) != -1)) {
      pos += len;
    }

    if (pos < n) {
      throw new JsonException("TODO"/* TODO(knorton): Add message. */);
    }

    return String.valueOf(buffer);
  }
View Full Code Here

    while (true) {
      c = next();
      switch (c) {
        case '\r':
        case '\n':
          throw new JsonException("");
        case '\\':
          c = next();
          switch (c) {
            case 'b':
              buffer.append('\b');
View Full Code Here

             case ']':
               return array;
             case ',':
               break;
             default:
               throw new JsonException("Invalid array: expected , or ]");
           }
       }
     }
   }
View Full Code Here

  JsonObject parseObject() throws JsonException {
    final JsonObject object = jsonFactory.createObject();
    int c = nextNonWhitespace();
    if (c != '{') {
      throw new JsonException(
          "Payload does not begin with '{'.  Got " + c + "("
              + Character.valueOf((char) c) + ")");
    }

    while (true) {
      c = nextNonWhitespace();
      switch (c) {
        case '}':
          // We're done.
          return object;
        case '"':
        case '\'':
          back(c);
          // Ready to start a key.
          final String key = nextString(c);
          if (nextNonWhitespace() != ':') {
            throw new JsonException(
                "Invalid object: expecting \":\"");
          }
          // TODO(knorton): Make sure this key is not already set.
          object.put(key, nextValue());
          switch (nextNonWhitespace()) {
            case ',':
              break;
            case '}':
              return object;
            default:
              throw new JsonException(
                  "Invalid object: expecting } or ,");
          }
          break;
        case ',':
          break;
        default:
          if (lenient && (Character.isDigit((char) c) || Character.isLetterOrDigit((char) c)))
        {
          StringBuffer keyBuffer = new StringBuffer();
          keyBuffer.append(c);
          while (true) {
            c = next();
            if (Character.isDigit((char) c) || Character.isLetterOrDigit((char) c)) {
              keyBuffer.append(c);
            } else {
              back(c);
              break;
            }
          }
          if (nextNonWhitespace() != ':') {
            throw new JsonException(
                "Invalid object: expecting \":\"");
          }
          // TODO(knorton): Make sure this key is not already set.
          object.put(keyBuffer.toString(), nextValue());
          switch (nextNonWhitespace()) {
            case ',':
              break;
            case '}':
              return object;
            default:
              throw new JsonException(
                  "Invalid object: expecting } or ,");
          }

        }else{
          throw new JsonException("Invalid object: ");
        }
      }
    }
  }
View Full Code Here

  private JsonNumber getNumberForLiteral(String literal)
      throws JsonException {
    try {
      return jsonFactory.create(Double.parseDouble(literal));
    } catch (NumberFormatException e) {
      throw new JsonException("Invalid number literal: " + literal);
    }
  }
View Full Code Here

TOP

Related Classes of elemental.json.JsonException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.