javiercbk / json_to_dart

Library that generates dart classes from json strings
https://javiercbk.github.io/json_to_dart/
The Unlicense
1.33k stars 371 forks source link

Exception inside single object or variable breaks the whole process of parsing. #53

Open binod-techindustan opened 4 years ago

binod-techindustan commented 4 years ago

How can we handle exception during parsing, so that it doesn't breaks the whole process of parsing, instead assign null value to that variable or object?

javiercbk commented 4 years ago

Hi, thanks for your report. Can you share an example of the json that is triggering the fail? It will be easier for me to check what is failing and what can be done...thank you

binod-techindustan commented 4 years ago

@javiercbk I have been using this conversion for my app since beginning. Sometime the backend returns different data type mainly like instead of String we are getting int or reverse. So it cause exception and break the whole process of parsing. So basically I want is whenever such exception occurs I just want to assign a value like null instead of failing whole process.

javiercbk commented 4 years ago

Let me see if I am understanding correctly.

Currently you are generating your dart classes using this tool with this sample json:

{
  "strProp": "server sends a string here"
}

So you generate your parser and things are looking good, until one day the you get this json:

{
  "strProp": 123
}

The code generated by this library will throw an exception whenever you attempt to parse an int as a string.

If this is currently your problem, and forgive me if I am not understanding correctly, then you have bigger problems.

I doubt that any library can handle an API contract change (in this case sending an int instead of a string).

You can grab those properties you know might change their type to dynamic and remove any parsing related. The downside is that you will end up checking the type every time you need to use such value.

Aside from that, I'm really out of ideas. Although this library is prepared to make type hinting and some other nice stuff, I found myself with little time to keep on adding features to something that currently gets a developer 80% close to the goal of parsing json.

This library generates code that aims to be performant because I use it to generate parsers for my mobile applications, I could have added some type assertion and do my best to save you from these cases, but that would make things:

Did I nailed on the issue? If I did, what ideas do you have for solving this issue for future users?

Thanks