json - Ignore the "Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token" error -
i using api external partner. unfortunately, returned response not seem have fixed structure. ideally, api contract means won't violated keeps happening.
anyways, happening field in json response map sometimes, out of blue list.
for example, suppose following response get:
{ "majorinfo" : { "a" : "b" }, "minorinfo" : { "c" : "d" } }
but on rare occasion i'd list instead of map or other violation of contract.
for example:
{ "majorinfo" : { "a" : "b" }, "minorinfo" : [] }
i using jackson map response pojo. in cases, when contract violated, error,
exception in thread "main" com.fasterxml.jackson.databind.jsonmappingexception: can not deserialize instance of java.util.linkedhashmap out of start_array token
in case, lose information in field majorinfo though adhered contract. there way can ignore field when not adhere contract? in case, majorinfo member of pojo correctly set minorinfo member null.
i know @jsonignoreproperties(ignoreunknown = true) always ignore minorinfo field. ignored when field not adhere contract. possible?
i tried
mapper.configure(deserializationfeature.fail_on_unknown_properties, false);
but did not work either.
is other solution possible? external partner not going change api sure. so, workable solution our end?
thank you
edit: 1 solution have pojo both variants , put code in try catch block. may have worked if json response had 1 field violated contract , in 1 particular way. response i'm getting huge , third violation have caught on third field. can't keep putting try catch blocks , third violation i've realized best bet ignore fields violating it.
you can write own java method transforming input json "standard" json adheres contract, , use jackson on it.
something along lines of:
private string transform(string input) { string result = input; if (result.contains("\"minorinfo\" : []")) { result = result.replace("\"minorinfo\" : []", ""); } return result; }
Comments
Post a Comment