#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 */
return 0;
}
-static int virJSONParserHandleNull(void * ctx)
+static int virJSONParserHandleNull(void *ctx)
{
virJSONParserPtr parser = ctx;
virJSONValuePtr value = virJSONValueNewNull();
return 1;
}
-static int virJSONParserHandleBoolean(void * ctx, int boolean_)
+static int virJSONParserHandleBoolean(void *ctx, int boolean_)
{
virJSONParserPtr parser = ctx;
virJSONValuePtr value = virJSONValueNewBoolean(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);
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,
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;
return 1;
}
-static int virJSONParserHandleStartMap(void * ctx)
+static int virJSONParserHandleStartMap(void *ctx)
{
virJSONParserPtr parser = ctx;
virJSONValuePtr value = virJSONValueNewObject();
}
-static int virJSONParserHandleEndMap(void * ctx)
+static int virJSONParserHandleEndMap(void *ctx)
{
virJSONParserPtr parser = ctx;
virJSONParserStatePtr state;
return 1;
}
-static int virJSONParserHandleStartArray(void * ctx)
+static int virJSONParserHandleStartArray(void *ctx)
{
virJSONParserPtr parser = ctx;
virJSONValuePtr value = virJSONValueNewArray();
return 1;
}
-static int virJSONParserHandleEndArray(void * ctx)
+static int virJSONParserHandleEndArray(void *ctx)
{
virJSONParserPtr parser = ctx;
virJSONParserStatePtr state;
/* 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,
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();