[libvirt] [PATCH v2] Add support for YAJL version 2 API/ABI

Daniel P. Berrange berrange at redhat.com
Mon May 9 16:50:49 UTC 2011


Version 2.0.0 or yajl changed API. It is fairly trivial for us to
cope with both APIs in libvirt, so adapt.

* configure.ac: Probe for yajl2 API
* src/util/json.c: Conditional support for yajl2 API
---
 configure.ac    |    8 ++++++
 src/util/json.c |   74 +++++++++++++++++++++++++++++++++++++++++-------------
 2 files changed, 64 insertions(+), 18 deletions(-)

diff --git a/configure.ac b/configure.ac
index dcec371..62c0560 100644
--- a/configure.ac
+++ b/configure.ac
@@ -878,6 +878,7 @@ AC_ARG_WITH([yajl],
 
 YAJL_CFLAGS=
 YAJL_LIBS=
+with_yajl2=no
 if test "x$with_yajl" != "xno"; then
   if test "x$with_yajl" != "xyes" && test "x$with_yajl" != "xcheck"; then
     YAJL_CFLAGS="-I$with_yajl/include"
@@ -898,6 +899,9 @@ if test "x$with_yajl" != "xno"; then
     AC_CHECK_LIB([yajl], [yajl_parse],[
       YAJL_LIBS="$YAJL_LIBS -lyajl"
       with_yajl=yes
+      AC_CHECK_LIB([yajl], [yajl_tree_parse],[
+        with_yajl2=yes
+      ],[])
     ],[
       if test "x$with_yajl" = "xcheck" ; then
         with_yajl=no
@@ -914,6 +918,10 @@ if test "x$with_yajl" != "xno"; then
     AC_DEFINE_UNQUOTED([HAVE_YAJL], 1,
       [whether YAJL is available for JSON parsing/formatting])
   fi
+  if test "x$with_yajl2" = "xyes" ; then
+    AC_DEFINE_UNQUOTED([HAVE_YAJL2], 1,
+      [whether YAJL has API version 2])
+  fi
 fi
 AM_CONDITIONAL([HAVE_YAJL], [test "x$with_yajl" = "xyes"])
 AC_SUBST([YAJL_CFLAGS])
diff --git a/src/util/json.c b/src/util/json.c
index df4771d..8bbb87b 100644
--- a/src/util/json.c
+++ b/src/util/json.c
@@ -32,6 +32,13 @@
 #if HAVE_YAJL
 # include <yajl/yajl_gen.h>
 # include <yajl/yajl_parse.h>
+
+#ifdef HAVE_YAJL2
+#define yajl_size_t size_t
+#else
+#define yajl_size_t unsigned int
+#endif
+
 #endif
 
 /* XXX fixme */
@@ -672,7 +679,7 @@ static int virJSONParserInsertValue(virJSONParserPtr parser,
     return 0;
 }
 
-static int virJSONParserHandleNull(void * ctx)
+static int virJSONParserHandleNull(void *ctx)
 {
     virJSONParserPtr parser = ctx;
     virJSONValuePtr value = virJSONValueNewNull();
@@ -690,7 +697,7 @@ static int virJSONParserHandleNull(void * ctx)
     return 1;
 }
 
-static int virJSONParserHandleBoolean(void * ctx, int boolean_)
+static int virJSONParserHandleBoolean(void *ctx, int boolean_)
 {
     virJSONParserPtr parser = ctx;
     virJSONValuePtr value = virJSONValueNewBoolean(boolean_);
@@ -708,9 +715,9 @@ static int virJSONParserHandleBoolean(void * ctx, int boolean_)
     return 1;
 }
 
-static int virJSONParserHandleNumber(void * ctx,
-                                     const char * s,
-                                     unsigned int l)
+static int virJSONParserHandleNumber(void *ctx,
+                                     const char *s,
+                                     yajl_size_t l)
 {
     virJSONParserPtr parser = ctx;
     char *str = strndup(s, l);
@@ -734,9 +741,9 @@ static int virJSONParserHandleNumber(void * ctx,
     return 1;
 }
 
-static int virJSONParserHandleString(void * ctx,
-                                     const unsigned char * stringVal,
-                                     unsigned int stringLen)
+static int virJSONParserHandleString(void *ctx,
+                                     const unsigned char *stringVal,
+                                     yajl_size_t stringLen)
 {
     virJSONParserPtr parser = ctx;
     virJSONValuePtr value = virJSONValueNewStringLen((const char *)stringVal,
@@ -755,9 +762,9 @@ static int virJSONParserHandleString(void * ctx,
     return 1;
 }
 
-static int virJSONParserHandleMapKey(void * ctx,
-                                     const unsigned char * stringVal,
-                                     unsigned int stringLen)
+static int virJSONParserHandleMapKey(void *ctx,
+                                     const unsigned char *stringVal,
+                                     yajl_size_t stringLen)
 {
     virJSONParserPtr parser = ctx;
     virJSONParserStatePtr state;
@@ -776,7 +783,7 @@ static int virJSONParserHandleMapKey(void * ctx,
     return 1;
 }
 
-static int virJSONParserHandleStartMap(void * ctx)
+static int virJSONParserHandleStartMap(void *ctx)
 {
     virJSONParserPtr parser = ctx;
     virJSONValuePtr value = virJSONValueNewObject();
@@ -803,7 +810,7 @@ static int virJSONParserHandleStartMap(void * ctx)
 }
 
 
-static int virJSONParserHandleEndMap(void * ctx)
+static int virJSONParserHandleEndMap(void *ctx)
 {
     virJSONParserPtr parser = ctx;
     virJSONParserStatePtr state;
@@ -827,7 +834,7 @@ static int virJSONParserHandleEndMap(void * ctx)
     return 1;
 }
 
-static int virJSONParserHandleStartArray(void * ctx)
+static int virJSONParserHandleStartArray(void *ctx)
 {
     virJSONParserPtr parser = ctx;
     virJSONValuePtr value = virJSONValueNewArray();
@@ -853,7 +860,7 @@ static int virJSONParserHandleStartArray(void * ctx)
     return 1;
 }
 
-static int virJSONParserHandleEndArray(void * ctx)
+static int virJSONParserHandleEndArray(void *ctx)
 {
     virJSONParserPtr parser = ctx;
     virJSONParserStatePtr state;
@@ -895,14 +902,29 @@ static const yajl_callbacks parserCallbacks = {
 /* XXX add an incremental streaming parser - yajl trivially supports it */
 virJSONValuePtr virJSONValueFromString(const char *jsonstring)
 {
-    yajl_parser_config cfg = { 1, 1 };
     yajl_handle hand;
     virJSONParser parser = { NULL, NULL, 0 };
     virJSONValuePtr ret = NULL;
+#ifndef HAVE_YAJL2
+    yajl_parser_config cfg = { 1, 1 };
+#endif
 
     VIR_DEBUG("string=%s", jsonstring);
 
+#ifdef HAVE_YAJL2
+    hand = yajl_alloc(&parserCallbacks, NULL, &parser);
+    if (hand) {
+        yajl_config(hand, yajl_allow_comments, 1);
+        yajl_config(hand, yajl_dont_validate_strings, 0);
+    }
+#else
     hand = yajl_alloc(&parserCallbacks, &cfg, NULL, &parser);
+#endif
+    if (!hand) {
+        virJSONError(VIR_ERR_INTERNAL_ERROR, "%s",
+                     _("Unable to create JSON parser"));
+        goto cleanup;
+    }
 
     if (yajl_parse(hand,
                    (const unsigned char *)jsonstring,
@@ -1003,15 +1025,31 @@ static int virJSONValueToStringOne(virJSONValuePtr object,
 
 char *virJSONValueToString(virJSONValuePtr object)
 {
-    yajl_gen_config conf = { 0, " " }; /* Turns off pretty printing since QEMU can't cope */
     yajl_gen g;
     const unsigned char *str;
     char *ret = NULL;
-    unsigned int len;
+    yajl_size_t len;
+#ifndef HAVE_YAJL2
+    yajl_gen_config conf = { 0, " " }; /* Turns off pretty printing since QEMU can't cope */
+#endif
 
     VIR_DEBUG("object=%p", object);
 
+#ifdef HAVE_YAJL2
+    g = yajl_gen_alloc(NULL);
+    if (g) {
+        yajl_gen_config(g, yajl_gen_beautify, 0);
+        yajl_gen_config(g, yajl_gen_indent_string, " ");
+        yajl_gen_config(g, yajl_gen_validate_utf8, 1);
+    }
+#else
     g = yajl_gen_alloc(&conf, NULL);
+#endif
+    if (!g) {
+        virJSONError(VIR_ERR_INTERNAL_ERROR, "%s",
+                     _("Unable to create JSON formatter"));
+        goto cleanup;
+    }
 
     if (virJSONValueToStringOne(object, g) < 0) {
         virReportOOMError();
-- 
1.7.4.4




More information about the libvir-list mailing list