Giter Site home page Giter Site logo

Comments (13)

1N50MN14 avatar 1N50MN14 commented on May 19, 2024 2

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.

k-paxian avatar k-paxian commented on May 19, 2024 1

@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.

felipeleon73 avatar felipeleon73 commented on May 19, 2024 1

A great ⭐️!

from dart-json-mapper.

felipeleon73 avatar felipeleon73 commented on May 19, 2024 1

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> ()));

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.

k-paxian avatar k-paxian commented on May 19, 2024 1

@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.

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.

k-paxian avatar k-paxian commented on May 19, 2024

@felipeleon73, It has nothing to do with MobX, it was an inheritance issue.

from dart-json-mapper.

felipeleon73 avatar felipeleon73 commented on May 19, 2024

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.

k-paxian avatar k-paxian commented on May 19, 2024

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.

k-paxian avatar k-paxian commented on May 19, 2024

@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.

felipeleon73 avatar felipeleon73 commented on May 19, 2024

Wow!!!
Great great approach!
And thanks to you for the great job!

from dart-json-mapper.

k-paxian avatar k-paxian commented on May 19, 2024

@felipeleon73

Here you are https://github.com/k-paxian/dart-json-mapper-mobx, please check it out,
contributions are highly welcomed 👍

from dart-json-mapper.

k-paxian avatar k-paxian commented on May 19, 2024

@1N50MN14 done.

from dart-json-mapper.

1N50MN14 avatar 1N50MN14 commented on May 19, 2024

@k-paxian thank you!

from dart-json-mapper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.