jackson - Deserializing a list results in duplicates -
i have simple pojo have 1 list of strings , default get/set, have in json 2 different fields pojo , test code snippet below
public static class testclass{ public arraylist<string> names = null; public arraylist<string> getnames() { if(null == names) names = new arraylist<>(); return names; } public void setnames(arraylist<string> names) { this.names = names; } public arraylist<string> getnames_r() { return getnames(); } @override public string tostring() { return "testclass [names=" + names + "]"; } } @test public void testdeserializationsimple() throws jsonparseexception, jsonmappingexception, ioexception{ string justschool = "{\"names\":[\"second\",\"one\",\"two\",\"three\"],\"names_r\":[\"second\",\"one\",\"two\",\"three\"]}"; objectmapper mydefaultmapper= new objectmapper(); mydefaultmapper.setdateformat(coreutils.common_simple_date_format) .settimezone(timezone.gettimezone("utc")) .enable(serializationfeature.indent_output) .configure(deserializationfeature.fail_on_unknown_properties, false); testclass testreaddummy = mydefaultmapper.readvalue(justschool, testclass.class); system.out.println(" mapper test read = "+testreaddummy); //assertequals(testread.getliststring().size(),4); system.out.println("list = "+testreaddummy); assertequals(testreaddummy.names.size(), 4); }
try using @jsonignore
annotation in duplicate of list doesn't serialized, like:
@jsonignore public arraylist<string> getnames_r() { return getnames(); }
that should rid of duplicate field in serialized json.
hope helps,
jose luis
Comments
Post a Comment