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

support for nested items in @JsonKey(name:)

Open KorbinianMossandl opened this issue 7 years ago • 31 comments

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.

KorbinianMossandl avatar May 22 '19 12:05 KorbinianMossandl

Interesting idea. It is currently not supported.

kevmoo avatar May 24 '19 16:05 kevmoo

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

vanlooverenkoen avatar Jun 20 '19 12:06 vanlooverenkoen

I really need this feature. Is it supported now?

jayden320 avatar Aug 12 '19 11:08 jayden320

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

vanlooverenkoen avatar Oct 23 '19 16:10 vanlooverenkoen

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

k-paxian avatar Dec 21 '19 00:12 k-paxian

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

vanlooverenkoen avatar Dec 23 '19 06:12 vanlooverenkoen

PRs welcome here!

kevmoo avatar Dec 23 '19 21:12 kevmoo

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

NicolaVerbeeck avatar Dec 24 '19 14:12 NicolaVerbeeck

Necessary function !

mokai avatar Jan 03 '20 03:01 mokai

I really need this feature!

taoyimin avatar Jan 03 '20 03:01 taoyimin

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 avatar Mar 28 '20 15:03 masewo

@masewo and @NicolaChimerapps – I'll happily take a solid PR with testing, etc

kevmoo avatar Mar 28 '20 17:03 kevmoo

It's wishful for me.

ouyanghuacom avatar Apr 02 '20 09:04 ouyanghuacom

Any update on this ?

jeffaknine avatar Sep 22 '20 13:09 jeffaknine

Bumping as this is a must have.

miguelpruivo avatar Oct 23 '20 19:10 miguelpruivo

Pull requests welcome – beyond the core features offered now, this package is a "beyond work hours" project.

kevmoo avatar Oct 23 '20 19:10 kevmoo

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;
  }
}

hacker1024 avatar Jan 01 '21 05:01 hacker1024

How does 368a8c7 fix this?

NicolaVerbeeck avatar Sep 24 '21 09:09 NicolaVerbeeck

gah! Wrong issue!

kevmoo avatar Sep 24 '21 16:09 kevmoo

Any news on this? It's marked as help wanted and there's an open PR related to it :/

ciriousjoker avatar Dec 13 '21 11:12 ciriousjoker