Skip to contentSkip to conversation

json parser (v2)

PATCH In review Anthony Liguori 7 participants 28 messages Oct 17, 2009 – Oct 23, 2009 Kbuild

lore
Share & tools
lore
#0 lore raw

cover json parser (v2)

I fat-fingered the git send-email command the first time so here's the second iteration.

This series introduces a json parsing framework. The parser is a recursive decent parser and implements the full json spec except for the null keyword. We need to decide how we want to model null in order to support this.

In addition, we implement an extension to allow single quote strings. This is very useful for defining dictionaries in C.

The parser has a full test suite and introduces a boolean and float type.

It also supports varargs parsing. Here's an example of how that works:

QObject *obj;

obj = qobject_from_jsonf("{'id': %d, {'locked': %i, 'file': %s}}",

                           NULL,
                           bs->id, bs->locked, bs->file);

The parser also can embed full qobjects with the '%p' format.

To do streaming parsing, we need a lexer that can tokenize the input. We then just need a simple counter to look for balanced {'s and ['s. The lexer can just be a regular expression so it's pretty straight forward.

#1 lore raw

PATCH 1/11 Add append method to qstring and empty constructor

This allows qstring to be used for dynamic string construction.

---
 qstring.c |   37 ++++++++++++++++++++++++++++++++++++-
 qstring.h |    4 ++++
 2 files changed, 40 insertions(+), 1 deletions(-)
qstring.c +36 -1
index 6d411da..441a9e6 100644
@@ -21,6 +21,16 @@ static const QType qstring_type = {
 };
 
 /**
+ * qstring_new(): Create a new empty QString
+ *
+ * Return strong reference.
+ */
+QString *qstring_new(void)
+{
+    return qstring_from_str("");
+}
+
+/**
  * qstring_from_str(): Create a new QString from a regular C string
  *
  * Return strong reference.
@@ -30,12 +40,37 @@ QString *qstring_from_str(const char *str)
     QString *qstring;
 
     qstring = qemu_malloc(sizeof(*qstring));
-    qstring->string = qemu_strdup(str);
+
+    qstring->length = strlen(str);
+    qstring->capacity = qstring->length;
+
+    qstring->string = qemu_malloc(qstring->capacity + 1);
+    memcpy(qstring->string, str, qstring->length);
+    qstring->string[qstring->length] = 0;
+
     QOBJECT_INIT(qstring, &qstring_type);
 
     return qstring;
 }
 
+/* qstring_append(): Append a C string to a QString
+ */
+void qstring_append(QString *qstring, const char *str)
+{
+    size_t len = strlen(str);
+
+    if (qstring->capacity < (qstring->length + len)) {
+        qstring->capacity += len;
+        qstring->capacity *= 2; /* use exponential growth */
+
+        qstring->string = qemu_realloc(qstring->string, qstring->capacity + 1);
+    }
+
+    memcpy(qstring->string + qstring->length, str, len);
+    qstring->length += len;
+    qstring->string[qstring->length] = 0;
+}
+
 /**
  * qobject_to_qstring(): Convert a QObject to a QString
  */
qstring.h +4 -0
index e012cb7..65905d4 100644
@@ -6,10 +6,14 @@
 typedef struct QString {
     QObject_HEAD;
     char *string;
+    size_t length;
+    size_t capacity;
 } QString;
 
+QString *qstring_new(void);
 QString *qstring_from_str(const char *str);
 const char *qstring_get_str(const QString *qstring);
+void qstring_append(QString *qstring, const char *str);
 QString *qobject_to_qstring(const QObject *obj);
 
 #endif /* QSTRING_H */
Signature · 1 line
1.6.2.5
#2 lore raw

Re: [Qemu-devel] [PATCH 01/11] Add append method to qstring and empty constructor

Quoted 31 lines · On Sat, 17 Oct 2009 08:36:01 -0500 Anthony Liguori <[email protected]> wrote:

This allows qstring to be used for dynamic string construction.

---
 qstring.c |   37 ++++++++++++++++++++++++++++++++++++-
 qstring.h |    4 ++++
 2 files changed, 40 insertions(+), 1 deletions(-)
qstring.c +10 -0
index 6d411da..441a9e6 100644
@@ -21,6 +21,16 @@ static const QType qstring_type = {
 };
 
 /**
+ * qstring_new(): Create a new empty QString
+ *
+ * Return strong reference.
+ */
+QString *qstring_new(void)
+{
+    return qstring_from_str("");
+}
+
+/**
  * qstring_from_str(): Create a new QString from a regular C string
  *
  * Return strong reference.
@@ -30,12 +40,37 @@ QString *qstring_from_str(const char *str)
     QString *qstring;

qstring.c QString *qstring_from_str(const char *str) · line 49

Quoted 10 lines
qstring.c +7 -1
 
     qstring = qemu_malloc(sizeof(*qstring));
-    qstring->string = qemu_strdup(str);
+
+    qstring->length = strlen(str);
+    qstring->capacity = qstring->length;
+
+    qstring->string = qemu_malloc(qstring->capacity + 1);
+    memcpy(qstring->string, str, qstring->length);
+    qstring->string[qstring->length] = 0;

Couldn't this be:

qstring->string = qemu_strdup(str); qstring->length = qstring->capacity = strlen(str);

qstring.c QString *qstring_from_str(const char *str) · line 57

Quoted 8 lines
qstring.c +3 -0
+
     QOBJECT_INIT(qstring, &qstring_type);
 
     return qstring;
 }
 
+/* qstring_append(): Append a C string to a QString
+ */

Forgot the 'little roof' in the comment style. :-)

qstring.c QString *qstring_from_str(const char *str) · line 67

Quoted 10 lines
qstring.c +10 -0
+void qstring_append(QString *qstring, const char *str)
+{
+    size_t len = strlen(str);
+
+    if (qstring->capacity < (qstring->length + len)) {
+        qstring->capacity += len;
+        qstring->capacity *= 2; /* use exponential growth */
+
+        qstring->string = qemu_realloc(qstring->string, qstring->capacity + 1);
+    }

Why do we need to double it? Wouldn't be enough to only keep track
of the current string length and add 'len' to it? We could drop 'capacity' then.

qstring.c QString *qstring_from_str(const char *str) · line 71

Quoted 4 lines
qstring.c +4 -0
+
+    memcpy(qstring->string + qstring->length, str, len);
+    qstring->length += len;
+    qstring->string[qstring->length] = 0;

I would use strcat().

Quoted 24 lines
qstring.c +2 -0
+}
+
 /**
  * qobject_to_qstring(): Convert a QObject to a QString
  */
qstring.h +4 -0
index e012cb7..65905d4 100644
@@ -6,10 +6,14 @@
 typedef struct QString {
     QObject_HEAD;
     char *string;
+    size_t length;
+    size_t capacity;
 } QString;
 
+QString *qstring_new(void);
 QString *qstring_from_str(const char *str);
 const char *qstring_get_str(const QString *qstring);
+void qstring_append(QString *qstring, const char *str);
 QString *qobject_to_qstring(const QObject *obj);
 
 #endif /* QSTRING_H */
#3 lore raw

Re: [Qemu-devel] [PATCH 01/11] Add append method to qstring and empty constructor

Quoted 12 lines · Luiz Capitulino

qstring.c QString *qstring_from_str(const char *str) · line 49

Quoted 9 lines
     qstring = qemu_malloc(sizeof(*qstring));
qstring.c +7 -1
-    qstring->string = qemu_strdup(str);
+
+    qstring->length = strlen(str);
+    qstring->capacity = qstring->length;
+
+    qstring->string = qemu_malloc(qstring->capacity + 1);
+    memcpy(qstring->string, str, qstring->length);
+    qstring->string[qstring->length] = 0;

Couldn't this be:

Quoted 2 lines

qstring->string = qemu_strdup(str); qstring->length = qstring->capacity = strlen(str);

Probably to have one call to strlen() instead of two (one inside qemu_strdup()).

Quoted 6 lines
Quoted 6 lines
qstring.c +6 -0
+void qstring_append(QString *qstring, const char *str)
+{
+    size_t len = strlen(str);
+
+    if (qstring->capacity < (qstring->length + len)) {
+        qstring->capacity += len;
Quoted 8 lines

qstring.c QString *qstring_from_str(const char *str) · line 67

Quoted 4 lines
qstring.c +4 -0
+        qstring->capacity *= 2; /* use exponential growth */
+
+        qstring->string = qemu_realloc(qstring->string, qstring->capacity + 1);
+    }

Why do we need to double it? Wouldn't be enough to only keep track
of the current string length and add 'len' to it? We could drop 'capacity' then.

You need exponential growth if large stringes are to be grown in O(n) time where n is the number of characters, appended in small pieces.

Think about the time spent copying bytes every time qemu_realloc() is called.

If you just add 'len' each time, think about appending 1 byte 10^4 times. It will copy approximately 10^8/2 bytes, which is a lot just to make a string 10^4 bytes long.

But += len; *= 2 is not necessary. *= 2 is enough, provided the result is large enough.

Quoted 5 lines

qstring.c QString *qstring_from_str(const char *str) · line 71

Quoted 3 lines
qstring.c +3 -0
+    memcpy(qstring->string + qstring->length, str, len);
+    qstring->length += len;
+    qstring->string[qstring->length] = 0;

I would use strcat().

Again, that's an extra call to strlen(), traversing the string twice instead of once. Doesn't make much different for small strings, only large ones.

-- Jamie

#4 lore raw

PATCH 2/11 Add support for qfloat

In reply to Anthony Liguori

qfloat is a qobject wrapper for double precision floating points

---
 Makefile  |    3 +-
 qfloat.c  |   76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qfloat.h  |   29 +++++++++++++++++++++++
 qobject.h |    1 +
 4 files changed, 108 insertions(+), 1 deletions(-)
 create mode 100644 qfloat.c
 create mode 100644 qfloat.h
Makefile +2 -1
index e78a3d0..9294638 100644
@@ -125,7 +125,8 @@ obj-y += net.o net-queue.o
 obj-y += qemu-char.o aio.o net-checksum.o savevm.o
 obj-y += msmouse.o ps2.o
 obj-y += qdev.o qdev-properties.o
-obj-y += qint.o qstring.o qdict.o qlist.o qemu-config.o
+obj-y += qint.o qstring.o qdict.o qlist.o qfloat.o
+obj-y += qemu-config.o
 
 obj-$(CONFIG_BRLAPI) += baum.o
 obj-$(CONFIG_WIN32) += tap-win32.o
qfloat.c new file +76 -0

76 lines not on this page · raw message

qfloat.h new file +29 -0
new file mode 100644
index 0000000..9d67876
@@ -0,0 +1,29 @@
+/*
+ * QFloat Module
+ *
+ * Copyright IBM, Corp. 2009
+ *
+ * Authors:
+ *  Anthony Liguori   <[email protected]>
+ *
+ * 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.
+ *
+ */
+
+#ifndef QFLOAT_H
+#define QFLOAT_H
+
+#include <stdint.h>
+#include "qobject.h"
+
+typedef struct QFloat {
+    QObject_HEAD;
+    double value;
+} QFloat;
+
+QFloat *qfloat_from_double(double value);
+double qfloat_get_double(const QFloat *qi);
+QFloat *qobject_to_qfloat(const QObject *obj);
+
+#endif /* QFLOAT_H */
qobject.h +1 -0
index 4cc9287..da03a5e 100644
@@ -41,6 +41,7 @@ typedef enum {
     QTYPE_QSTRING,
     QTYPE_QDICT,
     QTYPE_QLIST,
+    QTYPE_QFLOAT,
 } qtype_code;
 
 struct QObject;
Signature · 1 line
1.6.2.5
#5 lore raw

Re: [Qemu-devel] [PATCH 02/11] Add support for qfloat

Quoted 42 lines · On Sat, 17 Oct 2009 08:36:02 -0500 Anthony Liguori <[email protected]> wrote:

qfloat is a qobject wrapper for double precision floating points

---
 Makefile  |    3 +-
 qfloat.c  |   76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qfloat.h  |   29 +++++++++++++++++++++++
 qobject.h |    1 +
 4 files changed, 108 insertions(+), 1 deletions(-)
 create mode 100644 qfloat.c
 create mode 100644 qfloat.h
Makefile +2 -1
index e78a3d0..9294638 100644
@@ -125,7 +125,8 @@ obj-y += net.o net-queue.o
 obj-y += qemu-char.o aio.o net-checksum.o savevm.o
 obj-y += msmouse.o ps2.o
 obj-y += qdev.o qdev-properties.o
-obj-y += qint.o qstring.o qdict.o qlist.o qemu-config.o
+obj-y += qint.o qstring.o qdict.o qlist.o qfloat.o
+obj-y += qemu-config.o
 
 obj-$(CONFIG_BRLAPI) += baum.o
 obj-$(CONFIG_WIN32) += tap-win32.o
qfloat.c new file +10 -0
new file mode 100644
index 0000000..05215f5
@@ -0,0 +1,76 @@
+/*
+ * QFloat Module
+ *
+ * Copyright (C) 2009 Red Hat Inc.
+ *
+ * Authors:
+ *  Luiz Capitulino <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.

qfloat.c line 20

Quoted 10 lines
qfloat.c new file +10 -0
+ *
+ * Copyright IBM, Corp. 2009
+ *
+ * Authors:
+ *  Anthony Liguori   <[email protected]>
+ *
+ * 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.
+ *
+ */

I see you're double licensing it to respect my initial choice, but you
can leave only LGPL for this new code.

I will send patches changing the ones written by me.

#6 lore raw

Re: [Qemu-devel] [PATCH 02/11] Add support for qfloat

Quoted 61 lines · Luiz Capitulino
Quoted 43 lines · On Sat, 17 Oct 2009 08:36:02 -0500 Anthony Liguori <[email protected]> wrote:

qfloat is a qobject wrapper for double precision floating points

---
 Makefile  |    3 +-
 qfloat.c  |   76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qfloat.h  |   29 +++++++++++++++++++++++
 qobject.h |    1 +
 4 files changed, 108 insertions(+), 1 deletions(-)
 create mode 100644 qfloat.c
 create mode 100644 qfloat.h
Makefile +2 -1
index e78a3d0..9294638 100644
@@ -125,7 +125,8 @@ obj-y += net.o net-queue.o
 obj-y += qemu-char.o aio.o net-checksum.o savevm.o
 obj-y += msmouse.o ps2.o
 obj-y += qdev.o qdev-properties.o
-obj-y += qint.o qstring.o qdict.o qlist.o qemu-config.o
+obj-y += qint.o qstring.o qdict.o qlist.o qfloat.o
+obj-y += qemu-config.o
 
 obj-$(CONFIG_BRLAPI) += baum.o
 obj-$(CONFIG_WIN32) += tap-win32.o
qfloat.c new file +10 -0
new file mode 100644
index 0000000..05215f5
@@ -0,0 +1,76 @@
+/*
+ * QFloat Module
+ *
+ * Copyright (C) 2009 Red Hat Inc.
+ *
+ * Authors:
+ *  Luiz Capitulino <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.

qfloat.c line 20

Quoted 10 lines
qfloat.c new file +10 -0
+ *
+ * Copyright IBM, Corp. 2009
+ *
+ * Authors:
+ *  Anthony Liguori   <[email protected]>
+ *
+ * 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.
+ *
+ */

I see you're double licensing it to respect my initial choice, but you
can leave only LGPL for this new code.

Quoted 2 lines

I will send patches changing the ones written by me.

Great, I'm hoping we can make the whole thing LGPL 2.1+ so that we can allow libraries like libvirt to use it.

Regards,

Anthony Liguori

Signature · 3 lines
Regards,

Anthony Liguori
#7 lore raw

Re: [Qemu-devel] [PATCH 02/11] Add support for qfloat

Quoted 14 lines · Anthony Liguori
Quoted 14 lines
Quoted 13 lines

+ * This work is licensed under the terms of the GNU GPL, version 2. See
+ * the COPYING file in the top-level directory.
+ *
+ * Copyright IBM, Corp. 2009
+ *
+ * Authors:
+ * Anthony Liguori <[email protected]>
+ *
+ * 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.
+ *
+ */

Quoted 7 lines
Quoted 5 lines

I see you're double licensing it to respect my initial choice, but you
can leave only LGPL for this new code.

I will send patches changing the ones written by me.

Great, I'm hoping we can make the whole thing LGPL 2.1+ so that we can allow libraries like libvirt to use it.

Why 'or later'? Let's just stick to a version that we know and accept. If you end up later liking version 3, you can change the text then.

Amit

#8 lore raw

Re: [Qemu-devel] [PATCH 02/11] Add support for qfloat

Quoted 19 lines · Amit Shah
Quoted 17 lines · Anthony Liguori
Quoted 17 lines
Quoted 4 lines

+ * This work is licensed under the terms of the GNU GPL, version 2. See
+ * the COPYING file in the top-level directory.
+ *
+ * Copyright IBM, Corp. 2009

Quoted 10 lines

+ *
+ * Authors:
+ * Anthony Liguori <[email protected]>
+ *
+ * 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.
+ *
+ */

I see you're double licensing it to respect my initial choice, but you
can leave only LGPL for this new code.

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

I will send patches changing the ones written by me.

Great, I'm hoping we can make the whole thing LGPL 2.1+ so that we can allow libraries like libvirt to use it.

Why 'or later'? Let's just stick to a version that we know and accept. If you end up later liking version 3, you can change the text then.

It really doesn't matter in the context of LGPL so I don't mind if we do 2.1 only.

Signature · 3 lines
Regards,

Anthony Liguori
#9 lore raw

Re: [Qemu-devel] [PATCH 02/11] Add support for qfloat

Quoted 25 lines · Anthony Liguori
Quoted 24 lines · Amit Shah
Quoted 21 lines · Anthony Liguori
Quoted 13 lines
Quoted 4 lines

+ * This work is licensed under the terms of the GNU GPL, version 2. See
+ * the COPYING file in the top-level directory.
+ *
+ * Copyright IBM, Corp. 2009

Quoted 9 lines

+ *
+ * Authors:
+ * Anthony Liguori <[email protected]>
+ *
+ * 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.
+ *
+ */

Quoted 5 lines

I see you're double licensing it to respect my initial choice, but you
can leave only LGPL for this new code.

I will send patches changing the ones written by me.

Great, I'm hoping we can make the whole thing LGPL 2.1+ so that we can allow libraries like libvirt to use it.

Quoted 6 lines
Quoted 3 lines

Why 'or later'? Let's just stick to a version that we know and accept. If you end up later liking version 3, you can change the text then.

It really doesn't matter in the context of LGPL so I don't mind if we do 2.1 only.

Yeah; better a known evil than an unknown one :-)

Amit

#10 lore raw

Re: [Qemu-devel] [PATCH 02/11] Add support for qfloat

In reply to Anthony Liguori
Quoted 2 lines · Anthony Liguori

It really doesn't matter in the context of LGPL so I don't mind if we do 2.1 only.

Is LGPL 2.1 compatible with LGPL 3 or GPL 3?

It would be a shame if it's compatible enough to use in libvirt but can't be used in a GPL 3 project.

I think the recent JSON + QObject stuff is nice enough it may be useful in other projects that have nothing to do with QEMU.

-- Jamie

#11 lore raw

Re: [Qemu-devel] [PATCH 02/11] Add support for qfloat

Quoted 8 lines · Jamie Lokier
Quoted 2 lines · Anthony Liguori

It really doesn't matter in the context of LGPL so I don't mind if we do 2.1 only.

Is LGPL 2.1 compatible with LGPL 3 or GPL 3?

It would be a shame if it's compatible enough to use in libvirt but can't be used in a GPL 3 project.

"LGPL 2.1 only" is not suitable for libvirt actually. Our license is explicitly using "LGPL v2.1 or later" so that we can be compatible with apps using (L)GPL3 too. So yeah I'd really recommend using 2.1+ too

Regards,
Daniel

Signature · 4 lines
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
#12 lore raw

PATCH 3/11 Add a test case for qfloat

In reply to Anthony Liguori
---
 Makefile       |    2 +
 check-qfloat.c |   81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 configure      |    2 +-
 3 files changed, 84 insertions(+), 1 deletions(-)
 create mode 100644 check-qfloat.c
Makefile +2 -0
index 9294638..aedb6c7 100644
@@ -216,6 +216,8 @@ check-qint: check-qint.o qint.o qemu-malloc.o
 check-qstring: check-qstring.o qstring.o qemu-malloc.o
 check-qdict: check-qdict.o qdict.o qint.o qstring.o qemu-malloc.o
 check-qlist: check-qlist.o qlist.o qint.o qemu-malloc.o
+check-qfloat: check-qfloat.o qfloat.o qemu-malloc.o
+
 
 clean:
 # avoid old build problems by removing potentially incorrect old files
check-qfloat.c new file +81 -0

81 lines not on this page · raw message

configure +1 -1
index ca6d45c..3e6f980 100755
@@ -2024,7 +2024,7 @@ if test `expr "$target_list" : ".*softmmu.*"` != 0 ; then
   if [ "$linux" = "yes" ] ; then
       tools="qemu-nbd\$(EXESUF) qemu-io\$(EXESUF) $tools"
     if [ "$check_utests" = "yes" ]; then
-      tools="check-qint check-qstring check-qdict check-qlist $tools"
+      tools="check-qint check-qstring check-qdict check-qlist check-qfloat $tools"
     fi
   elif test "$mingw32" = "yes" ; then
       tools="qemu-io\$(EXESUF) $tools"
Signature · 1 line
1.6.2.5
#13 lore raw

Re: [Qemu-devel] [PATCH 03/11] Add a test case for qfloat

Quoted 4 lines · Anthony Liguori
---
 Makefile       |    2 +
 check-qfloat.c |   81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Hello Anthony,

Shouldn't we put these tests under tests/something/ ?

Cheers

Quoted 122 lines
 configure      |    2 +-
 3 files changed, 84 insertions(+), 1 deletions(-)
 create mode 100644 check-qfloat.c
Makefile +2 -0
index 9294638..aedb6c7 100644
@@ -216,6 +216,8 @@ check-qint: check-qint.o qint.o qemu-malloc.o
 check-qstring: check-qstring.o qstring.o qemu-malloc.o
 check-qdict: check-qdict.o qdict.o qint.o qstring.o qemu-malloc.o
 check-qlist: check-qlist.o qlist.o qint.o qemu-malloc.o
+check-qfloat: check-qfloat.o qfloat.o qemu-malloc.o
+
 
 clean:
 # avoid old build problems by removing potentially incorrect old files
check-qfloat.c new file +81 -0
new file mode 100644
index 0000000..3758700
@@ -0,0 +1,81 @@
+/*
+ * QFloat unit-tests.
+ *
+ * Copyright (C) 2009 Red Hat Inc.
+ *
+ * Authors:
+ *  Luiz Capitulino <[email protected]>
+ *
+ * Copyright IBM, Corp. 2009
+ *
+ * Authors:
+ *  Anthony Liguori   <[email protected]>
+ *
+ * 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.
+ *
+ */
+#include <check.h>
+
+#include "qfloat.h"
+#include "qemu-common.h"
+
+/*
+ * Public Interface test-cases
+ *
+ * (with some violations to access 'private' data)
+ */
+
+START_TEST(qfloat_from_double_test)
+{
+    QFloat *qf;
+    const double value = -42.23423;
+
+    qf = qfloat_from_double(value);
+    fail_unless(qf != NULL);
+    fail_unless(qf->value == value);
+    fail_unless(qf->base.refcnt == 1);
+    fail_unless(qobject_type(QOBJECT(qf)) == QTYPE_QFLOAT);
+
+    // destroy doesn't exit yet
+    qemu_free(qf);
+}
+END_TEST
+
+START_TEST(qfloat_destroy_test)
+{
+    QFloat *qf = qfloat_from_double(0.0);
+    QDECREF(qf);
+}
+END_TEST
+
+static Suite *qfloat_suite(void)
+{
+    Suite *s;
+    TCase *qfloat_public_tcase;
+
+    s = suite_create("QFloat test-suite");
+
+    qfloat_public_tcase = tcase_create("Public Interface");
+    suite_add_tcase(s, qfloat_public_tcase);
+    tcase_add_test(qfloat_public_tcase, qfloat_from_double_test);
+    tcase_add_test(qfloat_public_tcase, qfloat_destroy_test);
+
+    return s;
+}
+
+int main(void)
+{
+    int nf;
+    Suite *s;
+    SRunner *sr;
+
+    s = qfloat_suite();
+    sr = srunner_create(s);
+
+    srunner_run_all(sr, CK_NORMAL);
+    nf = srunner_ntests_failed(sr);
+    srunner_free(sr);
+
+    return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
+}
configure +1 -1
index ca6d45c..3e6f980 100755
@@ -2024,7 +2024,7 @@ if test `expr "$target_list" : ".*softmmu.*"` != 0 ; then
   if [ "$linux" = "yes" ] ; then
       tools="qemu-nbd\$(EXESUF) qemu-io\$(EXESUF) $tools"
     if [ "$check_utests" = "yes" ]; then
-      tools="check-qint check-qstring check-qdict check-qlist $tools"
+      tools="check-qint check-qstring check-qdict check-qlist check-qfloat $tools"
     fi
   elif test "$mingw32" = "yes" ; then
       tools="qemu-io\$(EXESUF) $tools"
Signature · 1 line
1.6.2.5
#14 lore raw

Re: [Qemu-devel] [PATCH 03/11] Add a test case for qfloat

Quoted 9 lines · Edgar E. Iglesias
Quoted 5 lines · Anthony Liguori
---
 Makefile       |    2 +
 check-qfloat.c |   81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Quoted 4 lines

Hello Anthony,

Shouldn't we put these tests under tests/something/ ?

Indeed, I think that would clean up things a bit too as the configure/Makefile integration is a bit awkward. I'll take a look.

Regards,

Anthony Liguori

#15 lore raw

PATCH 4/11 Add json->qobject parser

In reply to Anthony Liguori

This implements a json parser that generates qobjects. The intention is that this all can be eventually moved to a shared library to form the basis of the QMP client API. Unfortunately, qobject is currently GPL licensed so hopefully Luiz will switch it to LGPL.

The parser is a pretty straight forward recursive decent parser. It's based on the grammar available at json.org. The only thing it doesn't implement is support for the 'null' keyword. We have to decide how we want to implement it.

It also introduces an extension to json--the ability to support strings quoted with a single quote (similar to python). This makes writing dictionaries considerably nicer in C.

This module also introduces a varargs parsing function that allows a printf-like format to be used with the parsing function.

Finally, the parser returns the amount of input consumed. This is to make it possible to determine the message boundaries in a stream of input.

---
 Makefile |    2 +-
 qjson.c  |  695 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qjson.h  |   23 ++
 3 files changed, 719 insertions(+), 1 deletions(-)
 create mode 100644 qjson.c
 create mode 100644 qjson.h
Makefile +1 -1
index aedb6c7..1a99f66 100644
@@ -125,7 +125,7 @@ obj-y += net.o net-queue.o
 obj-y += qemu-char.o aio.o net-checksum.o savevm.o
 obj-y += msmouse.o ps2.o
 obj-y += qdev.o qdev-properties.o
-obj-y += qint.o qstring.o qdict.o qlist.o qfloat.o
+obj-y += qint.o qstring.o qdict.o qlist.o qfloat.o qjson.o
 obj-y += qemu-config.o
 
 obj-$(CONFIG_BRLAPI) += baum.o
qjson.c new file +695 -0

695 lines not on this page · raw message

qjson.h new file +23 -0
new file mode 100644
index 0000000..0c94954
@@ -0,0 +1,23 @@
+/*
+ * QJSON Module
+ *
+ * Copyright IBM, Corp. 2009
+ *
+ * Authors:
+ *  Anthony Liguori   <[email protected]>
+ *
+ * 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.
+ *
+ */
+
+#ifndef QJSON_H
+#define QJSON_H
+
+#include "qobject.h"
+
+QObject *qobject_from_json(const char *string, size_t *length);
+QObject *qobject_from_jsonf(const char *string, size_t *length, ...)
+    __attribute__((__format__ (__printf__, 1, 3)));
+
+#endif /* QJSON_H */
Signature · 1 line
1.6.2.5
#16 lore raw

Re: [Qemu-devel] [PATCH 04/11] Add json->qobject parser

qjson.h line 21

Quoted 3 lines · On Sat, 17 Oct 2009 08:36:04 -0500 Anthony Liguori <[email protected]> wrote:
qjson.h new file +3 -0
+QObject *qobject_from_json(const char *string, size_t *length);
+QObject *qobject_from_jsonf(const char *string, size_t *length, ...)
+    __attribute__((__format__ (__printf__, 1, 3)));

We need a wrapper for command handlers usage, they won't use 'length' and
I think we can abort() on error if the only possibility of failing is a wrong syntax.

#17 lore raw

PATCH 5/11 Add unit test for json parser

In reply to Anthony Liguori

I've attempted to make this a very thorough unit test for the json parser. It covers every rule and tries to test the corner cases of each rule.

There's some interesting things in this test case like a literal qobject type. It may be worth extract that into common code.

---
 Makefile      |    2 +-
 check-qjson.c |  586 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 configure     |    2 +-
 3 files changed, 588 insertions(+), 2 deletions(-)
 create mode 100644 check-qjson.c
Makefile +1 -1
index 1a99f66..8b80150 100644
@@ -217,7 +217,7 @@ check-qstring: check-qstring.o qstring.o qemu-malloc.o
 check-qdict: check-qdict.o qdict.o qint.o qstring.o qemu-malloc.o
 check-qlist: check-qlist.o qlist.o qint.o qemu-malloc.o
 check-qfloat: check-qfloat.o qfloat.o qemu-malloc.o
-
+check-qjson: check-qjson.o qjson.o qstring.o qint.o qdict.o qlist.o qfloat.o qemu-malloc.o
 
 clean:
 # avoid old build problems by removing potentially incorrect old files
check-qjson.c new file +586 -0

586 lines not on this page · raw message

configure +1 -1
index 3e6f980..24caaac 100755
@@ -2024,7 +2024,7 @@ if test `expr "$target_list" : ".*softmmu.*"` != 0 ; then
   if [ "$linux" = "yes" ] ; then
       tools="qemu-nbd\$(EXESUF) qemu-io\$(EXESUF) $tools"
     if [ "$check_utests" = "yes" ]; then
-      tools="check-qint check-qstring check-qdict check-qlist check-qfloat $tools"
+      tools="check-qint check-qstring check-qdict check-qlist check-qfloat check-qjson $tools"
     fi
   elif test "$mingw32" = "yes" ; then
       tools="qemu-io\$(EXESUF) $tools"
Signature · 1 line
1.6.2.5
#18 lore raw

PATCH 6/11 qobject: add QBool type

In reply to Anthony Liguori

We currently model as json bool as an int. This works fine on the server side but it means we cannot send back proper bools to the client. Introducing a proper QBool type fixes that.

---
 Makefile  |    2 +-
 qbool.c   |   76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qbool.h   |   29 +++++++++++++++++++++++
 qobject.h |    1 +
 4 files changed, 107 insertions(+), 1 deletions(-)
 create mode 100644 qbool.c
 create mode 100644 qbool.h
Makefile +1 -1
index 8b80150..095d2ce 100644
@@ -125,7 +125,7 @@ obj-y += net.o net-queue.o
 obj-y += qemu-char.o aio.o net-checksum.o savevm.o
 obj-y += msmouse.o ps2.o
 obj-y += qdev.o qdev-properties.o
-obj-y += qint.o qstring.o qdict.o qlist.o qfloat.o qjson.o
+obj-y += qint.o qstring.o qdict.o qlist.o qfloat.o qbool.o qjson.o
 obj-y += qemu-config.o
 
 obj-$(CONFIG_BRLAPI) += baum.o
qbool.c new file +76 -0

76 lines not on this page · raw message

qbool.h new file +29 -0
new file mode 100644
index 0000000..fe66fcd
@@ -0,0 +1,29 @@
+/*
+ * QBool Module
+ *
+ * Copyright IBM, Corp. 2009
+ *
+ * Authors:
+ *  Anthony Liguori   <[email protected]>
+ *
+ * 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.
+ *
+ */
+
+#ifndef QBOOL_H
+#define QBOOL_H
+
+#include <stdint.h>
+#include "qobject.h"
+
+typedef struct QBool {
+    QObject_HEAD;
+    int value;
+} QBool;
+
+QBool *qbool_from_int(int value);
+int qbool_get_int(const QBool *qb);
+QBool *qobject_to_qbool(const QObject *obj);
+
+#endif /* QBOOL_H */
qobject.h +1 -0
index da03a5e..167b607 100644
@@ -42,6 +42,7 @@ typedef enum {
     QTYPE_QDICT,
     QTYPE_QLIST,
     QTYPE_QFLOAT,
+    QTYPE_QBOOL,
 } qtype_code;
 
 struct QObject;
Signature · 1 line
1.6.2.5
#19 lore raw

Re: [Qemu-devel] [PATCH 06/11] qobject: add QBool type

Quoted 3 lines · On Sat, 17 Oct 2009 08:36:06 -0500 Anthony Liguori <[email protected]> wrote:

We currently model as json bool as an int. This works fine on the server side but it means we cannot send back proper bools to the client. Introducing a proper QBool type fixes that.

As we talked earlier today, I think it would be simpler to have QTrue,
QFalse and QNull as static objects.

We could define that static objects get reference counting disabled, I don't
see issues in doing that and we get a very simple model.

The big patch bellow does it, only compiled tested though.

If you don't agree with this model, we'll have to allocate QNull objects.
Actually I would suggest a destroy() method which always resets the refcount to 1, but then it will have the concurrency problems on threaded applications you've mentioned.

Makefile +1 -1
index e78a3d0..1f985cf 100644
@@ -125,7 +125,7 @@ obj-y += net.o net-queue.o
 obj-y += qemu-char.o aio.o net-checksum.o savevm.o
 obj-y += msmouse.o ps2.o
 obj-y += qdev.o qdev-properties.o
-obj-y += qint.o qstring.o qdict.o qlist.o qemu-config.o
+obj-y += qint.o qstring.o qdict.o qlist.o qmisc.o qemu-config.o
 
 obj-$(CONFIG_BRLAPI) += baum.o
 obj-$(CONFIG_WIN32) += tap-win32.o
qmisc.c new file +31 -0
new file mode 100644
index 0000000..38a87dd
@@ -0,0 +1,31 @@
+#include "qmisc.h"
+
+/*
+ * QBool
+ */
+
+static const QType qbool_type = {
+    .code = QTYPE_QBOOL,
+};
+
+const QBool QTrue = {
+     QOBJECT_INIT_STATIC(&qbool_type),
+     .value = 1
+};
+
+const QBool QFalse = {
+     QOBJECT_INIT_STATIC(&qbool_type),
+     .value = 0
+};
+
+/*
+ * QNull
+ */
+
+static const QType qnull_type = {
+    .code = QTYPE_QNULL,
+};
+
+const qnull QNull = {
+     QOBJECT_INIT_STATIC(&qnull_type),
+};
qmisc.h new file +38 -0
new file mode 100644
index 0000000..dd9d5a9
@@ -0,0 +1,38 @@
+#ifndef QMISC_H
+#define QMISC_H
+
+#include "qobject.h"
+
+/*
+ * QBool
+ */
+
+typedef struct QBool {
+    QObject_HEAD;
+    int value;
+} QBool;
+
+extern const QBool QTrue;
+extern const QBool QFalse;
+
+static inline int qbool_is_true(const QBool *qbool)
+{
+    return qbool->value;
+}
+
+static inline int qbool_is_false(const QBool *qbool)
+{
+    return !qbool->value;
+}
+
+/*
+ * QNull
+ */
+
+typedef struct qnull {
+    QObject_HEAD;
+} qnull;
+
+extern const qnull QNull;
+
+#endif /* QMISC_H */
qobject.h +7 -4
index 4cc9287..81e4d94 100644
@@ -41,6 +41,8 @@ typedef enum {
     QTYPE_QSTRING,
     QTYPE_QDICT,
     QTYPE_QLIST,
+    QTYPE_QBOOL,
+    QTYPE_QNULL,
 } qtype_code;
 
 struct QObject;
@@ -75,12 +77,15 @@ typedef struct QObject {
     obj->base.refcnt = 1;               \
     obj->base.type   = qtype_type
 
+#define QOBJECT_INIT_STATIC(qtype_type) \
+    .base.type = qtype_type
+
 /**
  * qobject_incref(): Increment QObject's reference count
  */
 static inline void qobject_incref(QObject *obj)
 {
-    if (obj)
+    if (obj && obj->type->destroy)
         obj->refcnt++;
 }
 
@@ -90,9 +95,7 @@ static inline void qobject_incref(QObject *obj)
  */
 static inline void qobject_decref(QObject *obj)
 {
-    if (obj && --obj->refcnt == 0) {
-        assert(obj->type != NULL);
-        assert(obj->type->destroy != NULL);
+    if (obj && obj->type->destroy && --obj->refcnt == 0) {
         obj->type->destroy(obj);
     }
 }
#20 lore raw

Re: [Qemu-devel] [PATCH 06/11] qobject: add QBool type

Quoted 15 lines · Luiz Capitulino
Quoted 4 lines · On Sat, 17 Oct 2009 08:36:06 -0500 Anthony Liguori <[email protected]> wrote:

We currently model as json bool as an int. This works fine on the server side but it means we cannot send back proper bools to the client. Introducing a proper QBool type fixes that.

As we talked earlier today, I think it would be simpler to have QTrue,
QFalse and QNull as static objects.

We could define that static objects get reference counting disabled, I don't
see issues in doing that and we get a very simple model.

Quoted 7 lines

The big patch bellow does it, only compiled tested though.

If you don't agree with this model, we'll have to allocate QNull objects.
Actually I would suggest a destroy() method which always resets the refcount to 1, but then it will have the concurrency problems on threaded applications you've mentioned.

A lot of object systems have dealt with this in various ways. The trouble with reference counting is usually thread safety. CLR goes to great lengths to make their objects reference counting thread safe and it comes at a cost. Python doesn't do anything explicit and it's one of the reasons that they're stuck with a big lock.

Since I'd like to expose QObject to client libraries, having a single static instance would be pretty unfortunate as it would imply a weird locking semantics. Disabling reference counts for QNull/QBool seems unfortunate because it suggests that different semantics for certain objects.

I think this is a premature optimization. I don't think we need to try and save the memory here.

Regards,

Anthony Liguori

#21 lore raw

Re: [Qemu-devel] [PATCH 06/11] qobject: add QBool type

Quoted 30 lines · On Mon, 19 Oct 2009 09:17:38 -0500 Anthony Liguori <[email protected]> wrote:
Quoted 15 lines · Luiz Capitulino
Quoted 4 lines · On Sat, 17 Oct 2009 08:36:06 -0500 Anthony Liguori <[email protected]> wrote:

We currently model as json bool as an int. This works fine on the server side but it means we cannot send back proper bools to the client. Introducing a proper QBool type fixes that.

As we talked earlier today, I think it would be simpler to have QTrue,
QFalse and QNull as static objects.

We could define that static objects get reference counting disabled, I don't
see issues in doing that and we get a very simple model.

Quoted 7 lines

The big patch bellow does it, only compiled tested though.

If you don't agree with this model, we'll have to allocate QNull objects.
Actually I would suggest a destroy() method which always resets the refcount to 1, but then it will have the concurrency problems on threaded applications you've mentioned.

A lot of object systems have dealt with this in various ways. The trouble with reference counting is usually thread safety. CLR goes to great lengths to make their objects reference counting thread safe and it comes at a cost. Python doesn't do anything explicit and it's one of the reasons that they're stuck with a big lock.

Quoted 8 lines

Since I'd like to expose QObject to client libraries, having a single static instance would be pretty unfortunate as it would imply a weird locking semantics. Disabling reference counts for QNull/QBool seems unfortunate because it suggests that different semantics for certain objects.

I think this is a premature optimization. I don't think we need to try and save the memory here.

You can add QNull in your series then.

#22 lore raw

PATCH 7/11 qjson: Use QBool for true/false keywords

In reply to Anthony Liguori

Instead of parsing true/false as QInt, parse them as QBool and update the test suite accordingly.

---
 Makefile      |    2 +-
 check-qjson.c |   19 ++++++++++---------
 qjson.c       |    5 +++--
 3 files changed, 14 insertions(+), 12 deletions(-)
Makefile +1 -1
index 095d2ce..fe6c9bf 100644
@@ -217,7 +217,7 @@ check-qstring: check-qstring.o qstring.o qemu-malloc.o
 check-qdict: check-qdict.o qdict.o qint.o qstring.o qemu-malloc.o
 check-qlist: check-qlist.o qlist.o qint.o qemu-malloc.o
 check-qfloat: check-qfloat.o qfloat.o qemu-malloc.o
-check-qjson: check-qjson.o qjson.o qstring.o qint.o qdict.o qlist.o qfloat.o qemu-malloc.o
+check-qjson: check-qjson.o qjson.o qstring.o qint.o qdict.o qlist.o qfloat.o qbool.o qemu-malloc.o
 
 clean:
 # avoid old build problems by removing potentially incorrect old files
check-qjson.c +10 -9
index 0f661b9..32aa33c 100644
@@ -15,6 +15,7 @@
 #include "qdict.h"
 #include "qlist.h"
 #include "qfloat.h"
+#include "qbool.h"
 #include "qjson.h"
 
 #include "qemu-common.h"
@@ -259,28 +260,28 @@ END_TEST
 START_TEST(keyword_literal)
 {
     QObject *obj;
-    QInt *qint;
+    QBool *qbool;
     size_t length = 0;
 
     obj = qobject_from_json("true", &length);
     fail_unless(obj != NULL);
-    fail_unless(qobject_type(obj) == QTYPE_QINT);
+    fail_unless(qobject_type(obj) == QTYPE_QBOOL);
     fail_unless(length == 4);
 
-    qint = qobject_to_qint(obj);
-    fail_unless(qint_get_int(qint) != 0);
+    qbool = qobject_to_qbool(obj);
+    fail_unless(qbool_get_int(qbool) != 0);
 
-    QDECREF(qint);
+    QDECREF(qbool);
 
     obj = qobject_from_json("false", &length);
     fail_unless(obj != NULL);
-    fail_unless(qobject_type(obj) == QTYPE_QINT);
+    fail_unless(qobject_type(obj) == QTYPE_QBOOL);
     fail_unless(length == 5);
 
-    qint = qobject_to_qint(obj);
-    fail_unless(qint_get_int(qint) == 0);
+    qbool = qobject_to_qbool(obj);
+    fail_unless(qbool_get_int(qbool) == 0);
 
-    QDECREF(qint);
+    QDECREF(qbool);
 }
 END_TEST
 
qjson.c +3 -2
index 64cecc6..ce2c942 100644
@@ -17,6 +17,7 @@
 #include "qdict.h"
 #include "qlist.h"
 #include "qfloat.h"
+#include "qbool.h"
 #include "qjson.h"
 
 typedef struct JSONParserContext
@@ -627,9 +628,9 @@ static QObject *parse_keyword(JSONParserContext *ctxt, const char *data, size_t
     }
 
     if (strcmp(qstring_get_str(str), "true") == 0) {
-        obj = QOBJECT(qint_from_int(1));
+        obj = QOBJECT(qbool_from_int(1));
     } else if (strcmp(qstring_get_str(str), "false") == 0) {
-        obj = QOBJECT(qint_from_int(0));
+        obj = QOBJECT(qbool_from_int(0));
     }
 
     if (obj) {
Signature · 1 line
1.6.2.5
#23 lore raw

PATCH 8/11 qjson: add %i for parsing bools

In reply to Anthony Liguori

Not a wonderful choice in conversion characters but nothing else seems better.

---
 qjson.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
qjson.c +6 -0
index ce2c942..9279bfb 100644
@@ -339,6 +339,12 @@ static QObject *parse_number(JSONParserContext *ctxt,
                 parse_error(ctxt, ptr, "invalid escape sequence");
                 goto out;
             }
+        } else if (*ptr == 'i') {
+            int val;
+            ptr++;
+            *length = ptr - data;
+            val = va_arg(*ap, int);
+            return QOBJECT(qbool_from_int(val));
         } else if (*ptr == 'f') {
             double val;
             ptr++;
Signature · 1 line
1.6.2.5
#24 lore raw

PATCH 9/11 qjson: add unit test for varargs bool parsing

In reply to Anthony Liguori
---
 check-qjson.c |   21 +++++++++++++++++++++
 1 files changed, 21 insertions(+), 0 deletions(-)
check-qjson.c +21 -0
index 32aa33c..8760941 100644
@@ -9,6 +9,7 @@
  *
  */
 #include <check.h>
+#include <stdbool.h>
 
 #include "qstring.h"
 #include "qint.h"
@@ -282,6 +283,26 @@ START_TEST(keyword_literal)
     fail_unless(qbool_get_int(qbool) == 0);
 
     QDECREF(qbool);
+
+    obj = qobject_from_jsonf("%i", &length, false);
+    fail_unless(obj != NULL);
+    fail_unless(qobject_type(obj) == QTYPE_QBOOL);
+    fail_unless(length == 2);
+
+    qbool = qobject_to_qbool(obj);
+    fail_unless(qbool_get_int(qbool) == 0);
+
+    QDECREF(qbool);
+    
+    obj = qobject_from_jsonf("%i", &length, true);
+    fail_unless(obj != NULL);
+    fail_unless(qobject_type(obj) == QTYPE_QBOOL);
+    fail_unless(length == 2);
+
+    qbool = qobject_to_qbool(obj);
+    fail_unless(qbool_get_int(qbool) != 0);
+
+    QDECREF(qbool);
 }
 END_TEST
 
Signature · 1 line
1.6.2.5
#25 lore raw

PATCH 10/11 qjson: add vararg format for embedded qobjects

In reply to Anthony Liguori

%p lets you specify an already constructed qobject. It takes a weak reference.

---
 qjson.c |   25 +++++++++++++++++++++++++
 1 files changed, 25 insertions(+), 0 deletions(-)
qjson.c +25 -0
index 9279bfb..7bedaf3 100644
@@ -648,6 +648,28 @@ static QObject *parse_keyword(JSONParserContext *ctxt, const char *data, size_t
     return obj;
 }
 
+static QObject *parse_qobject(JSONParserContext *ctxt, const char *data, size_t *length, va_list *ap)
+{
+    const char *ptr = data;
+    QObject *obj = NULL;
+
+    ptr += parse_skip(ctxt, ptr);
+
+    if (ap && *ptr == '%') {
+        ptr++;
+        if (*ptr == 'p') {
+            ptr++;
+
+            obj = va_arg(*ap, QObject *);
+            qobject_incref(obj);
+            *length = (ptr - data);
+            return obj;
+        }
+    }
+
+    return NULL;
+}
+
 static QObject *parse_value(JSONParserContext *ctxt, const char *string, size_t *length, va_list *ap)
 {
     QObject *obj;
@@ -665,6 +687,9 @@ static QObject *parse_value(JSONParserContext *ctxt, const char *string, size_t
     if (obj == NULL) {
         obj = parse_keyword(ctxt, string, length);
     }
+    if (obj == NULL) {
+        obj = parse_qobject(ctxt, string, length, ap);
+    }
 
     return obj;
 }
Signature · 1 line
1.6.2.5
#26 lore raw

PATCH 11/11 qjson: add unit test to check %p format

In reply to Anthony Liguori
---
 check-qjson.c |   33 ++++++++++++++++++++++++++++++++-
 1 files changed, 32 insertions(+), 1 deletions(-)
check-qjson.c +32 -1
index 8760941..9be1448 100644
@@ -551,11 +551,38 @@ START_TEST(simple_whitespace)
 }
 END_TEST
 
+START_TEST(simple_varargs)
+{
+    QObject *embedded_obj;
+    QObject *obj;
+    LiteralQObject decoded = QLIT_QLIST(((LiteralQObject[]){
+            QLIT_QINT(1),
+            QLIT_QINT(2),
+            QLIT_QLIST(((LiteralQObject[]){
+                        QLIT_QINT(32),
+                        QLIT_QINT(42),
+                        {}})),
+            {}}));
+
+    embedded_obj = qobject_from_json("[32, 42]", NULL);
+    fail_unless(embedded_obj != NULL);
+
+    obj = qobject_from_jsonf("[%d, 2, %p]", NULL, 1, embedded_obj);
+    fail_unless(obj != NULL);
+
+    qobject_decref(embedded_obj);
+
+    fail_unless(compare_litqobj_to_qobj(&decoded, obj) == 1);
+
+    qobject_decref(obj);
+}
+END_TEST
+
 static Suite *qjson_suite(void)
 {
     Suite *suite;
     TCase *string_literals, *number_literals, *keyword_literals;
-    TCase *dicts, *lists, *whitespace;
+    TCase *dicts, *lists, *whitespace, *varargs;
 
     string_literals = tcase_create("String Literals");
     tcase_add_test(string_literals, simple_string);
@@ -580,6 +607,9 @@ static Suite *qjson_suite(void)
     whitespace = tcase_create("Whitespace");
     tcase_add_test(whitespace, simple_whitespace);
 
+    varargs = tcase_create("Varargs");
+    tcase_add_test(varargs, simple_varargs);
+
     suite = suite_create("QJSON test-suite");
     suite_add_tcase(suite, string_literals);
     suite_add_tcase(suite, number_literals);
@@ -587,6 +617,7 @@ static Suite *qjson_suite(void)
     suite_add_tcase(suite, dicts);
     suite_add_tcase(suite, lists);
     suite_add_tcase(suite, whitespace);
+    suite_add_tcase(suite, varargs);
 
     return suite;
 }
Signature · 1 line
1.6.2.5
#27 lore raw
In reply to Anthony Liguori
Quoted 2 lines · On Sat, 17 Oct 2009 08:36:00 -0500 Anthony Liguori <[email protected]> wrote:

I fat-fingered the git send-email command the first time so here's the second iteration.

Got it now..

Quoted 8 lines

This series introduces a json parsing framework. The parser is a recursive decent parser and implements the full json spec except for the null keyword. We need to decide how we want to model null in order to support this.

In addition, we implement an extension to allow single quote strings. This is very useful for defining dictionaries in C.

The parser has a full test suite and introduces a boolean and float type.

Very nice, this is obviously way beyond the simple approach I was following,
I have some minor comments and will reply some patches. Will also run some tests later today, but I don't think I'll find any issue (given that you have written unit-tests!).

What's you merge plan for your own patches? :)