1/*
2 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25#ifndef SHARE_UTILITIES_JSON_HPP
26#define SHARE_UTILITIES_JSON_HPP
27
28#include "memory/allocation.hpp"
29#include "utilities/globalDefinitions.hpp"
30#include "utilities/ostream.hpp"
31
32class JSON : public ResourceObj {
33 protected:
34 JSON(const char* text, bool silent, outputStream* st);
35 void parse();
36 bool valid();
37
38 typedef enum {
39 JSON_NONE,
40 JSON_OBJECT_BEGIN,
41 JSON_OBJECT_END,
42 JSON_ARRAY_BEGIN,
43 JSON_ARRAY_END,
44 JSON_KEY,
45 JSON_STRING,
46 JSON_NUMBER_INT,
47 JSON_NUMBER_FLOAT,
48 JSON_TRUE,
49 JSON_FALSE,
50 JSON_NULL
51 } JSON_TYPE;
52
53 typedef union {
54 int64_t int_value;
55 uint64_t uint_value;
56 double double_value;
57
58 struct {
59 const char* start;
60 size_t length;
61 } str;
62 } JSON_VAL;
63
64 typedef enum {
65 INTERNAL_ERROR,
66 SYNTAX_ERROR,
67 KEY_ERROR,
68 VALUE_ERROR
69 } JSON_ERROR;
70
71 void error(JSON_ERROR e, const char* format, ...) ATTRIBUTE_PRINTF(3, 4);
72 outputStream* _st;
73
74 private:
75 const char* start;
76 const char* pos;
77
78 // For error printing
79 const char* mark; // Error marker
80 uint level;
81 uint line;
82 uint column;
83
84 bool silent;
85 bool _valid;
86
87 bool parse_json_value();
88 bool parse_json_object();
89 bool parse_json_array();
90 bool parse_json_string(bool key = false);
91 bool parse_json_key();
92 bool parse_json_number();
93 bool parse_json_symbol(const char* name, JSON_TYPE symbol);
94
95 virtual bool callback(JSON_TYPE t, JSON_VAL* v, uint level) = 0;
96
97 void mark_pos();
98 u_char next();
99 u_char peek();
100 u_char peek(size_t i);
101 int expect_any(const char* valid_chars, const char* error_msg, JSON_ERROR e = SYNTAX_ERROR);
102 bool expect_string(const char* expected_string, const char* error_msg = "", JSON_ERROR e = SYNTAX_ERROR);
103 size_t skip(size_t i);
104 int skip_to_token();
105 u_char skip_to(u_char want);
106 u_char skip_line_comment();
107 int skip_block_comment();
108
109 const char* strerror(JSON_ERROR e);
110};
111
112#endif // SHARE_UTILITIES_JSON_HPP
113