I have written code to serialize objects to JSON and BSON. According to my output, the BSON produced is greater in size than the JSON. Is this expected?

From my code for Bson.class (using Jackson and 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);
}

From my 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() and toString() as above)

My POJOs are Person.class and Address.class.

In my main 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());

The ouput:

Bson : 301, data : -
Json : 285, data : [{"name":"Ali Bin Baba","birthd...

My Question:

  1. 这个输出是真的,还是我的代码错了?
  2. 有没有判断/测试、比较BSON和JSON大小的建议?

推荐答案

BSON FAQ人中:

BSON is designed to be efficient in space, but in many cases is not much more efficient than JSON. In some cases BSON uses even more space than JSON. The reason for this is another of the BSON design goals: traversability. BSON adds some "extra" information to documents, like length prefixes, that make it easy and fast to traverse.

BSON is also designed to be fast to encode and decode. For example, integers are stored as 32 (or 64) bit integers, so they don't need to be parsed to and from text. This uses more space than JSON for small integers, but is much faster to parse.

For a string field, the overhead in JSON is 6 bytes -- 4 quotes, a colon and a comma. In BSON it's 7 -- entry type byte, null terminator to field name, 4 byte string length, null terminator to value.

For an integer field, the JSON length depends on the size of the number. "1" is just one byte. "1000000" is 7 bytes. In BSON both of these would be a 4 byte 32 bit integer. The situation with floating point numbers is similar.

BSON不打算更小.它的目的是更接近计算机本机工作的 struct ,这样它就可以更有效地工作--这是"光"的意思之一.

如果您没有追求极高的性能水平(设计BSON的MongoDB开发人员就是这样),那么我建议使用JSON——人性化的可读性对开发人员来说是一个巨大的好处.只要您使用像Jackson这样的库,以后迁移到BSON应该不难——正如您可以看到的,您自己的BSON和JSON类几乎完全相同.

请记住,如果大小是一个问题,JSON和BSON都应该很好地压缩.

Json相关问答推荐

两种情况下过滤的JOLT规范

JQ-JSON将键转换为对象

Jolt-在数组列表中插入新的字段

解析Ansible AWS ec2_security_group中的json以提取安全组ID

Jolt-Json转换:通过引用标识符(而不是索引)设置值

将 json 转换为 jsonb 安全吗?

xidel:是否可以从 JSON 对象中检索特定的嵌套值?

如何在 Flutter 中遍历嵌套的动态 JSON 文件

在 rust 中从 API 反序列化 serde_json

使用 jq 将键值行转换为 json

如何在 jQuery 中循环遍历 JSON 数组?

python,将Json写入文件

如何为名称/值 struct 创建 JSON 模式?

如何在 django rest 框架中定义列表字段?

使用 GSON 解析嵌套的 JSON 数据

嵌套 JSON:如何向对象添加(推送)新项目?

使用 Python 3 读取 JSON 文件

[__NSCFNumber 长度]:发送到实例 UITableView 的无法识别的 Select 器

使用适用于 Python 的 Google API - 我从哪里获取 client_secrets.json 文件?

如何遍历 JSON 中的条目?