json_serializable.dart icon indicating copy to clipboard operation
json_serializable.dart copied to clipboard

Serialize without key

Open sulthanalihsan opened this issue 4 years ago • 2 comments

Hi, i just wanna ask something like, sometime our backend dev return the response like this

[ { "data" : "data1"}, { "data" : "data2"}, { "data" : "data3"} ]

how to handle that with JsonSerializable, a list data without key, thankyou

sulthanalihsan avatar Dec 06 '21 04:12 sulthanalihsan

It could be work well with custom wrapper

class ListWrap {
    List<dynamic> list
    
    factory ListWrap.fromJson(List<dynamic> json) => _$ListWrapToJson({"list": json});

    List<dynamic> toJson() => _$ListWrapToJson(this)["list"]
}

But will not work with freezed https://github.com/rrousselGit/freezed/issues/451

may consider add a pair option fromJson, toJson to @JsonSerializable or even just @JsonKey(inline:true)

morlay avatar Dec 09 '21 13:12 morlay

Are there any updates to this problem?

Can't find any mention in the internet about serializing JSON with root list structure.

or even just @JsonKey(inline:true)

This looks great for me.

eshfield avatar Dec 11 '23 12:12 eshfield