使用jackson在json中发送字节数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11546917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:27:34  来源:igfitidea点击:

Sending a byte array in json using Hymanson

jsonHymanson

提问by Sudhagar Sachin

I want to form a JSON with two fields mimetype and value.The value field should take byte array as its value.

我想用两个字段 mimetype 和 value 形成一个 JSON。 value 字段应该将字节数组作为它的值。

{

  "mimetype":"text/plain",

  "value":"dasdsaAssadsadasd212sadasd"//this value is of type byte[]

}

How can I accomplish this task?

我怎样才能完成这个任务?

As of now I am using toString()method to convert the byte array into String and form the JSON.

截至目前,我正在使用toString()方法将字节数组转换为字符串并形成 JSON。

回答by StaxMan

If you are using Hymanson for JSON parsing, it can automatically convert byte[]to/from Base64 encoded Strings via data-binding.

如果您使用 Hymanson 进行 JSON 解析,它可以byte[]通过数据绑定自动转换为/从 Base64 编码的字符串。

Or, if you want low-level access, both JsonParserand JsonGeneratorhave binary access methods (writeBinary, readBinary) to do the same at level of JSON token stream.

或者,如果你想低级别的访问,双方JsonParserJsonGenerator有二进制访问方法(writeBinary,readBinary)做同样的JSON令牌流的水平。

For automatic approach, consider POJO like:

对于自动方法,请考虑 POJO,例如:

public class Message {
  public String mimetype;
  public byte[] value;
}

and to create JSON, you could do:

要创建 JSON,您可以执行以下操作:

Message msg = ...;
String jsonStr = new ObjectMapper().writeValueAsString(msg);

or, more commonly would write it out with:

或者,更常见的是将它写出来:

OutputStream out = ...;
new ObjectMapper().writeValue(out, msg);

回答by Alberto Estrella

You can write your own CustomSerializer like this one:

您可以像这样编写自己的 CustomSerializer:

public class ByteArraySerializer extends JsonSerializer<byte[]> {

@Override
public void serialize(byte[] bytes, JsonGenerator jgen,
        SerializerProvider provider) throws IOException,
        JsonProcessingException {
    jgen.writeStartArray();

    for (byte b : bytes) {
        jgen.writeNumber(unsignedToBytes(b));
    }

    jgen.writeEndArray();

}

private static int unsignedToBytes(byte b) {
    return b & 0xFF;
  }

}

This one returns an unsigned byte array representation instead of a Base64 string.

这个返回一个无符号字节数组表示而不是 Base64 字符串。

How to use it with your POJO:

如何在您的 POJO 中使用它:

public class YourPojo {

    @JsonProperty("mimetype")
    private String mimetype;
    @JsonProperty("value")
    private byte[] value;



    public String getMimetype() { return this.mimetype; }
    public void setMimetype(String mimetype) { this.mimetype = mimetype; }

    @JsonSerialize(using= com.example.yourapp.ByteArraySerializer.class)
    public byte[] getValue() { return this.value; }
    public void setValue(String value) { this.value = value; }


}

And here is an example of it's output:

这是它的输出示例:

{
    "mimetype": "text/plain",
    "value": [
        81,
        109,
        70,
        122,
        90,
        83,
        65,
        50,
        78,
        67,
        66,
        84,
        100,
        72,
        74,
        108,
        89,
        87,
        48,
        61
    ]
}

P.S.: This serializer is a mix of some answers that I found on StackOverflow.

PS:这个序列化程序是我在 StackOverflow 上找到的一些答案的混合。

回答by Dreen

You might want to use Base64which converts binary data to a string. Most programming languages have implementations of base64 encoding and decoding. If you want to decode/encode in a browser, see this question.

您可能希望使用Base64将二进制数据转换为字符串。大多数编程语言都有 base64 编码和解码的实现。如果您想在浏览器中解码/编码,请参阅此问题