diff mbox

[v3,2/5] QJSON: Add JSON writer

Message ID 1419604968-87437-3-git-send-email-agraf@suse.de
State New
Headers show

Commit Message

Alexander Graf Dec. 26, 2014, 2:42 p.m. UTC
To support programmatic JSON assembly while keeping the code that generates it
readable, this patch introduces a simple JSON writer. It emits JSON serially
into a buffer in memory.

The nice thing about this writer is its simplicity and low memory overhead.
Unlike the QMP JSON writer, this one does not need to spawn QObjects for every
element it wants to represent.

This is a prerequisite for the migration stream format description generator.

Signed-off-by: Alexander Graf <agraf@suse.de>

---

v2 -> v3:

  - QOMify the QJSON object, makes for easier destruction
---
 include/qjson.h |  3 ++-
 qjson.c         | 39 ++++++++++++++++++++++++++++++++++++---
 2 files changed, 38 insertions(+), 4 deletions(-)

Comments

Eric Blake Jan. 6, 2015, 3:44 p.m. UTC | #1
On 12/26/2014 07:42 AM, Alexander Graf wrote:
> To support programmatic JSON assembly while keeping the code that generates it
> readable, this patch introduces a simple JSON writer. It emits JSON serially
> into a buffer in memory.
> 
> The nice thing about this writer is its simplicity and low memory overhead.
> Unlike the QMP JSON writer, this one does not need to spawn QObjects for every
> element it wants to represent.
> 
> This is a prerequisite for the migration stream format description generator.
> 
> Signed-off-by: Alexander Graf <agraf@suse.de>
> 
> ---
> 
> v2 -> v3:
> 
>   - QOMify the QJSON object, makes for easier destruction
> ---
>  include/qjson.h |  3 ++-
>  qjson.c         | 39 ++++++++++++++++++++++++++++++++++++---
>  2 files changed, 38 insertions(+), 4 deletions(-)
> 
> diff --git a/include/qjson.h b/include/qjson.h
> index 8f8c145..7c54fdf 100644
> --- a/include/qjson.h
> +++ b/include/qjson.h
> @@ -4,7 +4,7 @@
>   * Copyright Alexander Graf
>   *
>   * Authors:
> - *  Alexander Graf <agraf@suse.de
> + *  Alexander Graf <agraf@suse.de>

Umm, this should be squashed into 1/5.

> @@ -85,9 +88,7 @@ const char *qjson_get_str(QJSON *json)
>  
>  QJSON *qjson_new(void)
>  {
> -    QJSON *json = g_new(QJSON, 1);
> -    json->str = qstring_from_str("{ ");
> -    json->omit_comma = true;
> +    QJSON *json = (QJSON *)object_new(TYPE_QJSON);
>      return json;

This undoes the dangling object that I complained about on patch 1;
maybe there's some more squashing to do?...


> +static void qjson_initfn(Object *obj)
> +{
> +    QJSON *json = (QJSON *)object_dynamic_cast(obj, TYPE_QJSON);
> +    assert(json);
> +
> +    json->str = qstring_from_str("{ ");
> +    json->omit_comma = true;

...or is it still an incomplete object, just now in a different location?
Alexander Graf Jan. 6, 2015, 9:16 p.m. UTC | #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1



On 06.01.15 16:44, Eric Blake wrote:
> On 12/26/2014 07:42 AM, Alexander Graf wrote:
>> To support programmatic JSON assembly while keeping the code that
>> generates it readable, this patch introduces a simple JSON
>> writer. It emits JSON serially into a buffer in memory.
>> 
>> The nice thing about this writer is its simplicity and low memory
>> overhead. Unlike the QMP JSON writer, this one does not need to
>> spawn QObjects for every element it wants to represent.
>> 
>> This is a prerequisite for the migration stream format
>> description generator.
>> 
>> Signed-off-by: Alexander Graf <agraf@suse.de>
>> 
>> ---
>> 
>> v2 -> v3:
>> 
>> - QOMify the QJSON object, makes for easier destruction --- 
>> include/qjson.h |  3 ++- qjson.c         | 39
>> ++++++++++++++++++++++++++++++++++++--- 2 files changed, 38
>> insertions(+), 4 deletions(-)
>> 
>> diff --git a/include/qjson.h b/include/qjson.h index
>> 8f8c145..7c54fdf 100644 --- a/include/qjson.h +++
>> b/include/qjson.h @@ -4,7 +4,7 @@ * Copyright Alexander Graf * *
>> Authors: - *  Alexander Graf <agraf@suse.de + *  Alexander Graf
>> <agraf@suse.de>
> 
> Umm, this should be squashed into 1/5.

Honestly, I think the whole patch should've gotten squashed into 1/5.
I'm not quite sure what went wrong here :).

> 
>> @@ -85,9 +88,7 @@ const char *qjson_get_str(QJSON *json)
>> 
>> QJSON *qjson_new(void) { -    QJSON *json = g_new(QJSON, 1); -
>> json->str = qstring_from_str("{ "); -    json->omit_comma =
>> true; +    QJSON *json = (QJSON *)object_new(TYPE_QJSON); return
>> json;
> 
> This undoes the dangling object that I complained about on patch
> 1; maybe there's some more squashing to do?...
> 
> 
>> +static void qjson_initfn(Object *obj) +{ +    QJSON *json =
>> (QJSON *)object_dynamic_cast(obj, TYPE_QJSON); +
>> assert(json); + +    json->str = qstring_from_str("{ "); +
>> json->omit_comma = true;
> 
> ...or is it still an incomplete object, just now in a different
> location?

The idea is that you call finish and only then receive the string. I
don't think doing that from get_str would be too much of an
improvement - it'd still be wrong if we're in the middle of assembling
an object.


Alex
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.22 (Darwin)
Comment: GPGTools - http://gpgtools.org

iQIcBAEBAgAGBQJUrFC2AAoJECszeR4D/txgFUoP/A79amlLyEBoD7/G1jHttwS2
sYKst8ZM+v//dB5Xe+DzU53XgflAkpUoz27zSveluD1A3YQ3zpFYraujaewSn++m
sJZS/7bS9WND0vhKdGj41iJXdjxSlHqcdrsKRqYR4MaaTjH8eVoabECS+CcdK0xc
R+x08gSUI22+tCKPnudRMm3mlVqJ5O+1KbTYShQQlP5go1p6UN32LONgZ4u9oJPL
uQ1vkkrHleHa+RoW4HzRCSO+LQuY/y8CMV1ufS50pILSoX5C/jDdOzqvpc3fR9Ba
X1dWVATsF8gk+Hu4sNtWAqiPSsSGg/V0CnBASRYwt/skoNtXoZ++/UWr8B+F/IKc
h2UCdO64TlbbDtGYGn/4ZQuUIsGLPvm0yN8J1LshVt85wWJaNe2dw8hirxjzCFoB
A6YnueXbaxkHlQkVXL0u2E/EUnmYlRl8JauFBxVDuw6MQecQtc7MIFS0zr8XyKDt
IxspJpOKGXAuU7oK+1kmy2aOlMJCVwgdthCv9qDaQl39pTpyF38Jf8kOAFWSkz+C
bQqq4EZrI4vMlHcwY4a06YnFH7Ek8Q7YZdFj/W2CIJKsIMh0k79mFjm4r54JrIUF
UTXoR/DuZd/zRGLBnj8PoBsfiXk3ZGJCtAyrlns2T32sEbiJmL3lqggiyKzh3+Dd
S9NeyhQvJ/xvYPQ89N7p
=Zod1
-----END PGP SIGNATURE-----
diff mbox

Patch

diff --git a/include/qjson.h b/include/qjson.h
index 8f8c145..7c54fdf 100644
--- a/include/qjson.h
+++ b/include/qjson.h
@@ -4,7 +4,7 @@ 
  * Copyright Alexander Graf
  *
  * Authors:
- *  Alexander Graf <agraf@suse.de
+ *  Alexander Graf <agraf@suse.de>
  *
  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  * See the COPYING.LIB file in the top-level directory.
@@ -13,6 +13,7 @@ 
 #ifndef QEMU_QJSON_H
 #define QEMU_QJSON_H
 
+#define TYPE_QJSON "QJSON"
 typedef struct QJSON QJSON;
 
 QJSON *qjson_new(void);
diff --git a/qjson.c b/qjson.c
index 7a7cd72..8d911d0 100644
--- a/qjson.c
+++ b/qjson.c
@@ -15,8 +15,11 @@ 
 #include <stdbool.h>
 #include <glib.h>
 #include <qjson.h>
+#include <qemu/module.h>
+#include <qom/object.h>
 
 struct QJSON {
+    Object obj;
     QString *str;
     bool omit_comma;
     unsigned long self_size_offset;
@@ -85,9 +88,7 @@  const char *qjson_get_str(QJSON *json)
 
 QJSON *qjson_new(void)
 {
-    QJSON *json = g_new(QJSON, 1);
-    json->str = qstring_from_str("{ ");
-    json->omit_comma = true;
+    QJSON *json = (QJSON *)object_new(TYPE_QJSON);
     return json;
 }
 
@@ -95,3 +96,35 @@  void qjson_finish(QJSON *json)
 {
     json_end_object(json);
 }
+
+static void qjson_initfn(Object *obj)
+{
+    QJSON *json = (QJSON *)object_dynamic_cast(obj, TYPE_QJSON);
+    assert(json);
+
+    json->str = qstring_from_str("{ ");
+    json->omit_comma = true;
+}
+
+static void qjson_finalizefn(Object *obj)
+{
+    QJSON *json = (QJSON *)object_dynamic_cast(obj, TYPE_QJSON);
+
+    assert(json);
+    qobject_decref(QOBJECT(json->str));
+}
+
+static const TypeInfo qjson_type_info = {
+    .name = TYPE_QJSON,
+    .parent = TYPE_OBJECT,
+    .instance_size = sizeof(QJSON),
+    .instance_init = qjson_initfn,
+    .instance_finalize = qjson_finalizefn,
+};
+
+static void qjson_register_types(void)
+{
+    type_register_static(&qjson_type_info);
+}
+
+type_init(qjson_register_types)