]> mj.ucw.cz Git - libucw.git/commitdiff
JSON: Null, true, and false use a single static instance
authorMartin Mares <mj@ucw.cz>
Wed, 8 Jul 2015 21:14:34 +0000 (23:14 +0200)
committerMartin Mares <mj@ucw.cz>
Wed, 8 Jul 2015 21:14:34 +0000 (23:14 +0200)
ucw-json/json.h

index 74620498387ba588bf822ce80bc1badda066fe5c..16c4a93208f70e306e13552c8f1933be3e312a89 100644 (file)
@@ -167,17 +167,20 @@ struct json_pair {
 struct json_node *json_new_node(struct json_context *js, enum json_node_type type);
 
 /** Creates a new null value. **/
-static inline struct json_node *json_new_null(struct json_context *js)
+static inline struct json_node *json_new_null(struct json_context *js UNUSED)
 {
-  return json_new_node(js, JSON_NULL);
+  static const struct json_node static_null = { .type = JSON_NULL };
+  return (struct json_node *) &static_null;
 }
 
 /** Creates a new boolean value. **/
-static inline struct json_node *json_new_bool(struct json_context *js, bool value)
+static inline struct json_node *json_new_bool(struct json_context *js UNUSED, bool value)
 {
-  struct json_node *n = json_new_node(js, JSON_BOOLEAN);
-  n->boolean = value;
-  return n;
+  static const struct json_node static_bool[2] = {
+    [0] = { .type = JSON_BOOLEAN, .boolean = 0 },
+    [1] = { .type = JSON_BOOLEAN, .boolean = 1 },
+  };
+  return (struct json_node *) &static_bool[value];
 }
 
 /** Creates a new numeric value. **/