@Test public void testResolution() { JsonValueResolver resolver = new JsonValueResolver(); ResolutionContext ctx = new DummyResolutionContext(); MustacheEngineBuilder.newBuilder() .omitServiceLoaderConfigurationExtensions() .addResolver(resolver) .setProperty(JsonValueResolver.ENABLED_KEY, true).build(); assertNull(resolver.resolve(null, "foo", ctx)); assertNull(resolver.resolve("bar", "foo", ctx)); assertEquals(Boolean.TRUE, resolver.resolve(JsonValue.TRUE, "unwrapThis", ctx)); assertNull(resolver.resolve(JsonValue.TRUE, "whatever", ctx)); assertEquals(Placeholder.NULL, resolver.resolve(JsonValue.NULL, "unwrapThis", ctx)); JsonObject jsonObject = Json.createObjectBuilder().add("foo", "bar") .add("baz", true).build(); assertEquals("bar", resolver.resolve(jsonObject, "foo", ctx)); assertEquals(Boolean.TRUE, resolver.resolve(jsonObject, "baz", ctx)); JsonArray jsonArray = Json.createArrayBuilder().add(true).add(1) .build(); assertEquals(true, resolver.resolve(jsonArray, "0", ctx)); assertEquals(BigDecimal.ONE, resolver.resolve(jsonArray, "1", ctx)); }
@Override public Object resolve(Object contextObject, String name, ResolutionContext context) { if (contextObject == null || !(contextObject instanceof JsonValue)) { return null; } JsonValue jsonValue = (JsonValue) contextObject; if (ValueType.ARRAY.equals(jsonValue.getValueType()) && isAnIndex(name)) { JsonArray jsonArray = (JsonArray) jsonValue; final Integer index = getIndexValue(name, context.getKey(), jsonArray.size()); if (index != null) { return unwrapJsonValueIfNecessary(jsonArray.get(index)); } } else if (ValueType.OBJECT.equals(jsonValue.getValueType())) { JsonObject jsonObject = (JsonObject) jsonValue; JsonValue member = jsonObject.get(name); if (member != null) { return unwrapJsonValueIfNecessary(member); } } else if (name.equals(NAME_UNWRAP_THIS)) { return unwrapJsonValueIfNecessary(jsonValue); } return null; }
JsonValueResolver extends IndexResolver implements Validateable { @Override public Object resolve(Object contextObject, String name, ResolutionContext context) { if (contextObject == null || !(contextObject instanceof JsonValue)) { return null; } JsonValue jsonValue = (JsonValue) contextObject; if (ValueType.ARRAY.equals(jsonValue.getValueType()) && isAnIndex(name)) { JsonArray jsonArray = (JsonArray) jsonValue; final Integer index = getIndexValue(name, context.getKey(), jsonArray.size()); if (index != null) { return unwrapJsonValueIfNecessary(jsonArray.get(index)); } } else if (ValueType.OBJECT.equals(jsonValue.getValueType())) { JsonObject jsonObject = (JsonObject) jsonValue; JsonValue member = jsonObject.get(name); if (member != null) { return unwrapJsonValueIfNecessary(member); } } else if (name.equals(NAME_UNWRAP_THIS)) { return unwrapJsonValueIfNecessary(jsonValue); } return null; } }
JsonValueResolver extends IndexResolver implements Validateable { @Override public Object resolve(Object contextObject, String name, ResolutionContext context) { if (contextObject == null || !(contextObject instanceof JsonValue)) { return null; } JsonValue jsonValue = (JsonValue) contextObject; if (ValueType.ARRAY.equals(jsonValue.getValueType()) && isAnIndex(name)) { JsonArray jsonArray = (JsonArray) jsonValue; final Integer index = getIndexValue(name, context.getKey(), jsonArray.size()); if (index != null) { return unwrapJsonValueIfNecessary(jsonArray.get(index)); } } else if (ValueType.OBJECT.equals(jsonValue.getValueType())) { JsonObject jsonObject = (JsonObject) jsonValue; JsonValue member = jsonObject.get(name); if (member != null) { return unwrapJsonValueIfNecessary(member); } } else if (name.equals(NAME_UNWRAP_THIS)) { return unwrapJsonValueIfNecessary(jsonValue); } return null; } JsonValueResolver();  JsonValueResolver(int priority); }
JsonValueResolver extends IndexResolver implements Validateable { @Override public Object resolve(Object contextObject, String name, ResolutionContext context) { if (contextObject == null || !(contextObject instanceof JsonValue)) { return null; } JsonValue jsonValue = (JsonValue) contextObject; if (ValueType.ARRAY.equals(jsonValue.getValueType()) && isAnIndex(name)) { JsonArray jsonArray = (JsonArray) jsonValue; final Integer index = getIndexValue(name, context.getKey(), jsonArray.size()); if (index != null) { return unwrapJsonValueIfNecessary(jsonArray.get(index)); } } else if (ValueType.OBJECT.equals(jsonValue.getValueType())) { JsonObject jsonObject = (JsonObject) jsonValue; JsonValue member = jsonObject.get(name); if (member != null) { return unwrapJsonValueIfNecessary(member); } } else if (name.equals(NAME_UNWRAP_THIS)) { return unwrapJsonValueIfNecessary(jsonValue); } return null; } JsonValueResolver();  JsonValueResolver(int priority); @Override Object resolve(Object contextObject, String name,
            ResolutionContext context); @Override Hint createHint(Object contextObject, String name,
            ResolutionContext context); @Override void init(Configuration configuration); @Override Set<ConfigurationKey> getConfigurationKeys(); }
JsonValueResolver extends IndexResolver implements Validateable { @Override public Object resolve(Object contextObject, String name, ResolutionContext context) { if (contextObject == null || !(contextObject instanceof JsonValue)) { return null; } JsonValue jsonValue = (JsonValue) contextObject; if (ValueType.ARRAY.equals(jsonValue.getValueType()) && isAnIndex(name)) { JsonArray jsonArray = (JsonArray) jsonValue; final Integer index = getIndexValue(name, context.getKey(), jsonArray.size()); if (index != null) { return unwrapJsonValueIfNecessary(jsonArray.get(index)); } } else if (ValueType.OBJECT.equals(jsonValue.getValueType())) { JsonObject jsonObject = (JsonObject) jsonValue; JsonValue member = jsonObject.get(name); if (member != null) { return unwrapJsonValueIfNecessary(member); } } else if (name.equals(NAME_UNWRAP_THIS)) { return unwrapJsonValueIfNecessary(jsonValue); } return null; } JsonValueResolver();  JsonValueResolver(int priority); @Override Object resolve(Object contextObject, String name,
            ResolutionContext context); @Override Hint createHint(Object contextObject, String name,
            ResolutionContext context); @Override void init(Configuration configuration); @Override Set<ConfigurationKey> getConfigurationKeys(); static final int JSON_VALUE_RESOLVER_PRIORITY; static final ConfigurationKey ENABLED_KEY; static final String NAME_UNWRAP_THIS; }