virjson.h 7.8 KB
Newer Older
1
/*
2
 * virjson.h: JSON object parsing/formatting
3
 *
M
Martin Kletzander 已提交
4
 * Copyright (C) 2009, 2012-2015 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17
 * 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
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24
 *
 */


#ifndef __VIR_JSON_H_
25
# define __VIR_JSON_H_
26

27
# include "internal.h"
28
# include "virbitmap.h"
29

30 31
# include <stdarg.h>

32

E
Eric Blake 已提交
33
typedef enum {
34 35 36 37 38 39
    VIR_JSON_TYPE_OBJECT,
    VIR_JSON_TYPE_ARRAY,
    VIR_JSON_TYPE_STRING,
    VIR_JSON_TYPE_NUMBER,
    VIR_JSON_TYPE_BOOLEAN,
    VIR_JSON_TYPE_NULL,
E
Eric Blake 已提交
40
} virJSONType;
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

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 {
61
    size_t npairs;
62 63 64 65
    virJSONObjectPairPtr pairs;
};

struct _virJSONArray {
66
    size_t nvalues;
67 68 69 70
    virJSONValuePtr *values;
};

struct _virJSONValue {
E
Eric Blake 已提交
71 72
    int type; /* enum virJSONType */
    bool protect; /* prevents deletion when embedded in another object */
73 74 75 76 77 78 79 80 81 82 83

    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);
84
void virJSONValueHashFree(void *opaque, const void *name);
85

86 87 88 89
int virJSONValueObjectCreate(virJSONValuePtr *obj, ...)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_SENTINEL;
int virJSONValueObjectCreateVArgs(virJSONValuePtr *obj, va_list args)
    ATTRIBUTE_NONNULL(1);
90 91 92 93 94
int virJSONValueObjectAdd(virJSONValuePtr obj, ...)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_SENTINEL;
int virJSONValueObjectAddVArgs(virJSONValuePtr obj, va_list args)
    ATTRIBUTE_NONNULL(1);

95

96 97 98 99 100 101 102 103 104 105 106
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);
107
virJSONValuePtr virJSONValueNewArrayFromBitmap(virBitmapPtr bitmap);
108 109 110 111 112 113

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);
114 115
virJSONValuePtr virJSONValueObjectGetByType(virJSONValuePtr object,
                                            const char *key, virJSONType type);
116

117 118
bool virJSONValueIsObject(virJSONValuePtr object);

119
bool virJSONValueIsArray(virJSONValuePtr array);
120
ssize_t virJSONValueArraySize(const virJSONValue *array);
121
virJSONValuePtr virJSONValueArrayGet(virJSONValuePtr object, unsigned int element);
122
virJSONValuePtr virJSONValueArraySteal(virJSONValuePtr object, unsigned int element);
123 124 125 126 127 128
typedef int (*virJSONArrayIteratorFunc)(size_t pos,
                                        virJSONValuePtr item,
                                        void *opaque);
int virJSONValueArrayForeachSteal(virJSONValuePtr array,
                                  virJSONArrayIteratorFunc cb,
                                  void *opaque);
129

130 131 132 133
int virJSONValueObjectKeysNumber(virJSONValuePtr object);
const char *virJSONValueObjectGetKey(virJSONValuePtr object, unsigned int n);
virJSONValuePtr virJSONValueObjectGetValue(virJSONValuePtr object, unsigned int n);

134
const char *virJSONValueGetString(virJSONValuePtr object);
135 136 137 138 139
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);
J
Jiri Denemark 已提交
140
int virJSONValueGetBoolean(virJSONValuePtr object, bool *value);
141 142
int virJSONValueGetArrayAsBitmap(const virJSONValue *val, virBitmapPtr *bitmap)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
143 144 145 146 147
bool virJSONValueIsNull(virJSONValuePtr object);
virJSONValuePtr virJSONValueObjectGetObject(virJSONValuePtr object,
                                            const char *key);
virJSONValuePtr virJSONValueObjectGetArray(virJSONValuePtr object,
                                           const char *key);
148 149
virJSONValuePtr virJSONValueObjectStealArray(virJSONValuePtr object,
                                             const char *key);
150

151
const char *virJSONValueObjectGetString(virJSONValuePtr object, const char *key);
152 153 154 155 156
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);
J
Jiri Denemark 已提交
157
int virJSONValueObjectGetBoolean(virJSONValuePtr object, const char *key, bool *value);
158 159 160 161 162 163 164 165 166 167 168
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);

169 170 171 172
int virJSONValueObjectRemoveKey(virJSONValuePtr object, const char *key,
                                virJSONValuePtr *value)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);

173
virJSONValuePtr virJSONValueFromString(const char *jsonstring);
174 175
char *virJSONValueToString(virJSONValuePtr object,
                           bool pretty);
176

177
typedef int (*virJSONValueObjectIteratorFunc)(const char *key,
178
                                              virJSONValuePtr value,
179 180
                                              void *opaque);

181
int virJSONValueObjectForeachKeyValue(virJSONValuePtr object,
182 183 184
                                      virJSONValueObjectIteratorFunc cb,
                                      void *opaque);

185
virJSONValuePtr virJSONValueCopy(const virJSONValue *in);
M
Martin Kletzander 已提交
186

187 188
char *virJSONStringReformat(const char *jsonstr, bool pretty);

189
#endif /* __VIR_JSON_H_ */