You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by nw...@apache.org on 2015/05/27 15:57:41 UTC
[2/8] lucy git commit: Helper functions to extract numbers from JSON
data
Helper functions to extract numbers from JSON data
Prepare for the removal of Obj_To_I64, Obj_To_F64, and Obj_To_Bool.
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/8b4f0606
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/8b4f0606
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/8b4f0606
Branch: refs/heads/master
Commit: 8b4f06069348c6f2aff545fcc63b2bc551c7cdec
Parents: 46dd03c
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Sat May 23 15:11:30 2015 +0200
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Sat May 23 17:09:12 2015 +0200
----------------------------------------------------------------------
core/Lucy/Util/Json.c | 63 ++++++++++++++++++++++++++++++++++++++++++++
core/Lucy/Util/Json.cfh | 18 +++++++++++++
2 files changed, 81 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucy/blob/8b4f0606/core/Lucy/Util/Json.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Util/Json.c b/core/Lucy/Util/Json.c
index f9b9930..4b589ea 100644
--- a/core/Lucy/Util/Json.c
+++ b/core/Lucy/Util/Json.c
@@ -701,3 +701,66 @@ S_set_error(CharBuf *buf, const char *json, const char *limit, int line,
Err_set_error(Err_new(mess));
}
+int64_t
+Json_obj_to_i64(Obj *obj) {
+ int64_t retval = 0;
+
+ if (!obj) {
+ THROW(ERR, "Can't extract integer from NULL");
+ }
+ else if (Obj_Is_A(obj, STRING)) {
+ retval = Str_To_I64((String*)obj);
+ }
+ else if (Obj_Is_A(obj, NUM)) {
+ retval = Num_To_I64((Num*)obj);
+ }
+ else {
+ THROW(ERR, "Can't extract integer from object of type %o",
+ Obj_Get_Class_Name(obj));
+ }
+
+ return retval;
+}
+
+double
+Json_obj_to_f64(Obj *obj) {
+ double retval = 0;
+
+ if (!obj) {
+ THROW(ERR, "Can't extract float from NULL");
+ }
+ else if (Obj_Is_A(obj, STRING)) {
+ retval = Str_To_F64((String*)obj);
+ }
+ else if (Obj_Is_A(obj, NUM)) {
+ retval = Num_To_F64((Num*)obj);
+ }
+ else {
+ THROW(ERR, "Can't extract float from object of type %o",
+ Obj_Get_Class_Name(obj));
+ }
+
+ return retval;
+}
+
+bool
+Json_obj_to_bool(Obj *obj) {
+ bool retval = false;
+
+ if (!obj) {
+ THROW(ERR, "Can't extract bool from NULL");
+ }
+ else if (Obj_Is_A(obj, STRING)) {
+ retval = (Str_To_I64((String*)obj) != 0);
+ }
+ else if (Obj_Is_A(obj, NUM)) {
+ retval = Num_To_Bool((Num*)obj);
+ }
+ else {
+ THROW(ERR, "Can't extract bool from object of type %o",
+ Obj_Get_Class_Name(obj));
+ }
+
+ return retval;
+}
+
http://git-wip-us.apache.org/repos/asf/lucy/blob/8b4f0606/core/Lucy/Util/Json.cfh
----------------------------------------------------------------------
diff --git a/core/Lucy/Util/Json.cfh b/core/Lucy/Util/Json.cfh
index ff8d32e..6a0e6a6 100644
--- a/core/Lucy/Util/Json.cfh
+++ b/core/Lucy/Util/Json.cfh
@@ -57,6 +57,24 @@ class Lucy::Util::Json inherits Clownfish::Obj {
*/
inert void
set_tolerant(bool tolerant);
+
+ /** Extract an integer value from an object that is part of a JSON
+ * structure. Throws an error if the object isn't a String or Num.
+ */
+ inert int64_t
+ obj_to_i64(Obj *obj);
+
+ /** Extract a float value from an object that is part of a JSON
+ * structure. Throws an error if the object isn't a String or Num.
+ */
+ inert double
+ obj_to_f64(Obj *obj);
+
+ /** Extract a bool value from an object that is part of a JSON
+ * structure. Throws an error if the object isn't a String or Num.
+ */
+ inert bool
+ obj_to_bool(Obj *obj);
}