I am parsing a large JSONArray (33000 items approximately) of JSON objects. I am converting the JSON objects into my own objects in my app. It looks a bit like the following:
Card card = Card.build(cardObj.getString("name"))
.setFoo(cardObj.getString("foo"))
.setBar(cardObj.getInt("bar"))
.setBaz(cardObj.getString("baz"))
... about 10 more
The Problem is that all of the cards (in the raw JSON) do not have the same keys. Because of this, I would have to have a try catch block for every single line of code in my parser, because any exception would have to be handled before the code can continue executing. For example, as in the snippet above, if I were parsing an object that didn't have bar, then the code will immediately jump to the exception handling, skipping the step where it sets the baz value.
My Question: Is there a way of doing this where I can avoid try-catching every single line? I know that you can't ignore exceptions, but perhaps there is something similar that may help.
A Better Example
try {
Card card = Card.build(cardObj.getString("name"))
.setFoo(cardObj.getString("foo"))
.setBar(cardObj.getInt("bar"))
.setBaz(cardObj.getString("baz"))
} catch (JSONException e) {
// The code will jump here for any exception.
}
If the code fails while setting foo or bar, the execution context will jump to the error handling step inside of the catch block. The function calls following the offending line will never run, therefore bar or baz may never be set, even if they exist in the original JSON. I can see no way to prevent this other than try-catching every single line or by checking to make sure every single value exists beforehand. Is there no better way of doing this?
throwskeyword?