Adopt Protocol Buffers instead of JSON
Protocol Buffers are a data interchange format used in Google. (also developed by Google)
It looks like as follows.
package main;
message Book {
required string title = 1;
required int32 page_number = 2;
required string author_name = 3;
}
Protocol buffers encoded data are binary. They are encoded really efficiently.
You can compare them with JSON. Protocol buffers encoded messages using .proto
above can be represented in JSON as follows.
{
"title": "SomeTitle",
"page_count": 12,
"author_name": "SomeAuthor"
}
What is different between Protocol Buffers and JSON is serializing and deserializing. Because protocol buffers specifies its structure in .proto
(You saw above), Protocol Buffers serialized data can be deserialized easily as follows. (Assume that bookData
is protobuf serialized data.)
Book *book = [Book parseFromData:bookData]
The structure is described in .proto
and you can share it in the backend and the clients. The model definition is kept DRY.
And it also has redundancy about data structure because it specifies properties with id. It does not depends on property name.
So you can change any property name in production. (I changed page_number
to page_count
)
package main;
message Book {
required string title = 1;
required int32 page_count = 2;
required string author_name = 3;
}
Even you change some property name, clients can receive and parse messages properly. Clients just recognize page_count
as page_number
. Of course it is not necessary to rewrite some codes.