/** * Constructs a {@code java.util.stream.Collector} that implements a "group by" operation on the * input {@code JsonValue} elements. A classifier function maps the input {@code JsonValue}s to keys, and * the {@code JsonValue}s are partitioned into groups according to the value of the key. * The {@code JsonValue}s in each group are added to a {@code JsonArray}. The key and the * {@code JsonArray} in each group becomes the name/value pair of the resultant {@code JsonObject}. * * @param classifier a function mapping the input {@code JsonValue}s to a String, producing keys * @return the constructed {@code Collector} */ public static Collector<JsonValue, Map<String, JsonArrayBuilder>, JsonObject> groupingBy(Function<JsonValue, String> classifier) { return groupingBy(classifier, toJsonArray()); } }
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)); } }
private JsonParsingException parsingException(JsonToken token, String expectedTokens) { JsonLocation location = getLastCharLocation(); return new JsonParsingException( JsonMessages.PARSER_INVALID_TOKEN(token, location, expectedTokens), location); }
@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; }
private JsonParsingException unexpectedChar(int ch) { JsonLocation location = getLastCharLocation(); return new JsonParsingException( JsonMessages.TOKENIZER_UNEXPECTED_CHAR(ch, location), location); }
@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; }
private JsonParsingException expectedChar(int unexpected, char expected) { JsonLocation location = getLastCharLocation(); return new JsonParsingException( JsonMessages.TOKENIZER_EXPECTED_CHAR(unexpected, location, expected), location); }
@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 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 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 JsonObject readObject() { if (readDone) { throw new IllegalStateException(JsonMessages.READER_READ_ALREADY_CALLED()); } readDone = true; if (parser.hasNext()) { try { parser.next(); return parser.getObject(); } catch (IllegalStateException ise) { throw new JsonParsingException(ise.getMessage(), ise, parser.getLastCharLocation()); } } throw new JsonException(JsonMessages.INTERNAL_ERROR()); }
@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, 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 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 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 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 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 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(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; }