From: Alexis Laferrière Date: Mon, 26 Jan 2015 01:36:43 +0000 (-0500) Subject: examples/calculator: add services to save and load from Json X-Git-Tag: v0.7.3~21^2~2 X-Git-Url: http://nitlanguage.org?hp=865263ce0217c507628aebe2bdcea72bddf76d0a examples/calculator: add services to save and load from Json Signed-off-by: Alexis Laferrière --- diff --git a/examples/calculator/src/calculator_logic.nit b/examples/calculator/src/calculator_logic.nit index 57d3b5a..be55201 100644 --- a/examples/calculator/src/calculator_logic.nit +++ b/examples/calculator/src/calculator_logic.nit @@ -15,6 +15,8 @@ # Business logic of a calculator module calculator_logic +import json::dynamic + # Hold the state of the calculator and its services class CalculatorContext # Result of the last operation @@ -118,6 +120,52 @@ class CalculatorContext self.result = result self.current = null end + + # Serialize calculator state to Json + fun to_json: String + do + # Do not save NaN nor inf + var result = self.result + if result != null and (result.to_f.is_nan or result.to_f.is_inf != 0) then result = null + + var self_last_op = self.last_op + var last_op + if self_last_op == null then + last_op = "null" + else last_op = "\"{self_last_op}\"" + + var self_current = self.current + var current + if self_current == null then + current = "null" + else current = "\"{self_current}\"" + + return """ +{ + "result": {{{result or else "null"}}}, + "last_op": {{{last_op}}}, + "current": {{{current}}} +}""" + end + + # Load calculator state from Json + init from_json(json_string: String) + do + var json = json_string.to_json_value + if json.is_error then + print "Loading state failed: {json.to_error}" + return + end + + var result = json["result"] + if result.is_numeric then self.result = result.to_numeric + + var last_op = json["last_op"] + if last_op.is_string then self.last_op = last_op.to_s.chars.first + + var current = json["current"] + if current.is_string then self.current = new FlatBuffer.from(current.to_s) + end end redef universal Float