2009-11-03 18:59:18 +00:00
|
|
|
/*
|
2012-12-12 17:53:50 +00:00
|
|
|
* virjson.h: JSON object parsing/formatting
|
2009-11-03 18:59:18 +00:00
|
|
|
*
|
2014-08-21 10:23:41 +00:00
|
|
|
* Copyright (C) 2009, 2012-2015 Red Hat, Inc.
|
2009-11-03 18:59:18 +00:00
|
|
|
* Copyright (C) 2009 Daniel P. Berrange
|
|
|
|
*
|
|
|
|
* 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
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2009-11-03 18:59:18 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef __VIR_JSON_H_
|
2010-03-09 18:22:22 +00:00
|
|
|
# define __VIR_JSON_H_
|
2009-11-03 18:59:18 +00:00
|
|
|
|
2010-03-09 18:22:22 +00:00
|
|
|
# include "internal.h"
|
2015-01-13 14:06:16 +00:00
|
|
|
# include "virbitmap.h"
|
2009-11-03 18:59:18 +00:00
|
|
|
|
2014-09-22 14:35:02 +00:00
|
|
|
# include <stdarg.h>
|
|
|
|
|
2009-11-03 18:59:18 +00:00
|
|
|
|
2012-04-05 19:15:03 +00:00
|
|
|
typedef enum {
|
2009-11-03 18:59:18 +00:00
|
|
|
VIR_JSON_TYPE_OBJECT,
|
|
|
|
VIR_JSON_TYPE_ARRAY,
|
|
|
|
VIR_JSON_TYPE_STRING,
|
|
|
|
VIR_JSON_TYPE_NUMBER,
|
|
|
|
VIR_JSON_TYPE_BOOLEAN,
|
|
|
|
VIR_JSON_TYPE_NULL,
|
2012-04-05 19:15:03 +00:00
|
|
|
} virJSONType;
|
2009-11-03 18:59:18 +00:00
|
|
|
|
|
|
|
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 {
|
2013-04-26 14:59:02 +00:00
|
|
|
size_t npairs;
|
2009-11-03 18:59:18 +00:00
|
|
|
virJSONObjectPairPtr pairs;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _virJSONArray {
|
2013-04-26 14:59:02 +00:00
|
|
|
size_t nvalues;
|
2009-11-03 18:59:18 +00:00
|
|
|
virJSONValuePtr *values;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _virJSONValue {
|
2012-04-05 19:15:03 +00:00
|
|
|
int type; /* enum virJSONType */
|
|
|
|
bool protect; /* prevents deletion when embedded in another object */
|
2009-11-03 18:59:18 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2014-09-22 14:35:02 +00:00
|
|
|
int virJSONValueObjectCreate(virJSONValuePtr *obj, ...)
|
|
|
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_SENTINEL;
|
|
|
|
int virJSONValueObjectCreateVArgs(virJSONValuePtr *obj, va_list args)
|
|
|
|
ATTRIBUTE_NONNULL(1);
|
2015-01-12 15:35:17 +00:00
|
|
|
int virJSONValueObjectAdd(virJSONValuePtr obj, ...)
|
|
|
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_SENTINEL;
|
|
|
|
int virJSONValueObjectAddVArgs(virJSONValuePtr obj, va_list args)
|
|
|
|
ATTRIBUTE_NONNULL(1);
|
|
|
|
|
2014-09-22 14:35:02 +00:00
|
|
|
|
2009-11-03 18:59:18 +00:00
|
|
|
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);
|
2015-01-13 14:06:16 +00:00
|
|
|
virJSONValuePtr virJSONValueNewArrayFromBitmap(virBitmapPtr bitmap);
|
2009-11-03 18:59:18 +00:00
|
|
|
|
|
|
|
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);
|
2015-06-19 23:13:03 +00:00
|
|
|
virJSONValuePtr virJSONValueObjectGetByType(virJSONValuePtr object,
|
|
|
|
const char *key, virJSONType type);
|
2009-11-03 18:59:18 +00:00
|
|
|
|
2014-06-16 13:48:11 +00:00
|
|
|
bool virJSONValueIsArray(virJSONValuePtr array);
|
2015-10-08 08:17:42 +00:00
|
|
|
ssize_t virJSONValueArraySize(const virJSONValue *array);
|
2009-11-03 18:59:18 +00:00
|
|
|
virJSONValuePtr virJSONValueArrayGet(virJSONValuePtr object, unsigned int element);
|
2014-06-16 13:48:11 +00:00
|
|
|
virJSONValuePtr virJSONValueArraySteal(virJSONValuePtr object, unsigned int element);
|
2016-10-17 11:44:16 +00:00
|
|
|
typedef int (*virJSONArrayIteratorFunc)(size_t pos,
|
|
|
|
virJSONValuePtr item,
|
|
|
|
void *opaque);
|
|
|
|
int virJSONValueArrayForeachSteal(virJSONValuePtr array,
|
|
|
|
virJSONArrayIteratorFunc cb,
|
|
|
|
void *opaque);
|
2009-11-03 18:59:18 +00:00
|
|
|
|
2012-05-02 18:32:37 +00:00
|
|
|
int virJSONValueObjectKeysNumber(virJSONValuePtr object);
|
|
|
|
const char *virJSONValueObjectGetKey(virJSONValuePtr object, unsigned int n);
|
|
|
|
virJSONValuePtr virJSONValueObjectGetValue(virJSONValuePtr object, unsigned int n);
|
|
|
|
|
2010-01-22 13:22:53 +00:00
|
|
|
const char *virJSONValueGetString(virJSONValuePtr object);
|
2009-11-03 18:59:18 +00:00
|
|
|
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);
|
2011-05-05 11:48:31 +00:00
|
|
|
int virJSONValueGetBoolean(virJSONValuePtr object, bool *value);
|
2015-01-13 14:06:16 +00:00
|
|
|
int virJSONValueGetArrayAsBitmap(const virJSONValue *val, virBitmapPtr *bitmap)
|
|
|
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
|
2015-06-19 23:13:03 +00:00
|
|
|
bool virJSONValueIsNull(virJSONValuePtr object);
|
|
|
|
virJSONValuePtr virJSONValueObjectGetObject(virJSONValuePtr object,
|
|
|
|
const char *key);
|
|
|
|
virJSONValuePtr virJSONValueObjectGetArray(virJSONValuePtr object,
|
|
|
|
const char *key);
|
2016-10-03 18:45:13 +00:00
|
|
|
virJSONValuePtr virJSONValueObjectStealArray(virJSONValuePtr object,
|
|
|
|
const char *key);
|
2009-11-03 18:59:18 +00:00
|
|
|
|
2010-01-22 13:22:53 +00:00
|
|
|
const char *virJSONValueObjectGetString(virJSONValuePtr object, const char *key);
|
2009-11-03 18:59:18 +00:00
|
|
|
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);
|
2011-05-05 11:48:31 +00:00
|
|
|
int virJSONValueObjectGetBoolean(virJSONValuePtr object, const char *key, bool *value);
|
2009-11-03 18:59:18 +00:00
|
|
|
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);
|
|
|
|
|
2013-04-26 14:59:02 +00:00
|
|
|
int virJSONValueObjectRemoveKey(virJSONValuePtr object, const char *key,
|
|
|
|
virJSONValuePtr *value)
|
|
|
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
|
|
|
|
|
2009-11-03 18:59:18 +00:00
|
|
|
virJSONValuePtr virJSONValueFromString(const char *jsonstring);
|
2012-08-09 10:46:29 +00:00
|
|
|
char *virJSONValueToString(virJSONValuePtr object,
|
|
|
|
bool pretty);
|
2009-11-03 18:59:18 +00:00
|
|
|
|
2015-01-13 15:43:30 +00:00
|
|
|
typedef int (*virJSONValueObjectIteratorFunc)(const char *key,
|
2016-12-14 14:25:31 +00:00
|
|
|
virJSONValuePtr value,
|
2015-01-13 15:43:30 +00:00
|
|
|
void *opaque);
|
|
|
|
|
2016-12-14 14:25:31 +00:00
|
|
|
int virJSONValueObjectForeachKeyValue(virJSONValuePtr object,
|
2015-01-13 15:43:30 +00:00
|
|
|
virJSONValueObjectIteratorFunc cb,
|
|
|
|
void *opaque);
|
|
|
|
|
2016-07-27 08:39:23 +00:00
|
|
|
virJSONValuePtr virJSONValueCopy(const virJSONValue *in);
|
2014-08-21 10:23:41 +00:00
|
|
|
|
2009-11-03 18:59:18 +00:00
|
|
|
#endif /* __VIR_JSON_H_ */
|