简体   繁体   English

如何序列化具有一个成员(即json字符串)的Java对象

[英]How can I serialize a Java object that has one member which is a json string

I have a Pojo that contains one member displayPropsJson which is a clientside json string. 我有一个Pojo,其中包含一个成员displayPropsJson ,这是一个客户端json字符串。 It is validated with a JSON schema before storing on the server. 在存储在服务器上之前,将使用JSON模式对其进行验证。 ie

public class Item {
    Long id; //23
    String name; //"itemsName"
    String displayPropsJson; // "{\"bold\" : true, \"htmlAllowed\" : true, \"icon\" :\"star.jpg\" }"
}

I'd like the serialized version of this to output the displayPropsJson as displayProps sub object for example: 我想要此的序列化版本将displayPropsJson作为displayProps子对象输出:

{
   "id" :23,
   "name: : "itemsName",
   "displayProps" : {
         "bold" : true,
         "htmlAllowed" : true,
         "icon" : "star.jpg"
    }

} }

How can I do this with a Jackson serializer that outputs elements and the json string as json? 我该如何使用将元素和json字符串输出为json的Jackson序列化程序来做到这一点? The displayPropsJson will vary but is always valid json. displayPropsJson会有所不同,但始终是有效的json。

Yes I am sure this can be done with custom Jackson serializer. 是的,我确定可以使用自定义Jackson序列化程序来完成。 Another thing you could do is implement JsonSerializable, } 您可以做的另一件事是实现JsonSerializable,}

Yet another possibility is to implement the JsonSerializable interface 另一个可能性是实现JsonSerializable接口

A final possibility would be to switch libraries and use Google's GSON , which makes it easy to serialize objects into and out of json. 最后一种可能性是切换库并使用Google的GSON ,这使得将对象序列化为json和从json序列化变得容易。

You can consider two options apart of creating a custom serializer. 除了创建自定义序列化程序外,您可以考虑两个选项。

  • Use the @JsonRawString annotation to mark a String field that should be serialized as is without quoting of characters. 使用@JsonRawString批注来标记@JsonRawString序列化而不带引号的String字段。
  • Make the ObjectMapper available inside your object instance ( consider value injection ) and provide a getter method that returns JsonNode deserialized from your json string value. 使ObjectMapper在对象实例内部可用( 考虑值注入 ),并提供一个getter方法,该方法返回从json字符串值反序列化的JsonNode

Here is an example demonstrating both: 这是展示这两个示例的示例:

public class JacksonRawString {
    public static class Item {
        final private ObjectMapper mapper;

        public Long id = 23l;
        public String name = "itemsName";
        @JsonRawValue
        public String displayPropsJson = "{\"bold\" : true, \"htmlAllowed\" : true, " +
            "\"icon\" :\"star.jpg\" }";

        public JsonNode getDisplayPropsJson2() throws IOException {
            return mapper.readTree(displayPropsJson);
        }

        public Item(ObjectMapper mapper) {
            this.mapper = mapper;
        }
    }

    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(
                mapper.writerWithDefaultPrettyPrinter().writeValueAsString(new Item(mapper)));
    }
}

Output: 输出:

{
  "id" : 23,
  "name" : "itemsName",
  "displayPropsJson" : {"bold" : true, "htmlAllowed" : true, "icon" :"star.jpg" },
  "displayPropsJson2" : {
    "bold" : true,
    "htmlAllowed" : true,
    "icon" : "star.jpg"
  }
}

Note that the displayPropsJson2 get pretty output since it was serialized as JsonNode 请注意,由于displayPropsJson2被序列化为JsonNode ,因此获得了漂亮的输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将具有json-format-string的对象序列化为json - Serialize an object which has a json-format-string to json 如何使用Gson序列化具有Context作为私有成员的自定义对象? - How can I serialize a custom object that has a Context as a private member using Gson? 如何在Java集合中转换为json响应的字符串? - how can i convert this string which is json response in java collection? java - 如何将对象序列化为Java中同一对象内的方法? - How can I Serialize an object as a method inside the same object in java? 使用 Java 中的枚举成员序列化 object? - Serialize an object with enum member in Java? 如何轻松地将Java图形序列化为JSON? - How can I easily serialize Java graph to/from JSON? 不推荐使用GSON。 我们可以使用哪一个来对Java对象与JSON进行序列化和反序列化? - GSON is deprecated. Which one we can use to serialize and deserialize Java objects to and from JSON? 如何序列化/反序列化(JSON)ArrayLIst <A>,它<B>在Java中将ArrayLIst<B>作为字段</a> - How to serialize/deserialize (JSON) ArrayLIst<A> which has an ArrayLIst<B> as field in Java 在Java中将对象序列化为json? - Serialize an object to json in Java? 如何通过Gson将以下JSON字符串转换为Java Object? - How can I convert the following JSON string to Java Object by Gson?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM