我编写了将对象序列化为JSON和BSON的代码.根据我的输出,生成的BSON的大小比JSON大.这是预期的吗?
从我的代码Bson.class(使用杰克逊和bson4jackson)
private ByteArrayOutputStream baos = new ByteArrayOutputStream();
private BsonFactory fac = new BsonFactory();
private ObjectMapper mapper = new ObjectMapper(fac);
public Bson(Object obj) throws JsonGenerationException,
JsonMappingException, IOException {
mapper.writeValue(baos, obj);
}
public int size() {
return baos.size();
}
public String toString() {
byte[] bytes = baos.toByteArray();
return new String(bytes);
}
Run Code Online (Sandbox Code Playgroud)
从我的 Json.class
private ByteArrayOutputStream baos = new ByteArrayOutputStream();
private ObjectMapper mapper = new ObjectMapper();
public Json(Object obj) throws JsonGenerationException,
JsonMappingException, IOException {
mapper.writeValue(baos, obj);
}
Run Code Online (Sandbox Code Playgroud)
(size()和 …
这是我的FacilityDTO
@JsonRootName("Facility")
@XmlAccessorType(XmlAccessType.NONE)
@XmlType(name = "Facility", propOrder = { "id", "name", "totalQuantity" })
public class FacilityDTO implements Serializable {
private static final long serialVersionUID = 1L;
@XmlElement(required = true)
private String id;
@XmlElement(required = true)
private String name;
@XmlElement(required = true)
private double totalQuantity;
public FacilityDTO() {
}
public FacilityDTO(Facility facility) {
this.name = facility.getName();
this.totalQuantity = facility.getTotalQuantity();
this.id = facility.getId();
}// getters setter
Run Code Online (Sandbox Code Playgroud)
这是我的留言机构作家
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new Jdk7Module());
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
mapper.writeValue(out, …Run Code Online (Sandbox Code Playgroud)