在 Jackson ObjectMapper 上序列化时禁用 Base64 编码
Disable Base64 encoding while Serializing on Jackson ObjectMapper
我正在使用 Jackson 2.2.3
。当我将对象序列化为 JSON 时,所有 byte[]
属性在生成的 json.
中被编码为 base64
如何禁用它?
这是我的代码。
DataHolder.java
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"expectedData"
})
@XmlRootElement(name = "dataHolder")
public class DataHolder {
private final static long serialVersionUID = 1L;
@XmlElement(required = true, type = String.class)
@XmlJavaTypeAdapter(HexBinaryAdapter.class)
@XmlSchemaType(name = "hexBinary")
protected byte[] expectedData;
...
}
JSON 输出:
{
...
"dataHolder": {
"expectedData": "c29tZXRleHQ=",
}
...
}
我在这里创建自定义序列化程序:
public class BytesToStringSerializer extends StdSerializer<byte[]> {
public BytesToStringSerializer() {
super(byte[].class);
}
protected BytesToStringSerializer(Class<byte[]> t) {
super(t);
}
@Override
public void serialize(byte[] value, JsonGenerator gen, SerializerProvider provider) throws IOException {
String yourReadableString = new String(value, StandardCharsets.UTF_8);
gen.writeString(yourReadableString);
}
}
你的 DataHolder
class :
public class DataHolder {
@JsonSerialize(using = BytesToStringSerializer.class)
byte[] expectedData;
public DataHolder(byte[] expectedData) {
this.expectedData = expectedData;
}
}
和 Main class 用于测试:
public class Main {
public static void main(String[] args) throws JsonProcessingException {
byte[] someBytes = "Hello world".getBytes(StandardCharsets.UTF_8);
DataHolder dataHolder = new DataHolder(someBytes);
ObjectMapper objectMapper = new ObjectMapper();
String output = objectMapper.writeValueAsString(dataHolder);
System.out.println(output);
}
}
输出为:
{"expectedData":"Hello world"}
请记住,您应该使用适合您的编码。如果您的 byte[]
数组不代表任何可读的内容,您应该将其保留为 base64
格式。
编辑:
配置 ObjectMapper
全局注册模块及其序列化程序:
public class Main {
public static void main(String[] args) throws JsonProcessingException {
byte[] someBytes = "Hello world".getBytes(StandardCharsets.UTF_8);
DataHolder dataHolder = new DataHolder(someBytes);
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(byte[].class, new BytesToStringSerializer());
objectMapper.registerModule(module);
String output = objectMapper.writeValueAsString(dataHolder);
System.out.println(output);
}
}
记得为您的数据提供 getter 和 setter。如果您不希望 getter 和 setter 配置 objectMapper 并设置字段可见性:
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
我正在使用 Jackson 2.2.3
。当我将对象序列化为 JSON 时,所有 byte[]
属性在生成的 json.
base64
如何禁用它?
这是我的代码。
DataHolder.java
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"expectedData"
})
@XmlRootElement(name = "dataHolder")
public class DataHolder {
private final static long serialVersionUID = 1L;
@XmlElement(required = true, type = String.class)
@XmlJavaTypeAdapter(HexBinaryAdapter.class)
@XmlSchemaType(name = "hexBinary")
protected byte[] expectedData;
...
}
JSON 输出:
{
...
"dataHolder": {
"expectedData": "c29tZXRleHQ=",
}
...
}
我在这里创建自定义序列化程序:
public class BytesToStringSerializer extends StdSerializer<byte[]> {
public BytesToStringSerializer() {
super(byte[].class);
}
protected BytesToStringSerializer(Class<byte[]> t) {
super(t);
}
@Override
public void serialize(byte[] value, JsonGenerator gen, SerializerProvider provider) throws IOException {
String yourReadableString = new String(value, StandardCharsets.UTF_8);
gen.writeString(yourReadableString);
}
}
你的 DataHolder
class :
public class DataHolder {
@JsonSerialize(using = BytesToStringSerializer.class)
byte[] expectedData;
public DataHolder(byte[] expectedData) {
this.expectedData = expectedData;
}
}
和 Main class 用于测试:
public class Main {
public static void main(String[] args) throws JsonProcessingException {
byte[] someBytes = "Hello world".getBytes(StandardCharsets.UTF_8);
DataHolder dataHolder = new DataHolder(someBytes);
ObjectMapper objectMapper = new ObjectMapper();
String output = objectMapper.writeValueAsString(dataHolder);
System.out.println(output);
}
}
输出为:
{"expectedData":"Hello world"}
请记住,您应该使用适合您的编码。如果您的 byte[]
数组不代表任何可读的内容,您应该将其保留为 base64
格式。
编辑:
配置 ObjectMapper
全局注册模块及其序列化程序:
public class Main {
public static void main(String[] args) throws JsonProcessingException {
byte[] someBytes = "Hello world".getBytes(StandardCharsets.UTF_8);
DataHolder dataHolder = new DataHolder(someBytes);
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(byte[].class, new BytesToStringSerializer());
objectMapper.registerModule(module);
String output = objectMapper.writeValueAsString(dataHolder);
System.out.println(output);
}
}
记得为您的数据提供 getter 和 setter。如果您不希望 getter 和 setter 配置 objectMapper 并设置字段可见性:
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);