Write your own library to convert json to object and vice versa in Flutter.

Tram Ho

Through many months of sharpening the history of the Flutter war. I myself am fed up with having to decode from json to object every time the api request comes in. Everyone was confused, confused, desperate, so I came to a decision that is, to try to write something zui zui during the end task. With a lazy nature, detesting boring jobs, so it’s okay, drag me to decode json to treat it beautifully so that the next day I will sweat less.

Question

Nowadays, you know, it’s hard to find an application that only runs locally without communicating with the server. So whether you like it or not, JSON is still something you encounter as usual. Basically, we have two ways to convert a JSON to Object:

  1. Manual serialization (Convert manually) aka running on rice.
  2. Automated serialization (Convert automatically) Depending on the complexity of the data to choose the appropriate implementation, but there is a pain called optimize size in Flutter brings. Unlike Java, Swift can read property names in Object, Flutter has disabled that feature, making it difficult for us to implement libraries like Gson….

Although we cannot use runtim reflection with Flutter, some libraries provide them with similar easy-to-use APIs but are instead based on code generation. That is generating code. Hearing 2 words generate code hurts my heart. The idea behind this gen code is to read your declared class file, filter out the properties in it, and implement another class that implements the decode and encode json functions. Finally create a reference from the original class to the generated class.

It sounds reasonable, I like it, but I’m too lazy to type commands. I still want it to do like Gson, Codable.

Idea

If you think about it carefully, manual decoding takes a lot of time and effort, decoding with gencode is a bit lazy, and I don’t like it very much. Well, in the lazy it has an idea. A breeze passed, suddenly thought. Well, now that I can’t read the properties, I just need to write it once, it’s better than converting it manually. Although not as smart as Gson or Codable, it still saves some effort. Json actually has 2 types: Array, or Object Array, I will discuss later, but with Object, it is essentially data in the form: Key => Value

Key is always of type String, now my job is to determine how many of its Value types to convert to the correct data type. Value in JSON is one primary type: Number or String The second one is Array: This array can be an array of Primary Type or an array of other Json Objects, and can also be a mix of the two. (The type of mix is too difficult, processed later, probably rarely encountered)

Then based on the above analysis we can deploy the idea into code as follows:

1. Create an abstract class to convert key => value

This abstract task defines two properties: String key, and T value (T is a generic type, T can be null) Next is to define a function fromJson() which takes a dynamic value and returns a dynamic value.

2. Create a Codable class to convert Primary Type

Simply re-implement the fromJson method and cast the data type back to the desired type.

3. Create a CodableObject class to convert Object Type in Json

This is to convert a Json Object to an Object Dart.

4. CodableList class used to convert an Array

This class simply decodes the objects in the json array

5. Create data types for ease of use

Don’t laugh, I have a weird name

6. Create Encode and Decode Classes

Decode has the task of converting Json to Object in Flutter Encode has the opposite task of converting Object to Json. Note: the fields you want to decode, or encode must be declared in the properties functions.

Specific examples

1. Elementary level difficulty test

Declare a User class with the following properties: fullname, age, address

You can see the code is extremely neat and clean, unlike the rice-based guy. Result:

2. Increase the difficulty of the game

I will proceed to convert a more complex Object as follows:

  1. Dog: ps:// Not doggy

  1. Cat

  1. ColorCat

  1. ListDog

  1. Run the program

I’ll let you guys try it out and see if you get the results you want.

Summary

This article is for the purpose of sharing knowledge for fun, if you find it interesting, you can support me. Otherwise, don’t throw stones. I’m not an expert, if there’s anything I’m missing, you can comment below, I’ll read and listen to your opinions.

Thank you for taking the time to read this far, thank you and best regards.

Share the news now