0 x1 preface

As a new iOS developer with Flutter, this part of the JSON transformation model has always been a headache for me. There are generally two kinds of schemes that can be found on the Internet

0x11 Create a model file by parsing JSON

The reflection is disabled in flutter, so we cannot dynamically assign values to the model. We need to create fromJson method after fromJson method, and then take data from that method and assign it to the model. This is a piece of repetitive and very boring code. This method also has a series of problems in the project:

  • It does not conform to our normal development habits. Generally, what we get is the interface field definition, and we do not have the corresponding JSON file, so it is unnecessary to add some work to build a JSON file.
  • There is no way to do some fault tolerance with the data, and when you do the old business migration, this aspect of the problem will be exposed, and it will make people crazy.
  • It is difficult to add and delete fields later

Tools to address

0x12 json_serializable

Json_serializable uses annotations in dart to automatically generate code. You can think of annotations in the same way as compiling a plugin, parsing the fields defined in the model during precompilation and then creating the corresponding fromJson methods based on those fields. This approach is similar to the above solution in some ways, but it solves some of the problems mentioned above to a certain extent:

  • It is more in line with our development habits to define properties according to interface fields.
  • The script generates the code to make it easier to add and remove fields.

Remaining problems:

  • You still can’t do fault tolerance on the data.
  • There is still some repetitive code that needs to be added manually, as shown in the red box below.

0x2 a more convenient solution

Let’s first comb the iOS YYModel framework is how to do JSON automatic transfer model. The first step is to parse all the property names and types defined by Runtime. Then use these property names to assign the values in json to the model. So if we want to build a similar set of libraries we need the following capabilities

0x21 Resolve all attribute names and attribute types in the model

The Flutter disables reflections in the Dart, so it’s not realistic to expect the API of the system. It’s easy to imagine that we could manually output this into a global map. To use it, go to the map using the className. Manual is definitely not realistic, this part of the data is regular, so it is very appropriate to do through the script. If you’ve studied the implementation of Json_serializable, you know the annotations. We can use annotations or custom Builders to perform a precompilation, where we can parse the class and get parameter names and parameter types. After parsing this data, create a file and output a global map of the parsed data in a specific format to the file you created. I will not talk about the specific implementation, only talk about the scheme. For details on the use of annotations, see the annotation-related materials

0x22 Dynamically Created object

Through the above steps we can resolve the type of the attribute, attribute name. For custom objects, there is a need to create corresponding objects based on the class name. Create the object with the closure, use the className as the key and the closure as the value, and then put the data into the map resolved in the previous step.

className : () => className()
Copy the code

0x23 Dynamic assignment

After creating an object dynamically, we need to be able to dynamically assign values to the properties of that object. Similar to the way we created objects earlier, we can do this with closures, and we output the following format into the map above

name : (dynamic instance, value) => instance.name = value
Copy the code

0x24 Dynamic value

In the model to JSON function, we need to be able to dynamically retrieve the data in the object with a String name. Similar to what we did with closures above. We need to output the following format to the previous map

name : (dynamic instance) => instaned.name
Copy the code

0x25 General flow chart

0x3 hn_easy_model

I’ve implemented a framework here that, once configured, is extremely inexpensive to use and friendlier to clients who turn to writing their flutter. Frame address, welcome to try.