0

As the title, what kind of format will people use to encode body message for http2.0? I understand http2.0 has improved a lot on efficiency by binary encoding message header. How about message body such as application/json? Will it also become binary, e.g true or false value will only take one bit? number 200 will only take one byte instead of three ASCII chars?

I honestly don't know why we design JSON as textual format in the first place? Isn't binary format more efficient on storing number and boolean? If we use textual format, why we still say json has diverse data type: number, string, bool, array? What is the point of these data types since they are all just string and ascii encoded.

------------------------------------update-------------------------

After I successfully figure out how to create http2 server and how to use wireshark to decrypt the message. I realize the json is still encode using ascii not binary: How to use wireshark to view encrypted application data between curl client and http2 server using PKCS12 key/cert enter image description here 200 is still three bytes even using http2

Stan
  • 602
  • 6
  • 23

1 Answers1

0

The HTTP/2 protocol is opaque with respect to application data, meaning that whatever format the application decides to send, HTTP/2 will send.

For JSON, if the application converts a JSON string to UTF-8 bytes, those are the bytes that will be sent, HTTP/2 does not do any further encoding. In your example, in this case, true will occupy 4 bytes, 200 3 bytes, etc.

If your application gzips the JSON bytes and sends the gzipped bytes, HTTP/2 will send the gzipped bytes.

The question about why the JSON design was textual rather than binary is a much larger question that goes back to historical reasons, simplicity and human readability, but primarily to the fact that a JSON string can be fed back to the JavaScript interpreter to get back a JavaScript object.

There are binary JSON-like formats now (e.g. CBOR, but also many others) that have good support in any language.

sbordet
  • 16,856
  • 1
  • 50
  • 45
  • So can I understand that data type such as number, array, string is just in a semantic format. String is is the bytes stream with staring quotes and ending quotes. Number is digit bytes without quotes. Array is something with brackets and quote, etc ? – Stan Aug 18 '22 at 08:05
  • ->The HTTP/2 protocol is opaque with respect to application data Does this mean regarding application data, http2 and http1.1 are almost same in terms of efficiency since both can choose what format to send in: gzip, binary, ascii/text? – Stan Aug 18 '22 at 08:08
  • JSON is specified here: https://json.org. Regarding HTTP/1.1 vs HTTP/2 efficiency, see [this answer](https://stackoverflow.com/questions/44019565/http-2-file-download). – sbordet Aug 18 '22 at 12:12