skip to Main Content

I need to serialize and deserialize a really huge object. This is an object that has many fields which themselves are complex objects with many fields that are complex objects etc.
I know I could write fromJson and toJson but there are really a lot of these objects and that would take at least two weeks to write all the jsons.
I’d like to ask if there’s maybe an easier way to do it? In the end I need an object that can be recreated.
For example, let’s say I need to serialize object MAN.
It has many objects, like leg, hand, liver, lungs, heart etc. Every of these objects is constructed of many other objects like veins, muscles etc. and these objects consist of smaller objects like cells etc.
Is it possible somehow to iterate through all subobjects and get their fields with values? Something like reflection?
I mead something like the debugger does that shows all fields with their type and value.

2

Answers


  1. you could look this way freezed, yet another code generator for data-classes/unions/pattern-matching/cloning. it will take care of your complex models, nesting of models, also add equals and hashcode to these classes. in addition, you could use service like this quicktype to speed up converting yours jsons to freezed models.

    example here The Best Way generates models in Flutter | freezed

    Login or Signup to reply.
  2. you can use json_serializable to do the work.

    The tool automatically generates toJSON and fromJSON methods according to the config of the class.

    Whether the class is huge or small, the serialize and deserialize are working in the same way.

    By the way, If you are using VSCode, you can create a Snippet to do something that is repeated like create the model class.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search