Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
binod-techindustan opened this issue May 4, 2020 · 3 comments
Labels
question Further information is requested

Comments

@binod-techindustan
Copy link

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?

@binod-techindustan binod-techindustan changed the title Exception inside single object breaks the whole process of parsing. Exception inside single object or variable breaks the whole process of parsing. May 4, 2020
@javiercbk
Copy link
Owner

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
Copy link
Author

@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
Copy link
Owner

javiercbk commented May 5, 2020

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:

  • Less performant
  • A mess to write and read
  • If you have an int and suddenly a string arrives, then I'm either force to throw an exception or fail silently which would be much worse
  • For most APIs this use case is useless
  • I would have to pour days of work that might will save a developer's 2 minutes of work.

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

Thanks

@javiercbk javiercbk added the question Further information is requested label May 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants