2014-04-29 18 views
7

ho la seguente classe:JSON - Impossibile serializzare JSONObject entro oggetto utilizzando Jackson

class A{  
    String abc; 
    String def; 
    // appropriate getters and setters with JsonProperty Annotation 
} 

e chiamo Jacksons objectMapper.writeValueAsString(A) che funziona bene.

Ora ho bisogno di aggiungere un altro membro di istanza:

class A{  
    String abc; 
    String def; 
    JSONObject newMember; // No, I cannot Stringify it, it needs to be JSONObject 
    // appropriate getters and setters with JsonProperty Annotation 
} 

ma quando ho serializzare, sto diventando un'eccezione:

org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer 

ho provato JSONNode ma è dato in uscita come {outerjson: " {innerjson} "} non {outerjson: {innerjson}}.

È possibile utilizzare Jackson per ottenere l'output di cui sopra, ovvero JSONObject in JSONObject?

enter image description here

+1

È possibile visualizzare l'output previsto da un dato input? –

+2

Perché non utilizzare il '' 'ObjectNode''' fornito da Jackson? – oceansize

risposta

0

Beh, se non è possibile sostituire il JSONObject su un POJO o di una mappa, allora si può scrivere un custom serializer. Ecco un esempio:

public class JacksonJSONObject { 

    public static class MyObject { 
     public final String string; 
     public final JSONObject object; 

     @JsonCreator 
     public MyObject(@JsonProperty("string") String string, @JsonProperty("object") JSONObject object) { 
      this.string = string; 
      this.object = object; 
     } 

     @Override 
     public String toString() { 
      return "MyObject{" + 
        "string='" + string + '\'' + 
        ", object=" + object + 
        '}'; 
     } 
    } 

    public static void main(String[] args) throws IOException { 
     ObjectMapper mapper = new ObjectMapper(); 
     SimpleModule module = new SimpleModule("org.json"); 
     module.addSerializer(JSONObject.class, new JsonSerializer<JSONObject>() { 
      @Override 
      public void serialize(JSONObject value, JsonGenerator jgen, SerializerProvider provider) throws IOException { 
      jgen.writeRawValue(value.toString()); 
      } 
     }); 
     module.addDeserializer(JSONObject.class, new JsonDeserializer<JSONObject>() { 
      @Override 
      public JSONObject deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { 
       Map<String, Object> bean = jp.readValueAs(new TypeReference<Map<String, Object>>() {}); 
       return new JSONObject(bean); 
      } 
     }); 
     mapper.registerModule(module); 
     JSONObject object = new JSONObject(Collections.singletonMap("key", "value")); 
     String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(new MyObject("string", object)); 

     System.out.println("JSON: " + json); 
     System.out.println("Object: " + mapper.readValue(json, MyObject.class)); 
    } 
} 

uscita:

JSON: { 
    "string" : "string", 
    "object" : {"key":"value"} 
} 
Object: MyObject{string='string', object={"key":"value"}} 
0

Uso JsonNode invece di JSONObject.

JsonNode jsonNode = JsonLoader.fromString(YOUR_STRING); 
Problemi correlati