Why should I use a human readable file format?

后端 未结 24 1953
日久生厌
日久生厌 2020-12-04 17:53

Why should I use a human readable file format in preference to a binary one? Is there ever a situation when this isn\'t the case?

EDIT: I did have this as an explana

24条回答
  •  臣服心动
    2020-12-04 18:14

    It entirely depends on the situation.

    Benefits of a human readable format:

    • You can read it in its "native" format
    • You can write it yourself, e.g. for unit tests - or even for real content, depending on what it's for

    Probable benefits of a binary format:

    • Easier to parse (in terms of code)
    • Faster to parse
    • More efficient in terms of space
    • Easier to control (any time you need text in there, you can ensure it's UTF-8 encoded, and length prefixed etc)
    • Easier to include opaque binary data efficiently (images, etc - with a text format you'd be getting into base64)

    Don't forget that you can always implement a binary format but produce tools to convert to/from a human-readable format as well. That's what the Protocol Buffers framework does - it's actually pretty rare IME to need to parse a text version of a protocol buffer, but it's really handy to be able to write it out as text.

    EDIT: Just in case this ends up being an accepted answer, you should also bear in mind the point made by starblue: Human readable forms are much better for diffing. I suspect it would be feasible to design a binary format which is appropriate for diffing (and where a human-readable diff could be generated) but out-of-the-box support from existing diff tools will be better for text.

提交回复
热议问题