Json_serializable.dart: Serialization of nested serializable objects

Created on 7 Oct 2017  路  3Comments  路  Source: google/json_serializable.dart

Looks like serializing of annotated object with nested annotated objects via toJson() are not supported or I am completely don't understand how to do it.

Calling method toJson() even for your Person object (which is in your example along with nested Order and Item, code unchanged) fails to include serialized output of nested objects into final output.

Order order1 = new Order()
  ..count = 2
  ..itemNumber = 1
  ..isRushed = true
  ..item = new Item()
  ..itemNumber = 234
  ..isRushed=false;

Person person = new Person('Alexey', 'Kartavenko', new DateTime.now(), orders: <Order>[order1]);
print(person.toJson());

Result:

{firstName: Alexey, lastName: Kartavenko, date-of-birth: 2017-10-07T15:15:11.616592, last-order: null, orders: [Instance of 'Order']}

Looking into generated model code I don't see any attempts to call toJson() method of Order object. Is this by design or am I missing something obvious here?

Most helpful comment

Ah, I see. I need not to call person.toJson() directly but pass person to JSON.encode().

All 3 comments

Ah, I see. I need not to call person.toJson() directly but pass person to JSON.encode().

FWIW - I did the same thing and spent some time spinning on this. Might be good to show a little more usage in the readme to prevent others from the same issue.

Ah, I see. I need not to call person.toJson() directly but pass person to JSON.encode().

.... This is so stupid.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bsutton picture bsutton  路  6Comments

fengqiangboy picture fengqiangboy  路  5Comments

fzyzcjy picture fzyzcjy  路  5Comments

enyo picture enyo  路  5Comments

AlexBigCheese picture AlexBigCheese  路  3Comments