JSON - 无法使用Jackson在Object中序列化JSONObject

时间:2014-04-29 14:46:45

标签: java json serialization jackson

我有以下课程:

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

我打电话给Jacksons objectMapper.writeValueAsString(A),效果很好。

现在我需要添加另一个实例成员:

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 
}

但是当我序列化时,我得到了例外:

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

我尝试了JSONNode,但它将Output作为{outerjson: {innerjson} }而不是{outerjson:{innerjson}}。

是否可以使用Jackson实现上述输出,即JSONObject中的JSONObject?

enter image description here

4 个答案:

答案 0 :(得分:1)

好吧,如果你不能替换POJO或Map上的JSONObject,那么你可以写一个custom serializer。这是一个例子:

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));
    }
}

输出:

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

答案 1 :(得分:0)

使用JsonNode而不是JSONObject。

JsonNode jsonNode = JsonLoader.fromString(YOUR_STRING);

答案 2 :(得分:0)

使用jackson-datatype-json-org

// import com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule;

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JsonOrgModule());

请参见https://github.com/FasterXML/jackson-datatype-json-org

答案 3 :(得分:0)

对属性使用input并实现自定义序列化程序。

@JsonSerialize