@Test public void testExtractArray() { String json = "{\"foo\" : [\"bar\", \"baz\"]}"; assertEquals("[\"bar\",\"baz\"]", pointer(json, "/foo").evaluate(null)); }
@Override public Object evaluate(Object object) { final String json = getExpression(0).evaluate(object, String.class); final String pointerSpec = getExpression(1).evaluate(object, String.class); JsonPointer expectedPointer = JsonPointer.compile(pointerSpec); if (json == null) return null; try (JsonParser parser = factory.createParser(json)) { while (parser.nextToken() != END_OF_STREAM) { final JsonPointer pointer = parser.getParsingContext().pathAsPointer(); if (pointer.equals(expectedPointer) && parser.currentTokenId() != JsonTokenId.ID_FIELD_NAME) { switch (parser.currentTokenId()) { case JsonTokenId.ID_STRING: return parser.getText(); case JsonTokenId.ID_NUMBER_FLOAT: return parser.getFloatValue(); case JsonTokenId.ID_NUMBER_INT: return parser.getIntValue(); case JsonTokenId.ID_TRUE: case JsonTokenId.ID_FALSE: return parser.getBooleanValue(); default: StringWriter writer = new StringWriter(); try (final JsonGenerator generator = factory.createGenerator(writer)) { serializeContents(parser, generator); } return writer.toString(); } } } } catch (IOException e) { throw new RuntimeException(e); } return null; }
JsonPointerFunction extends FunctionExpressionImpl { @Override public Object evaluate(Object object) { final String json = getExpression(0).evaluate(object, String.class); final String pointerSpec = getExpression(1).evaluate(object, String.class); JsonPointer expectedPointer = JsonPointer.compile(pointerSpec); if (json == null) return null; try (JsonParser parser = factory.createParser(json)) { while (parser.nextToken() != END_OF_STREAM) { final JsonPointer pointer = parser.getParsingContext().pathAsPointer(); if (pointer.equals(expectedPointer) && parser.currentTokenId() != JsonTokenId.ID_FIELD_NAME) { switch (parser.currentTokenId()) { case JsonTokenId.ID_STRING: return parser.getText(); case JsonTokenId.ID_NUMBER_FLOAT: return parser.getFloatValue(); case JsonTokenId.ID_NUMBER_INT: return parser.getIntValue(); case JsonTokenId.ID_TRUE: case JsonTokenId.ID_FALSE: return parser.getBooleanValue(); default: StringWriter writer = new StringWriter(); try (final JsonGenerator generator = factory.createGenerator(writer)) { serializeContents(parser, generator); } return writer.toString(); } } } } catch (IOException e) { throw new RuntimeException(e); } return null; } }
JsonPointerFunction extends FunctionExpressionImpl { @Override public Object evaluate(Object object) { final String json = getExpression(0).evaluate(object, String.class); final String pointerSpec = getExpression(1).evaluate(object, String.class); JsonPointer expectedPointer = JsonPointer.compile(pointerSpec); if (json == null) return null; try (JsonParser parser = factory.createParser(json)) { while (parser.nextToken() != END_OF_STREAM) { final JsonPointer pointer = parser.getParsingContext().pathAsPointer(); if (pointer.equals(expectedPointer) && parser.currentTokenId() != JsonTokenId.ID_FIELD_NAME) { switch (parser.currentTokenId()) { case JsonTokenId.ID_STRING: return parser.getText(); case JsonTokenId.ID_NUMBER_FLOAT: return parser.getFloatValue(); case JsonTokenId.ID_NUMBER_INT: return parser.getIntValue(); case JsonTokenId.ID_TRUE: case JsonTokenId.ID_FALSE: return parser.getBooleanValue(); default: StringWriter writer = new StringWriter(); try (final JsonGenerator generator = factory.createGenerator(writer)) { serializeContents(parser, generator); } return writer.toString(); } } } } catch (IOException e) { throw new RuntimeException(e); } return null; } JsonPointerFunction(); }
JsonPointerFunction extends FunctionExpressionImpl { @Override public Object evaluate(Object object) { final String json = getExpression(0).evaluate(object, String.class); final String pointerSpec = getExpression(1).evaluate(object, String.class); JsonPointer expectedPointer = JsonPointer.compile(pointerSpec); if (json == null) return null; try (JsonParser parser = factory.createParser(json)) { while (parser.nextToken() != END_OF_STREAM) { final JsonPointer pointer = parser.getParsingContext().pathAsPointer(); if (pointer.equals(expectedPointer) && parser.currentTokenId() != JsonTokenId.ID_FIELD_NAME) { switch (parser.currentTokenId()) { case JsonTokenId.ID_STRING: return parser.getText(); case JsonTokenId.ID_NUMBER_FLOAT: return parser.getFloatValue(); case JsonTokenId.ID_NUMBER_INT: return parser.getIntValue(); case JsonTokenId.ID_TRUE: case JsonTokenId.ID_FALSE: return parser.getBooleanValue(); default: StringWriter writer = new StringWriter(); try (final JsonGenerator generator = factory.createGenerator(writer)) { serializeContents(parser, generator); } return writer.toString(); } } } } catch (IOException e) { throw new RuntimeException(e); } return null; } JsonPointerFunction(); @Override Object evaluate(Object object); }
JsonPointerFunction extends FunctionExpressionImpl { @Override public Object evaluate(Object object) { final String json = getExpression(0).evaluate(object, String.class); final String pointerSpec = getExpression(1).evaluate(object, String.class); JsonPointer expectedPointer = JsonPointer.compile(pointerSpec); if (json == null) return null; try (JsonParser parser = factory.createParser(json)) { while (parser.nextToken() != END_OF_STREAM) { final JsonPointer pointer = parser.getParsingContext().pathAsPointer(); if (pointer.equals(expectedPointer) && parser.currentTokenId() != JsonTokenId.ID_FIELD_NAME) { switch (parser.currentTokenId()) { case JsonTokenId.ID_STRING: return parser.getText(); case JsonTokenId.ID_NUMBER_FLOAT: return parser.getFloatValue(); case JsonTokenId.ID_NUMBER_INT: return parser.getIntValue(); case JsonTokenId.ID_TRUE: case JsonTokenId.ID_FALSE: return parser.getBooleanValue(); default: StringWriter writer = new StringWriter(); try (final JsonGenerator generator = factory.createGenerator(writer)) { serializeContents(parser, generator); } return writer.toString(); } } } } catch (IOException e) { throw new RuntimeException(e); } return null; } JsonPointerFunction(); @Override Object evaluate(Object object); static FunctionName NAME; }