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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2019-06-18 16:13:08 +00:00
|
|
|
#pragma once
|
2009-11-03 18:59:18 +00:00
|
|
|
|
2019-06-18 16:13:08 +00:00
|
|
|
#include "internal.h"
|
|
|
|
#include "virbitmap.h"
|
|
|
|
#include "virbuffer.h"
|
2009-11-03 18:59:18 +00:00
|
|
|
|
2019-06-18 16:13:08 +00:00
|
|
|
#include <stdarg.h>
|
2014-09-22 14:35:02 +00:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
void virJSONValueFree(virJSONValuePtr value);
|
2019-11-21 19:27:58 +00:00
|
|
|
void virJSONValueHashFree(void *opaque);
|
2009-11-03 18:59:18 +00:00
|
|
|
|
2018-03-20 13:07:26 +00:00
|
|
|
virJSONType virJSONValueGetType(const virJSONValue *value);
|
|
|
|
|
2014-09-22 14:35:02 +00:00
|
|
|
int virJSONValueObjectCreate(virJSONValuePtr *obj, ...)
|
2019-10-14 12:13:31 +00:00
|
|
|
ATTRIBUTE_NONNULL(1) G_GNUC_NULL_TERMINATED;
|
2014-09-22 14:35:02 +00:00
|
|
|
int virJSONValueObjectCreateVArgs(virJSONValuePtr *obj, va_list args)
|
|
|
|
ATTRIBUTE_NONNULL(1);
|
2015-01-12 15:35:17 +00:00
|
|
|
int virJSONValueObjectAdd(virJSONValuePtr obj, ...)
|
2019-10-14 12:13:31 +00:00
|
|
|
ATTRIBUTE_NONNULL(1) G_GNUC_NULL_TERMINATED;
|
2015-01-12 15:35:17 +00:00
|
|
|
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);
|
2018-08-13 11:39:56 +00:00
|
|
|
virJSONValuePtr virJSONValueNewStringLen(const char *data, size_t length);
|
2009-11-03 18:59:18 +00:00
|
|
|
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);
|
2020-01-28 14:26:49 +00:00
|
|
|
int virJSONValueArrayConcat(virJSONValuePtr a,
|
|
|
|
virJSONValuePtr c);
|
2009-11-03 18:59:18 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2017-06-26 09:32:35 +00:00
|
|
|
bool virJSONValueIsObject(virJSONValuePtr object);
|
|
|
|
|
2014-06-16 13:48:11 +00:00
|
|
|
bool virJSONValueIsArray(virJSONValuePtr array);
|
2018-04-19 21:29:02 +00:00
|
|
|
size_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);
|
2018-03-29 18:38:35 +00:00
|
|
|
const char *virJSONValueGetNumberString(virJSONValuePtr number);
|
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);
|
2018-03-28 07:01:30 +00:00
|
|
|
virJSONValuePtr virJSONValueObjectStealObject(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);
|
2018-01-31 10:54:31 +00:00
|
|
|
const char *virJSONValueObjectGetStringOrNumber(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);
|
2020-05-14 06:09:56 +00:00
|
|
|
int virJSONValueObjectAppendStringPrintf(virJSONValuePtr object, const char *key, const char *fmt, ...)
|
|
|
|
G_GNUC_PRINTF(3, 4);
|
2019-02-05 17:45:05 +00:00
|
|
|
int virJSONValueObjectPrependString(virJSONValuePtr object, const char *key, const char *value);
|
2009-11-03 18:59:18 +00:00
|
|
|
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);
|
|
|
|
|
2019-01-25 03:32:55 +00:00
|
|
|
int virJSONValueArrayAppendString(virJSONValuePtr object, const char *value);
|
|
|
|
|
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);
|
2019-03-26 16:56:14 +00:00
|
|
|
int virJSONValueToBuffer(virJSONValuePtr object,
|
|
|
|
virBufferPtr buf,
|
|
|
|
bool pretty)
|
2019-10-14 12:25:14 +00:00
|
|
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) G_GNUC_WARN_UNUSED_RESULT;
|
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
|
|
|
|
2016-11-30 09:46:57 +00:00
|
|
|
char *virJSONStringReformat(const char *jsonstr, bool pretty);
|
|
|
|
|
2017-06-26 14:29:04 +00:00
|
|
|
virJSONValuePtr virJSONValueObjectDeflatten(virJSONValuePtr json);
|
|
|
|
|
2019-10-15 12:47:50 +00:00
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virJSONValue, virJSONValueFree);
|