Closed KorbinianMossandl closed 2 years ago
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 – 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;
}
}
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 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!
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 anyString
can be a valid key in a JSON map. So we'd need to supportList<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;
}
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
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: https://github.com/google/json_serializable.dart/issues/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 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;
}
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.dartThat 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
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 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,
There is no solution to this problem in 2024
@phamquoctrongnta check https://github.com/google/json_serializable.dart/commit/2dfffd0df2abc4592b37e75de2ee75932711c503.
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.