1 /*
2 * Copyright (C) 2011 Citrix Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; version 2.1 only. with the special
7 * exception on linking described in file LICENSE.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 */
14
15 #ifndef LIBXL_JSON_H
16 #define LIBXL_JSON_H
17
18 #include <yajl/yajl_gen.h>
19 #include <yajl/yajl_parse.h>
20
21 #ifdef HAVE_YAJL_YAJL_VERSION_H
22 # include <yajl/yajl_version.h>
23 #endif
24
25 yajl_gen_status libxl__uint64_gen_json(yajl_gen hand, uint64_t val);
26 yajl_gen_status libxl_defbool_gen_json(yajl_gen hand, libxl_defbool *p);
27 yajl_gen_status libxl_uuid_gen_json(yajl_gen hand, libxl_uuid *p);
28 yajl_gen_status libxl_mac_gen_json(yajl_gen hand, libxl_mac *p);
29 yajl_gen_status libxl_bitmap_gen_json(yajl_gen hand, libxl_bitmap *p);
30 yajl_gen_status libxl_cpuid_policy_list_gen_json(yajl_gen hand,
31 libxl_cpuid_policy_list *p);
32 yajl_gen_status libxl_string_list_gen_json(yajl_gen hand, libxl_string_list *p);
33 yajl_gen_status libxl_key_value_list_gen_json(yajl_gen hand,
34 libxl_key_value_list *p);
35 yajl_gen_status libxl_hwcap_gen_json(yajl_gen hand, libxl_hwcap *p);
36 yajl_gen_status libxl_ms_vm_genid_gen_json(yajl_gen hand, libxl_ms_vm_genid *p);
37
38 #include <_libxl_types_json.h>
39
40 /* YAJL version check */
41 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
42 # define HAVE_YAJL_V2 1
43 #endif
44
45 #ifdef HAVE_YAJL_V2
46
47 typedef size_t libxl_yajl_length;
48
libxl__yajl_alloc(const yajl_callbacks * callbacks,yajl_alloc_funcs * allocFuncs,void * ctx)49 static inline yajl_handle libxl__yajl_alloc(const yajl_callbacks *callbacks,
50 yajl_alloc_funcs *allocFuncs,
51 void *ctx)
52 {
53 yajl_handle hand = yajl_alloc(callbacks, allocFuncs, ctx);
54 if (hand)
55 yajl_config(hand, yajl_allow_trailing_garbage, 1);
56 return hand;
57 }
58
libxl_yajl_gen_alloc(const yajl_alloc_funcs * allocFuncs)59 static inline yajl_gen libxl_yajl_gen_alloc(const yajl_alloc_funcs *allocFuncs)
60 {
61 yajl_gen g;
62 g = yajl_gen_alloc(allocFuncs);
63 if (g)
64 yajl_gen_config(g, yajl_gen_beautify, 1);
65 return g;
66 }
67
68 #else /* !HAVE_YAJL_V2 */
69
70 #define yajl_complete_parse yajl_parse_complete
71
72 typedef unsigned int libxl_yajl_length;
73
libxl__yajl_alloc(const yajl_callbacks * callbacks,const yajl_alloc_funcs * allocFuncs,void * ctx)74 static inline yajl_handle libxl__yajl_alloc(const yajl_callbacks *callbacks,
75 const yajl_alloc_funcs *allocFuncs,
76 void *ctx)
77 {
78 yajl_parser_config cfg = {
79 .allowComments = 1,
80 .checkUTF8 = 1,
81 };
82 return yajl_alloc(callbacks, &cfg, allocFuncs, ctx);
83 }
84
libxl_yajl_gen_alloc(const yajl_alloc_funcs * allocFuncs)85 static inline yajl_gen libxl_yajl_gen_alloc(const yajl_alloc_funcs *allocFuncs)
86 {
87 yajl_gen_config conf = { 1, " " };
88 return yajl_gen_alloc(&conf, allocFuncs);
89 }
90
91 #endif /* !HAVE_YAJL_V2 */
92
93 yajl_gen_status libxl_domain_config_gen_json(yajl_gen hand,
94 libxl_domain_config *p);
95
96 #endif /* LIBXL_JSON_H */
97