-
Notifications
You must be signed in to change notification settings - Fork 405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support for nested items in @JsonKey(name:) #490
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 @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. WIP, not ready for production |
Necessary function ! |
I really need this feature! |
I forked @NicolaChimerapps fork and corrected two small errors: |
@masewo and @NicolaChimerapps – I'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 – beyond 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;
}
} |
Allows specifying an alternative constructor to invoke when creating a `fromJson` helper. Closes #490
Allows specifying an alternative constructor to invoke when creating a `fromJson` helper. Closes #490
How does 368a8c7 fix this? |
gah! Wrong issue! |
Any news on this? It's marked as help wanted and there's an open PR related to it :/ |
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 |
How about something like @JsonSerialiable(explicitJson: true, keySeparator: ">")
class Person {
@JsonKey(name: 'name>fist_name')
String name;
}
|
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 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. |
In my case I didn't need the @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;
} |
I am also having this exact same problem with the explicit json converter |
Chiming in to show support/need for this functionality. |
Up |
I'm use next solution @JsonKey(readValue: nestedReader, name: 'extras/apiKey') List<String>? extras, where 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
update: class NestedJsonKey extends JsonKey {
const NestedJsonKey({
required super.name,
}) : super(readValue: nestedReader);
}
and use it as @NestedJsonKey(name: 'lastLocation/lng') double? test, |
There is no solution to this problem in 2024 |
@phamquoctrongnta check 2dfffd0. |
It would be great if we could directly access nested items in the
name
String of@JsonKey
. Consider this Json:I would like to do:
Sorry if this is already possible, but i could not find anything.
The text was updated successfully, but these errors were encountered: