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

Parsing error when building for web #210

Closed rgjurgens closed 1 year ago

rgjurgens commented 1 year ago

Hi, I'm using version ^2.2.6+1 and while parsing the exact same thing works fine for iOS and Android, it doesn't work fine on web. I couldn't find the exact issue, but the library fails on: Expected a value of type 'String', but got one of type 'Null' , on exactly this code block:

    final objectInstance = context.options.template ??
        (classInfo.classMirror.isEnum
            ? null
            : classInfo.classMirror.newInstance(
                classInfo
                    .getJsonConstructor(context.options.scheme)!
                    .constructorName,
                positionalArguments,
                namedArguments));

in the JSON fed to the library, none of the values are actually null, so I wonder what the error message comes from? Is this a known issue?

Thanks for the help!!!

k-paxian commented 1 year ago

I guess you have a class with the constructor arguments, more info here or here Probably you'd ease the constructor parameter to String? instead of String Or you'd need to isolate the issue on a clean project to investigate further from there

rgjurgens commented 1 year ago

Thanks! Yes, I isolated the issue in a clean project. It seems that indeed using named variables with required prefix was giving issues. btw I sponsored you for your awesome work.

k-paxian commented 1 year ago

Thank you so much 🙏 Feel free to challenge me anytime

rgjurgens commented 1 year ago

Well, I have one more question, but I'm not sure this is the right place to put it: sometimes, I find it hard to debug why parsing went wrong in your framework. For instance, when a JSON field is missing. Is it easy to add to the logging on what parsing failed exactly? Now we only get: flutter: type 'Null' is not a subtype of type 'String'

k-paxian commented 1 year ago

Surely we could enhance the error handling, we need to define the use cases first. If you could illustrate the example, when it happens. Does it relates to the omitted constructor parameters only?

rgjurgens commented 1 year ago

No, it happens for instance in this case:

@jsonSerializable
class Person {
  final String name;
  final int age;
  }

and the JSON trying to parse is this:

{
   "name":15
}

Where age is missing.

rgjurgens commented 1 year ago

However when using your framework in a standalone project however, I see a breakpoint happening at the exact right place in mapper.g.dart. But this is not happening in my main project and I think because the code is in a try catch block.

k-paxian commented 1 year ago

Ok, let's make an improvement then. I'll try to make it better and will be back to you for a feedback 🙂

rgjurgens commented 1 year ago

So far, it seems to work nicely!!! Thanks a lot! I'll come with more feedback later 🙏