Commit | Line | Data |
---|---|---|
3abe9331 | 1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (c) 2000-2015 Ericsson Telecom AB | |
3 | // All rights reserved. This program and the accompanying materials | |
4 | // are made available under the terms of the Eclipse Public License v1.0 | |
5 | // which accompanies this distribution, and is available at | |
6 | // http://www.eclipse.org/legal/epl-v10.html | |
7 | /////////////////////////////////////////////////////////////////////////////// | |
8 | ||
970ed795 EL |
9 | #ifndef JSON_TOKENIZER_HH |
10 | #define JSON_TOKENIZER_HH | |
11 | ||
12 | #include <cstddef> | |
13 | ||
14 | /** JSON token types */ | |
15 | enum json_token_t { | |
16 | JSON_TOKEN_ERROR = 0, // not actually a token, used when get_next_token() fails | |
17 | JSON_TOKEN_NONE, // not actually a token, used for initializing | |
18 | JSON_TOKEN_OBJECT_START, // "{" | |
19 | JSON_TOKEN_OBJECT_END, // "}" | |
20 | JSON_TOKEN_ARRAY_START, // "[" | |
21 | JSON_TOKEN_ARRAY_END, // "]" | |
22 | JSON_TOKEN_NAME, // field name (key) in a JSON object, followed by ":" | |
23 | JSON_TOKEN_NUMBER, // JSON number value | |
24 | JSON_TOKEN_STRING, // JSON string value | |
25 | JSON_TOKEN_LITERAL_TRUE, // "true" value | |
26 | JSON_TOKEN_LITERAL_FALSE, // "false" value | |
27 | JSON_TOKEN_LITERAL_NULL // "null" value | |
28 | }; | |
29 | ||
30 | /** A class for building and processing JSON documents. Stores the document in a buffer. | |
31 | * Can build JSON documents by inserting tokens into an empty buffer. | |
32 | * Can extract tokens from an existing JSON document. */ | |
33 | class JSON_Tokenizer { | |
34 | ||
35 | private: | |
36 | ||
37 | /** The buffer that stores the JSON document | |
38 | * This is a buffer with exponential allocation (expstring), only uses expstring | |
39 | * memory operations from memory.h (ex.: mputstr, mputprintf) */ | |
40 | char* buf_ptr; | |
41 | ||
42 | /** Number of bytes currently in the buffer */ | |
43 | size_t buf_len; | |
44 | ||
45 | /** Current position in the buffer */ | |
46 | size_t buf_pos; | |
47 | ||
48 | /** Current depth in the JSON document (only used if pretty printing is set */ | |
49 | unsigned int depth; | |
50 | ||
51 | /** Stores the previous JSON token inserted by put_next_token() */ | |
52 | json_token_t previous_token; | |
53 | ||
54 | /** Activates or deactivates pretty printing | |
55 | * If active, put_next_token() and put_separator() will add extra newlines | |
56 | * and indenting to the JSON code to make it more readable for you humans, | |
57 | * otherwise it will be compact (no white spaces). */ | |
58 | bool pretty; | |
59 | ||
60 | /** Initializes the properties of the tokenizer. | |
61 | * The buffer is initialized with the parameter data (unless it's empty). */ | |
62 | void init(const char* p_buf, const size_t p_buf_len); | |
63 | ||
64 | /** Inserts a character to the end of the buffer */ | |
65 | void put_c(const char c); | |
66 | ||
67 | /** Inserts a null-terminated string to the end of the buffer */ | |
68 | void put_s(const char* s); | |
69 | ||
70 | /** Indents a new line in JSON code depending on the current depth. | |
71 | * If the maximum depth is reached, the code is not indented further. | |
72 | * Used only if pretty printing is set. */ | |
73 | void put_depth(); | |
74 | ||
75 | /** Skips white spaces until a non-white-space character is found. | |
76 | * Returns false if the end of the buffer is reached before a non-white-space | |
77 | * character is found, otherwise returns true. */ | |
78 | bool skip_white_spaces(); | |
79 | ||
80 | /** Attempts to find a JSON string at the current buffer position. | |
81 | * Returns true if a valid string is found before the end of the buffer | |
82 | * is reached, otherwise returns false. */ | |
83 | bool check_for_string(); | |
84 | ||
85 | /** Attempts to find a JSON number at the current buffer position. | |
86 | * For number format see http://json.org/. | |
87 | * Returns true if a valid number is found before the end of the buffer | |
88 | * is reached, otherwise returns false. */ | |
89 | bool check_for_number(); | |
90 | ||
91 | /** Checks if the current character in the buffer is a valid JSON separator. | |
92 | * Separators are: commas (,), colons (:) and curly and square brackets ({}[]). | |
93 | * This function also steps over the separator if it's a comma. | |
94 | * Returns true if a separator is found, otherwise returns false. */ | |
95 | bool check_for_separator(); | |
96 | ||
97 | /** Attempts to find a specific JSON literal at the current buffer position. | |
98 | * Returns true if the literal is found, otherwise returns false. | |
99 | * @param p_literal [in] Literal value to find */ | |
100 | bool check_for_literal(const char* p_literal); | |
101 | ||
102 | /** Adds a separating comma (,) if the previous token is a value, or an object or | |
103 | * array end mark. */ | |
104 | void put_separator(); | |
105 | ||
106 | /** No copy constructor. Implement if needed. */ | |
107 | JSON_Tokenizer(const JSON_Tokenizer&); | |
108 | ||
109 | /** No assignment operator. Implement if needed. */ | |
110 | JSON_Tokenizer& operator=(const JSON_Tokenizer&); | |
111 | ||
112 | public: | |
113 | /** Constructs a tokenizer with an empty buffer. | |
114 | * Use put_next_token() to build a JSON document and get_buffer()/get_buffer_length() to retrieve it */ | |
115 | JSON_Tokenizer(bool p_pretty = false) : pretty(p_pretty) { init(0, 0); } | |
116 | ||
117 | /** Constructs a tokenizer with the buffer parameter. | |
118 | * Use get_next_token() to read JSON tokens and get_pos()/set_pos() to move around in the buffer */ | |
119 | JSON_Tokenizer(const char* p_buf, const size_t p_buf_len) : pretty(false) { init(p_buf, p_buf_len); } | |
120 | ||
121 | /** Destructor. Frees the buffer. */ | |
122 | ~JSON_Tokenizer(); | |
123 | ||
124 | /** Reinitializes the tokenizer with a new buffer. */ | |
125 | inline void set_buffer(const char* p_buf, const size_t p_buf_len) { init(p_buf, p_buf_len); } | |
126 | ||
127 | /** Retrieves the buffer containing the JSON document. */ | |
128 | inline const char* get_buffer() { return buf_ptr; } | |
129 | ||
130 | /** Retrieves the length of the buffer containing the JSON document. */ | |
131 | inline size_t get_buffer_length() { return buf_len; } | |
132 | ||
133 | /** Extracts a JSON token from the current buffer position. | |
134 | * @param p_token [out] Extracted token type, or JSON_TOKEN_ERROR if no token | |
135 | * could be extracted, or JSON_TOKEN_NONE if the buffer end is reached | |
136 | * @param p_token_str [out] A pointer to the token data (if any): | |
137 | * the name of a JSON object field (without quotes), or the string representation | |
138 | * of a JSON number, or a JSON string (with quotes and double-escaped). | |
139 | * @param p_str_len [out] The character length of the token data (if there is data) | |
140 | * @return The number of characters extracted | |
141 | * @note The token data is not copied, *p_token_str will point to the start of the | |
142 | * data in the tokenizer's buffer. */ | |
143 | int get_next_token(json_token_t* p_token, char** p_token_str, size_t* p_str_len); | |
144 | ||
145 | /** Gets the current read position in the buffer. | |
146 | * This is where get_next_token() will read from next. */ | |
147 | inline size_t get_buf_pos() { return buf_pos; } | |
148 | ||
149 | /** Sets the current read position in the buffer. | |
150 | * This is where get_next_buffer() will read from next. */ | |
151 | inline void set_buf_pos(const size_t p_buf_pos) { buf_pos = p_buf_pos; } | |
152 | ||
153 | /** Adds the specified JSON token to end of the buffer. | |
154 | * @param p_token [in] Token type | |
155 | * @param p_token_str [in] The name of a JSON object field (without quotes), or | |
156 | * the string representation of a JSON number, or a JSON string (with quotes | |
157 | * and double-escaped). For all the other tokens this parameter will be ignored. | |
158 | * @return The number of characters added to the JSON document */ | |
159 | int put_next_token(json_token_t p_token, const char* p_token_str = 0); | |
160 | ||
3f84031e | 161 | /** Adds raw data to the end of the buffer. |
162 | * @param p_data [in] Pointer to the beginning of the data | |
163 | * @param p_len [in] Length of the data in bytes */ | |
164 | void put_raw_data(const char* p_data, size_t p_len); | |
165 | ||
970ed795 EL |
166 | }; // class JSON_Tokenizer |
167 | ||
168 | // A dummy JSON tokenizer, use when there is no actual JSON document | |
169 | static JSON_Tokenizer DUMMY_BUFFER; | |
170 | ||
3abe9331 | 171 | /** Converts a string into a JSON string by replacing all control characters |
172 | * with JSON escape sequences, if available, or with the \uHHHH escape sequence. | |
173 | * The string is also wrapped inside a set of double quotes and all double quotes | |
174 | * and backslash characters are double-escaped. | |
175 | * | |
176 | * Returns an expstring, that needs to be freed. */ | |
177 | extern char* convert_to_json_string(const char* str); | |
178 | ||
970ed795 EL |
179 | |
180 | #endif /* JSON_TOKENIZER_HH */ | |
181 |