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

Daniel P. Berrange berrange at redhat.com
Tue May 3 17:12:44 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 |   44 +++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 47 insertions(+), 5 deletions(-)

diff --git a/configure.ac b/configure.ac
index 7c68bca..dc87347 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 0daeae9..80dbca4 100644
--- a/src/util/json.c
+++ b/src/util/json.c
@@ -709,7 +709,12 @@ static int virJSONParserHandleBoolean(void * ctx, int boolean_)
 
 static int virJSONParserHandleNumber(void * ctx,
                                      const char * s,
-                                     unsigned int l)
+#ifdef HAVE_YAJL2
+                                     size_t l
+#else
+                                     unsigned int l
+#endif
+    )
 {
     virJSONParserPtr parser = ctx;
     char *str = strndup(s, l);
@@ -735,7 +740,12 @@ static int virJSONParserHandleNumber(void * ctx,
 
 static int virJSONParserHandleString(void * ctx,
                                      const unsigned char * stringVal,
-                                     unsigned int stringLen)
+#ifdef HAVE_YAJL2
+                                     size_t stringLen
+#else
+                                     unsigned int stringLen
+#endif
+    )
 {
     virJSONParserPtr parser = ctx;
     virJSONValuePtr value = virJSONValueNewStringLen((const char *)stringVal,
@@ -756,7 +766,12 @@ static int virJSONParserHandleString(void * ctx,
 
 static int virJSONParserHandleMapKey(void * ctx,
                                      const unsigned char * stringVal,
-                                     unsigned int stringLen)
+#ifdef HAVE_YAJL2
+                                     size_t stringLen
+#else
+                                     unsigned int stringLen
+#endif
+    )
 {
     virJSONParserPtr parser = ctx;
     virJSONParserStatePtr state;
@@ -894,14 +909,22 @@ 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);
+    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 (yajl_parse(hand,
                    (const unsigned char *)jsonstring,
@@ -1002,15 +1025,26 @@ 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;
+#ifdef HAVE_YAJL2
+    size_t len;
+#else
     unsigned int len;
+    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);
+    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 (virJSONValueToStringOne(object, g) < 0) {
         virReportOOMError();
-- 
1.7.4.4




More information about the libvir-list mailing list