antibrumm / jackson-antpathfilter Goto Github PK
View Code? Open in Web Editor NEWAntPathPropertyFilter for Jackson2
AntPathPropertyFilter for Jackson2
Havent really tested performance of the filter itself yet. Would be good to gather some ideas of how to improve it for complex objects.
To make the example working i had to change this line:
mapper.addMixIn(Object.class, AntPathFilterMixin.class);
to
mapper.addMixInAnnotations(Object.class, AntPathFilterMixin.class);
I am using jackson 2.8.1
Using Spring Boot 2.0.4.RELEASE, which is based on Spring 5.0.8.RELEASE.
When following the instructions to setup the AntPathFilter for "Spring 4.2.2+", that is, extending DelegatingWebMvcConfiguration
, then an exception occurs in cases when the AntPathFilter should not be applied. (The problem is somewhat similar to this post on SO.) Things work fine for controller methods that apply the AntPathFilter for serialization of their response body.
This configuration creates problems for controllers that do not apply the AntPathFilter:
@Configuration
public class WebConfig extends DelegatingWebMvcConfiguration {
@Override
public void configureMessageConverters(final List<HttpMessageConverter<?>> messageConverters) {
// Add a MappingJackson2HttpMessageConverter so that
// objectMapper.writeFiltered
// is using the objectMapper configured with the needed Mixin
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder
.json()
.mixIn(Object.class, AntPathFilterMixin.class)
.build();
messageConverters.add(new MappingJackson2HttpMessageConverter(objectMapper));
addDefaultHttpMessageConverters(messageConverters);
}
}
When a controller doesn't use the AntPathFilter, the following exception occurs:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot resolve PropertyFilter with id 'antPathFilter'; no FilterProvider configured
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1191) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:312) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.std.StdSerializer.findPropertyFilter(StdSerializer.java:426) ~[jackson-databind-2.9.6.jar:2.9.6]
...
If I try to configure other AntPathPropertyFilters with different names (e.g. MyService -> new AntPathPropertyFilter("**.someproperty") it appears to fail. The library also appears to work only as long as you have a filter of the name antPathFilter. Is there some workaround to allow different names for the path filter? It's entirely possible that I've just missed something, but it wasn't obvious fooling around with it for ~1 hour.
It makes more sense to use the !
character then the -
as it's the standard negating character in java and also used for example in NPM/Node builds.
The following does not work correctly:
ObjectMapper mapper = buildObjectMapper();
String[] includedFieldNames = { "id", "judgementNo", "judgementDate" ,"courthouse","courthouse.name", "@loaded"};
mapper.addMixIn(Object.class, AntPathFilterMixin.class);
com.fasterxml.jackson.databind.type.CollectionType collectionType = mapper.getTypeFactory().constructCollectionType(List.class, Object.class);
// Create map
FilterProvider filterProvider = new SimpleFilterProvider().addFilter("antPathFilter", new AntPathPropertyFilter(includedFieldNames));
mapper.setFilterProvider(filterProvider);
List<Object> map = mapper.convertValue(judgements, collectionType);
It does not serialize court properties, so it comes empty in the map. It seems that it does not guess the path correctly in this.include(writer, jgen).
I am out of time currently, so can't provide more details right now, in case you needed them.
Hello,
Thanks for the addon - works great. I have a new request, which is to support filtering of nested array objects. How could achieve that?
For example:
{
"user_id": 123,
"name": "foo",
"roles": [
{
"name": "ROLE_USER",
"description": "Regular users."
},
{
"name": "ROLE_ADMIN",
"description": "Administrators."
}
]
}
How to filter out the roles[*].description
from the above Json string? Thanks!
Hi. I'm using version 1.0.0 and when there is a loop in collections, I get "infinite recursion" Jackson error showing the relation that is causing the problem but when I've tested new version 1.0.2, I'm only getting "java.lang.OutOfMemoryError: GC overhead limit" error, without any other useful information. Because that I'm using the previous version again.
Your library is very useful to deal with collections, using JPA and REST. So, I'll be attentive to new releases. Thank you very much.
How to use it with JAX-RS? I tried to set the filter in a WriterInterceptor
but subsequent HTTP requests overlaps property filters and the outcome becomes unpredictable.
I have a requirement to filter Json objects using Ant like paths. AntPathFilter library sounds like a perfect solution but unfortunately my data is in flexible data structures such as Jackson ObjectNode and Java Map. I couldn't get it to work with Jackson ObjectNode.
@Test
public void testAntPathFilterObject() throws Exception {
ObjectNode user = JsonNodeFactory.instance.objectNode();
user.put("name", "Venkat").put("dept", "Eng").putObject("manager").put("name", "Scot").put("dept", "Eng");
String[] filter = new String[] {"!manager"};
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.addMixIn(Object.class, AntPathFilterMixin.class);
FilterProvider filterProvider = new SimpleFilterProvider().addFilter("antPathFilter", new AntPathPropertyFilter(filter));
objectMapper.setFilters(filterProvider);
String json = objectMapper.writeValueAsString(user);
System.out.println("json="+json);
}
This gives me
json={"name":"Venkat","dept":"Eng","manager":{"name":"Scot","dept":"Eng"}}
But the following code with POJO works as expected:
public void testAntPathFilterPOJO() throws Exception {
User user = new User();
User manager = new User();
manager.setName("Scot");
manager.setDept("Eng");
user.setName("Venkat");
user.setDept("Eng");
user.setManager(manager);
String[] filter = new String[] {"!manager"};
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.addMixIn(Object.class, AntPathFilterMixin.class);
FilterProvider filterProvider = new SimpleFilterProvider().addFilter("antPathFilter", new AntPathPropertyFilter(filter));
objectMapper.setFilters(filterProvider);
String json = objectMapper.writeValueAsString(user);
System.out.println("json="+json);
}
This produces intended result:
json={"name":"Venkat","dept":"Eng"}
Does the library work with non-POJOs? It will be great to have AntPathFilter working with non-POJO such as Jackson JsonNode, Map, etc.
Jackson2Helper instance is uses a common object mapper shared across all requests, so if one of the requests sets specific properties for filtering, then a request happening around the same could accidentally use the same property filter.
Is there a way to re use object mapper?
Hi,
I've got the following json :
[
{
"id": "test",
"firstName": "Hello",
"lastName": "World"
}
]
I know it's not an object, it's my entity list inserted into a paginate element. Is it possible to filter on such case ? I couldn't make it work.
Cheers
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.