summaryrefslogtreecommitdiff
path: root/json-glib/json-object.c
diff options
context:
space:
mode:
authorPhilip Withnall <philip.withnall@collabora.co.uk>2015-11-07 14:17:31 +0100
committerEmmanuele Bassi <ebassi@gnome.org>2016-03-01 14:53:00 +0000
commit58f479b60eb2db4c73605d469d68a8ffd8679327 (patch)
treeb414ce65be7a8d493234fcf325521a69d0ea5bd4 /json-glib/json-object.c
parenta82b93ba60dd0f54660990df86ba0cf7fc74c9a8 (diff)
downloadjson-glib-58f479b60eb2db4c73605d469d68a8ffd8679327.tar.gz
core: Add immutability support to core objects
Add an immutable mode to JsonNode, JsonObject, JsonArray and JsonValue. This is an optional mode which objects enter by calling json_*_seal(). It is a one-way transition, which means that we can build and manipulate objects as much as desired, before sealing them and enjoying the benefits of immutable objects: no need to take copies when handling them, persistent hash values (still to be implemented). https://bugzilla.gnome.org/show_bug.cgi?id=756121
Diffstat (limited to 'json-glib/json-object.c')
-rw-r--r--json-glib/json-object.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/json-glib/json-object.c b/json-glib/json-object.c
index 214ffef..574d04d 100644
--- a/json-glib/json-object.c
+++ b/json-glib/json-object.c
@@ -117,6 +117,57 @@ json_object_unref (JsonObject *object)
}
}
+/**
+ * json_object_seal:
+ * @object: a #JsonObject
+ *
+ * Seals the #JsonObject, making it immutable to further changes. This will
+ * recursively seal all members of the object too.
+ *
+ * If the @object is already immutable, this is a no-op.
+ *
+ * Since: UNRELEASED
+ */
+void
+json_object_seal (JsonObject *object)
+{
+ JsonObjectIter iter;
+ JsonNode *node;
+
+ g_return_if_fail (object != NULL);
+ g_return_if_fail (object->ref_count > 0);
+
+ if (object->immutable)
+ return;
+
+ /* Propagate to all members. */
+ json_object_iter_init (&iter, object);
+
+ while (json_object_iter_next (&iter, NULL, &node))
+ json_node_seal (node);
+
+ object->immutable = TRUE;
+}
+
+/**
+ * json_object_is_immutable:
+ * @object: a #JsonObject
+ *
+ * Check whether the given @object has been marked as immutable by calling
+ * json_object_seal() on it.
+ *
+ * Since: UNRELEASED
+ * Returns: %TRUE if the @object is immutable
+ */
+gboolean
+json_object_is_immutable (JsonObject *object)
+{
+ g_return_val_if_fail (object != NULL, FALSE);
+ g_return_val_if_fail (object->ref_count > 0, FALSE);
+
+ return object->immutable;
+}
+
static inline void
object_set_member_internal (JsonObject *object,
const gchar *member_name,