次の JSON の例があるとします。
{
"title" : "tttt",
"custom" : {
"a" : "aaaaa",
"b" : "bbbbb",
"c" : {
"d" : "dddd"
}
}
}
これを次のクラスに逆シリアル化したい:
public class Article {
private String title;
private String custom;
public void setTitle(String title) { this.title = title; }
public String getTitle() { return title; }
public void setCustom(String custom) { this.custom = custom; }
public String getCustom() { return custom; }
}
私が望む結果は、タイトルが通常どおり逆シリアル化されることですが、「カスタム」の下のjsonサブツリーは逆シリアル化されず、生のjson文字列として設定されます。
ObjectMapper mapper = new ObjectMapper();
Article article = mapper.readValue(content, Article.class);
assertEquals(article.getTitle(), "tttt");
assertEquals(article.getCustom(),
"{\"a\" : \"aaaaa\"," +
"\"b\" : \"bbbbb\"," +
"\"c\" : {" +
"\"d\" : \"dddd\" " +
"}" +
"}");
もう 1 つの重要な注意点は、カスタム ノードの下にある元の JSON を変更して、エスケープされた json を使用することはできないため、文字列として扱われることです。