@Override public JsonGenerator createGenerator(OutputStream out) { return new JsonGeneratorImpl(out, bufferPool); }
@Override public void close() { writeDone = true; generator.close(); }
void writeInt(int num) { int size; if (num == Integer.MIN_VALUE) { size = INT_MIN_VALUE_CHARS.length; } else { size = (num < 0) ? stringSize(-num) + 1 : stringSize(num); } if (len+size >= buf.length) { flushBuffer(); } if (num == Integer.MIN_VALUE) { System.arraycopy(INT_MIN_VALUE_CHARS, 0, buf, len, size); } else { fillIntChars(num, buf, len+size); } len += size; }
@Override public JsonGenerator write(String value) { checkContextForValue(); writeComma(); writeEscapedString(value); popFieldContext(); return this; }
private JsonGenerator writeName(String name) { writeComma(); writeEscapedString(name); writeColon(); return this; }
@Override public JsonGenerator writeNull() { checkContextForValue(); writeComma(); writeString("null"); popFieldContext(); return this; }
@Override public void writeArray(JsonArray array) { if (writeDone) { throw new IllegalStateException(JsonMessages.WRITER_WRITE_ALREADY_CALLED()); } writeDone = true; generator.writeStartArray(); for(JsonValue value : array) { generator.write(value); } generator.writeEnd(); // Flush the generator's buffered contents. This won't work for byte // streams as intermediary OutputStreamWriter buffers. generator.flushBuffer(); // Flush buffered contents but not the byte stream. generator.flush() // does OutputStreamWriter#flushBuffer (package private) and underlying // byte stream#flush(). Here underlying stream's flush() is no-op. if (os != null) { generator.flush(); } }
@Override public void writeObject(JsonObject object) { if (writeDone) { throw new IllegalStateException(JsonMessages.WRITER_WRITE_ALREADY_CALLED()); } writeDone = true; generator.writeStartObject(); for(Map.Entry<String, JsonValue> e : object.entrySet()) { generator.write(e.getKey(), e.getValue()); } generator.writeEnd(); // Flush the generator's buffered contents. This won't work for byte // streams as intermediary OutputStreamWriter buffers. generator.flushBuffer(); // Flush buffered contents but not the byte stream. generator.flush() // does OutputStreamWriter#flushBuffer (package private) and underlying // byte stream#flush(). Here underlying stream's flush() is no-op. if (os != null) { generator.flush(); } }
@Override public JsonGenerator write(JsonValue value) { checkContextForValue(); writeStartArray(); for(JsonValue child: array) { write(child); writeEnd(); break; case OBJECT: JsonObject object = (JsonObject)value; writeStartObject(); for(Map.Entry<String, JsonValue> member: object.entrySet()) { write(member.getKey(), member.getValue()); writeEnd(); break; case STRING: JsonString str = (JsonString)value; write(str.getString()); break; case NUMBER: JsonNumber number = (JsonNumber)value; writeValue(number.toString()); popFieldContext(); break; case TRUE: write(true); break;
case ARRAY: JsonArray array = (JsonArray)value; writeStartArray(name); for(JsonValue child: array) { write(child); writeEnd(); break; case OBJECT: JsonObject object = (JsonObject)value; writeStartObject(name); for(Map.Entry<String, JsonValue> member: object.entrySet()) { write(member.getKey(), member.getValue()); writeEnd(); break; case STRING: JsonString str = (JsonString)value; write(name, str.getString()); break; case NUMBER: JsonNumber number = (JsonNumber)value; writeValue(name, number.toString()); break; case TRUE: write(name, true); break; case FALSE: write(name, false);
@Override public void write(JsonValue value) { switch (value.getValueType()) { case OBJECT: writeObject((JsonObject) value); return; case ARRAY: writeArray((JsonArray) value); return; default: if (writeDone) { throw new IllegalStateException(JsonMessages.WRITER_WRITE_ALREADY_CALLED()); } writeDone = true; generator.write(value); generator.flushBuffer(); if (os != null) { generator.flush(); } } }
private void writeValue(String name, String value) { writeComma(); writeEscapedString(name); writeColon(); writeString(value); }
@Override public JsonGenerator write(int value) { checkContextForValue(); writeComma(); writeInt(value); popFieldContext(); 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 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 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; }
void writeChar(char c) { if (len >= buf.length) { flushBuffer(); } buf[len++] = c; }
void writeEscapedString(String string) { writeChar('"'); int len = string.length(); for(int i = 0; i < len; i++) { writeString(string, begin, end); if (i == len) { break; case '"': case '\\': writeChar('\\'); writeChar(c); break; case '\b': writeChar('\\'); writeChar('b'); break; case '\f': writeChar('\\'); writeChar('f'); break; case '\n': writeChar('\\'); writeChar('n'); break; case '\r': writeChar('\\'); writeChar('r'); break; case '\t': writeChar('\\'); writeChar('t'); break; default: String hex = "000" + Integer.toHexString(c);
private void writeValue(String value) { writeComma(); writeString(value); }
@Override public JsonGenerator writeEnd() { writeNewLine(); indentLevel--; writeIndent(); super.writeEnd(); return this; }