gibahjoe / openapi-generator-dart

Openapi Generator for Dart/Flutter
BSD 3-Clause "New" or "Revised" License
128 stars 34 forks source link

Compatible with json_serializable #52

Closed flodaniel closed 3 years ago

flodaniel commented 3 years ago

I am trying to use this package together with the common json_serializable and json_annotation packages (https://pub.dev/packages/json_serializable, https://pub.dev/packages/json_annotation).

However, I am unable to find a way how to the implement a toJSON and fromJSON for the classes generated by openapi-generator-dart. There seems to be an internal implementation, but I cannot access it.

Is there a way to make the packages work together? Otherwise I would have to move away from this package, as I need to store the objects as a JSON. I am using the Generator.dio.

Thanks!

gibahjoe commented 3 years ago

Hi, the duo generators make use of BuiltValue. So, what you are interested in is how to generate json from built value.

You can use some thing like this var

contact1 = Contact((b) => b
..id = 1
..fullName = "Stack Secrets");

print(serializers.serialize(contact1));

You can read more about it here https://stacksecrets.com/flutter/json-serialization-in-dart-flutter-with-built_value

Note that you do not need to build your own serialisers as the generated sdk already comes with it.

flodaniel commented 3 years ago

Thanks! I was looking for that! :)