|
1 /* |
|
2 * Copyright (c) 2015, 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_VM_UTILITIES_JSON_HPP |
|
26 #define SHARE_VM_UTILITIES_JSON_HPP |
|
27 |
|
28 #include "memory/allocation.hpp" |
|
29 #include "utilities/globalDefinitions.hpp" |
|
30 #include "utilities/ostream.hpp" |
|
31 |
|
32 class 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 double double_value; |
|
56 |
|
57 struct { |
|
58 const char* start; |
|
59 size_t length; |
|
60 } str; |
|
61 } JSON_VAL; |
|
62 |
|
63 typedef enum { |
|
64 INTERNAL_ERROR, |
|
65 SYNTAX_ERROR, |
|
66 KEY_ERROR, |
|
67 VALUE_ERROR |
|
68 } JSON_ERROR; |
|
69 |
|
70 void error(JSON_ERROR e, const char* format, ...) ATTRIBUTE_PRINTF(3, 4); |
|
71 outputStream* _st; |
|
72 |
|
73 private: |
|
74 const char* start; |
|
75 const char* pos; |
|
76 |
|
77 // For error printing |
|
78 const char* mark; // Error marker |
|
79 uint level; |
|
80 uint line; |
|
81 uint column; |
|
82 |
|
83 bool silent; |
|
84 bool _valid; |
|
85 |
|
86 bool parse_json_value(); |
|
87 bool parse_json_object(); |
|
88 bool parse_json_array(); |
|
89 bool parse_json_string(bool key = false); |
|
90 bool parse_json_key(); |
|
91 bool parse_json_number(); |
|
92 bool parse_json_symbol(const char* name, JSON_TYPE symbol); |
|
93 |
|
94 virtual bool callback(JSON_TYPE t, JSON_VAL* v, uint level) = 0; |
|
95 |
|
96 void mark_pos(); |
|
97 u_char next(); |
|
98 u_char peek(); |
|
99 u_char peek(size_t i); |
|
100 int expect_any(const char* valid_chars, const char* error_msg, JSON_ERROR e = SYNTAX_ERROR); |
|
101 bool expect_string(const char* expected_string, const char* error_msg = "", JSON_ERROR e = SYNTAX_ERROR); |
|
102 size_t skip(size_t i); |
|
103 int skip_to_token(); |
|
104 u_char skip_to(u_char want); |
|
105 u_char skip_line_comment(); |
|
106 int skip_block_comment(); |
|
107 |
|
108 const char* strerror(JSON_ERROR e); |
|
109 }; |
|
110 |
|
111 #ifndef PRODUCT |
|
112 class JSONTest : public JSON { |
|
113 public: |
|
114 static bool test(); |
|
115 |
|
116 private: |
|
117 JSONTest(const char* text); |
|
118 static void test(const char* json, bool valid); |
|
119 |
|
120 void log(uint level, const char* format, ...) ATTRIBUTE_PRINTF(3, 4); |
|
121 |
|
122 bool callback(JSON_TYPE t, JSON_VAL* v, uint level); |
|
123 JSON_TYPE prev; |
|
124 }; |
|
125 #endif |
|
126 |
|
127 #endif // SHARE_VM_UTILITIES_JSON_HPP |