File: JSON_parser-c.c

package info (click to toggle)
libguestfs 1%3A1.44.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 118,932 kB
  • sloc: ansic: 458,017; ml: 51,424; sh: 13,191; java: 9,578; makefile: 7,931; cs: 6,328; haskell: 5,674; python: 3,871; perl: 3,528; erlang: 2,446; xml: 1,347; ruby: 350; pascal: 257; javascript: 157; lex: 135; yacc: 128; cpp: 10
file content (169 lines) | stat: -rw-r--r-- 4,819 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* JSON parser
 * Copyright (C) 2015-2019 Red Hat Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <config.h>

#include <caml/alloc.h>
#include <caml/fail.h>
#include <caml/memory.h>
#include <caml/mlvalues.h>

#include <jansson.h>

#include <stdio.h>
#include <string.h>

#define JSON_NULL       (Val_int (0)) /* Variants without parameters. */
#define JSON_STRING_TAG 0             /* Variants with parameters. */
#define JSON_INT_TAG    1
#define JSON_FLOAT_TAG  2
#define JSON_BOOL_TAG   3
#define JSON_LIST_TAG   4
#define JSON_DICT_TAG   5

value virt_builder_json_parser_tree_parse (value stringv);
value virt_builder_json_parser_tree_parse_file (value stringv);

static value
convert_json_t (json_t *val, int level)
{
  CAMLparam0 ();
  CAMLlocal5 (rv, v, tv, sv, consv);

  if (level > 20)
    caml_invalid_argument ("too many levels of object/array nesting");

  if (json_is_object (val)) {
    const char *key;
    json_t *jvalue;

    rv = caml_alloc (1, JSON_DICT_TAG);
    v = Val_int (0);
    /* This will create the OCaml list backwards, but JSON
     * dictionaries are supposed to be unordered so that shouldn't
     * matter, right?  Well except that for some consumers this does
     * matter (eg. simplestreams which incorrectly uses a dict when it
     * really should use an array).
     */
    json_object_foreach (val, key, jvalue) {
      tv = caml_alloc_tuple (2);
      sv = caml_copy_string (key);
      Store_field (tv, 0, sv);
      sv = convert_json_t (jvalue, level + 1);
      Store_field (tv, 1, sv);
      consv = caml_alloc (2, 0);
      Store_field (consv, 1, v);
      Store_field (consv, 0, tv);
      v = consv;
    }
    Store_field (rv, 0, v);
  }
  else if (json_is_array (val)) {
    const size_t len = json_array_size (val);
    size_t i;
    json_t *jvalue;

    rv = caml_alloc (1, JSON_LIST_TAG);
    v = Val_int (0);
    for (i = 0; i < len; ++i) {
      /* Note we have to create the OCaml list backwards. */
      jvalue = json_array_get (val, len-i-1);
      tv = convert_json_t (jvalue, level + 1);
      consv = caml_alloc (2, 0);
      Store_field (consv, 1, v);
      Store_field (consv, 0, tv);
      v = consv;
    }
    Store_field (rv, 0, v);
  }
  else if (json_is_string (val)) {
    rv = caml_alloc (1, JSON_STRING_TAG);
    v = caml_copy_string (json_string_value (val));
    Store_field (rv, 0, v);
  }
  else if (json_is_real (val)) {
    rv = caml_alloc (1, JSON_FLOAT_TAG);
    v = caml_copy_double (json_real_value (val));
    Store_field (rv, 0, v);
  }
  else if (json_is_integer (val)) {
    rv = caml_alloc (1, JSON_INT_TAG);
    v = caml_copy_int64 (json_integer_value (val));
    Store_field (rv, 0, v);
  }
  else if (json_is_true (val)) {
    rv = caml_alloc (1, JSON_BOOL_TAG);
    Store_field (rv, 0, Val_true);
  }
  else if (json_is_false (val)) {
    rv = caml_alloc (1, JSON_BOOL_TAG);
    Store_field (rv, 0, Val_false);
  }
  else
    rv = JSON_NULL;

  CAMLreturn (rv);
}

value
virt_builder_json_parser_tree_parse (value stringv)
{
  CAMLparam1 (stringv);
  CAMLlocal1 (rv);
  json_t *tree;
  json_error_t err;

  tree = json_loads (String_val (stringv), JSON_DECODE_ANY, &err);
  if (tree == NULL) {
    char buf[256 + JSON_ERROR_TEXT_LENGTH];
    if (strlen (err.text) > 0)
      snprintf (buf, sizeof buf, "JSON parse error: %s", err.text);
    else
      snprintf (buf, sizeof buf, "unknown JSON parse error");
    caml_invalid_argument (buf);
  }

  rv = convert_json_t (tree, 1);
  json_decref (tree);

  CAMLreturn (rv);
}

value
virt_builder_json_parser_tree_parse_file (value filev)
{
  CAMLparam1 (filev);
  CAMLlocal1 (rv);
  json_t *tree;
  json_error_t err;

  tree = json_load_file (String_val (filev), JSON_DECODE_ANY, &err);
  if (tree == NULL) {
    char buf[1024 + JSON_ERROR_TEXT_LENGTH];
    if (strlen (err.text) > 0)
      snprintf (buf, sizeof buf, "%s: JSON parse error: %s", String_val (filev), err.text);
    else
      snprintf (buf, sizeof buf, "%s: unknown JSON parse error", String_val (filev));
    caml_invalid_argument (buf);
  }

  rv = convert_json_t (tree, 1);
  json_decref (tree);

  CAMLreturn (rv);
}