Comments (9)
We recently added fromJson
, toJson
properties to JsonKey
that let you provide methods to handle conversion yourself. You could detect if the value is a list or a single value and populate the property accordingly.
from json_serializable.dart.
Thanks, I appreciate the clarification. I know this isn't a help forum, but for the life of me I can't figure out what syntax I need to use with the fromJson and there isn't really any documentation for those functions. You mind helping me understand how I can use fromJson to handle either a list or a single item in the following case?
@JsonKey(name: 'book',)
final List<Book> books;
BooksContainer(
this.books,
);
from json_serializable.dart.
Something like
List<Book> _decodeBooks(Object input) {
// your code here
}
class MyClass {
@JsonKey(name: 'book', fromJson: _decodeBooks)
final List<Book> books;
}
from json_serializable.dart.
from json_serializable.dart.
from json_serializable.dart.
I don't want to hastily open another issue. Yet after implementing fromJSON, debugging and running from Android Studio works fine, but release builds give a type mismatch error in my fromJSON method.
06-16 10:23:59.541 6695 6712 E flutter : [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
06-16 10:23:59.541 6695 6712 E flutter : type '_OneByteString' is not a subtype of type 'int' of 'index'
06-16 10:23:59.541 6695 6712 E flutter : #0 _GrowableList.[] (dart:core/runtime/libgrowable_array.dart:0)
Is there a problem in my code or is this an actual issue?
The offending method is below and here is the repository: https://github.com/fatherchill/world_liturgy_app/tree/CalendarAndCollects
List<dynamic> _decodePrayerBookorService(itemOrList){
// debugPrint(itemOrList);
print('begin decode Prayerbook or Service');
List<dynamic> list = [];
if (itemOrList == null){
return null;
}
if (itemOrList.runtimeType.toString() == 'List<dynamic>'){
list = itemOrList;
}else{
list.add(itemOrList);
}
print(list.toString().substring(0,20));
if (list.first['service'] != null){
return list.map((e) =>
e == null ? null : new PrayerBook.fromJson(e as Map<String, dynamic>))
?.toList();
}else if (list.first['section'] != null){
return list.map((e) =>
e == null ? null : new Service.fromJson(e as Map<String, dynamic>))
?.toList();
}else{
return null;
}
}
from json_serializable.dart.
List<dynamic> list = [];
if (itemOrList == null){
return null;
}
if (itemOrList.runtimeType.toString() == 'List<dynamic>'){
list = itemOrList;
}else{
list.add(itemOrList);
}
Don't do that!
if (itemOrList == null){
return null;
}
List list;
if (itemOrList.runtimeType.toString() == 'List<dynamic>'){
list = itemOrList;
}else{
list = [itemOrList];
}
Not sure if that'll help...but it's better...
from json_serializable.dart.
from json_serializable.dart.
For anyone else coming to this issue due to: More than one field has the JSON key for name <name>
, you might be in this situation
class MyClass
{
SomeType A;
@JsonKey(name: 'A')
SomeType B;
}
It is possible if you are trying to rename a field on deserialization. You can solve it like this:
class MyClass
{
@JsonKey(ignore: true)
SomeType A;
@JsonKey(name: 'A')
SomeType B;
}
from json_serializable.dart.
Related Issues (20)
- Conflict between json_serializable and auto_route during build_runner execution HOT 1
- Suggestion: Better support for `createPerFieldToJson`
- Use @JsonConverter for map keys.
- Guidance on using `json_serializable` helper utilities
- Use of a generic JsonConverter
- How can I make it ignore "setter"?
- Generate an object to access json keys HOT 2
- More descriptive error messages when deserialising HOT 2
- When generating a List, the list type is invalid HOT 4
- Question: Will this packages support `extension types` like `kotlinx.serialization`?
- Allow extension type as Map keys if it extends one of primitive json types or has toJson() method
- [Enhancement] more options for `FieldRename`
- need fromJson with Type
- `JsonKey.fromJson` seems to be ignored HOT 1
- How to use fromJson and toJson to split it into 2 fields
- Support for nullable types that default to non-null HOT 1
- JsonKey + JsonConverter
- Generics complicate self-referential class structure
- Feature request: FromJson format to local time HOT 2
- Decimal, API and SQLite HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json_serializable.dart.