json - How can I use Jackson mapper for java.io.Serializable type fields? -


I have a situation where POJO extends an intangible super class, which is getId () And setId () using the java.io.Serializable type (the code shown below). Whenever I deserializing JSON string to its solid POJOs, I except the following:

  thread "main" com.fasterxml.jackson.databind.JsonMappingException exception: do not build say Java Can. Iio.Serializable, Problem: Abstract type should either be mapped to concrete type, custom deserializer, or [source: java.io.StringReader@6fd90825; To be instantiated with additional types of information; Line: 1, Column: 2] com.fasterxml on (through the reference range: com.demo.jackson.AClass [ "id"]) com.fasterxml.jackson.databind.JsonMappingException.from (JsonMappingException.java:164) On. Com.fasterxml.jackson.databind.deser at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserialize (AbstractDeserializer.java:140) at jackson.databind.DeserializationContext.instantiationException (DeserializationContext.java:716) .SettableBeanProperty.deserialize ( SettableBeanProperty.java:525) com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet (MethodProperty.java:99) on com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize (BeanDeserializer.java:242) com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize (BeanDeserializer.java:118) on com.fasterxml.jackson.databind.ObjectReader._bindAndClose (ObjectReader.java:1270) on com.fasterxml.jackson.databind.ObjectReader .readValue (ObjectReader.java:897)   

Java code:

abstract super class

  Public abstract work C abstract (protected serial zero setID) (last serial ID); Safe essence cerealable getId (); }   

Implementation class: AClass

  public square AClass Extended AbstractClass {Private Long ID; The name of the private string; Public long iid () {return id; } Public Zero Set ID (SerialJable ID) {this.id = (Long) ID; } Public string getName () {return name; } Public Zero Setname (string name) {this.name = name; }}   

Implementation classes: BClass

  public class BClass AbstractClass {Private string ID; The name of the private string; Public string getId () {return ID; } Public Zero Set ID (Serial ID) {this.id = (string) ID; } Public string getName () {return name; } Public Zero Setname (string name) {this.name = name; Public class JsonSerializerTest {Public Static Zero Main (string [] args) Exceptions thrown {Final ObjectMapper objectMapper = New ObjectMapper ()}   

Test class ; In order of objectMapper; } Private Static Zero Serialize throws exceptions {Last string jsonString = "{\" id \ ": 123, \" name \ ": \" AClass \ "}; Final Object Reader Object Reader = ObjectMaper. Reader (ACLSS class); Last AClass a = objectReader.readValue (jsonString); Println (a); Can a person provide some hint?

~ NN

We had only one condition, Must use java.io.Serializable as We have no problem with the serialization, but we have a problem in Deserialization. In our application, we use strings in JOSN objects, so Stadent String Deseilerizer is working and in principle - you can use it in conjunction with any implementation of SerialJobs. (Apache CXF and Jackson 2.4.1 are used):

Launch Jackson Provider:

  // Jackson JSON Provider Object Mapper Mapper = on New Object Mapper ( ); Mapper.registerModule (new identifiableDiscyclable module ()); Jackson Jacob JasonPriver JP = New Jackson Jacob Jason Provider (Mapper, Jackson Jacob Jason Provider. DEFAULT_ANNOTATIONS); Jp.configure (serialization feature.indcIiPUT, Indent Jason);   

and module:

  import com.fasterxml.jackson.databind.deser.std.StringDeserializer; Import com.fasterxml.jackson.databind.module.SimpleModule; Import Java.io.Serializable; / ** * java.io.Serializable classes to deserialize the Jackson module. * / Public class provides recognizable narrative module simple modules {public identifiable completion module () {addDeserializer (Serializable.class, new string_assiser ()); }}    

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -