Json_serializable.dart: support for nested items in @JsonKey(name:)

Created on 22 May 2019  路  17Comments  路  Source: google/json_serializable.dart

It would be great if we could directly access nested items in the name String of @JsonKey. Consider this Json:

"root_item": {
    "items": [
        {
            "name": "first nested item"
        },
        {
            "name": "second nested item"
        }
    ]
}

I would like to do:

@JsonKey(name: "root_item/items")
List<NestedItem> nestedItems;

Sorry if this is already possible, but i could not find anything.

P2 medium help wanted enhancement

Most helpful comment

Pull requests welcome 鈥撀燽eyond the core features offered now, this package is a "beyond work hours" project.

All 17 comments

Interesting idea. It is currently not supported.

This should really be added. Now we have to create extra objects because we had to use a crappy backend. Using a simple api to flatten your backend models

I really need this feature. Is it supported now?

Is there a roadmap for this feature? What timeframe could we expect for a P2?

If you guys are still waiting for that feature, this library already has it implemented
https://github.com/k-paxian/dart-json-mapper#nesting-configuration

@KorbinianMossandl thank you for a great idea! This thing is called rfc6901

Thanks. but I need to use json_serializable. Because I am using retrofit.dart that uses toJson and fromJson.

PRs welcome here!

I am working on an implementation for this.
Rough draft can be found here: https://github.com/Chimerapps/json_serializable/tree/feature/json_path

WIP, not ready for production

Necessary function !

I really need this feature!

I forked @NicolaChimerapps fork and corrected two small errors:
https://github.com/masewo/json_serializable/tree/feature/json_path
For now it is working for me.

@masewo and @NicolaChimerapps 鈥撀營'll happily take a solid PR with testing, etc

It's wishful for me.

Any update on this ?

Bumping as this is a must have.

Pull requests welcome 鈥撀燽eyond the core features offered now, this package is a "beyond work hours" project.

If #783 gets merged, this will be possible, albeit in a convoluted manner:

@JsonSerializable()
class MyObject {
  @JsonKey(name: 'myRegularField')
  final String myRegularField;

  @JsonKey(extra: true)
  final String myNestedField;

  MyObject({
    required this.myRegularField,
    required this.myNestedField,
  });

  factory MyObject.fromJson(Map<String, dynamic> json) {
    return _$MyObjectFromJson(
      json,
      myNestedField: json['myInnerObject']['myNestedField'] as String,
    );
  }

  Map<String, dynamic> toJson() {
    final output = _$MyObjectToJson(this);
    output['myInnerObject'] = {'myNestedField': myNestedField};
    return output;
  }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

AlexBigCheese picture AlexBigCheese  路  3Comments

HosseinArabbeigi picture HosseinArabbeigi  路  3Comments

volskaya picture volskaya  路  4Comments

fzyzcjy picture fzyzcjy  路  5Comments

enyo picture enyo  路  5Comments