Jackson: Serialize only 10 Properties out of 100 -
jackson default includes public getters. if have hundreds of getters , want serialize few of them, how can achieve this?
i don't want use @jsonignore
annotation specify others hundred properties.
you can use jackson's views or filters select serialization form of object. here example using filter picks properties given names:
public class jacksonfilter { @jsonfilter("filter") public static class bean { private final string field1; private final string field12; private final string field10; private final string field100; public bean(string field1, string field12, string field10, string field100) { this.field1 = field1; this.field12 = field12; this.field10 = field10; this.field100 = field100; } public string getfield1() { return field1; } public string getfield12() { return field12; } public string getfield10() { return field10; } public string getfield100() { return field100; } } public static void main(string[] args) throws jsonprocessingexception { objectmapper mapper = new objectmapper(); simplefilterprovider filters = new simplefilterprovider(); filters.addfilter("filter", simplebeanpropertyfilter.serializeallexcept("field12", "field100")); mapper.setfilters(filters); bean bean = new bean("a", "b", "c", "d"); system.out.println(mapper.writerwithdefaultprettyprinter().writevalueasstring(bean)); } }
output:
{ "field1" : "a", "field10" : "c" }
Comments
Post a Comment