Comments (13)
Hi @k-paxian thanks for supporting mobx!
Bumping mobx from ^0.3.9
to latest ^0.4.0+1
I'm now getting the following error:
Because dart_json_mapper_mobx 1.0.0 depends on mobx ^0.3.10 and no versions of dart_json_mapper_mobx match >1.0.0 <2.0.0, dart_json_mapper_mobx ^1.0.0 requires mobx ^0.3.10.
Would it be possible to update pubspec mobx version?
Thank you ;)
from dart-json-mapper.
@felipeleon73 Thank you for letting me know about this.
Don't worry we'll make it happen, as soon as I'll reproduce this.
from dart-json-mapper.
A great ⭐️!
from dart-json-mapper.
The proposed solution, however, presents a problem.
The ObservableList is not serialized as a standard list, and this creates problems with any webapi:
{
"mailingList": "[\"[email protected]\",\"[email protected]\",\"[email protected]\"]"
}
A better solution is to recognize the ObservableList as Iterable, modifying the file json_mapper.dart at lines 662-664 as follows:
bool get isIterable {
return isList || isSet || typeName.indexOf ("ObservableList <") == 0;
}
In this way it is enough to use a valueDecorator:
JsonMapper.registerValueDecorator <ObservableList <String>> ((value) => ObservableList <String> .of (value.cast <String> ()));
"mailingList": [
"[email protected]",
"[email protected]",
"[email protected]"
]
Would it be possible to implement a way (perhaps a JSonProperty) to indicate that an object is Iterable without relying on the name?
Or better yet indicate that the object is a List, Set or Map?
from dart-json-mapper.
@felipeleon73 Indeed, haven't noticed it myself. Thank you for vanishing it out.
Although, Thanks to your hint above I've got even better approach ironed out in my mind.
I'm gonna decouple typeInfo related code out of this library, this will opens the door for a range of specific "adapter" libraries one per each popular use case.
- MobX, https://github.com/k-paxian/dart-json-mapper-mobx
- Fixnum, https://pub.dev/packages/fixnum
- etc.
Each adapter library will contain a bundle of pre-configured:
- custom converters
- custom typeInfo decorators
- custom value decorators
So, in essence, you'll have to throw in another library dependency + 1 line of code,
and you are ready to jet 🚀
dependencies:
dart_json_mapper: any
dart_json_mapper_mobx: any
Thank you for Ideas!
from dart-json-mapper.
@felipeleon73, It has nothing to do with MobX, it was an inheritance issue.
from dart-json-mapper.
Ok. Thanx!
Now it works with Lists ... how can I make mobx ObservableList work?
If I replace the line
List<String> mailingList = List<String>();
with the line
ObservableList<String> mailingList = ObservableList<String>();
serialization works correctly, but deserialization gives me an error:
Exception has occurred.
FormatException (FormatException: Unexpected character (at character 1)
[email protected]
^
)
I tried to register both a registerValueDecorator and a registerConverter but without success.
The question could also be: how to manage a custom implementation of a collection?
from dart-json-mapper.
Quick answer is a custom converter for everything custom.
I'll try to put up an example for your case during the day
from dart-json-mapper.
@felipeleon73
Ok, here is the code
import 'dart:convert' show JsonDecoder, JsonEncoder;
import 'package:mobx/mobx.dart';
import 'package:dart_json_mapper/annotations.dart';
import 'package:dart_json_mapper/converters.dart';
import 'package:dart_json_mapper/json_mapper.dart';
class ObservableListStringConverter implements ICustomConverter<dynamic> {
const ObservableListStringConverter() : super();
static JsonDecoder jsonDecoder = JsonDecoder();
static JsonEncoder jsonEncoder = JsonEncoder();
@override
ObservableList<String> fromJSON(dynamic jsonValue,
[JsonProperty jsonProperty]) =>
ObservableList<String>.of((jsonValue is String)
? jsonDecoder.convert(jsonValue).cast<String>()
: jsonValue);
@override
dynamic toJSON(dynamic object, [JsonProperty jsonProperty]) {
final ObservableList<String> ols = object;
return jsonEncoder.convert(ols.toList());
}
}
@jsonSerializable
class MobX {
ObservableList<String> mailingList = ObservableList<String>();
MobX(this.mailingList);
}
test("Custom ObservableList<String> converter", () {
// given
final String json =
'''{"mailingList":"[\\"[email protected]\\",\\"[email protected]\\",\\"[email protected]\\"]"}''';
MobX m = MobX(ObservableList<String>.of(
['[email protected]', '[email protected]', '[email protected]']));
// when
JsonMapper.registerConverter<ObservableList<String>>(
ObservableListStringConverter());
final String targetJson = JsonMapper.serialize(m, '');
final MobX instance = JsonMapper.deserialize(targetJson);
// then
expect(targetJson, json);
expect(instance, TypeMatcher<MobX>());
expect(instance.mailingList, TypeMatcher<ObservableList<String>>());
JsonMapper.registerConverter<ObservableList<String>>(defaultConverter);
});
BTW, now you owe me a ⭐️
from dart-json-mapper.
Wow!!!
Great great approach!
And thanks to you for the great job!
from dart-json-mapper.
Here you are https://github.com/k-paxian/dart-json-mapper-mobx, please check it out,
contributions are highly welcomed 👍
from dart-json-mapper.
@1N50MN14 done.
from dart-json-mapper.
@k-paxian thank you!
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
- Problem converting enum with following uppercase character - second edition :) HOT 10
- 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.