哪一个是更轻的,JSON还是BSON?
我已经编写了将对象序列化为JSON和BSON的代码。根据我的输出,生成的BSON比JSON大。这是意料之中的吗?
从我的Bson代码.class
(使用Jackson和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);
}
从我的 Json.class
private ByteArrayOutputStream baos = new ByteArrayOutputStream();
private ObjectMapper mapper = new ObjectMapper();
public Json(Object obj) throws JsonGenerationException,
JsonMappingException, IOException {
mapper.writeValue(baos, obj);
}
(size()
同上)toString()
我的 POJO 是 和 。Person.class
Address.class
在我的主类中:
Address a = new Address("Jln Koli", "90121", "Vila", "Belgium");
Person p = new Person("Ali Bin Baba", new Date(), 90.0, 12, a);
List<Person> persons = new LinkedList<>();
persons.add(p);
persons.add(p);
Bson bson = new Bson(persons);
Json json = new Json(persons);
System.out.println("Bson : " + bson.size() + ", data : " + bson.toString());
System.out.println("Json : " + json.size() + ", data : " + json.toString());
输出:
Bson : 301, data : -
Json : 285, data : [{"name":"Ali Bin Baba","birthd...
我的问题:
- 该输出是真的,还是我的代码有误?
- 任何检查/测试,比较BSON和JSON大小的建议?