哪一个更轻,JSON还是BSON? [英] Which one is lighter, JSON or BSON?

查看:154
本文介绍了哪一个更轻,JSON还是BSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了将对象序列化为JSON和BSON的代码。根据我的输出,生成的BSON的大小比JSON大。这是预期的吗?

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?

从我的代码 Bson.class (使用Jackson和bson4jackson)

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);
}

来自我的 Json.class

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() 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...

我的问题:


  1. 这个输出是真的,还是我的代码错了?

  2. 有什么建议检查/测试,比较BSON和JSON的大小?


推荐答案

来自 BSON常见问题解答


BSON旨在提高空间效率,但在许多情况下不是
效率更高在某些情况下,BSON比JSON使用更多的空间
。原因是另一个BSON设计目标:
可穿越性.BSON为文档添加了一些额外信息,比如
长度前缀,可以轻松快速地遍历。

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还可以快速编码和解码。例如e,
整数存储为32(或64)位整数,因此它们不需要在文本中解析
。对于小的
整数,这比JSON使用更多的空间,但解析起来要快得多。

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.

对于字符串字段, JSON中的开销是6个字节 - 4个引号,冒号和逗号。在BSON中,它是7 - 条目类型字节,字段名称为空终止符,4字节字符串长度,值为null终止符。

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.

对于整数字段,JSON长度取决于关于号码的大小。 1只是一个字节。 1000000是7个字节。在BSON中,这两者都是4字节32位整数。浮点数的情况类似。

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不是更小。它旨在更接近计算机本机工作的结构,以便更有效地工作 - 这就是轻的一种含义。

BSON is not intended to be smaller. It is intended to be closer to the structures that computers work with natively, so that it can be worked with more efficiently -- that is one meaning of "light".

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

If you're not chasing extreme levels of performance (as the MongoDB developers who designed BSON are), then I would advise using JSON -- the human-readability is a great benefit to the developer. As long as you use a library like Jackson, migrating to BSON later should not be hard -- as you can see by how almost identical your own BSON and JSON classes are.

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

Bear in mind that if size is an issue, both JSON and BSON should compress well.

这篇关于哪一个更轻,JSON还是BSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆