简体   繁体   English

编码/解码JSON密钥?

[英]Encode/decode JSON keys?

I want to send a minified version of my JSON by minifying the keys . 我想通过缩小来发送JSON的缩小版本。

The Input JSON string obtained after marshalling my POJO to JSON: 将我的POJO编组为JSON后获得的Input JSON字符串

   {
       "stateTag" : 1,
       "contentSize" : 10,
       "content" : {
          "type" : "string",
          "value" : "Sid"
       }
   }

Desired JSON STRING which I want to send over the network to minimize payload: 我希望通过网络发送以减少有效负载的所需JSON STRING

{
   "st" : 1,
   "cs" : 10,
   "ct" : {
      "ty" : "string",
      "val" : "Sid"
   }
 }

Is there any standard way in java to achieve this ?? Java中有什么标准方法可以实现这一目标?

PS: My json string can be nested with other objects which too I will have to minify. PS:我的json字符串可以与其他对象嵌套在一起,我也必须缩小这些对象。

EDIT : 编辑

I cannot change my POJOs to provide annotations. 我无法更改POJO来提供注释。 I have XSD files from which I generate my java classes. 我有从中生成Java类的XSD文件。 So changing anything there is not an option. 因此,更改任何内容都是没有选择的。

在gson中使用批注...:在Class Member上添加@SerializedName("st")会将变量stateTag序列化为"st" : 1 ,将对象嵌套在json中的深度没有关系。

You can achieve this in Jackson by using @JsonProperty annotation. 您可以使用@JsonProperty批注在Jackson中实现。

public class Pojo {

    @JsonProperty(value = "st")
    private long stateTag;
    @JsonProperty(value = "cs")
    private long contentSize;
    @JsonProperty(value = "ct")
    private Content content;

    //getters setters
}

public class Content {

    @JsonProperty(value = "ty")
    private String type;
    @JsonProperty(value = "val")
    private String value;

}

public class App {

    public static void main(String... args) throws JsonProcessingException, IOException {

        ObjectMapper om = new ObjectMapper();

        Pojo myPojo = new Pojo(1, 10, new Content("string", "sid"));

        System.out.print(om.writerWithDefaultPrettyPrinter().writeValueAsString(myPojo));

    }

Outputs: 输出:

{
  "st" : 1,
  "cs" : 10,
  "ct" : {
    "ty" : "string",
    "val" : "sid"
  }
}

SOLUTION 2 (Using Custom Serializer): 解决方案2(使用自定义序列化程序):

This solution is specific to your pojo, it means for every pojo you will need a new serializer. 此解决方案特定于您的pojo,这意味着对于每个pojo,您都需要一个新的序列化器。

public class PojoSerializer extends JsonSerializer<Pojo> {

    @Override
    public void serialize(Pojo pojo, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
        /* your pojo */
        jgen.writeStartObject();
        jgen.writeNumberField("st", pojo.getStateTag());
        jgen.writeNumberField("cs", pojo.getContentSize());

        /* inner object */
        jgen.writeStartObject();
        jgen.writeStringField("ty", pojo.getContent().getType());
        jgen.writeStringField("val", pojo.getContent().getValue());
        jgen.writeEndObject();

        jgen.writeEndObject();
    }

    @Override
    public Class<Pojo> handledType() {
        return Pojo.class;
    }

}



ObjectMapper om = new ObjectMapper();

Pojo myPojo = new Pojo(1, 10, new Content("string", "sid"));

SimpleModule sm = new SimpleModule();
sm.addSerializer(new PojoSerializer());
System.out.print(om.registerModule(sm).writerWithDefaultPrettyPrinter().writeValueAsString(myPojo));

SOLUTION 3 (Using a naming strategy): This solution is a general solution. 解决方案3(使用命名策略):此解决方案是常规解决方案。

public class CustomNamingStrategy extends PropertyNamingStrategyBase {

    @Override
    public String translate(String propertyName) {

        // find a naming strategy here

        return propertyName;
    }

}

ObjectMapper om = new ObjectMapper();
Pojo myPojo = new Pojo(1, 10, new Content("string", "sid"));

om.setPropertyNamingStrategy(new CustomNamingStrategy());

  System.out.print(om.writerWithDefaultPrettyPrinter().writeValueAsString(myPojo));

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

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