Ho un problema deserializzare una stringa JSON usando Jackson (ma non ho problemi nel serializzare un oggetto su JSON).Deserializzazione JSON con Jackson - Perché JSONMappingException "Nessun costruttore adatto"?
Di seguito vi presento le classi che uso. Il problema nasce quando ho rececive un JSON-stringa (una ProtocolContainer che è stato serializzato altrove e recuperato tramite webservice) e voglio deserializzare esso:
JSON-string:
{"DataPacketJSONString":null,"DataPacketType":"MyPackage.DataPackets.LoginRequestReply","MessageId":6604,"SenderUsername":null,"SubPacket":{"__type":"LoginRequestReply:#MyPackage.DataPackets","Reason":"Wrong pass or username","Success":false,"Username":"User1"}}
cerco di deserializzare come questo:
ProtocolContainer ret = ProtocolContainer.Create(jsonString);
e il codice che si esegue in ProtocolContainer può essere visto di seguito. L'eccezione:
org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class MyPackage.ProtocolContainer]: can not instantiate from JSON object (need to add/enable type information?) at [Source: [email protected]; line: 1, column: 2]
Mi piacerebbe davvero apprezzare un certo input qui =) Thx!
ProtocolContainer.java - una classe contenitore che racchiude in sé i miei "sottopacchetti":
import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import MyPackage.DataPackets.*;
public class ProtocolContainer
{
public String SenderUsername;
public String DataPacketType;
public long MessageId;
public String DataPacketJSONString;
public DataPacket SubPacket;
public ProtocolContainer(DataPacket dp)
{
DataPacketType = dp.getClass().toString().substring(6);
SubPacket = dp;
}
public String toJSON()
{
try {
if (SubPacket != null)
this.DataPacketJSONString = ProtocolContainer.mapper.writeValueAsString(SubPacket);
return ProtocolContainer.mapper.writeValueAsString(this);
} catch (JsonGenerationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static ObjectMapper mapper = new ObjectMapper();
public static ProtocolContainer Create(String jsonString)
{
ProtocolContainer pc = null;
try {
pc = mapper.readValue(jsonString, ProtocolContainer.class); // error here!
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace(); // Exception when deserializing
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
if (pc != null && pc.DataPacketType == "LoginRequest")
pc.SubPacket = mapper.readValue(jsonString, LoginRequest.class);
}
catch (JsonParseException e)
{
e.printStackTrace();
}
catch (JsonMappingException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return pc;
}
}
DataPacket.java - una superclasse per tutti i miei datapackets
public class DataPacket
{
}
LoginRequestReply. java - a DataPacket
package MyPackage.DataPackets;
import MyPackage.DataPacket;
public class LoginRequestReply extends DataPacket
{
public boolean LoginOK;
public int UserId;
}
Follow-up: Dopo alcuni problemi, non viene visualizzato il seguente errore: * JsonMappingException: impossibile creare un'istanza del valore di tipo [tipo semplice, classe MyPackage.ProtocolContainer] da JSON String; nessun metodo costruttore/factory a stringa singola *. Se aggiungo un costruttore che accetta una stringa, quindi nessun errore, ma l'oggetto è "vuoto" ... Non penso che sia necessario aggiungere il supporto all'implementazione nel costruttore. ** Come dovrei risolvere questo? ** – Ted