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     return yajl_alloc(callbacks, allocFuncs, ctx);
54 }
55 
libxl_yajl_gen_alloc(const yajl_alloc_funcs * allocFuncs)56 static inline yajl_gen libxl_yajl_gen_alloc(const yajl_alloc_funcs *allocFuncs)
57 {
58     yajl_gen g;
59     g = yajl_gen_alloc(allocFuncs);
60     if (g)
61         yajl_gen_config(g, yajl_gen_beautify, 1);
62     return g;
63 }
64 
65 #else /* !HAVE_YAJL_V2 */
66 
67 #define yajl_complete_parse yajl_parse_complete
68 
69 typedef unsigned int libxl_yajl_length;
70 
libxl__yajl_alloc(const yajl_callbacks * callbacks,const yajl_alloc_funcs * allocFuncs,void * ctx)71 static inline yajl_handle libxl__yajl_alloc(const yajl_callbacks *callbacks,
72                                             const yajl_alloc_funcs *allocFuncs,
73                                             void *ctx)
74 {
75     yajl_parser_config cfg = {
76         .allowComments = 1,
77         .checkUTF8 = 1,
78     };
79     return yajl_alloc(callbacks, &cfg, allocFuncs, ctx);
80 }
81 
libxl_yajl_gen_alloc(const yajl_alloc_funcs * allocFuncs)82 static inline yajl_gen libxl_yajl_gen_alloc(const yajl_alloc_funcs *allocFuncs)
83 {
84     yajl_gen_config conf = { 1, "    " };
85     return yajl_gen_alloc(&conf, allocFuncs);
86 }
87 
88 #endif /* !HAVE_YAJL_V2 */
89 
90 yajl_gen_status libxl_domain_config_gen_json(yajl_gen hand,
91                                              libxl_domain_config *p);
92 
93 #endif /* LIBXL_JSON_H */
94