Merge: Deserialization manage errors & easily create Nit object from pure JSON
authorJean Privat <jean@pryen.org>
Tue, 30 Jun 2015 11:42:50 +0000 (07:42 -0400)
committerJean Privat <jean@pryen.org>
Tue, 30 Jun 2015 11:42:50 +0000 (07:42 -0400)
commit57021ef04bc69c11e4b31c513b4a6372abd50551
tree51906e2801648dd3582d1dbf6ea6451f548ae5e4
parent99a8e8ba4ec558f28c74fcf1771f3c214172caff
parent1f11591945187308342b627f2d51735cd0fd7cd0
Merge: Deserialization manage errors & easily create Nit object from pure JSON

This PR intro better error management to the deserialization phase, the abstract engine and the JSON implementation. Deserialization of a malformed JSON string or a JSON serialized object from a different program version do not crash the program anymore, it will instead raise errors. When errors are raised, the deserialized object may be inconsistent with uninitialized attribute. Depending on the context, the object may have to be discarded or, usually with the user consent, it can be used with possible fatal failures later.

### Cool bonus part
This PR also lighten the JSON format expected by the deserializer. This allow for nicer custom creation of Nit objects from JSON.

For example, given a Opportunity-like Web app using the Nit class `MeetupConfig` declared like so:

~~~nit
class MeetupConfig
    serialize

    var description: String
    var max_participants: nullable Int
    var answers: Array[FlatString]
end
~~~

On the client-side JavaScript code, one can create a JSON object that will be converted to a Nit instance of `MeetupConfig` by the JSON deserialization services:

~~~json
{"__class": "MeetupConfig", "description": "My Awesome Meetup",
 "max_participants": null, "answers": ["Pepperoni", "Chicken"]}
~~~

### Future work

* We may need to precise the types of a few errors. Especially to differentiate between fatal errors and errors that can be overlooked. There is already `IOError` and `AttributeTypeError` that should be considered fatal.
* Infer advanced array type, such as `Array[MeetupConfig]`. As of now, only arrays of string, int, float, objects and their nullable variants are created.

Pull-Request: #1544
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>
Reviewed-by: Jean Privat <jean@pryen.org>