Data serialization formats available in Swift

alicanbatur
NSIstanbul
Published in
3 min readOct 10, 2017

Hi all,

In this article, I want to show you there are many alternative ways of JSON serialization. They all have pros and cons but at least you should know a little about them.

What is serialization?

“In computer science, in the context of data storage, serialization is the process of translating data structures or object state into a format that can be stored or transmitted and reconstructed later.” There is also a deserialization concept which is reversing serialized data to our custom objects. I already wrote an article about it.

Where do we use serialization?

  • Transferring data over the wire.
  • Storing data.
  • Remote procedure calls. (RPC)

JSON, XML, protobuf, YAML, BSON, MessagePack are examples for data serialization. I guess most known ones are JSON and XML in mobile application development. Most preferred using JSON than XML because JSON is more readable with less size.

JSON (JavaScript Object Notation): Its being readable for human and easy to parse and generate by machines is the reason why it is choosen most. But in my opinion, readable response formats is very important to check data types and that makes JSON one step closer. Swift has got lots of 3rd party libraries for serializing&deserializing JSON. You can check them here. And also in my previous article you can see some of examples of them.

// Simple JSON format example
{
"name": "Ali Can"
"surname": "Batur"
"age": 29
}

XML (eXtensible Markup Language): Defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. XML’s being illegible and complex, causes it less preferred. Also parsing it needs more effort than JSON and some others. You can find 3rd party parsers here. Also there is SDK (foundation) support for this here.

<person>
<name>Ali Can</name>
<surname>Batur</surname>
<age>29</age>
</person>

protobuf (Google’s Protocol Buffers): “Protocol buffers are a flexible, efficient, automated mechanism for serializing structured data. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages. You can even update your data structure without breaking deployed programs that are compiled against the “old” format.” says Google. I think in the future, we will not need to write response model objects. One file for both API and client would be enough. Also  itself supports this technology here. I want to write a blog post just about protobufs. Let me hear your opinions about protobuf, pls :)

// Example content for an .proto file.
message Person {
int32 age = 1;
string name = 2;
string surname = 3;
}

YAML (YAML Ain’t Markup Language) : YAML is a readable data serialization standard for all programming languages. No need for brackets nor quotes. Syntax supports relational data. Especially used for viewing / editing of data structures: such as configuration files, dumping during debugging. Here a YAML framework is.

- name: Ali Can
surname: Batur
age: 29
devices:
- iPhone 7
- Apple Watch Series 2
- Macbook Pro

BSON (Binary JSON) : It’s a bin­ary-en­coded seri­al­iz­a­tion of JSON-like doc­u­ments. It also con­tains ex­ten­sions that al­low rep­res­ent­a­tion of data types that are not part of the JSON spec. JSON is a plain text format, and while binary data can be encoded in text, this has certain limitations and can make JSON files very big. BSON comes in to deal with these problems. You can find a library here.

MessagePack: It’s like JSON but fast and small. It’s another binary format serialization. It’s smaller than BSON, supports type-checking(static type). You can find a lib here.

Conclusion

In real life examples, JSON is the most choosen one for mobile app development to handle request-response data transfers. I also use JSON for all my projects, because it is being readable speeds up things I do. Other serializing formats are useful where they fits most. As an example, use YAML for your configuration file types. Althought JSON is very handy, I wonder protobuf most. I guess, it will give client-server architecture a new perspective. Btw, protobuf is not new, but I think it will be popular day-by-day.

Thanks for reading, feel free to comment.

--

--