private void checkContextForValue() { if ((!currentContext.first && currentContext.scope != Scope.IN_ARRAY && currentContext.scope != Scope.IN_FIELD) || (currentContext.first && currentContext.scope == Scope.IN_OBJECT)) { throw new JsonGenerationException( JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); } }
@Override public JsonGenerator writeEnd() { if (currentContext.scope == Scope.IN_NONE) { throw new JsonGenerationException("writeEnd() cannot be called in no context"); } writeChar(currentContext.scope == Scope.IN_ARRAY ? ']' : '}'); currentContext = stack.pop(); popFieldContext(); return this; }
@Override public JsonGenerator write(String name, double value) { if (currentContext.scope != Scope.IN_OBJECT) { throw new JsonGenerationException( JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); } if (Double.isInfinite(value) || Double.isNaN(value)) { throw new NumberFormatException(JsonMessages.GENERATOR_DOUBLE_INFINITE_NAN()); } writeName(name); writeString(String.valueOf(value)); return this; }
@Override public JsonGenerator write(String name, BigDecimal value) { if (currentContext.scope != Scope.IN_OBJECT) { throw new JsonGenerationException( JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); } writeName(name); writeString(String.valueOf(value)); return this; }
@Override public JsonGenerator writeStartArray() { if (currentContext.scope == Scope.IN_OBJECT) { throw new JsonGenerationException(JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); } if (currentContext.scope == Scope.IN_NONE && !currentContext.first) { throw new JsonGenerationException(JsonMessages.GENERATOR_ILLEGAL_MULTIPLE_TEXT()); } writeComma(); writeChar('['); stack.push(currentContext); currentContext = new Context(Scope.IN_ARRAY); return this; }
@Override public JsonGenerator write(String name, long value) { if (currentContext.scope != Scope.IN_OBJECT) { throw new JsonGenerationException( JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); } writeName(name); writeString(String.valueOf(value)); return this; }
@Override public JsonGenerator write(String name, BigInteger value) { if (currentContext.scope != Scope.IN_OBJECT) { throw new JsonGenerationException( JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); } writeName(name); writeString(String.valueOf(value)); return this; }
@Override public JsonGenerator writeKey(String name) { if (currentContext.scope != Scope.IN_OBJECT) { throw new JsonGenerationException( JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); } writeName(name); stack.push(currentContext); currentContext = new Context(Scope.IN_FIELD); currentContext.first = false; return this; }
@Override public JsonGenerator writeStartObject() { if (currentContext.scope == Scope.IN_OBJECT) { throw new JsonGenerationException(JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); } if (currentContext.scope == Scope.IN_NONE && !currentContext.first) { throw new JsonGenerationException(JsonMessages.GENERATOR_ILLEGAL_MULTIPLE_TEXT()); } writeComma(); writeChar('{'); stack.push(currentContext); currentContext = new Context(Scope.IN_OBJECT); return this; }
@Override public JsonGenerator write(String name, int value) { if (currentContext.scope != Scope.IN_OBJECT) { throw new JsonGenerationException( JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); } writeName(name); writeInt(value); return this; }
@Override public JsonGenerator writeNull(String name) { if (currentContext.scope != Scope.IN_OBJECT) { throw new JsonGenerationException( JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); } writeName(name); writeString("null"); return this; }
@Override public JsonGenerator write(String name, boolean value) { if (currentContext.scope != Scope.IN_OBJECT) { throw new JsonGenerationException( JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); } writeName(name); writeString(value? "true" : "false"); return this; }
@Override public JsonGenerator writeStartObject(String name) { if (currentContext.scope != Scope.IN_OBJECT) { throw new JsonGenerationException( JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); } writeName(name); writeChar('{'); stack.push(currentContext); currentContext = new Context(Scope.IN_OBJECT); return this; }
@Override public JsonGenerator write(String name, String fieldValue) { if (currentContext.scope != Scope.IN_OBJECT) { throw new JsonGenerationException( JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); } writeName(name); writeEscapedString(fieldValue); return this; }
@Override public JsonGenerator writeStartArray(String name) { if (currentContext.scope != Scope.IN_OBJECT) { throw new JsonGenerationException( JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); } writeName(name); writeChar('['); stack.push(currentContext); currentContext = new Context(Scope.IN_ARRAY); return this; }
@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 JsonGenerator write(String name, JsonValue value) { if (currentContext.scope != Scope.IN_OBJECT) { throw new JsonGenerationException( JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope));
private void checkContextForValue() { if ((!currentContext.first && currentContext.scope != Scope.IN_ARRAY && currentContext.scope != Scope.IN_FIELD) || (currentContext.first && currentContext.scope == Scope.IN_OBJECT)) { throw new JsonGenerationException( JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); } }
@Override public JsonGenerator writeEnd() { if (currentContext.scope == Scope.IN_NONE) { throw new JsonGenerationException("writeEnd() cannot be called in no context"); } writeChar(currentContext.scope == Scope.IN_ARRAY ? ']' : '}'); currentContext = stack.pop(); popFieldContext(); return this; }
@Override public JsonGenerator write(String name, double value) { if (currentContext.scope != Scope.IN_OBJECT) { throw new JsonGenerationException( JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); } if (Double.isInfinite(value) || Double.isNaN(value)) { throw new NumberFormatException(JsonMessages.GENERATOR_DOUBLE_INFINITE_NAN()); } writeName(name); writeString(String.valueOf(value)); return this; }