简体   繁体   English

Jackson:将JSON对象序列化为多个对象

[英]Jackson : Serialize a JSON object to multiple objects

I'm given a string which represents a json object of type Map<String, String> . 我给了一个字符串,它表示Map<String, String>类型的json对象。 There is a constraint on the number of characters in one json object. 一个json对象中的字符数受到限制。 I would have to convert the json object into Array of multiple json objects, if the number of characters in the JSON object exceeds the specified limit. 如果JSON对象中的字符数超过指定的限制,我将不得不将json对象转换为多个json对象的数组。 What is the easiest and clean way of doing this using Jackson Mapper? 使用Jackson Mapper进行此操作最简单,最干净的方法是什么?

For example: if this is the json object given is: 例如:如果这是给定的json对象,则为:

{
   "cfname":"Kob",
   "NAME_6":"Philharmonic Youth Orchestra",
   "NAME_5":"Pathways to Discovery: Engineering, Medicine & CSI",
   "NAME_4":"Fashion Design Camp",
   "IMAGE_URL_1":"http://res.cloudinary.com/pxxxxxx-jxxxxxx/image/upload/c_fill,h_230,w_260/v0000000000/vccrgwdekjkpdvpsrv4f.jpg",
   "IMAGE_URL_2":"http://res.cloudinary.com/pxxxxxx-jxxxxxx/image/upload/c_fill,h_230,w_260/v0000000000/wlom2u5525nyjjbttazw.jpg"
}

if the character count limit for one json object is 200. Then the output would be: 如果一个json对象的字符数限制为200。则输出为:

[
       {
          "cfname":"Kob",
          "NAME_6":"Philharmonic Youth Orchestra",
          "NAME_5":"Pathways to Discovery: Engineering, Medicine & CSI",
          "NAME_4":"Fashion Design Camp"
       },
       {
          "IMAGE_URL_1":"http://res.cloudinary.com/pxxxxxx-jxxxxxx/image/upload/c_fill,h_230,w_260/v0000000000/vccrgwdekjkpdvpsrv4f.jpg"
       },
       {
          "IMAGE_URL_2":"http://res.cloudinary.com/pxxxxxx-jxxxxxx/image/upload/c_fill,h_230,w_260/v0000000000/wlom2u5525nyjjbttazw.jpg"
       }
]
  1. We can assume that the length of any key-value pair in the given json object will be less than given character count limit. 我们可以假设给定json对象中任何键值对的长度都小于给定的字符数限制。
  2. we should ignore whitespaces and new line characters while counting characters. 我们在计数字符时应忽略空格和换行符。

Usecase : I have to add these json objects as a header to the email. 用例:我必须将这些json对象添加为电子邮件的标头。 Since smtp has a character limit of 1000 for each header value. 由于smtp每个标题值的字符数限制为1000。 I have to break it up into multiple json objects, each json object will be one header value. 我必须将其分解为多个json对象,每个json对象将是一个标头值。

Finally I just used the trivial approach. 最后,我只是使用了简单的方法。 Here is the code snippet I wrote, useful for others for quick use: 这是我写的代码片段,对其他人快速使用很有用:

int                 charCount = 11 + rcpt.length();
Map<String, String> map       = Maps.newHashMap();

for (Entry<String, String> e : mergeTags.entrySet()) {
  int cnt = charCount(e);

  if (charCount + cnt >= 987) {
    message.addHeader(TEMPLATE_MERGEVARS_HEADER, BINDER.writeValue(map));
    map = Maps.newHashMap();
    charCount = 11 + rcpt.length();
  }

  map.put(e.getKey(), e.getValue());
  charCount += cnt;
}

if (!map.isEmpty()) {
  message.addHeader(TEMPLATE_MERGEVARS_HEADER, BINDER.writeValue(map));
}

private static int charCount(Entry<String, String> e) {
  return e.getKey().length() + e.getValue().length() + 6;
}

message will contain all the header values as multi-map. 消息将包含所有标头值作为多图。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM