k-paxian / dart-json-mapper

Serialize / Deserialize Dart Objects to / from JSON
https://pub.dev/packages/dart_json_mapper
Other
399 stars 33 forks source link

Can`t use JsonMapper in production build for web platform #13

Open fidlip opened 5 years ago

fidlip commented 5 years ago

Given this code

import 'package:dart_json_mapper/json_mapper.dart';
import 'package:dart_json_mapper/annotations.dart';

import 'main.reflectable.dart';

@jsonSerializable
class TridaA {
  TridaA() {
    print("TridaA constructed");
  }
  String prop;
}

void main() async {
  // inicializace reflektoru
  initializeReflectable();

  print(".main");
  jsonSerializable.annotatedClasses.forEach((cls) {
    print("Annotated class ${cls.qualifiedName}");
  });

  print("allClasses ${jsonSerializable.annotatedClasses}");
  var mirror = jsonSerializable.annotatedClasses.firstWhere((cm) {
    print("   class ${cm.simpleName} ~ ${cm.reflectedType.toString()}");
    return cm.simpleName == "TridaA";
  });

  print("Instance by reflection: ${mirror.newInstance("", [], {})}");

  var map = Map<String, dynamic>.from({"b": Map<String, dynamic>.from({"prop": "test"})});

  var instance = JsonMapper.instance
    .deserializeObject(map, TridaA);

  print("Instance by JsonMapper: ${instance}");

}

Everything is OK if it is built with dartdevc, but when compiled with dart2js by: webdev serve web:43751 --hot-reload --release with minification, it raise error:

Uncaught It seems your class 'minified:a6' has not been annotated with @jsonSerializable at Object.c (localhost:43751/main.dart.js:2719:3) at bX.a2 (localhost:43751/main.dart.js:5437:20) at bX.bB (localhost:43751/main.dart.js:5442:30) at localhost:43751/main.dart.js:6122:49 at ef.a (localhost:43751/main.dart.js:3678:72) at ef.$2 (localhost:43751/main.dart.js:3804:23) at Object.jI (localhost:43751/main.dart.js:3659:3) at Object.fD (localhost:43751/main.dart.js:6124:10) at localhost:43751/main.dart.js:6532:8 at localhost:43751/main.dart.js:6527:55

k-paxian commented 5 years ago

@fidlip Thank you for the fix. It's absolutely acceptable.

fidlip commented 5 years ago

Thanks a lot!

masoodmrx commented 2 years ago

Hi @k-paxian I have the same problem when using generic types (https://github.com/k-paxian/dart-json-mapper/issues/101#issuecomment-1007344542) flutter web image

masoodmrx commented 2 years ago

Hello, please solve this problem I need it

jeansebastienZ commented 1 year ago

Hello @k-paxian , Hope you are fine :).

When i went into web production , the console output this one too : Uncaught It seems your class 'minified:x<dynamic>' has not been annotated with @jsonSerializable. Then can't load and use the library .

Do you get any ideas ?

k-paxian commented 1 year ago

@jeansebastienZ sure, this issue needs some more love & attention. I'd like to mitigate it at least and this time I'm struggling to focus myself on other topics. (including latest tech layoffs, I couldn't donate extra more personal time on OSS, occasion by occasion now, small issues etc.)

jeansebastienZ commented 1 year ago

Re @k-paxian , ✌🏽,

I tried avoiding minification with --profile however it still produce the error ( Uncaught It seems your class 'JSArray<dynamic>' has not been annotated with @jsonSerializable ) . --profile mode still doing tree-shaking and there is no way to deactivate it right now.

As it is not possible to build web in Debug , right now it's not possible for us to use JSON MAPPER for web. If you get any solution you are welcome 👍🏾

jeansebastienZ commented 1 year ago

Hello @k-paxian, Can you send me a mail please 😉 ?

k-paxian commented 1 year ago

@jeansebastienZ please try the latest version 2.2.9 and give more feedback

jeansebastienZ commented 1 year ago

Hello @k-paxian ,

First thanks you for digging on this problem ✌🏽, as my team need this feature for going in production mode.

I tried with the last version :

Capture d’écran 2023-01-29 à 19 40 15

I still get this in release mode ( by the way i did re-generate the main.mapper.g to be sure ) :

Capture d’écran 2023-01-29 à 20 09 19
k-paxian commented 1 year ago

Minification is a separate issue, the goal of the release was green unit tests first.

k-paxian commented 1 year ago

I need this web platform support myself, particularly for the dart 3.0 capable to build web components for easy embedding. So eventually this issue would be resolved, but I cannot promise any timelines :shrug:

jeansebastienZ commented 1 year ago

Re @k-paxian ,

Thanks you for the info ! I've just ran it in --profile mode and uploaded to a repository and it is working well ! Thanks you for your constant works on this repository .

likeconan commented 1 year ago

Hi @k-paxian I used --profile mode but it has error like below, could you tell me how to fix?

image
jeansebastienZ commented 1 year ago

Hello @likeconan , Did you update the version , clean your cache and regenerate the main.mapper.g ? Moreover are your sure every mdls you use are effectively used on a adapter before running your app ?

likeconan commented 1 year ago

@jeansebastienZ Hi, I used dart_json_mapper with 2.2.9 version. And finally got the reason, it's because I uploaded into aws s3 and using cloudfront to host. When using s3 static website, everything works fine

ma-pe commented 1 year ago

Thank you for the workaround.

I'd also be interested in a fix for the release mode, since now my JavaScript File weighs around 6Mb which takes quite long to load. Thank you!

ma-pe commented 1 year ago

I'd be interested in taking a look into it. Yet I have no idea where to start, since there is no explicit web code. @k-paxian do you have a hint for me? Thank you :)

k-paxian commented 1 year ago

You can start from https://pub.dev/packages/build_web_compilers And making a failing unit test, after minification process.

ma-pe commented 1 year ago

I tried. But actually don't have a clue what I am doing 😬