From 162ae431d8499aa4d77cb55ae3f266294aebab2f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Tue, 8 May 2018 19:52:02 -0400 Subject: [PATCH] json: rename and improve the quick and easy `deserialize_json` MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- lib/json/README.md | 2 +- lib/json/serialization_read.nit | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/json/README.md b/lib/json/README.md index b964d1c..94539c3 100644 --- a/lib/json/README.md +++ b/lib/json/README.md @@ -107,7 +107,7 @@ The type to recreate is either declared or inferred: 3. If all else fails, `JsonDeserializer` uses the static type of the attribute, or the type name passed to `deserialize`. -The method `from_json_string` is a shortcut to `JsonDeserializer` which prints +The method `deserialize_json` is a shortcut to `JsonDeserializer` which prints errors to the console. It is fit only for small scripts and other quick and dirty usage. ### Example diff --git a/lib/json/serialization_read.nit b/lib/json/serialization_read.nit index 8dd73f7..4b71cea 100644 --- a/lib/json/serialization_read.nit +++ b/lib/json/serialization_read.nit @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -# Services to read JSON: `from_json_string` and `JsonDeserializer` +# Services to read JSON: `deserialize_json` and `JsonDeserializer` module serialization_read import serialization::caching @@ -364,15 +364,17 @@ redef class Text # Deserialize a `nullable Object` from this JSON formatted string # + # If a `static_type` is given, only subtypes of the `static_type` are accepted. + # # Warning: Deserialization errors are reported with `print_error` and # may be returned as a partial object or as `null`. # # This method is not appropriate when errors need to be handled programmatically, # manually use a `JsonDeserializer` in such cases. - fun from_json_string: nullable Object + fun deserialize_json(static_type: nullable String): nullable Object do var deserializer = new JsonDeserializer(self) - var res = deserializer.deserialize + var res = deserializer.deserialize(static_type) if deserializer.errors.not_empty then print_error "Deserialization Errors: {deserializer.errors.join(", ")}" end -- 1.7.9.5