当我使用Jackson(de)序列化对象时,我试图将原始JSON包含在Java对象中。为了测试这个功能,我写了以下测试:
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:无法从START_OBJECT令牌中反序列化java.lang.String的实例 在[来源:java.io.StringReader@d70d7a; line:1,column:13](通过参考链:com.tnal.prism.cobalt.gather.testing.Pojo [“bar”])
为什么杰克逊在一个方向上运作良好但在向另一个方向运转时失败?看起来它应该能够再次将自己的输出作为输入。我知道我要做的是非正统的(一般的建议是为bar
创建一个内部对象,它有一个名为A
的属性,但我不想与这个JSON交互一点都不我的代码充当了这段代码的传递 - 我想要接受这个JSON并再次发送它而不会触及任何东西,因为当JSON更改时我不希望我的代码需要修改。
感谢您的建议。
编辑:使Pojo成为一个静态类,导致了不同的错误。答案 0 :(得分:54)
@JsonRawValue仅用于序列化,因为反向的处理有点棘手。实际上,它被添加以允许注入预编码的内容。
我想有可能添加对反向的支持,虽然这很尴尬:内容必须被解析,然后重新写回“原始”形式,这可能是也可能不相同(因为字符引用可能不同)。 这是一般情况。但也许这对某些问题有意义。
但我认为针对您的特定情况的解决方法是将类型指定为'java.lang.Object',因为这应该可以正常工作:对于序列化,String将按原样输出,并且对于反序列化,它将被反序列化为地图。实际上你可能想要有单独的getter / setter,如果是这样的话; getter将返回String以进行序列化(并且需要@JsonRawValue);和setter将采用Map或Object。如果有意义的话,你可以将它重新编码为String。
答案 1 :(得分:48)
在@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 :(得分:32)
我能够使用自定义反序列化程序(从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();
}
}
答案 3 :(得分:3)
这是您的内部类的问题。 Pojo
类是您的测试类的non-static inner class,而Jackson无法实例化该类。所以它可以序列化,但不能反序列化。
像这样重新定义你的课程:
public static class Pojo {
public String foo;
@JsonRawValue
public String bar;
}
请注意添加static
答案 4 :(得分:3)
添加到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;
}
}
答案 5 :(得分:3)
这甚至适用于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'浮点数。)
答案 6 :(得分:2)
这个简单的解决方案对我有用:
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,而不是对象,这不是我想要的。
答案 7 :(得分:1)
我有完全相同的问题。 我在这篇文章中找到了解决方案: Parse JSON tree to plain class using Jackson or its alternatives
查看最后一个答案。 通过为属性定义一个自定义setter,它将JsonNode作为参数并在jsonNode上调用toString方法来设置String属性,这一切都可以解决。
答案 8 :(得分:1)
使用对象可以正常工作......这种方法在两次反序列化原始值时会有一些开销。
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;
}
}
答案 9 :(得分:1)
以下是如何使用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);
答案 10 :(得分:1)
我遇到了类似的问题,但是使用的列表中包含很多JSON内容(List<String>
)。
public class Errors {
private Integer status;
private List<String> jsons;
}
我使用@JsonRawValue
批注管理序列化。但是对于反序列化,我不得不根据Roy的建议创建一个自定义的反序列化器。
public class Errors {
private Integer status;
@JsonRawValue
@JsonDeserialize(using = JsonListPassThroughDeserialzier.class)
private List<String> jsons;
}
下面您可以看到我的“列表”反序列化器。
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");
}
}