From 95fd5f93fcb1bba3c93a8d01e973848877abbede Mon Sep 17 00:00:00 2001 From: YNH Webdev Date: Tue, 24 Sep 2013 21:50:36 +0200 Subject: [PATCH] Simplify the json object by adding type specific put and replace --- library/kernel/json_object.e | 94 ++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/library/kernel/json_object.e b/library/kernel/json_object.e index 7085a258..01cd4e65 100644 --- a/library/kernel/json_object.e +++ b/library/kernel/json_object.e @@ -52,6 +52,57 @@ feature -- Change Element object.extend (l_value, key) end + put_string (value: detachable JSON_STRING; key: JSON_STRING) + -- Assuming there is no item of key `key', + -- insert `value' with `key'. + require + key_not_present: not has_key (key) + do + put (value, key) + end + + put_integer (value: detachable INTEGER_64; key: JSON_STRING) + -- Assuming there is no item of key `key', + -- insert `value' with `key'. + require + key_not_present: not has_key (key) + local + l_value: detachable JSON_NUMBER + do + if attached value as v then + create l_value.make_integer (v) + end + put (l_value, key) + end + + put_natural (value: detachable NATURAL_64; key: JSON_STRING) + -- Assuming there is no item of key `key', + -- insert `value' with `key'. + require + key_not_present: not has_key (key) + local + l_value: detachable JSON_NUMBER + do + if attached value as v then + create l_value.make_natural (v) + end + put (l_value, key) + end + + put_real (value: detachable DOUBLE; key: JSON_STRING) + -- Assuming there is no item of key `key', + -- insert `value' with `key'. + require + key_not_present: not has_key (key) + local + l_value: detachable JSON_NUMBER + do + if attached value as v then + create l_value.make_real (v) + end + put (l_value, key) + end + replace (value: detachable JSON_VALUE; key: JSON_STRING) -- Assuming there is no item of key `key', -- insert `value' with `key'. @@ -65,6 +116,49 @@ feature -- Change Element object.force (l_value, key) end + replace_string (value: detachable JSON_STRING; key: JSON_STRING) + -- Assuming there is no item of key `key', + -- insert `value' with `key'. + do + replace (value, key) + end + + replace_integer (value: detachable INTEGER_64; key: JSON_STRING) + -- Assuming there is no item of key `key', + -- insert `value' with `key'. + local + l_value: detachable JSON_NUMBER + do + if attached value as v then + create l_value.make_integer (v) + end + replace (l_value, key) + end + + replace_natural (value: detachable NATURAL_64; key: JSON_STRING) + -- Assuming there is no item of key `key', + -- insert `value' with `key'. + local + l_value: detachable JSON_NUMBER + do + if attached value as v then + create l_value.make_natural (v) + end + replace (l_value, key) + end + + replace_real (value: detachable DOUBLE; key: JSON_STRING) + -- Assuming there is no item of key `key', + -- insert `value' with `key'. + local + l_value: detachable JSON_NUMBER + do + if attached value as v then + create l_value.make_real (v) + end + replace (l_value, key) + end + remove (key: JSON_STRING) -- Remove item indexed by `key' if any. do