首页 文章

如何使用Jackson在对象中包含原始JSON?

提问于
浏览
89

当使用Jackson(de)序列化对象时,我试图在Java对象中包含原始JSON . 为了测试这个功能,我写了以下测试:

public static class Pojo {
    public String foo;

    @JsonRawValue
    public String bar;
}

@Test
public void test() throws JsonGenerationException, JsonMappingException, IOException {

    String foo = "one";
    String bar = "{\"A\":false}";

    Pojo pojo = new Pojo();
    pojo.foo = foo;
    pojo.bar = bar;

    String json = "{\"foo\":\"" + foo + "\",\"bar\":" + bar + "}";

    ObjectMapper objectMapper = new ObjectMapper();
    String output = objectMapper.writeValueAsString(pojo);
    System.out.println(output);
    assertEquals(json, output);

    Pojo deserialized = objectMapper.readValue(output, Pojo.class);
    assertEquals(foo, deserialized.foo);
    assertEquals(bar, deserialized.bar);
}

代码输出以下行:

{"foo":"one","bar":{"A":false}}

JSON正是我想要看的东西 . 不幸的是,在尝试将JSON读回到对象时,代码失败并出现异常 . 这是一个例外:

org.codehaus.jackson.map.JsonMappingException:无法在[来源:java.io.StringReader@d70d7a;中的START_OBJECT标记中反序列化java.lang.String的实例 . line:1,column:13](通过参考链:com.tnal.prism.cobalt.gather.testing.Pojo [“bar”])

为什么 Jackson 在一个方向上运作良好但在向另一个方向运转时失败?看起来它应该能够再次将自己的输出作为输入 . 我知道我想要做的是非正统的(一般的建议是为 bar 创建一个内部对象,它有一个名为_2586810的属性),但我不希望我的代码需要修改 .

感谢您的建议 .

编辑:使Pojo成为静态类,导致不同的错误 .

12 回答

  • 52

    我有完全相同的问题 . 我在这篇文章中找到了解决方案:Parse JSON tree to plain class using Jackson or its alternatives

    看看最后的答案 . 通过为属性定义一个自定义setter,它将JsonNode作为参数并在jsonNode上调用toString方法来设置String属性,这一切都可以解决 .

  • 46

    @JsonRawValue仅用于序列化,因为反向操作有点棘手 . 实际上,它被添加以允许注入预编码的内容 .

    我想有可能添加对反向的支持,虽然这很尴尬:内容必须被解析,然后重新写回“原始”形式,这可能是也可能不一样(因为字符引用)可能不同) . 这是一般情况 . 但也许这对某些问题是有意义的 .

    但我认为针对您的具体情况的解决方法是将类型指定为'java.lang.Object',因为这应该可以正常工作:对于序列化,String将按原样输出,对于反序列化,它将被反序列化为一张 Map . 实际上你可能想要有单独的getter / setter,如果是这样的话; getter将返回String以进行序列化(并且需要@JsonRawValue);和setter将采用Map或Object . 如果有意义的话,你可以将它重新编码为String .

  • 32

    @StaxMan回答之后,我做了以下作品,就像一个魅力:

    public class Pojo {
      Object json;
    
      @JsonRawValue
      public String getJson() {
        // default raw value: null or "[]"
        return json == null ? null : json.toString();
      }
    
      public void setJson(JsonNode node) {
        this.json = node;
      }
    }
    

    并且,为了忠实于最初的问题,这是工作测试:

    public class PojoTest {
      ObjectMapper mapper = new ObjectMapper();
    
      @Test
      public void test() throws IOException {
        Pojo pojo = new Pojo("{\"foo\":18}");
    
        String output = mapper.writeValueAsString(pojo);
        assertThat(output).isEqualTo("{\"json\":{\"foo\":18}}");
    
        Pojo deserialized = mapper.readValue(output, Pojo.class);
        assertThat(deserialized.json.toString()).isEqualTo("{\"foo\":18}");
        // deserialized.json == {"foo":18}
      }
    }
    
  • 2

    我能够使用自定义反序列化程序(从here剪切并粘贴)执行此操作

    package etc;
    
    import java.io.IOException;
    
    import com.fasterxml.jackson.core.JsonParser;
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.core.TreeNode;
    import com.fasterxml.jackson.databind.DeserializationContext;
    import com.fasterxml.jackson.databind.JsonDeserializer;
    
    /**
     * Keeps json value as json, does not try to deserialize it
     * @author roytruelove
     *
     */
    public class KeepAsJsonDeserialzier extends JsonDeserializer<String> {
    
        @Override
        public String deserialize(JsonParser jp, DeserializationContext ctxt)
                throws IOException, JsonProcessingException {
    
            TreeNode tree = jp.getCodec().readTree(jp);
            return tree.toString();
        }
    }
    
  • 1

    @JsonSetter可能有所帮助 . 查看我的示例('data'应该包含未解析的JSON):

    class Purchase
    {
        String data;
    
        @JsonProperty("signature")
        String signature;
    
        @JsonSetter("data")
        void setData(JsonNode data)
        {
            this.data = data.toString();
        }
    }
    
  • 4

    这甚至适用于JPA实体:

    private String json;
    
    @JsonRawValue
    public String getJson() {
        return json;
    }
    
    public void setJson(final String json) {
        this.json = json;
    }
    
    @JsonProperty(value = "json")
    public void setJsonRaw(JsonNode jsonNode) {
        // this leads to non-standard json, see discussion: 
        // setJson(jsonNode.toString());
    
        StringWriter stringWriter = new StringWriter();
        ObjectMapper objectMapper = new ObjectMapper();
        JsonGenerator generator = 
          new JsonFactory(objectMapper).createGenerator(stringWriter);
        generator.writeTree(n);
        setJson(stringWriter.toString());
    }
    

    理想情况下,ObjectMapper甚至JsonFactory来自上下文,并配置为正确处理您的JSON(标准或非标准值,例如'Infinity'浮点数) .

  • 3

    这是你的内部类的问题 . Pojo 类是您的测试类的non-static inner class,而Jackson无法实例化该类 . 所以它可以序列化,但不能反序列化 .

    像这样重新定义你的类:

    public static class Pojo {
        public String foo;
    
        @JsonRawValue
        public String bar;
    }
    

    注意添加 static

  • 1

    添加到Roy Truelove的伟大answer,这是如何注入自定义反序列化器以响应 @JsonRawValue 的外观:

    import com.fasterxml.jackson.databind.Module;
    
    @Component
    public class ModuleImpl extends Module {
    
        @Override
        public void setupModule(SetupContext context) {
            context.addBeanDeserializerModifier(new BeanDeserializerModifierImpl());
        }
    }
    

    import java.util.Iterator;
    
    import com.fasterxml.jackson.annotation.JsonRawValue;
    import com.fasterxml.jackson.databind.BeanDescription;
    import com.fasterxml.jackson.databind.DeserializationConfig;
    import com.fasterxml.jackson.databind.deser.BeanDeserializerBuilder;
    import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier;
    import com.fasterxml.jackson.databind.deser.SettableBeanProperty;
    
    public class BeanDeserializerModifierImpl extends BeanDeserializerModifier {
        @Override
        public BeanDeserializerBuilder updateBuilder(DeserializationConfig config, BeanDescription beanDesc, BeanDeserializerBuilder builder) {
            Iterator<SettableBeanProperty> it = builder.getProperties();
            while (it.hasNext()) {
                SettableBeanProperty p = it.next();
                if (p.getAnnotation(JsonRawValue.class) != null) {
                    builder.addOrReplaceProperty(p.withValueDeserializer(KeepAsJsonDeserialzier.INSTANCE), true);
                }
            }
            return builder;
        }
    }
    
  • 1

    这个简单的解决方案对我有用:

    public class MyObject {
        private Object rawJsonValue;
    
        public Object getRawJsonValue() {
            return rawJsonValue;
        }
    
        public void setRawJsonValue(Object rawJsonValue) {
            this.rawJsonValue = rawJsonValue;
        }
    }
    

    所以我能够在rawJsonValue变量中存储JSON的原始值,然后将其反序列化(作为对象)与其他字段返回JSON并通过我的REST发送是没有问题的 . 使用@JsonRawValue没有帮助我,因为存储的JSON被反序列化为String,而不是对象,这不是我想要的 .

  • 2

    使用一个对象可以正常工作......这种方法有两次开销反序列化原始值两次 .

    ObjectMapper mapper = new ObjectMapper();
    RawJsonValue value = new RawJsonValue();
    value.setRawValue(new RawHello(){{this.data = "universe...";}});
    String json = mapper.writeValueAsString(value);
    System.out.println(json);
    RawJsonValue result = mapper.readValue(json, RawJsonValue.class);
    json = mapper.writeValueAsString(result.getRawValue());
    System.out.println(json);
    RawHello hello = mapper.readValue(json, RawHello.class);
    System.out.println(hello.data);
    

    RawHello.java

    public class RawHello {
    
        public String data;
    }
    

    RawJsonValue.java

    public class RawJsonValue {
    
        private Object rawValue;
    
        public Object getRawValue() {
            return rawValue;
        }
    
        public void setRawValue(Object value) {
            this.rawValue = value;
        }
    }
    
  • 15

    这是一个完整的工作示例,说明如何使用Jackson模块使 @JsonRawValue 双向工作(序列化和反序列化):

    public class JsonRawValueDeserializerModule extends SimpleModule {
    
        public JsonRawValueDeserializerModule() {
            setDeserializerModifier(new JsonRawValueDeserializerModifier());
        }
    
        private static class JsonRawValueDeserializerModifier extends BeanDeserializerModifier {
            @Override
            public BeanDeserializerBuilder updateBuilder(DeserializationConfig config, BeanDescription beanDesc, BeanDeserializerBuilder builder) {
                builder.getProperties().forEachRemaining(property -> {
                    if (property.getAnnotation(JsonRawValue.class) != null) {
                        builder.addOrReplaceProperty(property.withValueDeserializer(JsonRawValueDeserializer.INSTANCE), true);
                    }
                });
                return builder;
            }
        }
    
        private static class JsonRawValueDeserializer extends JsonDeserializer<String> {
            private static final JsonDeserializer<String> INSTANCE = new JsonRawValueDeserializer();
    
            @Override
            public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
                return p.readValueAsTree().toString();
            }
        }
    }
    

    然后,您可以在创建 ObjectMapper 后注册该模块:

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new JsonRawValueDeserializerModule());
    
    String json = "{\"foo\":\"one\",\"bar\":{\"A\":false}}";
    Pojo deserialized = objectMapper.readValue(json, Pojo.class);
    
  • 1

    我遇到了类似的问题,但使用了包含大量JSON的列表( List<String> ) .

    public class Errors {
        private Integer status;
        private List<String> jsons;
    }
    

    我使用了#来管理序列化 @JsonRawValue 注释 . 但是对于反序列化,我必须根据Roy的建议创建一个自定义 deserializer .

    public class Errors {
    
        private Integer status;
    
        @JsonRawValue
        @JsonDeserialize(using = JsonListPassThroughDeserialzier.class)
        private List<String> jsons;
    
    }
    

    下面你可以看到我的“List”反序列化器 .

    public class JsonListPassThroughDeserializer extends JsonDeserializer<List<String>> {
    
        @Override
        public List<String> deserialize(JsonParser jp, DeserializationContext cxt) throws IOException, JsonProcessingException {
            if (jp.getCurrentToken() == JsonToken.START_ARRAY) {
                final List<String> list = new ArrayList<>();
                while (jp.nextToken() != JsonToken.END_ARRAY) {
                    list.add(jp.getCodec().readTree(jp).toString());
                }
                return list;
            }
            throw cxt.instantiationException(List.class, "Expected Json list");
        }
    }
    

相关问题