提交 9428f2ce 编写于 作者: D Daniel P. Berrange

Introduce a simple API for handling JSON data

This introduces simple API for handling JSON data. There is
an internal data structure 'virJSONValuePtr' which stores a
arbitrary nested JSON value (number, string, array, object,
nul, etc).  There are APIs for constructing/querying objects
and APIs for parsing/formatting string formatted JSON data.

This uses the YAJL library for parsing/formatting from

 http://lloyd.github.com/yajl/

* src/util/json.h, src/util/json.c: Data structures and APIs
  for representing JSON data, and parsing/formatting it
* configure.in: Add check for yajl library
* libvirt.spec.in: Add build requires for yajl
* src/Makefile.am: Add json.c/h
* src/libvirt_private.syms: Export JSON symbols to drivers
上级 563dc565
......@@ -645,6 +645,56 @@ AC_SUBST([SASL_CFLAGS])
AC_SUBST([SASL_LIBS])
dnl YAJL JSON library http://lloyd.github.com/yajl/
AC_ARG_WITH([yajl],
[ --with-yajl use YAJL for JSON parsing/formatting],
[],
[with_yajl=check])
YAJL_CFLAGS=
YAJL_LIBS=
if test "x$with_yajl" != "xno"; then
if test "x$with_yajl" != "xyes" -a "x$with_yajl" != "xcheck"; then
YAJL_CFLAGS="-I$with_yajl/include"
YAJL_LIBS="-L$with_yajl/lib"
fi
fail=0
old_cppflags="$CPPFLAGS"
old_ldflags="$LDFLAGS"
CPPFLAGS="$CPPFLAGS $YAJL_CFLAGS"
LDFLAGS="$LDFLAGS $YAJL_LIBS"
AC_CHECK_HEADER([yajl/yajl_common.h],[],[
if test "x$with_yajl" != "xcheck" ; then
with_yajl=no
else
fail=1
fi])
if test "x$with_yajl" != "xno" ; then
AC_CHECK_LIB([yajl], [yajl_parse],[
YAJL_LIBS="$YAJL_LIBS -lyajl"
with_yajl=yes
],[
if test "x$with_yajl" = "xcheck" ; then
with_yajl=no
else
fail=1
fi
])
fi
test $fail = 1 &&
AC_MSG_ERROR([You must install the YAJL development package in order to compile libvirt])
CPPFLAGS="$old_cppflags"
LDFLAGS="$old_ldflags"
if test "x$with_yajl" = "xyes" ; then
AC_DEFINE_UNQUOTED([HAVE_YAJL], 1,
[whether YAJL is available for JSON parsing/formatting])
fi
fi
AM_CONDITIONAL([HAVE_YAJL], [test "x$with_yajl" = "xyes"])
AC_SUBST([YAJL_CFLAGS])
AC_SUBST([YAJL_LIBS])
dnl PolicyKit library
POLKIT_CFLAGS=
POLKIT_LIBS=
......@@ -1859,6 +1909,11 @@ AC_MSG_NOTICE([ sasl: $SASL_CFLAGS $SASL_LIBS])
else
AC_MSG_NOTICE([ sasl: no])
fi
if test "$with_yajl" != "no" ; then
AC_MSG_NOTICE([ yajl: $YAJL_CFLAGS $YAJL_LIBS])
else
AC_MSG_NOTICE([ yajl: no])
fi
if test "$with_avahi" = "yes" ; then
AC_MSG_NOTICE([ avahi: $AVAHI_CFLAGS $AVAHI_LIBS])
else
......
......@@ -60,6 +60,7 @@
%define with_netcf 0%{!?_without_netcf:0}
%define with_udev 0%{!?_without_udev:0}
%define with_hal 0%{!?_without_hal:0}
%define with_yajl 0%{!?_without_yajl:0}
# Non-server/HV driver defaults which are always enabled
%define with_python 0%{!?_without_python:1}
......@@ -141,6 +142,11 @@
%define with_hal 0%{!?_without_hal:%{server_drivers}}
%endif
# Enable yajl library for JSON mode with QEMU
%if 0%{?fedora} >= 13 || 0%{?rhel} >= 6
%define with_yajl 0%{!?_without_yajl:%{server_drivers}}
%endif
# Force QEMU to run as non-root
%if 0%{?fedora} >= 12 || 0%{?rhel} >= 6
%define qemu_user qemu
......@@ -257,6 +263,9 @@ BuildRequires: hal-devel
BuildRequires: libudev-devel >= 145
BuildRequires: libpciaccess-devel >= 0.10.9
%endif
%if %{with_yajl}
BuildRequires: yajl-devel
%endif
%if %{with_avahi}
BuildRequires: avahi-devel
%endif
......@@ -495,6 +504,10 @@ of recent versions of Linux (and other OSes).
%define _without_udev --without-udev
%endif
%if ! %{with_yajl}
%define _without_yajl --without-yajl
%endif
%configure %{?_without_xen} \
%{?_without_qemu} \
%{?_without_openvz} \
......@@ -522,6 +535,7 @@ of recent versions of Linux (and other OSes).
%{?_without_selinux} \
%{?_without_hal} \
%{?_without_udev} \
%{?_without_yajl} \
--with-qemu-user=%{qemu_user} \
--with-qemu-group=%{qemu_group} \
--with-init-script=redhat \
......
......@@ -50,6 +50,7 @@ src/uml/uml_driver.c
src/util/bridge.c
src/util/conf.c
src/util/iptables.c
src/util/json.c
src/util/logging.c
src/util/pci.c
src/util/processinfo.c
......
......@@ -52,6 +52,7 @@ UTIL_SOURCES = \
util/hash.c util/hash.h \
util/iptables.c util/iptables.h \
util/ebtables.c util/ebtables.h \
util/json.c util/json.h \
util/logging.c util/logging.h \
util/memory.c util/memory.h \
util/pci.c util/pci.h \
......@@ -284,8 +285,8 @@ noinst_LTLIBRARIES = libvirt_util.la
libvirt_la_LIBADD = libvirt_util.la
libvirt_util_la_SOURCES = \
$(UTIL_SOURCES)
libvirt_util_la_CFLAGS = $(CAPNG_CFLAGS)
libvirt_util_la_LDFLAGS = $(CAPNG_LIBS)
libvirt_util_la_CFLAGS = $(CAPNG_CFLAGS) $(YAJL_CFLAGS)
libvirt_util_la_LDFLAGS = $(CAPNG_LIBS) $(YAJL_LIBS)
noinst_LTLIBRARIES += libvirt_conf.la
......@@ -829,12 +830,13 @@ libvirt_lxc_SOURCES = \
$(NODE_INFO_SOURCES) \
$(ENCRYPTION_CONF_SOURCES) \
$(DOMAIN_CONF_SOURCES)
libvirt_lxc_LDFLAGS = $(WARN_CFLAGS) $(COVERAGE_LDCFLAGS) $(CAPNG_LIBS)
libvirt_lxc_LDFLAGS = $(WARN_CFLAGS) $(COVERAGE_LDCFLAGS) $(CAPNG_LIBS) $(YAJL_LIBS)
libvirt_lxc_LDADD = $(LIBXML_LIBS) $(NUMACTL_LIBS) ../gnulib/lib/libgnu.la
libvirt_lxc_CFLAGS = \
$(LIBPARTED_CFLAGS) \
$(NUMACTL_CFLAGS) \
$(CAPNG_CFLAGS) \
$(YAJL_CFLAGS) \
-I@top_srcdir@/src/conf
endif
endif
......
......@@ -270,6 +270,53 @@ virRegisterDeviceMonitor;
virRegisterSecretDriver;
# json.h
virJSONValueFree;
virJSONValueNewString;
virJSONValueNewStringLen;
virJSONValueNewNumberInt;
virJSONValueNewNumberUint;
virJSONValueNewNumberLong;
virJSONValueNewNumberUlong;
virJSONValueNewNumberDouble;
virJSONValueNewBoolean;
virJSONValueNewNull;
virJSONValueNewArray;
virJSONValueNewObject;
virJSONValueObjectAppend;
virJSONValueObjectAppendString;
virJSONValueObjectAppendNumberInt;
virJSONValueObjectAppendNumberUint;
virJSONValueObjectAppendNumberLong;
virJSONValueObjectAppendNumberUlong;
virJSONValueObjectAppendNumberDouble;
virJSONValueObjectAppendBoolean;
virJSONValueObjectAppendNull;
virJSONValueArrayAppend;
virJSONValueObjectHasKey;
virJSONValueObjectGet;
virJSONValueArraySize;
virJSONValueArrayGet;
virJSONValueGetString;
virJSONValueGetNumberInt;
virJSONValueGetNumberUint;
virJSONValueGetNumberLong;
virJSONValueGetNumberUlong;
virJSONValueGetNumberDouble;
virJSONValueGetBoolean;
virJSONValueIsNull;
virJSONValueObjectGetString;
virJSONValueObjectGetNumberInt;
virJSONValueObjectGetNumberUint;
virJSONValueObjectGetNumberLong;
virJSONValueObjectGetNumberUlong;
virJSONValueObjectGetNumberDouble;
virJSONValueObjectGetBoolean;
virJSONValueObjectIsNull;
virJSONValueFromString;
virJSONValueToString;
# logging.h
virLogMessage;
virLogGetNbFilters;
......
此差异已折叠。
/*
* json.h: JSON object parsing/formatting
*
* Copyright (C) 2009 Daniel P. Berrange
* Copyright (C) 2009 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifndef __VIR_JSON_H_
#define __VIR_JSON_H_
#include "internal.h"
enum {
VIR_JSON_TYPE_OBJECT,
VIR_JSON_TYPE_ARRAY,
VIR_JSON_TYPE_STRING,
VIR_JSON_TYPE_NUMBER,
VIR_JSON_TYPE_BOOLEAN,
VIR_JSON_TYPE_NULL,
};
typedef struct _virJSONValue virJSONValue;
typedef virJSONValue *virJSONValuePtr;
typedef struct _virJSONObject virJSONObject;
typedef virJSONObject *virJSONObjectPtr;
typedef struct _virJSONObjectPair virJSONObjectPair;
typedef virJSONObjectPair *virJSONObjectPairPtr;
typedef struct _virJSONArray virJSONArray;
typedef virJSONArray *virJSONArrayPtr;
struct _virJSONObjectPair {
char *key;
virJSONValuePtr value;
};
struct _virJSONObject {
unsigned int npairs;
virJSONObjectPairPtr pairs;
};
struct _virJSONArray {
unsigned int nvalues;
virJSONValuePtr *values;
};
struct _virJSONValue {
int type;
union {
virJSONObject object;
virJSONArray array;
char *string;
char *number; /* int/float/etc format is context defined so we can't parse it here :-( */
int boolean;
} data;
};
void virJSONValueFree(virJSONValuePtr value);
virJSONValuePtr virJSONValueNewString(const char *data);
virJSONValuePtr virJSONValueNewStringLen(const char *data, size_t length);
virJSONValuePtr virJSONValueNewNumberInt(int data);
virJSONValuePtr virJSONValueNewNumberUint(unsigned int data);
virJSONValuePtr virJSONValueNewNumberLong(long long data);
virJSONValuePtr virJSONValueNewNumberUlong(unsigned long long data);
virJSONValuePtr virJSONValueNewNumberDouble(double data);
virJSONValuePtr virJSONValueNewBoolean(int boolean);
virJSONValuePtr virJSONValueNewNull(void);
virJSONValuePtr virJSONValueNewArray(void);
virJSONValuePtr virJSONValueNewObject(void);
int virJSONValueObjectAppend(virJSONValuePtr object, const char *key, virJSONValuePtr value);
int virJSONValueArrayAppend(virJSONValuePtr object, virJSONValuePtr value);
int virJSONValueObjectHasKey(virJSONValuePtr object, const char *key);
virJSONValuePtr virJSONValueObjectGet(virJSONValuePtr object, const char *key);
int virJSONValueArraySize(virJSONValuePtr object);
virJSONValuePtr virJSONValueArrayGet(virJSONValuePtr object, unsigned int element);
char *virJSONValueGetString(virJSONValuePtr object);
int virJSONValueGetNumberInt(virJSONValuePtr object, int *value);
int virJSONValueGetNumberUint(virJSONValuePtr object, unsigned int *value);
int virJSONValueGetNumberLong(virJSONValuePtr object, long long *value);
int virJSONValueGetNumberUlong(virJSONValuePtr object, unsigned long long *value);
int virJSONValueGetNumberDouble(virJSONValuePtr object, double *value);
int virJSONValueGetBoolean(virJSONValuePtr object);
int virJSONValueIsNull(virJSONValuePtr object);
char *virJSONValueObjectGetString(virJSONValuePtr object, const char *key);
int virJSONValueObjectGetNumberInt(virJSONValuePtr object, const char *key, int *value);
int virJSONValueObjectGetNumberUint(virJSONValuePtr object, const char *key, unsigned int *value);
int virJSONValueObjectGetNumberLong(virJSONValuePtr object, const char *key, long long *value);
int virJSONValueObjectGetNumberUlong(virJSONValuePtr object, const char *key, unsigned long long *value);
int virJSONValueObjectGetNumberDouble(virJSONValuePtr object, const char *key, double *value);
int virJSONValueObjectGetBoolean(virJSONValuePtr object, const char *key);
int virJSONValueObjectIsNull(virJSONValuePtr object, const char *key);
int virJSONValueObjectAppendString(virJSONValuePtr object, const char *key, const char *value);
int virJSONValueObjectAppendNumberInt(virJSONValuePtr object, const char *key, int number);
int virJSONValueObjectAppendNumberUint(virJSONValuePtr object, const char *key, unsigned int number);
int virJSONValueObjectAppendNumberLong(virJSONValuePtr object, const char *key, long long number);
int virJSONValueObjectAppendNumberUlong(virJSONValuePtr object, const char *key, unsigned long long number);
int virJSONValueObjectAppendNumberDouble(virJSONValuePtr object, const char *key, double number);
int virJSONValueObjectAppendBoolean(virJSONValuePtr object, const char *key, int boolean);
int virJSONValueObjectAppendNull(virJSONValuePtr object, const char *key);
virJSONValuePtr virJSONValueFromString(const char *jsonstring);
char *virJSONValueToString(virJSONValuePtr object);
#endif /* __VIR_JSON_H_ */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册