@Test public void testResolution() { JsonElementResolver resolver = new JsonElementResolver(); ResolutionContext ctx = new DummyResolutionContext(); MustacheEngineBuilder.newBuilder() .omitServiceLoaderConfigurationExtensions() .setProperty(JsonElementResolver.UNWRAP_JSON_PRIMITIVE_KEY, true) .setProperty(GsonValueConverter.ENABLED_KEY, false) .addResolver(resolver).build(); assertNull(resolver.resolve(null, "foo", ctx)); assertNull(resolver.resolve("bar", "foo", ctx)); assertEquals(Boolean.TRUE, resolver.resolve(new JsonPrimitive(true), "unwrapThis", ctx)); assertNull(resolver.resolve(new JsonPrimitive(true), "whatever", ctx)); assertEquals(Placeholder.NULL, resolver.resolve(JsonNull.INSTANCE, "unwrapThis", ctx)); JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("foo", "bar"); jsonObject.addProperty("baz", true); assertEquals("bar", resolver.resolve(jsonObject, "foo", ctx)); assertEquals(Boolean.TRUE, resolver.resolve(jsonObject, "baz", ctx)); JsonArray jsonArray = new JsonArray(); jsonArray.add(new JsonPrimitive(true)); jsonArray.add(new JsonPrimitive(1)); assertEquals(true, resolver.resolve(jsonArray, "0", ctx)); assertEquals(1, resolver.resolve(jsonArray, "1", ctx)); }
@Override public Object resolve(Object contextObject, String name, ResolutionContext context) { if (contextObject == null || !(contextObject instanceof JsonElement)) { return null; } JsonElement element = (JsonElement) contextObject; if (element.isJsonArray() && isAnIndex(name)) { JsonArray jsonArray = (JsonArray) element; final Integer index = getIndexValue(name, context.getKey(), jsonArray.size()); if (index != null) { return unwrapJsonElementIfNecessary(jsonArray.get(index), unwrapJsonPrimitive); } } else if (element.isJsonObject()) { JsonObject jsonObject = (JsonObject) element; JsonElement member = jsonObject.get(name); if (member != null) { return unwrapJsonElementIfNecessary(member, unwrapJsonPrimitive); } } else if (name.equals(NAME_UNWRAP_THIS)) { return unwrapJsonElementIfNecessary(element, true); } return null; }
JsonElementResolver extends IndexResolver { @Override public Object resolve(Object contextObject, String name, ResolutionContext context) { if (contextObject == null || !(contextObject instanceof JsonElement)) { return null; } JsonElement element = (JsonElement) contextObject; if (element.isJsonArray() && isAnIndex(name)) { JsonArray jsonArray = (JsonArray) element; final Integer index = getIndexValue(name, context.getKey(), jsonArray.size()); if (index != null) { return unwrapJsonElementIfNecessary(jsonArray.get(index), unwrapJsonPrimitive); } } else if (element.isJsonObject()) { JsonObject jsonObject = (JsonObject) element; JsonElement member = jsonObject.get(name); if (member != null) { return unwrapJsonElementIfNecessary(member, unwrapJsonPrimitive); } } else if (name.equals(NAME_UNWRAP_THIS)) { return unwrapJsonElementIfNecessary(element, true); } return null; } }
JsonElementResolver extends IndexResolver { @Override public Object resolve(Object contextObject, String name, ResolutionContext context) { if (contextObject == null || !(contextObject instanceof JsonElement)) { return null; } JsonElement element = (JsonElement) contextObject; if (element.isJsonArray() && isAnIndex(name)) { JsonArray jsonArray = (JsonArray) element; final Integer index = getIndexValue(name, context.getKey(), jsonArray.size()); if (index != null) { return unwrapJsonElementIfNecessary(jsonArray.get(index), unwrapJsonPrimitive); } } else if (element.isJsonObject()) { JsonObject jsonObject = (JsonObject) element; JsonElement member = jsonObject.get(name); if (member != null) { return unwrapJsonElementIfNecessary(member, unwrapJsonPrimitive); } } else if (name.equals(NAME_UNWRAP_THIS)) { return unwrapJsonElementIfNecessary(element, true); } return null; } JsonElementResolver();  JsonElementResolver(int priority); }
JsonElementResolver extends IndexResolver { @Override public Object resolve(Object contextObject, String name, ResolutionContext context) { if (contextObject == null || !(contextObject instanceof JsonElement)) { return null; } JsonElement element = (JsonElement) contextObject; if (element.isJsonArray() && isAnIndex(name)) { JsonArray jsonArray = (JsonArray) element; final Integer index = getIndexValue(name, context.getKey(), jsonArray.size()); if (index != null) { return unwrapJsonElementIfNecessary(jsonArray.get(index), unwrapJsonPrimitive); } } else if (element.isJsonObject()) { JsonObject jsonObject = (JsonObject) element; JsonElement member = jsonObject.get(name); if (member != null) { return unwrapJsonElementIfNecessary(member, unwrapJsonPrimitive); } } else if (name.equals(NAME_UNWRAP_THIS)) { return unwrapJsonElementIfNecessary(element, true); } return null; } JsonElementResolver();  JsonElementResolver(int priority); @Override Object resolve(Object contextObject, String name,
            ResolutionContext context); @Override void init(); @Override Set<ConfigurationKey> getConfigurationKeys(); @Override Hint createHint(Object contextObject, String name,
            ResolutionContext context); }
JsonElementResolver extends IndexResolver { @Override public Object resolve(Object contextObject, String name, ResolutionContext context) { if (contextObject == null || !(contextObject instanceof JsonElement)) { return null; } JsonElement element = (JsonElement) contextObject; if (element.isJsonArray() && isAnIndex(name)) { JsonArray jsonArray = (JsonArray) element; final Integer index = getIndexValue(name, context.getKey(), jsonArray.size()); if (index != null) { return unwrapJsonElementIfNecessary(jsonArray.get(index), unwrapJsonPrimitive); } } else if (element.isJsonObject()) { JsonObject jsonObject = (JsonObject) element; JsonElement member = jsonObject.get(name); if (member != null) { return unwrapJsonElementIfNecessary(member, unwrapJsonPrimitive); } } else if (name.equals(NAME_UNWRAP_THIS)) { return unwrapJsonElementIfNecessary(element, true); } return null; } JsonElementResolver();  JsonElementResolver(int priority); @Override Object resolve(Object contextObject, String name,
            ResolutionContext context); @Override void init(); @Override Set<ConfigurationKey> getConfigurationKeys(); @Override Hint createHint(Object contextObject, String name,
            ResolutionContext context); static final int JSON_ELEMENT_RESOLVER_PRIORITY; static final String NAME_UNWRAP_THIS; static final ConfigurationKey UNWRAP_JSON_PRIMITIVE_KEY; }