2009-11-03 13:59:18 -05:00
|
|
|
/*
|
2012-12-12 17:53:50 +00:00
|
|
|
* virjson.h: JSON object parsing/formatting
|
2009-11-03 13:59:18 -05:00
|
|
|
*
|
2014-08-21 12:23:41 +02:00
|
|
|
* Copyright (C) 2009, 2012-2015 Red Hat, Inc.
|
2009-11-03 13:59:18 -05: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 16:30:55 -06:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 18:06:23 +08:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2009-11-03 13:59:18 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2019-06-18 11:13:08 -05:00
|
|
|
#pragma once
|
2009-11-03 13:59:18 -05:00
|
|
|
|
2019-06-18 11:13:08 -05:00
|
|
|
#include "internal.h"
|
|
|
|
#include "virbuffer.h"
|
2009-11-03 13:59:18 -05:00
|
|
|
|
2019-06-18 11:13:08 -05:00
|
|
|
#include <stdarg.h>
|
2014-09-22 16:35:02 +02:00
|
|
|
|
2009-11-03 13:59:18 -05:00
|
|
|
|
2012-04-05 13:15:03 -06:00
|
|
|
typedef enum {
|
2009-11-03 13:59:18 -05: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 13:15:03 -06:00
|
|
|
} virJSONType;
|
2009-11-03 13:59:18 -05:00
|
|
|
|
|
|
|
typedef struct _virJSONValue virJSONValue;
|
|
|
|
|
2021-12-20 15:16:23 +01:00
|
|
|
void
|
|
|
|
virJSONValueFree(virJSONValue *value);
|
|
|
|
void
|
|
|
|
virJSONValueHashFree(void *opaque);
|
2009-11-03 13:59:18 -05:00
|
|
|
|
2021-12-20 15:16:23 +01:00
|
|
|
virJSONType
|
|
|
|
virJSONValueGetType(const virJSONValue *value);
|
2018-03-20 14:07:26 +01:00
|
|
|
|
2021-12-20 15:16:23 +01:00
|
|
|
int
|
|
|
|
virJSONValueObjectAdd(virJSONValue **obj, ...)
|
2019-10-14 14:13:31 +02:00
|
|
|
ATTRIBUTE_NONNULL(1) G_GNUC_NULL_TERMINATED;
|
2021-12-20 15:16:23 +01:00
|
|
|
int
|
|
|
|
virJSONValueObjectAddVArgs(virJSONValue **objptr,
|
|
|
|
va_list args)
|
2015-01-12 16:35:17 +01:00
|
|
|
ATTRIBUTE_NONNULL(1);
|
|
|
|
|
2014-09-22 16:35:02 +02:00
|
|
|
|
2021-12-20 15:16:23 +01:00
|
|
|
virJSONValue *
|
|
|
|
virJSONValueNewString(char *data);
|
|
|
|
virJSONValue *
|
|
|
|
virJSONValueNewNumberInt(int data);
|
|
|
|
virJSONValue *
|
|
|
|
virJSONValueNewNumberUint(unsigned int data);
|
|
|
|
virJSONValue *
|
|
|
|
virJSONValueNewNumberLong(long long data);
|
|
|
|
virJSONValue *
|
|
|
|
virJSONValueNewNumberUlong(unsigned long long data);
|
|
|
|
virJSONValue *
|
|
|
|
virJSONValueNewNumberDouble(double data);
|
|
|
|
virJSONValue *
|
|
|
|
virJSONValueNewBoolean(int boolean);
|
|
|
|
virJSONValue *
|
|
|
|
virJSONValueNewNull(void);
|
|
|
|
virJSONValue *
|
|
|
|
virJSONValueNewArray(void);
|
|
|
|
virJSONValue *
|
|
|
|
virJSONValueNewObject(void);
|
|
|
|
|
|
|
|
int
|
|
|
|
virJSONValueObjectAppend(virJSONValue *object,
|
|
|
|
const char *key,
|
|
|
|
virJSONValue **value);
|
|
|
|
int
|
|
|
|
virJSONValueArrayAppend(virJSONValue *object,
|
|
|
|
virJSONValue **value);
|
|
|
|
int
|
|
|
|
virJSONValueArrayConcat(virJSONValue *a,
|
|
|
|
virJSONValue *c);
|
|
|
|
|
|
|
|
int
|
|
|
|
virJSONValueObjectHasKey(virJSONValue *object,
|
|
|
|
const char *key);
|
|
|
|
virJSONValue *
|
|
|
|
virJSONValueObjectGet(virJSONValue *object,
|
|
|
|
const char *key);
|
|
|
|
virJSONValue *
|
|
|
|
virJSONValueObjectGetByType(virJSONValue *object,
|
|
|
|
const char *key,
|
|
|
|
virJSONType type);
|
|
|
|
|
|
|
|
bool
|
|
|
|
virJSONValueIsObject(virJSONValue *object);
|
|
|
|
|
|
|
|
bool
|
|
|
|
virJSONValueIsArray(virJSONValue *array);
|
|
|
|
size_t
|
|
|
|
virJSONValueArraySize(const virJSONValue *array);
|
|
|
|
virJSONValue *
|
|
|
|
virJSONValueArrayGet(virJSONValue *object,
|
|
|
|
unsigned int element);
|
|
|
|
virJSONValue *
|
|
|
|
virJSONValueArraySteal(virJSONValue *object,
|
|
|
|
unsigned int element);
|
2009-11-03 13:59:18 -05:00
|
|
|
|
2016-10-17 13:44:16 +02:00
|
|
|
typedef int (*virJSONArrayIteratorFunc)(size_t pos,
|
2021-03-11 08:16:13 +01:00
|
|
|
virJSONValue *item,
|
2016-10-17 13:44:16 +02:00
|
|
|
void *opaque);
|
2009-11-03 13:59:18 -05:00
|
|
|
|
2021-12-20 15:16:23 +01:00
|
|
|
int
|
|
|
|
virJSONValueArrayForeachSteal(virJSONValue *array,
|
|
|
|
virJSONArrayIteratorFunc cb,
|
|
|
|
void *opaque);
|
|
|
|
|
|
|
|
int
|
|
|
|
virJSONValueObjectKeysNumber(virJSONValue *object);
|
|
|
|
const char *
|
|
|
|
virJSONValueObjectGetKey(virJSONValue *object,
|
|
|
|
unsigned int n);
|
|
|
|
virJSONValue *
|
|
|
|
virJSONValueObjectGetValue(virJSONValue *object,
|
|
|
|
unsigned int n);
|
|
|
|
|
|
|
|
const char *
|
|
|
|
virJSONValueGetString(virJSONValue *object);
|
|
|
|
const char *
|
|
|
|
virJSONValueGetNumberString(virJSONValue *number);
|
|
|
|
int
|
|
|
|
virJSONValueGetNumberInt(virJSONValue *object,
|
|
|
|
int *value);
|
|
|
|
int
|
|
|
|
virJSONValueGetNumberUint(virJSONValue *object,
|
|
|
|
unsigned int *value);
|
|
|
|
int
|
|
|
|
virJSONValueGetNumberLong(virJSONValue *object,
|
|
|
|
long long *value);
|
|
|
|
int
|
|
|
|
virJSONValueGetNumberUlong(virJSONValue *object,
|
|
|
|
unsigned long long *value);
|
|
|
|
int
|
|
|
|
virJSONValueGetNumberDouble(virJSONValue *object,
|
|
|
|
double *value);
|
|
|
|
int
|
|
|
|
virJSONValueGetBoolean(virJSONValue *object,
|
|
|
|
bool *value);
|
|
|
|
|
|
|
|
virJSONValue *
|
|
|
|
virJSONValueObjectGetObject(virJSONValue *object,
|
|
|
|
const char *key);
|
|
|
|
virJSONValue *
|
|
|
|
virJSONValueObjectGetArray(virJSONValue *object,
|
|
|
|
const char *key);
|
|
|
|
virJSONValue *
|
|
|
|
virJSONValueObjectStealArray(virJSONValue *object,
|
|
|
|
const char *key);
|
|
|
|
virJSONValue *
|
|
|
|
virJSONValueObjectStealObject(virJSONValue *object,
|
|
|
|
const char *key);
|
|
|
|
const char *
|
|
|
|
virJSONValueObjectGetString(virJSONValue *object,
|
|
|
|
const char *key);
|
|
|
|
char **
|
|
|
|
virJSONValueObjectGetStringArray(virJSONValue *object,
|
|
|
|
const char *key);
|
|
|
|
const char *
|
|
|
|
virJSONValueObjectGetStringOrNumber(virJSONValue *object,
|
|
|
|
const char *key);
|
|
|
|
int
|
|
|
|
virJSONValueObjectGetNumberInt(virJSONValue *object,
|
|
|
|
const char *key,
|
|
|
|
int *value);
|
|
|
|
int
|
|
|
|
virJSONValueObjectGetNumberUint(virJSONValue *object,
|
|
|
|
const char *key,
|
|
|
|
unsigned int *value);
|
|
|
|
int
|
|
|
|
virJSONValueObjectGetNumberLong(virJSONValue *object,
|
|
|
|
const char *key,
|
|
|
|
long long *value);
|
|
|
|
int
|
|
|
|
virJSONValueObjectGetNumberUlong(virJSONValue *object,
|
|
|
|
const char *key,
|
|
|
|
unsigned long long *value);
|
|
|
|
int
|
|
|
|
virJSONValueObjectGetNumberDouble(virJSONValue *object,
|
|
|
|
const char *key,
|
|
|
|
double *value);
|
|
|
|
int
|
|
|
|
virJSONValueObjectGetBoolean(virJSONValue *object,
|
|
|
|
const char *key,
|
|
|
|
bool *value);
|
|
|
|
|
|
|
|
int
|
|
|
|
virJSONValueObjectAppendString(virJSONValue *object,
|
|
|
|
const char *key,
|
|
|
|
const char *value);
|
|
|
|
int
|
|
|
|
virJSONValueObjectAppendStringPrintf(virJSONValue *object,
|
|
|
|
const char *key,
|
|
|
|
const char *fmt,
|
|
|
|
...)
|
2020-05-14 08:09:56 +02:00
|
|
|
G_GNUC_PRINTF(3, 4);
|
2021-12-20 15:16:23 +01:00
|
|
|
int
|
|
|
|
virJSONValueObjectPrependString(virJSONValue *object,
|
|
|
|
const char *key,
|
|
|
|
const char *value);
|
|
|
|
int
|
|
|
|
virJSONValueObjectAppendNumberInt(virJSONValue *object,
|
|
|
|
const char *key,
|
|
|
|
int number);
|
|
|
|
int
|
|
|
|
virJSONValueObjectAppendNumberUint(virJSONValue *object,
|
|
|
|
const char *key,
|
|
|
|
unsigned int number);
|
|
|
|
int
|
|
|
|
virJSONValueObjectAppendNumberLong(virJSONValue *object,
|
|
|
|
const char *key,
|
|
|
|
long long number);
|
|
|
|
int
|
|
|
|
virJSONValueObjectAppendNumberUlong(virJSONValue *object,
|
|
|
|
const char *key,
|
|
|
|
unsigned long long number);
|
|
|
|
int
|
|
|
|
virJSONValueObjectAppendNumberDouble(virJSONValue *object,
|
|
|
|
const char *key,
|
|
|
|
double number);
|
|
|
|
int
|
|
|
|
virJSONValueObjectAppendBoolean(virJSONValue *object,
|
|
|
|
const char *key,
|
|
|
|
int boolean);
|
|
|
|
int
|
|
|
|
virJSONValueObjectAppendNull(virJSONValue *object,
|
|
|
|
const char *key);
|
|
|
|
|
|
|
|
int
|
|
|
|
virJSONValueObjectRemoveKey(virJSONValue *object,
|
|
|
|
const char *key,
|
|
|
|
virJSONValue **value)
|
2013-04-26 08:59:02 -06:00
|
|
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
|
|
|
|
|
2021-12-20 15:55:26 +01:00
|
|
|
void
|
|
|
|
virJSONValueObjectReplaceValue(virJSONValue *object,
|
|
|
|
const char *key,
|
|
|
|
virJSONValue **newval)
|
|
|
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
|
|
|
|
|
2021-12-20 15:16:23 +01:00
|
|
|
int
|
|
|
|
virJSONValueArrayAppendString(virJSONValue *object,
|
|
|
|
const char *value);
|
2019-01-24 21:32:55 -06:00
|
|
|
|
2021-12-20 15:16:23 +01:00
|
|
|
virJSONValue *
|
|
|
|
virJSONValueFromString(const char *jsonstring);
|
|
|
|
char *
|
|
|
|
virJSONValueToString(virJSONValue *object,
|
|
|
|
bool pretty);
|
|
|
|
int
|
|
|
|
virJSONValueToBuffer(virJSONValue *object,
|
|
|
|
virBuffer *buf,
|
|
|
|
bool pretty)
|
2019-10-14 14:25:14 +02:00
|
|
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) G_GNUC_WARN_UNUSED_RESULT;
|
2009-11-03 13:59:18 -05:00
|
|
|
|
2015-01-13 16:43:30 +01:00
|
|
|
typedef int (*virJSONValueObjectIteratorFunc)(const char *key,
|
2021-03-11 08:16:13 +01:00
|
|
|
virJSONValue *value,
|
2015-01-13 16:43:30 +01:00
|
|
|
void *opaque);
|
|
|
|
|
2021-12-20 15:16:23 +01:00
|
|
|
int
|
|
|
|
virJSONValueObjectForeachKeyValue(virJSONValue *object,
|
|
|
|
virJSONValueObjectIteratorFunc cb,
|
|
|
|
void *opaque);
|
2015-01-13 16:43:30 +01:00
|
|
|
|
2021-12-20 15:16:23 +01:00
|
|
|
virJSONValue *
|
|
|
|
virJSONValueCopy(const virJSONValue *in);
|
2014-08-21 12:23:41 +02:00
|
|
|
|
2021-12-20 15:16:23 +01:00
|
|
|
char *
|
|
|
|
virJSONStringReformat(const char *jsonstr,
|
|
|
|
bool pretty);
|
2016-11-30 10:46:57 +01:00
|
|
|
|
2021-12-20 15:16:23 +01:00
|
|
|
virJSONValue *
|
|
|
|
virJSONValueObjectDeflatten(virJSONValue *json);
|
2017-06-26 16:29:04 +02:00
|
|
|
|
2019-10-15 14:47:50 +02:00
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virJSONValue, virJSONValueFree);
|