-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Does dotted attribute notation work when using actual objects instead of JSON? #66
Comments
Yes, that would be good to have. Check the PR and it's unit tests. |
Maybe allow the ObjectMapper to be passed in or customized? For example, our looks like this: new ObjectMapper().
registerModule(new Jdk8Module()).
registerModule(new JavaTimeModule()).
disable(FAIL_ON_UNKNOWN_PROPERTIES).
setSerializationInclusion(NON_ABSENT).
disable(WRITE_DATES_AS_TIMESTAMPS).
enable(ALLOW_COMMENTS).
setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE).
setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss-Z")); Not all of that is relevant, but the Jdk8 and JavaTime modules probably are. |
Another question. Will the value present in TemplateContext (like in a custom Tag) be the original object, or a Map? If it is a map, then we'll just be converting the map back to an object instead of JSON back to an object in our custom tags. |
Ah yes, of course. Moved it to
I've not looked into this lib for a while, but I think it is. Give it a try. |
Whoops, I accidentally merged it (and automatically closed this issue). Feel free to re-open if needed. |
I'm getting a LinkedHashMap<String, Object> out of the TemplateContext instead of the instance of |
Maybe just add But then |
Ah, ok, I misunderstood. Yes, the instance is getting converted into a Perhaps there is, but I can't imagine a use-case where this would be useful (keeping the instance instead of a |
We are already using it in this way in our custom tags. I did a quick and dirty implementation like this: // horrible name...
abstract class BeanTag extends Tag {
protected BeanTag(String name) {
super(name);
}
protected <T> T getFromContext(String key, TemplateContext context, Class<T> clazz) {
Map<String, Object> map = (Map<String, Object>) context.get(key);
if (map == null || map.isEmpty()) return null;
return MAPPER.convertValue(map, clazz);
}
} And our tags just extend Adding it to |
We're trying to directly put an object into the render context like this:
template.render("some_top_level_name", someComplexObject);
And in the actual template, we can access the root object via
{{ some_top_level_name }}
but not any of its attributes, like{{ some_top_level_name.snake_case_attribute }}
or{{ some_top_level_name.camelCaseAttribute }}
Does this work? Are we doing it wrong?
The text was updated successfully, but these errors were encountered: