From 7a9abfd549110a45b1e4145b4fddf601f70dfba7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Mon, 29 Jun 2015 08:35:27 -0400 Subject: [PATCH] lib/json: document deserializing custom JSON code to Nit objects MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- lib/json/serialization.nit | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/lib/json/serialization.nit b/lib/json/serialization.nit index 19bea9e..35ade36 100644 --- a/lib/json/serialization.nit +++ b/lib/json/serialization.nit @@ -55,6 +55,37 @@ # assert alice.to_plain_json == """ # {"name": "Alice", "year_of_birth": 1978, "next_of_kin": {"name": "Bob", "year_of_birth": 1986, "next_of_kin": null}}""" # ~~~ +# +# ## JSON to Nit objects +# +# The `JsonDeserializer` support reading JSON code with minimal meta-data +# to easily create Nit object from client-side code or configuration files. +# Each JSON object must define the `__class` attribute with the corresponding +# Nit class and the expected attributes with its name in Nit followed by its value. +# +# ### Usage Example +# +# ~~~nitish +# import json::serialization +# +# class MeetupConfig +# serialize +# +# var description: String +# var max_participants: nullable Int +# var answers: Array[FlatString] +# end +# +# var json_code = """ +# {"__class": "MeetupConfig", "description": "My Awesome Meetup", "max_participants": null, "answers": ["Pepperoni", "Chicken"]}""" +# var deserializer = new JsonDeserializer(json_code) +# +# var meet = deserializer.deserialize +# assert meet isa MeetupConfig +# assert meet.description == "My Awesome Meetup" +# assert meet.max_participants == null +# assert meet.answers == ["Pepperoni", "Chicken"] +# ~~~ module serialization import ::serialization::caching -- 1.7.9.5