Skip to content

support for nested items in @JsonKey(name:) #490

@KorbinianMossandl

Description

@KorbinianMossandl

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.

Activity

kevmoo

kevmoo commented on May 24, 2019

@kevmoo
Collaborator

Interesting idea. It is currently not supported.

vanlooverenkoen

vanlooverenkoen commented on Jun 20, 2019

@vanlooverenkoen
Contributor

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

jayden320

jayden320 commented on Aug 12, 2019

@jayden320

I really need this feature. Is it supported now?

vanlooverenkoen

vanlooverenkoen commented on Oct 23, 2019

@vanlooverenkoen
Contributor

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

k-paxian

k-paxian commented on Dec 21, 2019

@k-paxian

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

vanlooverenkoen

vanlooverenkoen commented on Dec 23, 2019

@vanlooverenkoen
Contributor

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

kevmoo

kevmoo commented on Dec 23, 2019

@kevmoo
Collaborator

PRs welcome here!

NicolaVerbeeck

NicolaVerbeeck commented on Dec 24, 2019

@NicolaVerbeeck

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

mokai

mokai commented on Jan 3, 2020

@mokai

Necessary function !

taoyimin

taoyimin commented on Jan 3, 2020

@taoyimin

I really need this feature!

masewo

masewo commented on Mar 28, 2020

@masewo

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.

kevmoo

kevmoo commented on Mar 28, 2020

@kevmoo
Collaborator

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

16 remaining items

kevmoo

kevmoo commented on Dec 28, 2021

@kevmoo
Collaborator

There is NOT an open PR on this. That was a mistake.

The issue here: we need some other syntax for key that's not a String because any String can be a valid key in a JSON map. So we'd need to support List<String> for key – or add another value. Both of which are quite a bit of work!

ad-on-is

ad-on-is commented on Jan 29, 2022

@ad-on-is

There is NOT an open PR on this. That was a mistake.

The issue here: we need some other syntax for key that's not a String because any String can be a valid key in a JSON map. So we'd need to support List<String> for key – or add another value. Both of which are quite a bit of work!

How about something like

@JsonSerialiable(explicitJson: true, keySeparator: ">")


class Person {
@JsonKey(name: 'name>fist_name')
String name;
}
kevmoo

kevmoo commented on Feb 3, 2022

@kevmoo
Collaborator

After consideration, I've decided not to invest time here. It adds a LOT of complexity.

If you still want this feature, it's pretty easy to implement with a JsonConverter

See https://github.com/google/json_serializable.dart/blob/master/example/lib/nested_values_example.dart

qevka

qevka commented on Feb 7, 2022

@qevka

After consideration, I've decided not to invest time here. It adds a LOT of complexity.

If you still want this feature, it's pretty easy to implement with a JsonConverter

See https://github.com/google/json_serializable.dart/blob/master/example/lib/nested_values_example.dart

That would be nice if JsonConvert was actually working. I am having the same problem that is mentioned here: #1066

I tried downgrading my build_runner and json_annotation packages and also tried just using your code directly neither worked.

Alameen688

Alameen688 commented on Mar 21, 2022

@Alameen688

In my case I didn't need the toJson data to transform data back to the original structure I just needed to read the correct data and readValue worked perfectly fine in combination with deep_pick.

@JsonKey(readValue: readNestedItems)
List<NestedItem> nestedItems;
  static List<String, dynamic> readNestedItems(Map json, String key) {
    final fields = pick(json, key, 'items').asListOrEmpty(_yourFromPickFunction);
    return fields;
  }
Veeksi

Veeksi commented on Apr 14, 2022

@Veeksi

After consideration, I've decided not to invest time here. It adds a LOT of complexity.
If you still want this feature, it's pretty easy to implement with a JsonConverter
See https://github.com/google/json_serializable.dart/blob/master/example/lib/nested_values_example.dart

That would be nice if JsonConvert was actually working. I am having the same problem that is mentioned here: #1066

I tried downgrading my build_runner and json_annotation packages and also tried just using your code directly neither worked.

I am also having this exact same problem with the explicit json converter

JPaulMora

JPaulMora commented on Dec 16, 2022

@JPaulMora

Chiming in to show support/need for this functionality.

rignaneseleo

rignaneseleo commented on Nov 15, 2023

@rignaneseleo

Up

EvGeniyLell

EvGeniyLell commented on Dec 21, 2023

@EvGeniyLell

I'm use next solution

@JsonKey(readValue: nestedReader, name: 'extras/apiKey') List<String>? extras,

where nestedReader is

Object? nestedReader(Map json, String key) {
  final keys = key.split('/');
  return _nestedReader(json, keys);
}

Object? _nestedReader(final Object? object, Iterable<String> keys) {
  if (keys.isEmpty || object == null) {
    return object;
  }
  if (object is Map) {
    final subObject = object[keys.first];
    final subKeys = keys.skip(1);
    return _nestedReader(subObject, subKeys);
  }
  if (object is List) {
    return object.fold<dynamic>([], (list, subObject) {
      return list..add(_nestedReader(subObject, keys));
    });
  }
  return object;
}

for this test

void main() {
  final Map map = {
    'extras': [
      {'apiKey': 1, 'name': 'a'},
      {'apiKey': 3, 'name': 'b'},
      {'apiKey': 5, 'name': 'c'},
      {'apiKey': 7, 'name': 'd'},
    ]
  };

  final result = nestedReader(map, 'extra/apiKey');
  print('result $result');
}

result will be

result (1, 3, 5, 7)


update:
also we can declare

class NestedJsonKey extends JsonKey {
  const NestedJsonKey({
    required super.name,
  }) : super(readValue: nestedReader);
}

and use it as

@NestedJsonKey(name: 'lastLocation/lng') double? test,
devnta

devnta commented on Mar 11, 2024

@devnta

There is no solution to this problem in 2024

creativecreatorormaybenot

creativecreatorormaybenot commented on Mar 11, 2024

@creativecreatorormaybenot

@phamquoctrongnta check 2dfffd0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @kevmoo@EvGeniyLell@mokai@jayden320@masewo

        Issue actions

          support for nested items in @JsonKey(name:) · Issue #490 · google/json_serializable.dart