K
K
komarevtsev2016-04-30 09:46:05
Node.js
komarevtsev, 2016-04-30 09:46:05

Why feed data in json?

All textbooks write that data is usually transmitted in JSON format. I don't quite understand why it needs to be done this way?
Let's say I can transfer a javascript object without a json wrapper from a node js server via a socket connection.
Is it permissible to do this?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexey Ukolov, 2016-04-30
@alexey-m-ukolov

Let's say I can transfer a javascript object from a node js server through a socket connection without a wrapper in json
You can't, websockets are a text protocol, serialization is required to transfer objects, and json is ideal for this.

S
Sergey, 2016-04-30
@Sin_raben

documentation
void send(
in DOMString data
);
void send(
in ArrayBuffer data
);
void send(
in Blob data
);

V
VZVZ, 2016-04-30
@VZVZ

JSON does not always need to be done, but either in cases where it is required that not only the browser, but also mobile clients or some other ones work with the API (which do not need ready-made data in HTML, but need JSON for parsing), or when it is better that the client be fat. Also, JSON obviously makes debugging a little easier.
In general, ideally, the back-end of the site should be able to return both HTML for the browser (which JS does not need to parse => performance on the client), and JSON for other purposes, and maybe something else.
This is how it is done in VK, for example.
I'm talking about HTTP. If WebSockets encodes all fields in JSON, it's okay, because such a feint with ears is possible:
And you can also do this, elegantly combining the advantages of HTML and some of the advantages of JSON:
I saw something similar in Google services. An interesting decision, and definitely has the right to life.

S
Sergey, 2016-04-30
Protko @Fesor

json is a convenient data serialization format. The possibility of serialization / deserialization in json is available in literally all programming languages, which makes this format quite universal.
If you don't use it, you will have to implement something else. These can be other serialization formats, such as messagepack, or if you want, pack the data yourself as you want, depending on the task. But in this case, you will have to implement the serialization yourself, and this is the time. And time is money. Therefore, it should pay off (for example, in order to increase throughput, use your binary serialization format developed exclusively for the task, although this is sooooo rarely needed and I doubt that you will need it for the next 5-10 years).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question