void flushBuffer() { try { if (len > 0) { writer.write(buf, 0, len); len = 0; } } catch (IOException ioe) { throw new JsonException(JsonMessages.GENERATOR_WRITE_IO_ERR(), ioe); } }
@Override public JsonStructure remove() { throw new JsonException(JsonMessages.NODEREF_VALUE_CANNOT_REMOVE()); }
private void missingMember(String op, String member) { throw new JsonException(JsonMessages.PATCH_MEMBER_MISSING(op, member)); }
@Override public void flush() { flushBuffer(); try { writer.flush(); } catch (IOException ioe) { throw new JsonException(JsonMessages.GENERATOR_FLUSH_IO_ERR(), ioe); } }
@Override public void close() { try { tokenizer.close(); } catch (IOException e) { throw new JsonException(JsonMessages.PARSER_TOKENIZER_CLOSE_IO(), e); } }
@Override public JsonStructure add(JsonValue value) { switch (value.getValueType() ) { case OBJECT: case ARRAY: this.root = (JsonStructure) value; break; default: throw new JsonException(JsonMessages.NODEREF_VALUE_ADD_ERR()); } return root; }
/** * Returns the enum constant with the specified name. * * @param operationName {@code operationName} to convert to the enum constant. * @return the enum constant for given {@code operationName} * @throws JsonException if given {@code operationName} is not recognized */ public static Operation fromOperationName(String operationName) { for (Operation op : values()) { if (op.operationName().equalsIgnoreCase(operationName)) { return op; } } throw new JsonException("Illegal value for the operationName of the JSON patch operation: " + operationName); } }
private int peek() { try { if (readBegin == readEnd) { // need to fill the buffer int len = fillBuf(); if (len == -1) { return -1; } assert len != 0; readBegin = storeEnd; readEnd = readBegin+len; } return buf[readBegin]; } catch (IOException ioe) { throw new JsonException(JsonMessages.TOKENIZER_IO_ERR(), ioe); } }
private int read() { try { if (readBegin == readEnd) { // need to fill the buffer int len = fillBuf(); if (len == -1) { return -1; } assert len != 0; readBegin = storeEnd; readEnd = readBegin+len; } return buf[readBegin++]; } catch (IOException ioe) { throw new JsonException(JsonMessages.TOKENIZER_IO_ERR(), ioe); } }
static Scope createScope(JsonValue value) { if (value instanceof JsonArray) { return new ArrayScope((JsonArray)value); } else if (value instanceof JsonObject) { return new ObjectScope((JsonObject)value); } throw new JsonException(JsonMessages.PARSER_SCOPE_ERR(value)); } }
@Override public JsonObject replace(JsonValue value) { if (!contains()) { throw new JsonException(JsonMessages.NODEREF_OBJECT_MISSING(key)); } return add(value); } }
@Override public JsonObject remove() { if (!contains()) { throw new JsonException(JsonMessages.NODEREF_OBJECT_MISSING(key)); } return Json.createObjectBuilder(object).remove(key).build(); }
@Override public JsonValue get() { if (!contains()) { throw new JsonException(JsonMessages.NODEREF_OBJECT_MISSING(key)); } return object.get(key); }
@Override public void close() { if (currentContext.scope != Scope.IN_NONE || currentContext.first) { throw new JsonGenerationException(JsonMessages.GENERATOR_INCOMPLETE_JSON()); } flushBuffer(); try { writer.close(); } catch (IOException ioe) { throw new JsonException(JsonMessages.GENERATOR_CLOSE_IO_ERR(), ioe); } bufferPool.recycle(buf); }
@Override public JsonValue get() { if (!contains()) { throw new JsonException(JsonMessages.NODEREF_ARRAY_INDEX_ERR(index, array.size())); } return array.get(index); }
@Override public JsonArray readArray() { if (readDone) { throw new IllegalStateException(JsonMessages.READER_READ_ALREADY_CALLED()); } readDone = true; if (parser.hasNext()) { try { parser.next(); return parser.getArray(); } catch (IllegalStateException ise) { throw new JsonParsingException(ise.getMessage(), ise, parser.getLastCharLocation()); } } throw new JsonException(JsonMessages.INTERNAL_ERROR()); }
@Override public JsonValue readValue() { if (readDone) { throw new IllegalStateException(JsonMessages.READER_READ_ALREADY_CALLED()); } readDone = true; if (parser.hasNext()) { try { parser.next(); return parser.getValue(); } catch (IllegalStateException ise) { throw new JsonParsingException(ise.getMessage(), ise, parser.getLastCharLocation()); } } throw new JsonException(JsonMessages.INTERNAL_ERROR()); }
@Override public JsonArray remove() { if (!contains()) { throw new JsonException(JsonMessages.NODEREF_ARRAY_INDEX_ERR(index, array.size())); } JsonArrayBuilder builder = Json.createArrayBuilder(this.array); return builder.remove(index).build(); }
@Override public JsonArray replace(JsonValue value) { if (!contains()) { throw new JsonException(JsonMessages.NODEREF_ARRAY_INDEX_ERR(index, array.size())); } JsonArrayBuilder builder = Json.createArrayBuilder(this.array); return builder.set(index, value).build(); } }
@Override public JsonArray add(JsonValue value) { //TODO should we check for arrayoutofbounds? // The spec seems to say index = array.size() is allowed. This is handled as append JsonArrayBuilder builder = Json.createArrayBuilder(this.array); if (index == -1 || index == array.size()) { builder.add(value); } else { if(index < array.size()) { builder.add(index, value); } else { throw new JsonException(JsonMessages.NODEREF_ARRAY_INDEX_ERR(index, array.size())); } } return builder.build(); }