@Test public void testExtractComplex() { String json = "{\"menu\": {\n" + " \"id\": \"file\",\n" + " \"value\": \"File\",\n" + " \"popup\": {\n" + " \"menuitem\": [\n" + " {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},\n" + " {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},\n" + " {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}\n" + " ]\n" + " }\n" + "}}"; assertEquals( "{\"menuitem\":[{\"value\":\"New\",\"onclick\":\"CreateNewDoc()\"},{\"value\":\"Open\",\"onclick\":\"OpenDoc()\"},{\"value\":\"Close\",\"onclick\":\"CloseDoc()\"}]}", pointer(json, "/menu/popup").evaluate(null)); assertEquals( "[{\"value\":\"New\",\"onclick\":\"CreateNewDoc()\"},{\"value\":\"Open\",\"onclick\":\"OpenDoc()\"},{\"value\":\"Close\",\"onclick\":\"CloseDoc()\"}]", pointer(json, "/menu/popup/menuitem").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; }