Comments (10)
Thanks alot, it works 👍
I guess the versioning confused me, because that serilization failed for me the last time 😅
from dart-json-mapper.
I see, I'll take a look deeper then. Unit tests are running against the master branch code (i.e. latest version)
The fix is in 2.2.7+1
, so you should probably make sure this version is used actually
from dart-json-mapper.
I´ve just updated flutter and json_mapper to latest versions. And can confirm that the parsing now works as expected.
So I´ll guess I go with a custom "toBackendString()" solution when I need to parse enum values that goes over network. Unless you have a trick up your sleeve since you would take a deeper look? :)
from dart-json-mapper.
Sorry, I´ll try to clarify :)
This code, that did not work before with version 2.2.7:
@jsonSerializable
enum EDeviceRadioType { loRa, nBIoT, mBus, mobileInternet, wMBus }
@jsonSerializable
@Json(caseStyle: CaseStyle.pascal)
class PascalCaseModel {
String? id;
EDeviceRadioType radio = EDeviceRadioType.loRa;
}
test() async {
String json = '[{ "Id": "6267a8bf0a66709b2c7021bd", "Radio": "WMBus" }]';
List<PascalCaseModel>? target = JsonMapper.deserialize<List<PascalCaseModel>>(json);
}
... now works after updating to version 2.2.9
👍
However, it still does not work if I name the enums as defined in the backend I communicate with: { LoRa, NBIoT, MBus, MobileInternet, WMBus }, and this is expected as you stated earlier.
So my current solution is to first rename my enums which has this problem, in this case the EDeviceRadioType
, to camelCase: { loRa, nBIoT, mBus, mobileInternet, wMBus }
. And when I need to send an enum property to my backend over network, I´ll create a helper function (toBackendString()
for example) in the enum class file that translates 'wMBus
' to 'WMBus
'.
from dart-json-mapper.
Hi @fasterlars ,
I'm seeing you are still referring to an enum declared values in Pascal case instead of being declared using lowerCamelCase case.
@JsonSerializable()
enum EDeviceRadioType { LoRa, NBIoT, MBus, MobileInternet, WMBus }
it should work for this enum instead:
@JsonSerializable()
enum EDeviceRadioType { loRa, nBIoT, mBus, mobileInternet, wMBus }
Please take a look at the unit test here and here
Once again the assumption is that Dart code enum values are following lowerCamelCase style
from dart-json-mapper.
Thanks alot for the attention and your unit testing.
From the unit test, I can confirm that it is possible to convert "WMBus" string into the wMBus enum.
But this is not possible, if I create the same case in my application:
@jsonSerializable
enum EDeviceRadioType { loRa, nBIoT, mBus, mobileInternet, wMBus }
@jsonSerializable
@Json(caseStyle: CaseStyle.pascal)
class PascalCaseModel {
String? id;
EDeviceRadioType radio = EDeviceRadioType.loRa;
}
test() async {
String json = '[{ "Id": "6267a8bf0a66709b2c7021bd", "Radio": "WMBus" }]';
List<PascalCaseModel>? target = JsonMapper.deserialize<List<PascalCaseModel>>(json);
}
the deserialize function throws the _TypeError (type 'Null' is not a subtype of type 'EDeviceRadioType' of 'value')
exception.
I guess this might have something to do with versioning. I am using ^2.2.7
. And I cannot figure out what version you are using in your unit test setup, other than '>=2.1.12 <3.0.0
'. Will it then be 3.0.0
? If yes, versioning might be the cause of my problem?
Anyways. I use alot of different enums that my backend defines. In my app I define them in the same way the backend does to keep consistency, and with the same casing. All of them is in pascal, and some with the uppercasing in the second character the whole problem revolves around.
In the app I use these models, serialize and deserialize them, and send them over network to my backend. And when this happens, I of course need to send "WMBus", and not "wMBus" to the backend, or the call will fail.
So actually I think the most simple workaround for me is to create my own frontend-app-like enums ({ loRa, nBIoT, mBus, mobileInternet, wMBus }
) when this situation occurs, and when I need to send these values over network to the backend, I must use a enum.toBackendString()
function that translates the names correctly.
from dart-json-mapper.
I currently cant upgrade version to 2.2.7+1 because of incompatible dependable versions:
Because every version of flutter from sdk depends on collection 1.16.0 and dart_json_mapper >=2.2.7+1 <2.2.7+4 depends on collection ^1.17.0, flutter from sdk is incompatible with dart_json_mapper >=2.2.7+1 <2.2.7+4.
And because dart_json_mapper >=2.2.7+2 depends on intl ^0.18.0 and meter_installation_app depends on flutter from sdk, dart_json_mapper >=2.2.7+1 requires intl ^0.18.0.
So, because meter_installation_app depends on both dart_json_mapper ^2.2.7+1 and intl ^0.17.0, version solving failed.
pub get failed (1; So, because meter_installation_app depends on both dart_json_mapper ^2.2.7+1 and intl ^0.17.0, version solving failed.)
But I will upgrade flutter along with these versions very soon.
from dart-json-mapper.
Ok, nice to hear that. Could you clarify please the case with the "toBackendString()" solution
if ... parsing now works as expected.
what exactly doesn't work as expected? 😄 some examples would be useful
from dart-json-mapper.
Well I'm pretty sure you don't need this custom magic with toBackendString()
(that was originally the main aim to have this case style back and forth transparent) both ways serialize/deserialize.
I've added a test case to illustrate that behaviour here
So you just need to serialize the entire class before sending it to the backend and you'll have those enum values in PascalCase.
I really hope I've got your issue right 😄
from dart-json-mapper.
Awesome news, thanks for reaching out!
Ready to help with new troubles 😃
from dart-json-mapper.
Related Issues (20)
- ignoreNullMembers Annotation is ignored HOT 7
- Flutter Json Mapper (json_mapper) is failing to deserialize Enum HOT 1
- Cannot deserialize to a class with a constructor that takes an enum HOT 4
- Can't add sources to `build.yaml` using `flutter_translate_annotations` package HOT 7
- ignoreNullMembers seems to break the deserialization HOT 2
- Problem converting enum with following uppercase character HOT 9
- Adapter for https://pub.dev/packages/fast_immutable_collections
- `main.mapper.g.dart` is not generating all `@jsonSerializable` classes HOT 6
- `JsonMapper.copyWith` doesn't respect deep merge
- Parsing error when building for web HOT 9
- Improve class instance creation error handling HOT 4
- Omit serializing optional fields HOT 14
- Issues with deserialising Color HOT 3
- Dart Json Mapper looks like don't generate valueDecorators when models in different package HOT 1
- Default discriminatorValue may not work. HOT 4
- Using with Retrofit HOT 2
- Serialize generic type. HOT 1
- @Json takes no effect even if put on top class & processAnnotatedMembersOnly in option will ignore all members when serialization HOT 1
- Empty Reflectable file - regarding HOT 1
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 dart-json-mapper.