JSON parsing library by Andrii Mamchur https://github.com/amamchur/jsonlite

Dependents:   M2X_dev MTS_M2x_Example1 MTS_M2x_Example m2x-demo-all ... more

Committer:
Josh Hollenbeck
Date:
Fri Apr 25 02:12:53 2014 -0700
Revision:
1:807034181e02
Parent:
0:01a2f8de46c8
updated jsonlite to ver 1.1.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
citrusbyte 0:01a2f8de46c8 1 //
citrusbyte 0:01a2f8de46c8 2 // Copyright 2012-2013, Andrii Mamchur
citrusbyte 0:01a2f8de46c8 3 //
citrusbyte 0:01a2f8de46c8 4 // Licensed under the Apache License, Version 2.0 (the "License");
citrusbyte 0:01a2f8de46c8 5 // you may not use this file except in compliance with the License.
citrusbyte 0:01a2f8de46c8 6 // You may obtain a copy of the License at
citrusbyte 0:01a2f8de46c8 7 //
citrusbyte 0:01a2f8de46c8 8 // http://www.apache.org/licenses/LICENSE-2.0
citrusbyte 0:01a2f8de46c8 9 //
citrusbyte 0:01a2f8de46c8 10 // Unless required by applicable law or agreed to in writing, software
citrusbyte 0:01a2f8de46c8 11 // distributed under the License is distributed on an "AS IS" BASIS,
citrusbyte 0:01a2f8de46c8 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
citrusbyte 0:01a2f8de46c8 13 // See the License for the specific language governing permissions and
citrusbyte 0:01a2f8de46c8 14 // limitations under the License
citrusbyte 0:01a2f8de46c8 15
citrusbyte 0:01a2f8de46c8 16 #ifndef JSONLITE_H
citrusbyte 0:01a2f8de46c8 17 #define JSONLITE_H
citrusbyte 0:01a2f8de46c8 18
Josh Hollenbeck 1:807034181e02 19 // #include "jsonlite_builder.h"
Josh Hollenbeck 1:807034181e02 20 //
Josh Hollenbeck 1:807034181e02 21 // Copyright 2012-2013, Andrii Mamchur
Josh Hollenbeck 1:807034181e02 22 //
Josh Hollenbeck 1:807034181e02 23 // Licensed under the Apache License, Version 2.0 (the "License");
Josh Hollenbeck 1:807034181e02 24 // you may not use this file except in compliance with the License.
Josh Hollenbeck 1:807034181e02 25 // You may obtain a copy of the License at
Josh Hollenbeck 1:807034181e02 26 //
Josh Hollenbeck 1:807034181e02 27 // http://www.apache.org/licenses/LICENSE-2.0
Josh Hollenbeck 1:807034181e02 28 //
Josh Hollenbeck 1:807034181e02 29 // Unless required by applicable law or agreed to in writing, software
Josh Hollenbeck 1:807034181e02 30 // distributed under the License is distributed on an "AS IS" BASIS,
Josh Hollenbeck 1:807034181e02 31 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Josh Hollenbeck 1:807034181e02 32 // See the License for the specific language governing permissions and
Josh Hollenbeck 1:807034181e02 33 // limitations under the License
Josh Hollenbeck 1:807034181e02 34
Josh Hollenbeck 1:807034181e02 35 #ifndef JSONLITE_BUILDER_H
Josh Hollenbeck 1:807034181e02 36 #define JSONLITE_BUILDER_H
Josh Hollenbeck 1:807034181e02 37
Josh Hollenbeck 1:807034181e02 38 #include <stdio.h>
Josh Hollenbeck 1:807034181e02 39 // #include "jsonlite_types.h"
Josh Hollenbeck 1:807034181e02 40 //
Josh Hollenbeck 1:807034181e02 41 // Copyright 2012-2013, Andrii Mamchur
Josh Hollenbeck 1:807034181e02 42 //
Josh Hollenbeck 1:807034181e02 43 // Licensed under the Apache License, Version 2.0 (the "License");
Josh Hollenbeck 1:807034181e02 44 // you may not use this file except in compliance with the License.
Josh Hollenbeck 1:807034181e02 45 // You may obtain a copy of the License at
Josh Hollenbeck 1:807034181e02 46 //
Josh Hollenbeck 1:807034181e02 47 // http://www.apache.org/licenses/LICENSE-2.0
Josh Hollenbeck 1:807034181e02 48 //
Josh Hollenbeck 1:807034181e02 49 // Unless required by applicable law or agreed to in writing, software
Josh Hollenbeck 1:807034181e02 50 // distributed under the License is distributed on an "AS IS" BASIS,
Josh Hollenbeck 1:807034181e02 51 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Josh Hollenbeck 1:807034181e02 52 // See the License for the specific language governing permissions and
Josh Hollenbeck 1:807034181e02 53 // limitations under the License
Josh Hollenbeck 1:807034181e02 54
Josh Hollenbeck 1:807034181e02 55 #ifndef JSONLITE_TYPES_H
Josh Hollenbeck 1:807034181e02 56 #define JSONLITE_TYPES_H
Josh Hollenbeck 1:807034181e02 57
Josh Hollenbeck 1:807034181e02 58 typedef enum {
Josh Hollenbeck 1:807034181e02 59 jsonlite_result_unknown = -1,
Josh Hollenbeck 1:807034181e02 60 jsonlite_result_ok,
Josh Hollenbeck 1:807034181e02 61 jsonlite_result_end_of_stream,
Josh Hollenbeck 1:807034181e02 62 jsonlite_result_depth_limit,
Josh Hollenbeck 1:807034181e02 63 jsonlite_result_invalid_argument,
Josh Hollenbeck 1:807034181e02 64 jsonlite_result_expected_object_or_array,
Josh Hollenbeck 1:807034181e02 65 jsonlite_result_expected_value,
Josh Hollenbeck 1:807034181e02 66 jsonlite_result_expected_key_or_end,
Josh Hollenbeck 1:807034181e02 67 jsonlite_result_expected_key,
Josh Hollenbeck 1:807034181e02 68 jsonlite_result_expected_colon,
Josh Hollenbeck 1:807034181e02 69 jsonlite_result_expected_comma_or_end,
Josh Hollenbeck 1:807034181e02 70 jsonlite_result_invalid_escape,
Josh Hollenbeck 1:807034181e02 71 jsonlite_result_invalid_number,
Josh Hollenbeck 1:807034181e02 72 jsonlite_result_invalid_token,
Josh Hollenbeck 1:807034181e02 73 jsonlite_result_invalid_utf8,
Josh Hollenbeck 1:807034181e02 74 jsonlite_result_suspended,
Josh Hollenbeck 1:807034181e02 75
Josh Hollenbeck 1:807034181e02 76 jsonlite_result_not_allowed
Josh Hollenbeck 1:807034181e02 77 } jsonlite_result;
Josh Hollenbeck 1:807034181e02 78
Josh Hollenbeck 1:807034181e02 79
Josh Hollenbeck 1:807034181e02 80 #endif
Josh Hollenbeck 1:807034181e02 81
Josh Hollenbeck 1:807034181e02 82 // #include "jsonlite_stream.h"
Josh Hollenbeck 1:807034181e02 83 //
Josh Hollenbeck 1:807034181e02 84 // Copyright 2012-2013, Andrii Mamchur
Josh Hollenbeck 1:807034181e02 85 //
Josh Hollenbeck 1:807034181e02 86 // Licensed under the Apache License, Version 2.0 (the "License");
Josh Hollenbeck 1:807034181e02 87 // you may not use this file except in compliance with the License.
Josh Hollenbeck 1:807034181e02 88 // You may obtain a copy of the License at
Josh Hollenbeck 1:807034181e02 89 //
Josh Hollenbeck 1:807034181e02 90 // http://www.apache.org/licenses/LICENSE-2.0
Josh Hollenbeck 1:807034181e02 91 //
Josh Hollenbeck 1:807034181e02 92 // Unless required by applicable law or agreed to in writing, software
Josh Hollenbeck 1:807034181e02 93 // distributed under the License is distributed on an "AS IS" BASIS,
Josh Hollenbeck 1:807034181e02 94 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Josh Hollenbeck 1:807034181e02 95 // See the License for the specific language governing permissions and
Josh Hollenbeck 1:807034181e02 96 // limitations under the License
Josh Hollenbeck 1:807034181e02 97
Josh Hollenbeck 1:807034181e02 98 #ifndef JSONLITE_STREAM_H
Josh Hollenbeck 1:807034181e02 99 #define JSONLITE_STREAM_H
Josh Hollenbeck 1:807034181e02 100
Josh Hollenbeck 1:807034181e02 101 #include <stdio.h>
Josh Hollenbeck 1:807034181e02 102 #include <stdint.h>
Josh Hollenbeck 1:807034181e02 103
Josh Hollenbeck 1:807034181e02 104 #ifdef __cplusplus
Josh Hollenbeck 1:807034181e02 105 extern "C" {
Josh Hollenbeck 1:807034181e02 106 #endif
Josh Hollenbeck 1:807034181e02 107
Josh Hollenbeck 1:807034181e02 108 struct jsonlite_stream_struct;
Josh Hollenbeck 1:807034181e02 109 typedef struct jsonlite_stream_struct const * jsonlite_stream;
Josh Hollenbeck 1:807034181e02 110
Josh Hollenbeck 1:807034181e02 111 typedef int (*jsonlite_stream_write_fn)(jsonlite_stream stream, const void *data, size_t length);
Josh Hollenbeck 1:807034181e02 112 typedef void (*jsonlite_stream_release_fn)(jsonlite_stream stream);
Josh Hollenbeck 1:807034181e02 113
Josh Hollenbeck 1:807034181e02 114 int jsonlite_stream_write(jsonlite_stream stream, const void *data, size_t length);
Josh Hollenbeck 1:807034181e02 115 void jsonlite_stream_release(jsonlite_stream stream);
Josh Hollenbeck 1:807034181e02 116
Josh Hollenbeck 1:807034181e02 117 jsonlite_stream jsonlite_mem_stream_init(size_t block_size);
Josh Hollenbeck 1:807034181e02 118 size_t jsonlite_mem_stream_data(jsonlite_stream stream, uint8_t **data, size_t extra_bytes);
Josh Hollenbeck 1:807034181e02 119
Josh Hollenbeck 1:807034181e02 120 jsonlite_stream jsonlite_static_mem_stream_init(void *buffer, size_t size);
Josh Hollenbeck 1:807034181e02 121 size_t jsonlite_static_mem_stream_written_bytes(jsonlite_stream stream);
Josh Hollenbeck 1:807034181e02 122
Josh Hollenbeck 1:807034181e02 123 jsonlite_stream jsonlite_file_stream_init(FILE *file);
Josh Hollenbeck 1:807034181e02 124
Josh Hollenbeck 1:807034181e02 125 extern jsonlite_stream jsonlite_null_stream;
Josh Hollenbeck 1:807034181e02 126 extern jsonlite_stream jsonlite_stdout_stream;
Josh Hollenbeck 1:807034181e02 127
Josh Hollenbeck 1:807034181e02 128 #ifdef __cplusplus
Josh Hollenbeck 1:807034181e02 129 }
Josh Hollenbeck 1:807034181e02 130 #endif
Josh Hollenbeck 1:807034181e02 131
Josh Hollenbeck 1:807034181e02 132 #endif
Josh Hollenbeck 1:807034181e02 133
Josh Hollenbeck 1:807034181e02 134
Josh Hollenbeck 1:807034181e02 135 #ifdef __cplusplus
Josh Hollenbeck 1:807034181e02 136 extern "C" {
Josh Hollenbeck 1:807034181e02 137 #endif
Josh Hollenbeck 1:807034181e02 138
Josh Hollenbeck 1:807034181e02 139 struct jsonlite_builder_struct;
Josh Hollenbeck 1:807034181e02 140 typedef struct jsonlite_builder_struct* jsonlite_builder;
Josh Hollenbeck 1:807034181e02 141
Josh Hollenbeck 1:807034181e02 142 /** @brief Creates and initializes new instance of builder object.
Josh Hollenbeck 1:807034181e02 143 *
Josh Hollenbeck 1:807034181e02 144 * You should release jsonlite_builder object using ::jsonlite_builder_release.
Josh Hollenbeck 1:807034181e02 145 * @see jsonlite_builder
Josh Hollenbeck 1:807034181e02 146 * @see jsonlite_builder_release
Josh Hollenbeck 1:807034181e02 147 * @param depth the builder depth
Josh Hollenbeck 1:807034181e02 148 * @return jsonlite_builder object
Josh Hollenbeck 1:807034181e02 149 */
Josh Hollenbeck 1:807034181e02 150 jsonlite_builder jsonlite_builder_init(size_t depth, jsonlite_stream stream);
Josh Hollenbeck 1:807034181e02 151
Josh Hollenbeck 1:807034181e02 152 /** \brief Releases builder object.
Josh Hollenbeck 1:807034181e02 153 *
Josh Hollenbeck 1:807034181e02 154 * If builder is NULL, jsonlite_builder_release does nothing.
Josh Hollenbeck 1:807034181e02 155 * @see jsonlite_builder
Josh Hollenbeck 1:807034181e02 156 * @see jsonlite_result
Josh Hollenbeck 1:807034181e02 157 * @param builder the builder object
Josh Hollenbeck 1:807034181e02 158 * @return jsonlite_result_invalid_argument when builder is NULL; otherwise jsonlite_result_ok.
Josh Hollenbeck 1:807034181e02 159 */
Josh Hollenbeck 1:807034181e02 160 jsonlite_result jsonlite_builder_release(jsonlite_builder builder);
Josh Hollenbeck 1:807034181e02 161
Josh Hollenbeck 1:807034181e02 162 /** \brief Sets beautify indentation. Default is 0.
Josh Hollenbeck 1:807034181e02 163 *
Josh Hollenbeck 1:807034181e02 164 * @see jsonlite_builder
Josh Hollenbeck 1:807034181e02 165 * @see jsonlite_result
Josh Hollenbeck 1:807034181e02 166 * @param builder the builder object
Josh Hollenbeck 1:807034181e02 167 * @param indentation the beautify indentation; 0 - disabled
Josh Hollenbeck 1:807034181e02 168 * @return jsonlite_result_invalid_argument when builder is NULL; otherwise jsonlite_result_ok.
Josh Hollenbeck 1:807034181e02 169 */
Josh Hollenbeck 1:807034181e02 170 jsonlite_result jsonlite_builder_set_indentation(jsonlite_builder builder, size_t indentation);
Josh Hollenbeck 1:807034181e02 171
Josh Hollenbeck 1:807034181e02 172 /** \brief Sets format for double values. Default is "%.16g".
Josh Hollenbeck 1:807034181e02 173 *
Josh Hollenbeck 1:807034181e02 174 * jsonlite_builder_set_double_format copies format parameter and you can safety release it.
Josh Hollenbeck 1:807034181e02 175 * @see jsonlite_builder
Josh Hollenbeck 1:807034181e02 176 * @see jsonlite_result
Josh Hollenbeck 1:807034181e02 177 * @param builder the builder object
Josh Hollenbeck 1:807034181e02 178 * @param format the double format; see sprintf function for details
Josh Hollenbeck 1:807034181e02 179 * @return jsonlite_result_invalid_argument when builder or format are NULL; otherwise jsonlite_result_ok.
Josh Hollenbeck 1:807034181e02 180 */
Josh Hollenbeck 1:807034181e02 181 jsonlite_result jsonlite_builder_set_double_format(jsonlite_builder builder, const char *format);
Josh Hollenbeck 1:807034181e02 182
Josh Hollenbeck 1:807034181e02 183 /** \brief Begin JSON object.
Josh Hollenbeck 1:807034181e02 184 *
Josh Hollenbeck 1:807034181e02 185 * @see jsonlite_builder
Josh Hollenbeck 1:807034181e02 186 * @see jsonlite_result
Josh Hollenbeck 1:807034181e02 187 * @param builder the builder object
Josh Hollenbeck 1:807034181e02 188 * @return jsonlite_result_invalid_argument when builder is NULL;
Josh Hollenbeck 1:807034181e02 189 * jsonlite_result_not_allowed when operation is not allowed;
Josh Hollenbeck 1:807034181e02 190 * otherwise jsonlite_result_ok.
Josh Hollenbeck 1:807034181e02 191 */
Josh Hollenbeck 1:807034181e02 192 jsonlite_result jsonlite_builder_object_begin(jsonlite_builder builder);
Josh Hollenbeck 1:807034181e02 193
Josh Hollenbeck 1:807034181e02 194 /** \brief End JSON object.
Josh Hollenbeck 1:807034181e02 195 *
Josh Hollenbeck 1:807034181e02 196 * @see jsonlite_builder
Josh Hollenbeck 1:807034181e02 197 * @see jsonlite_result
Josh Hollenbeck 1:807034181e02 198 * @param builder the builder object
Josh Hollenbeck 1:807034181e02 199 * @return jsonlite_result_invalid_argument when builder is NULL;
Josh Hollenbeck 1:807034181e02 200 * jsonlite_result_not_allowed when operation is not allowed;
Josh Hollenbeck 1:807034181e02 201 * otherwise jsonlite_result_ok.
Josh Hollenbeck 1:807034181e02 202 */
Josh Hollenbeck 1:807034181e02 203 jsonlite_result jsonlite_builder_object_end(jsonlite_builder builder);
Josh Hollenbeck 1:807034181e02 204
Josh Hollenbeck 1:807034181e02 205 /** \brief Begin JSON array.
Josh Hollenbeck 1:807034181e02 206 *
Josh Hollenbeck 1:807034181e02 207 * @see jsonlite_builder
Josh Hollenbeck 1:807034181e02 208 * @see jsonlite_result
Josh Hollenbeck 1:807034181e02 209 * @param builder the builder object
Josh Hollenbeck 1:807034181e02 210 * @return jsonlite_result_invalid_argument when builder is NULL;
Josh Hollenbeck 1:807034181e02 211 * jsonlite_result_not_allowed when operation is not allowed;
Josh Hollenbeck 1:807034181e02 212 * otherwise jsonlite_result_ok.
Josh Hollenbeck 1:807034181e02 213 */
Josh Hollenbeck 1:807034181e02 214 jsonlite_result jsonlite_builder_array_begin(jsonlite_builder builder);
Josh Hollenbeck 1:807034181e02 215
Josh Hollenbeck 1:807034181e02 216 /** \brief End JSON array.
Josh Hollenbeck 1:807034181e02 217 *
Josh Hollenbeck 1:807034181e02 218 * @see jsonlite_builder
Josh Hollenbeck 1:807034181e02 219 * @see jsonlite_result
Josh Hollenbeck 1:807034181e02 220 * @param builder the builder object
Josh Hollenbeck 1:807034181e02 221 * @return jsonlite_result_invalid_argument when builder is NULL;
Josh Hollenbeck 1:807034181e02 222 * jsonlite_result_not_allowed when operation is not allowed;
Josh Hollenbeck 1:807034181e02 223 * otherwise jsonlite_result_ok.
Josh Hollenbeck 1:807034181e02 224 */
Josh Hollenbeck 1:807034181e02 225 jsonlite_result jsonlite_builder_array_end(jsonlite_builder builder);
Josh Hollenbeck 1:807034181e02 226
Josh Hollenbeck 1:807034181e02 227 /** \brief Write JSON key.
Josh Hollenbeck 1:807034181e02 228 *
Josh Hollenbeck 1:807034181e02 229 * jsonlite_builder_key performs two-character sequence escape for
Josh Hollenbeck 1:807034181e02 230 * U+0022, U+005C, U+002F, U+0008, U+000C, U+000A, U+000D and U+0009
Josh Hollenbeck 1:807034181e02 231 * @see jsonlite_builder
Josh Hollenbeck 1:807034181e02 232 * @see jsonlite_result
Josh Hollenbeck 1:807034181e02 233 * @param builder the builder object
Josh Hollenbeck 1:807034181e02 234 * @param data the UTF-8 encoded string
Josh Hollenbeck 1:807034181e02 235 * @param length the string length
Josh Hollenbeck 1:807034181e02 236 * @return jsonlite_result_invalid_argument when builder or data are NULL;
Josh Hollenbeck 1:807034181e02 237 * jsonlite_result_not_allowed when operation is not allowed;
Josh Hollenbeck 1:807034181e02 238 * otherwise jsonlite_result_ok.
Josh Hollenbeck 1:807034181e02 239 */
Josh Hollenbeck 1:807034181e02 240 jsonlite_result jsonlite_builder_key(jsonlite_builder builder, const char *data, size_t length);
Josh Hollenbeck 1:807034181e02 241
Josh Hollenbeck 1:807034181e02 242 /** \brief Write string value.
Josh Hollenbeck 1:807034181e02 243 *
Josh Hollenbeck 1:807034181e02 244 * jsonlite_builder_key performs two-character sequence escape for
Josh Hollenbeck 1:807034181e02 245 * U+0022, U+005C, U+002F, U+0008, U+000C, U+000A, U+000D and U+0009
Josh Hollenbeck 1:807034181e02 246 * @see jsonlite_builder
Josh Hollenbeck 1:807034181e02 247 * @see jsonlite_result
Josh Hollenbeck 1:807034181e02 248 * @param builder the builder object
Josh Hollenbeck 1:807034181e02 249 * @param data the UTF-8 encoded string
Josh Hollenbeck 1:807034181e02 250 * @param length the string length
Josh Hollenbeck 1:807034181e02 251 * @return jsonlite_result_invalid_argument when builder or data are NULL;
Josh Hollenbeck 1:807034181e02 252 * jsonlite_result_not_allowed when operation is not allowed;
Josh Hollenbeck 1:807034181e02 253 * otherwise jsonlite_result_ok.
Josh Hollenbeck 1:807034181e02 254 */
Josh Hollenbeck 1:807034181e02 255 jsonlite_result jsonlite_builder_string(jsonlite_builder builder, const char *data, size_t length);
Josh Hollenbeck 1:807034181e02 256
Josh Hollenbeck 1:807034181e02 257 /** \brief Write integer value.
Josh Hollenbeck 1:807034181e02 258 *
Josh Hollenbeck 1:807034181e02 259 * @see jsonlite_builder
Josh Hollenbeck 1:807034181e02 260 * @see jsonlite_result
Josh Hollenbeck 1:807034181e02 261 * @param builder the builder object
Josh Hollenbeck 1:807034181e02 262 * @param value the integer value
Josh Hollenbeck 1:807034181e02 263 * @return jsonlite_result_invalid_argument when builder is NULL;
Josh Hollenbeck 1:807034181e02 264 * jsonlite_result_not_allowed when operation is not allowed;
Josh Hollenbeck 1:807034181e02 265 * otherwise jsonlite_result_ok.
Josh Hollenbeck 1:807034181e02 266 */
Josh Hollenbeck 1:807034181e02 267 jsonlite_result jsonlite_builder_int(jsonlite_builder builder, long long value);
Josh Hollenbeck 1:807034181e02 268
Josh Hollenbeck 1:807034181e02 269 /** \brief Write double value.
Josh Hollenbeck 1:807034181e02 270 *
Josh Hollenbeck 1:807034181e02 271 * @see jsonlite_builder
Josh Hollenbeck 1:807034181e02 272 * @see jsonlite_result
Josh Hollenbeck 1:807034181e02 273 * @param builder the builder object
Josh Hollenbeck 1:807034181e02 274 * @param value the double value
Josh Hollenbeck 1:807034181e02 275 * @return jsonlite_result_invalid_argument when builder is NULL;
Josh Hollenbeck 1:807034181e02 276 * jsonlite_result_not_allowed when operation is not allowed;
Josh Hollenbeck 1:807034181e02 277 * otherwise jsonlite_result_ok.
Josh Hollenbeck 1:807034181e02 278 */
Josh Hollenbeck 1:807034181e02 279 jsonlite_result jsonlite_builder_double(jsonlite_builder builder, double value);
Josh Hollenbeck 1:807034181e02 280
Josh Hollenbeck 1:807034181e02 281 /** \brief Write true value.
Josh Hollenbeck 1:807034181e02 282 *
Josh Hollenbeck 1:807034181e02 283 * @see jsonlite_builder
Josh Hollenbeck 1:807034181e02 284 * @see jsonlite_result
Josh Hollenbeck 1:807034181e02 285 * @param builder the builder object
Josh Hollenbeck 1:807034181e02 286 * @return jsonlite_result_invalid_argument when builder is NULL;
Josh Hollenbeck 1:807034181e02 287 * jsonlite_result_not_allowed when operation is not allowed;
Josh Hollenbeck 1:807034181e02 288 * otherwise jsonlite_result_ok.
Josh Hollenbeck 1:807034181e02 289 */
Josh Hollenbeck 1:807034181e02 290 jsonlite_result jsonlite_builder_true(jsonlite_builder builder);
Josh Hollenbeck 1:807034181e02 291
Josh Hollenbeck 1:807034181e02 292 /** \brief Write false value.
Josh Hollenbeck 1:807034181e02 293 *
Josh Hollenbeck 1:807034181e02 294 * @see jsonlite_builder
Josh Hollenbeck 1:807034181e02 295 * @see jsonlite_result
Josh Hollenbeck 1:807034181e02 296 * @param builder the builder object
Josh Hollenbeck 1:807034181e02 297 * @return jsonlite_result_invalid_argument when builder is NULL;
Josh Hollenbeck 1:807034181e02 298 * jsonlite_result_not_allowed when operation is not allowed;
Josh Hollenbeck 1:807034181e02 299 * otherwise jsonlite_result_ok.
Josh Hollenbeck 1:807034181e02 300 */
Josh Hollenbeck 1:807034181e02 301 jsonlite_result jsonlite_builder_false(jsonlite_builder builder);
Josh Hollenbeck 1:807034181e02 302
Josh Hollenbeck 1:807034181e02 303 /** \brief Write null value.
Josh Hollenbeck 1:807034181e02 304 *
Josh Hollenbeck 1:807034181e02 305 * @see jsonlite_builder
Josh Hollenbeck 1:807034181e02 306 * @see jsonlite_result
Josh Hollenbeck 1:807034181e02 307 * @param builder the builder object
Josh Hollenbeck 1:807034181e02 308 * @return jsonlite_result_invalid_argument when builder is NULL;
Josh Hollenbeck 1:807034181e02 309 * jsonlite_result_not_allowed when operation is not allowed;
Josh Hollenbeck 1:807034181e02 310 * otherwise jsonlite_result_ok.
Josh Hollenbeck 1:807034181e02 311 */
Josh Hollenbeck 1:807034181e02 312 jsonlite_result jsonlite_builder_null(jsonlite_builder builder);
Josh Hollenbeck 1:807034181e02 313
Josh Hollenbeck 1:807034181e02 314 /** \brief Write raw key.
Josh Hollenbeck 1:807034181e02 315 *
Josh Hollenbeck 1:807034181e02 316 * jsonlite_builder_raw_key does not perform any transformation.
Josh Hollenbeck 1:807034181e02 317 * jsonlite_builder_raw_key wraps raw key with '"' (U+0022).
Josh Hollenbeck 1:807034181e02 318 * If data already was wrapped with '"' use following practice jsonlite_builder_raw_key(d, data + 1, size - 2);
Josh Hollenbeck 1:807034181e02 319 * @see jsonlite_builder
Josh Hollenbeck 1:807034181e02 320 * @see jsonlite_result
Josh Hollenbeck 1:807034181e02 321 * @param builder the builder object
Josh Hollenbeck 1:807034181e02 322 * @param data the raw data
Josh Hollenbeck 1:807034181e02 323 * @param length the data length
Josh Hollenbeck 1:807034181e02 324 * @return jsonlite_result_invalid_argument when builder or data are NULL;
Josh Hollenbeck 1:807034181e02 325 * jsonlite_result_not_allowed when operation is not allowed;
Josh Hollenbeck 1:807034181e02 326 * otherwise jsonlite_result_ok.
Josh Hollenbeck 1:807034181e02 327 */
Josh Hollenbeck 1:807034181e02 328 jsonlite_result jsonlite_builder_raw_key(jsonlite_builder builder, const void *data, size_t length);
Josh Hollenbeck 1:807034181e02 329
Josh Hollenbeck 1:807034181e02 330 /** \brief Write raw string.
Josh Hollenbeck 1:807034181e02 331 *
Josh Hollenbeck 1:807034181e02 332 * jsonlite_builder_raw_string does not perform any transformation.
Josh Hollenbeck 1:807034181e02 333 * jsonlite_builder_raw_string wraps raw string with '"' (U+0022).
Josh Hollenbeck 1:807034181e02 334 * If data already was wrapped with '"' use following practice jsonlite_builder_raw_string(d, data + 1, size - 2);
Josh Hollenbeck 1:807034181e02 335 * @see jsonlite_builder
Josh Hollenbeck 1:807034181e02 336 * @see jsonlite_result
Josh Hollenbeck 1:807034181e02 337 * @param builder the builder object
Josh Hollenbeck 1:807034181e02 338 * @param data the raw data
Josh Hollenbeck 1:807034181e02 339 * @param length the data length
Josh Hollenbeck 1:807034181e02 340 * @return jsonlite_result_invalid_argument when builder or data are NULL;
Josh Hollenbeck 1:807034181e02 341 * jsonlite_result_not_allowed when operation is not allowed;
Josh Hollenbeck 1:807034181e02 342 * otherwise jsonlite_result_ok.
Josh Hollenbeck 1:807034181e02 343 */
Josh Hollenbeck 1:807034181e02 344 jsonlite_result jsonlite_builder_raw_string(jsonlite_builder builder, const void *data, size_t length);
Josh Hollenbeck 1:807034181e02 345
Josh Hollenbeck 1:807034181e02 346 /** \brief Write raw value.
Josh Hollenbeck 1:807034181e02 347 *
Josh Hollenbeck 1:807034181e02 348 * jsonlite_builder_raw_value does not perform any transformation.
Josh Hollenbeck 1:807034181e02 349 * jsonlite_builder_raw_value does not wrap raw value with '"' (U+0022).
Josh Hollenbeck 1:807034181e02 350 * @see jsonlite_builder
Josh Hollenbeck 1:807034181e02 351 * @see jsonlite_result
Josh Hollenbeck 1:807034181e02 352 * @param builder the builder object
Josh Hollenbeck 1:807034181e02 353 * @param data the raw data
Josh Hollenbeck 1:807034181e02 354 * @param length the data length
Josh Hollenbeck 1:807034181e02 355 * @return jsonlite_result_invalid_argument when builder or data are NULL;
Josh Hollenbeck 1:807034181e02 356 * jsonlite_result_not_allowed when operation is not allowed;
Josh Hollenbeck 1:807034181e02 357 * otherwise jsonlite_result_ok.
Josh Hollenbeck 1:807034181e02 358 */
Josh Hollenbeck 1:807034181e02 359 jsonlite_result jsonlite_builder_raw_value(jsonlite_builder builder, const void *data, size_t length);
Josh Hollenbeck 1:807034181e02 360
Josh Hollenbeck 1:807034181e02 361 jsonlite_result jsonlite_builder_base64_value(jsonlite_builder builder, const void *data, size_t length);
Josh Hollenbeck 1:807034181e02 362
Josh Hollenbeck 1:807034181e02 363 #ifdef __cplusplus
Josh Hollenbeck 1:807034181e02 364 }
Josh Hollenbeck 1:807034181e02 365 #endif
Josh Hollenbeck 1:807034181e02 366
Josh Hollenbeck 1:807034181e02 367 #endif
Josh Hollenbeck 1:807034181e02 368
citrusbyte 0:01a2f8de46c8 369 // #include "jsonlite_parser.h"
citrusbyte 0:01a2f8de46c8 370 //
citrusbyte 0:01a2f8de46c8 371 // Copyright 2012-2013, Andrii Mamchur
citrusbyte 0:01a2f8de46c8 372 //
citrusbyte 0:01a2f8de46c8 373 // Licensed under the Apache License, Version 2.0 (the "License");
citrusbyte 0:01a2f8de46c8 374 // you may not use this file except in compliance with the License.
citrusbyte 0:01a2f8de46c8 375 // You may obtain a copy of the License at
citrusbyte 0:01a2f8de46c8 376 //
citrusbyte 0:01a2f8de46c8 377 // http://www.apache.org/licenses/LICENSE-2.0
citrusbyte 0:01a2f8de46c8 378 //
citrusbyte 0:01a2f8de46c8 379 // Unless required by applicable law or agreed to in writing, software
citrusbyte 0:01a2f8de46c8 380 // distributed under the License is distributed on an "AS IS" BASIS,
citrusbyte 0:01a2f8de46c8 381 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
citrusbyte 0:01a2f8de46c8 382 // See the License for the specific language governing permissions and
citrusbyte 0:01a2f8de46c8 383 // limitations under the License
citrusbyte 0:01a2f8de46c8 384
citrusbyte 0:01a2f8de46c8 385 #ifndef JSONLITE_PARSER_H
citrusbyte 0:01a2f8de46c8 386 #define JSONLITE_PARSER_H
citrusbyte 0:01a2f8de46c8 387
citrusbyte 0:01a2f8de46c8 388 #include <stdio.h>
citrusbyte 0:01a2f8de46c8 389 #include <stddef.h>
citrusbyte 0:01a2f8de46c8 390 #include <stdint.h>
citrusbyte 0:01a2f8de46c8 391 // #include "jsonlite_token.h"
citrusbyte 0:01a2f8de46c8 392 //
citrusbyte 0:01a2f8de46c8 393 // Copyright 2012-2013, Andrii Mamchur
citrusbyte 0:01a2f8de46c8 394 //
citrusbyte 0:01a2f8de46c8 395 // Licensed under the Apache License, Version 2.0 (the "License");
citrusbyte 0:01a2f8de46c8 396 // you may not use this file except in compliance with the License.
citrusbyte 0:01a2f8de46c8 397 // You may obtain a copy of the License at
citrusbyte 0:01a2f8de46c8 398 //
citrusbyte 0:01a2f8de46c8 399 // http://www.apache.org/licenses/LICENSE-2.0
citrusbyte 0:01a2f8de46c8 400 //
citrusbyte 0:01a2f8de46c8 401 // Unless required by applicable law or agreed to in writing, software
citrusbyte 0:01a2f8de46c8 402 // distributed under the License is distributed on an "AS IS" BASIS,
citrusbyte 0:01a2f8de46c8 403 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
citrusbyte 0:01a2f8de46c8 404 // See the License for the specific language governing permissions and
citrusbyte 0:01a2f8de46c8 405 // limitations under the License
citrusbyte 0:01a2f8de46c8 406
citrusbyte 0:01a2f8de46c8 407 #ifndef JSONLITE_TOKEN_H
citrusbyte 0:01a2f8de46c8 408 #define JSONLITE_TOKEN_H
citrusbyte 0:01a2f8de46c8 409
citrusbyte 0:01a2f8de46c8 410 #include <stdio.h>
citrusbyte 0:01a2f8de46c8 411 #include <stddef.h>
citrusbyte 0:01a2f8de46c8 412 #include <stdint.h>
citrusbyte 0:01a2f8de46c8 413
citrusbyte 0:01a2f8de46c8 414 #ifdef __cplusplus
citrusbyte 0:01a2f8de46c8 415 extern "C" {
citrusbyte 0:01a2f8de46c8 416 #endif
citrusbyte 0:01a2f8de46c8 417 struct jsonlite_token;
citrusbyte 0:01a2f8de46c8 418 struct jsonlite_parser_struct;
citrusbyte 0:01a2f8de46c8 419
citrusbyte 0:01a2f8de46c8 420 /** @brief Provides the hints for number token parsing.
Josh Hollenbeck 1:807034181e02 421 *
citrusbyte 0:01a2f8de46c8 422 * This values is valid for jsonlite_parser_callbacks::number_found callback only.
citrusbyte 0:01a2f8de46c8 423 */
citrusbyte 0:01a2f8de46c8 424 typedef enum {
citrusbyte 0:01a2f8de46c8 425 /** @brief Indicates that number token has integer part.
Josh Hollenbeck 1:807034181e02 426 *
citrusbyte 0:01a2f8de46c8 427 * @note
Josh Hollenbeck 1:807034181e02 428 * This flag is always set because of JSON number always has integer part (value .123 is not allowed).
citrusbyte 0:01a2f8de46c8 429 */
citrusbyte 0:01a2f8de46c8 430 jsonlite_number_int = 0x01,
Josh Hollenbeck 1:807034181e02 431
citrusbyte 0:01a2f8de46c8 432 /** @brief Indicates that number token has fraction part.
citrusbyte 0:01a2f8de46c8 433 *
citrusbyte 0:01a2f8de46c8 434 * This flag will set if token has an fraction part. For example: 123.987;
citrusbyte 0:01a2f8de46c8 435 * in current case fraction part is .987.
citrusbyte 0:01a2f8de46c8 436 */
citrusbyte 0:01a2f8de46c8 437 jsonlite_number_frac = 0x02,
Josh Hollenbeck 1:807034181e02 438
citrusbyte 0:01a2f8de46c8 439 /** @brief Indicates that number token has exponent part.
citrusbyte 0:01a2f8de46c8 440 *
citrusbyte 0:01a2f8de46c8 441 * This flag will set if token has an exponent part.
citrusbyte 0:01a2f8de46c8 442 *
Josh Hollenbeck 1:807034181e02 443 * For example:
citrusbyte 0:01a2f8de46c8 444 * For integer values: 123e5, 123E5, 123E+5, 123e+5;
citrusbyte 0:01a2f8de46c8 445 * all of this numbers are equal to each other and has exponent part.
citrusbyte 0:01a2f8de46c8 446 *
citrusbyte 0:01a2f8de46c8 447 * An other case 12300000 is also equals to previous numbers but has no exponent part.
citrusbyte 0:01a2f8de46c8 448 *
citrusbyte 0:01a2f8de46c8 449 * For floating point values: 123.01e5, 123.01E5, 123.01E+5, 123.01e+5;
citrusbyte 0:01a2f8de46c8 450 * all of this number are equal to each other and has exponent part.
citrusbyte 0:01a2f8de46c8 451 * An other case 12301000 is also equals to previous numbers but has no exponent part.
citrusbyte 0:01a2f8de46c8 452 */
citrusbyte 0:01a2f8de46c8 453 jsonlite_number_exp = 0x04,
Josh Hollenbeck 1:807034181e02 454
citrusbyte 0:01a2f8de46c8 455 /** @brief Indicates that number token has negative value.
citrusbyte 0:01a2f8de46c8 456 *
citrusbyte 0:01a2f8de46c8 457 * This flag will set if token starts with '-' character.
citrusbyte 0:01a2f8de46c8 458 */
citrusbyte 0:01a2f8de46c8 459 jsonlite_number_negative = 0x08,
Josh Hollenbeck 1:807034181e02 460
citrusbyte 0:01a2f8de46c8 461 /** @brief Indicates that number token starts with zero character.
citrusbyte 0:01a2f8de46c8 462 */
citrusbyte 0:01a2f8de46c8 463 jsonlite_number_zero_leading = 0x10,
citrusbyte 0:01a2f8de46c8 464 \
citrusbyte 0:01a2f8de46c8 465 /** @brief Indicates that number token starts with digit that is greater 0.
citrusbyte 0:01a2f8de46c8 466 */
citrusbyte 0:01a2f8de46c8 467 jsonlite_number_digit_leading = 0x20
citrusbyte 0:01a2f8de46c8 468 } jsonlite_number_type;
Josh Hollenbeck 1:807034181e02 469
citrusbyte 0:01a2f8de46c8 470 /** @brief Provides the hints for string token parsing.
citrusbyte 0:01a2f8de46c8 471 *
citrusbyte 0:01a2f8de46c8 472 * This values is valid for jsonlite_parser_callbacks::string_found
citrusbyte 0:01a2f8de46c8 473 * and jsonlite_parser_callbacks::key_found callbacks only.
citrusbyte 0:01a2f8de46c8 474 */
citrusbyte 0:01a2f8de46c8 475 typedef enum {
citrusbyte 0:01a2f8de46c8 476 /** @brief Indicates that string token contains ASCII characters.
citrusbyte 0:01a2f8de46c8 477 *
citrusbyte 0:01a2f8de46c8 478 * @note
citrusbyte 0:01a2f8de46c8 479 * This flag is always set because of JSON string always has ASCII characters.
citrusbyte 0:01a2f8de46c8 480 */
citrusbyte 0:01a2f8de46c8 481 jsonlite_string_ascii = 0x01,
Josh Hollenbeck 1:807034181e02 482
citrusbyte 0:01a2f8de46c8 483 /** @brief Indicates that string token has the sequences of UTF-8 characters.
citrusbyte 0:01a2f8de46c8 484 *
citrusbyte 0:01a2f8de46c8 485 * @note
citrusbyte 0:01a2f8de46c8 486 * This flag will set if string token has 2, 3 or 4 subsequently.
citrusbyte 0:01a2f8de46c8 487 */
citrusbyte 0:01a2f8de46c8 488 jsonlite_string_utf8 = 0x02,
Josh Hollenbeck 1:807034181e02 489
citrusbyte 0:01a2f8de46c8 490 /** @brief Indicates that string token has an escaped character(s).
citrusbyte 0:01a2f8de46c8 491 *
citrusbyte 0:01a2f8de46c8 492 * This flag will be set if string token has one or more following escaped character:
citrusbyte 0:01a2f8de46c8 493 * - \\"
citrusbyte 0:01a2f8de46c8 494 * - \\\\
citrusbyte 0:01a2f8de46c8 495 * - \\n
citrusbyte 0:01a2f8de46c8 496 * - \\r
citrusbyte 0:01a2f8de46c8 497 * - \\/
citrusbyte 0:01a2f8de46c8 498 * - \\b
citrusbyte 0:01a2f8de46c8 499 * - \\f
citrusbyte 0:01a2f8de46c8 500 * - \\t
citrusbyte 0:01a2f8de46c8 501 */
citrusbyte 0:01a2f8de46c8 502 jsonlite_string_escape = 0x04,
Josh Hollenbeck 1:807034181e02 503
citrusbyte 0:01a2f8de46c8 504 /** @brief Indicates that string token has one or more unicode escaped character(s).
citrusbyte 0:01a2f8de46c8 505 *
citrusbyte 0:01a2f8de46c8 506 * This flag will be set if string token has \\uXXXX escape - where (XXXX is an unicode character code)
citrusbyte 0:01a2f8de46c8 507 */
Josh Hollenbeck 1:807034181e02 508 jsonlite_string_unicode_escape = 0x08,
Josh Hollenbeck 1:807034181e02 509
Josh Hollenbeck 1:807034181e02 510
Josh Hollenbeck 1:807034181e02 511 /** @brief Indicates that string token has one or more unicode noncharacter(s).
Josh Hollenbeck 1:807034181e02 512 *
Josh Hollenbeck 1:807034181e02 513 * This flag will be set if string token has \\uFDD0-\\uFDEF and \\uFFFE-\\uFFFF unicode character
Josh Hollenbeck 1:807034181e02 514 */
Josh Hollenbeck 1:807034181e02 515 jsonlite_string_unicode_noncharacter = 0x10
citrusbyte 0:01a2f8de46c8 516 } jsonlite_string_type;
Josh Hollenbeck 1:807034181e02 517
citrusbyte 0:01a2f8de46c8 518 /** @brief Contains information about parsed token.
citrusbyte 0:01a2f8de46c8 519 */
citrusbyte 0:01a2f8de46c8 520 typedef struct jsonlite_token {
citrusbyte 0:01a2f8de46c8 521 /** @brief This variable is reserved for high-level libraries.
citrusbyte 0:01a2f8de46c8 522 */
citrusbyte 0:01a2f8de46c8 523 void *ext;
Josh Hollenbeck 1:807034181e02 524
citrusbyte 0:01a2f8de46c8 525 /** @brief Contains the start position of token.
citrusbyte 0:01a2f8de46c8 526 */
citrusbyte 0:01a2f8de46c8 527 const uint8_t *start;
Josh Hollenbeck 1:807034181e02 528
citrusbyte 0:01a2f8de46c8 529 /** @brief Contains the end position of tokens.
citrusbyte 0:01a2f8de46c8 530 *
citrusbyte 0:01a2f8de46c8 531 * End position does not below to token, it should be interpreted as position of zero character.
citrusbyte 0:01a2f8de46c8 532 * @note
Josh Hollenbeck 1:807034181e02 533 * To measure token length you can use following expression: token->end - token->start.
citrusbyte 0:01a2f8de46c8 534 */
citrusbyte 0:01a2f8de46c8 535 const uint8_t *end;
citrusbyte 0:01a2f8de46c8 536
citrusbyte 0:01a2f8de46c8 537 /** @brief Contains the hints for token parsing.
citrusbyte 0:01a2f8de46c8 538 */
citrusbyte 0:01a2f8de46c8 539 union {
citrusbyte 0:01a2f8de46c8 540 /** @brief Contains the hints for number token parsing.
citrusbyte 0:01a2f8de46c8 541 */
Josh Hollenbeck 1:807034181e02 542 jsonlite_number_type number;
Josh Hollenbeck 1:807034181e02 543
citrusbyte 0:01a2f8de46c8 544 /** @brief Contains the hints for string token parsing.
citrusbyte 0:01a2f8de46c8 545 */
Josh Hollenbeck 1:807034181e02 546 jsonlite_string_type string;
Josh Hollenbeck 1:807034181e02 547 } type;
citrusbyte 0:01a2f8de46c8 548 } jsonlite_token;
citrusbyte 0:01a2f8de46c8 549
Josh Hollenbeck 1:807034181e02 550
citrusbyte 0:01a2f8de46c8 551 /** @brief Returns a size of memory that is required for token conversion to UTF-8 string.
citrusbyte 0:01a2f8de46c8 552 * @param ts jsonlite token
citrusbyte 0:01a2f8de46c8 553 * @return 0 if ts is NULL; otherwise required size of for token conversion.
citrusbyte 0:01a2f8de46c8 554 */
Josh Hollenbeck 1:807034181e02 555 size_t jsonlite_token_size_of_uft8(jsonlite_token *ts);
Josh Hollenbeck 1:807034181e02 556
citrusbyte 0:01a2f8de46c8 557 /** @brief Converts specified token to UTF-8 string.
citrusbyte 0:01a2f8de46c8 558 *
citrusbyte 0:01a2f8de46c8 559 * Function converts specified token to UTF-8 string encoding and copy zero terminated string to buffer.
citrusbyte 0:01a2f8de46c8 560 * @note
citrusbyte 0:01a2f8de46c8 561 * Function will alloc memory by itself if *buffer == NULL.
citrusbyte 0:01a2f8de46c8 562 * In this case you are responsible for memory releasing by using free() function.
citrusbyte 0:01a2f8de46c8 563 * @param ts jsonlite token
citrusbyte 0:01a2f8de46c8 564 * @return length in bytes of converted string.
citrusbyte 0:01a2f8de46c8 565 */
Josh Hollenbeck 1:807034181e02 566 size_t jsonlite_token_to_uft8(jsonlite_token *ts, uint8_t **buffer);
Josh Hollenbeck 1:807034181e02 567
citrusbyte 0:01a2f8de46c8 568 /** @brief Returns a size of memory that is required for token conversion to UTF-16 string.
citrusbyte 0:01a2f8de46c8 569 * @param ts jsonlite token
citrusbyte 0:01a2f8de46c8 570 * @return 0 if ts is NULL; otherwise required size of for token conversion.
citrusbyte 0:01a2f8de46c8 571 */
Josh Hollenbeck 1:807034181e02 572 size_t jsonlite_token_size_of_uft16(jsonlite_token *ts);
Josh Hollenbeck 1:807034181e02 573
citrusbyte 0:01a2f8de46c8 574 /** @brief Converts specified token to UTF-16 string.
citrusbyte 0:01a2f8de46c8 575 *
citrusbyte 0:01a2f8de46c8 576 * Function converts specified token to UTF-16 string encoding and copy zero terminated string to buffer.
citrusbyte 0:01a2f8de46c8 577 * @note
citrusbyte 0:01a2f8de46c8 578 * Function will alloc memory by itself if *buffer == NULL.
citrusbyte 0:01a2f8de46c8 579 * In this case you are responsible for memory releasing by using free() function.
citrusbyte 0:01a2f8de46c8 580 * @param ts jsonlite token
citrusbyte 0:01a2f8de46c8 581 * @return length in bytes of converted string.
citrusbyte 0:01a2f8de46c8 582 */
Josh Hollenbeck 1:807034181e02 583 size_t jsonlite_token_to_uft16(jsonlite_token *ts, uint16_t **buffer);
citrusbyte 0:01a2f8de46c8 584
Josh Hollenbeck 1:807034181e02 585 size_t jsonlite_token_size_of_base64_binary(jsonlite_token *ts);
Josh Hollenbeck 1:807034181e02 586 size_t jsonlite_token_base64_to_binary(jsonlite_token *ts, void **buffer);
Josh Hollenbeck 1:807034181e02 587
Josh Hollenbeck 1:807034181e02 588 long jsonlite_token_to_long(jsonlite_token *token);
Josh Hollenbeck 1:807034181e02 589 long long jsonlite_token_to_long_long(jsonlite_token *token);
Josh Hollenbeck 1:807034181e02 590
citrusbyte 0:01a2f8de46c8 591 #ifdef __cplusplus
citrusbyte 0:01a2f8de46c8 592 }
citrusbyte 0:01a2f8de46c8 593 #endif
citrusbyte 0:01a2f8de46c8 594
citrusbyte 0:01a2f8de46c8 595 #endif
citrusbyte 0:01a2f8de46c8 596
citrusbyte 0:01a2f8de46c8 597 // #include "jsonlite_types.h"
citrusbyte 0:01a2f8de46c8 598
citrusbyte 0:01a2f8de46c8 599
citrusbyte 0:01a2f8de46c8 600 #ifdef __cplusplus
citrusbyte 0:01a2f8de46c8 601 extern "C" {
citrusbyte 0:01a2f8de46c8 602 #endif
Josh Hollenbeck 1:807034181e02 603
citrusbyte 0:01a2f8de46c8 604 struct jsonlite_parser_struct;
citrusbyte 0:01a2f8de46c8 605 typedef struct jsonlite_parser_struct* jsonlite_parser;
Josh Hollenbeck 1:807034181e02 606
citrusbyte 0:01a2f8de46c8 607 /** @brief Contains callback information.
citrusbyte 0:01a2f8de46c8 608 */
citrusbyte 0:01a2f8de46c8 609 typedef struct {
citrusbyte 0:01a2f8de46c8 610 /** @brief jsonlite parser object that initiate callback.
citrusbyte 0:01a2f8de46c8 611 * @note
citrusbyte 0:01a2f8de46c8 612 * You can use ::jsonlite_parser_suspend to stop tokenization.
citrusbyte 0:01a2f8de46c8 613 */
citrusbyte 0:01a2f8de46c8 614 jsonlite_parser parser;
Josh Hollenbeck 1:807034181e02 615
citrusbyte 0:01a2f8de46c8 616 /** @brief Reserved for client usage.
citrusbyte 0:01a2f8de46c8 617 */
citrusbyte 0:01a2f8de46c8 618 void *client_state;
citrusbyte 0:01a2f8de46c8 619 } jsonlite_callback_context;
Josh Hollenbeck 1:807034181e02 620
citrusbyte 0:01a2f8de46c8 621 /** @brief Type of value callback function.
citrusbyte 0:01a2f8de46c8 622 */
citrusbyte 0:01a2f8de46c8 623 typedef void (*jsonlite_value_callback)(jsonlite_callback_context *, jsonlite_token *);
Josh Hollenbeck 1:807034181e02 624
citrusbyte 0:01a2f8de46c8 625 /** @brief Type of state callback function.
citrusbyte 0:01a2f8de46c8 626 */
citrusbyte 0:01a2f8de46c8 627 typedef void (*jsonlite_state_callback)(jsonlite_callback_context *);
Josh Hollenbeck 1:807034181e02 628
citrusbyte 0:01a2f8de46c8 629 /** @brief Contains references to client callback functions.
Josh Hollenbeck 1:807034181e02 630 *
citrusbyte 0:01a2f8de46c8 631 * You can use the global jsonlite_default_callbacks constant to initialize default values.
citrusbyte 0:01a2f8de46c8 632 */
citrusbyte 0:01a2f8de46c8 633 typedef struct {
citrusbyte 0:01a2f8de46c8 634 /** @brief Called when parser finished tokenization.
citrusbyte 0:01a2f8de46c8 635 * You can retrieve result of parsing using jsonlite_parser_get_result.
citrusbyte 0:01a2f8de46c8 636 */
citrusbyte 0:01a2f8de46c8 637 jsonlite_state_callback parse_finished;
Josh Hollenbeck 1:807034181e02 638
citrusbyte 0:01a2f8de46c8 639 /** @brief Called when parser found object start.
citrusbyte 0:01a2f8de46c8 640 */
citrusbyte 0:01a2f8de46c8 641 jsonlite_state_callback object_start;
Josh Hollenbeck 1:807034181e02 642
citrusbyte 0:01a2f8de46c8 643 /** @brief Called when parser found object end.
citrusbyte 0:01a2f8de46c8 644 */
citrusbyte 0:01a2f8de46c8 645 jsonlite_state_callback object_end;
Josh Hollenbeck 1:807034181e02 646
citrusbyte 0:01a2f8de46c8 647 /** @brief Called when parser found array start.
citrusbyte 0:01a2f8de46c8 648 */
citrusbyte 0:01a2f8de46c8 649 jsonlite_state_callback array_start;
Josh Hollenbeck 1:807034181e02 650
citrusbyte 0:01a2f8de46c8 651 /** @brief Called when parser found array end.
citrusbyte 0:01a2f8de46c8 652 */
citrusbyte 0:01a2f8de46c8 653 jsonlite_state_callback array_end;
Josh Hollenbeck 1:807034181e02 654
citrusbyte 0:01a2f8de46c8 655 /** @brief Called when parser found \a true token.
citrusbyte 0:01a2f8de46c8 656 */
citrusbyte 0:01a2f8de46c8 657 jsonlite_state_callback true_found;
Josh Hollenbeck 1:807034181e02 658
citrusbyte 0:01a2f8de46c8 659 /** @brief Called when parser found \a false token.
citrusbyte 0:01a2f8de46c8 660 */
citrusbyte 0:01a2f8de46c8 661 jsonlite_state_callback false_found;
Josh Hollenbeck 1:807034181e02 662
citrusbyte 0:01a2f8de46c8 663 /** @brief Called when parser found \a null token.
citrusbyte 0:01a2f8de46c8 664 */
citrusbyte 0:01a2f8de46c8 665 jsonlite_state_callback null_found;
Josh Hollenbeck 1:807034181e02 666
citrusbyte 0:01a2f8de46c8 667 /** @brief Called when parser found key token.
citrusbyte 0:01a2f8de46c8 668 */
citrusbyte 0:01a2f8de46c8 669 jsonlite_value_callback key_found;
Josh Hollenbeck 1:807034181e02 670
citrusbyte 0:01a2f8de46c8 671 /** @brief Called when parser found string token.
citrusbyte 0:01a2f8de46c8 672 */
citrusbyte 0:01a2f8de46c8 673 jsonlite_value_callback string_found;
Josh Hollenbeck 1:807034181e02 674
citrusbyte 0:01a2f8de46c8 675 /** @brief Called when parser found number token.
citrusbyte 0:01a2f8de46c8 676 */
citrusbyte 0:01a2f8de46c8 677 jsonlite_value_callback number_found;
Josh Hollenbeck 1:807034181e02 678
citrusbyte 0:01a2f8de46c8 679 /** @brief Callbacks' context, will be past as first parameter of callback function.
citrusbyte 0:01a2f8de46c8 680 */
citrusbyte 0:01a2f8de46c8 681 jsonlite_callback_context context;
citrusbyte 0:01a2f8de46c8 682 } jsonlite_parser_callbacks;
citrusbyte 0:01a2f8de46c8 683
citrusbyte 0:01a2f8de46c8 684 /** @brief Estimates memory usage.
citrusbyte 0:01a2f8de46c8 685 * @note
citrusbyte 0:01a2f8de46c8 686 * This value depends on CPU architectures.
citrusbyte 0:01a2f8de46c8 687 * @param depth the parsing depth.
citrusbyte 0:01a2f8de46c8 688 * @return Estimated size in bytes.
citrusbyte 0:01a2f8de46c8 689 */
citrusbyte 0:01a2f8de46c8 690 size_t jsonlite_parser_estimate_size(size_t depth);
citrusbyte 0:01a2f8de46c8 691
citrusbyte 0:01a2f8de46c8 692 /** @brief Creates and initializes new instance of parser object.
citrusbyte 0:01a2f8de46c8 693 *
citrusbyte 0:01a2f8de46c8 694 * You should release jsonlite_parser object using ::jsonlite_parser_release.
citrusbyte 0:01a2f8de46c8 695 * @see jsonlite_parser
citrusbyte 0:01a2f8de46c8 696 * @see jsonlite_parser_release
citrusbyte 0:01a2f8de46c8 697 * @param depth the parsing depth.
citrusbyte 0:01a2f8de46c8 698 * @return jsonlite_parser object.
citrusbyte 0:01a2f8de46c8 699 */
citrusbyte 0:01a2f8de46c8 700 jsonlite_parser jsonlite_parser_init(size_t depth);
Josh Hollenbeck 1:807034181e02 701
Josh Hollenbeck 1:807034181e02 702 /** @brief Initializes memory for parser object.
Josh Hollenbeck 1:807034181e02 703 *
Josh Hollenbeck 1:807034181e02 704 * You should release internal resources using ::jsonlite_parser_cleanup
Josh Hollenbeck 1:807034181e02 705 * @see jsonlite_parser
Josh Hollenbeck 1:807034181e02 706 * @see jsonlite_parser_reset
Josh Hollenbeck 1:807034181e02 707 * @param memory the memory for parser.
Josh Hollenbeck 1:807034181e02 708 * @param size the memory size.
Josh Hollenbeck 1:807034181e02 709 * @return jsonlite_parser object.
Josh Hollenbeck 1:807034181e02 710 */
Josh Hollenbeck 1:807034181e02 711 jsonlite_parser jsonlite_parser_init_memory(void *memory, size_t size);
Josh Hollenbeck 1:807034181e02 712
citrusbyte 0:01a2f8de46c8 713 /** \brief Copies provided callbacks structure to parser object.
citrusbyte 0:01a2f8de46c8 714 * @see jsonlite_parser
citrusbyte 0:01a2f8de46c8 715 * @see jsonlite_parser_callbacks
citrusbyte 0:01a2f8de46c8 716 * @see jsonlite_result
citrusbyte 0:01a2f8de46c8 717 * @param parser the parser object.
citrusbyte 0:01a2f8de46c8 718 * @param parser the callbacks object.
citrusbyte 0:01a2f8de46c8 719 * @return jsonlite_result_invalid_argument when parser or cbs are NULL; otherwise jsonlite_result_ok.
citrusbyte 0:01a2f8de46c8 720 */
citrusbyte 0:01a2f8de46c8 721 jsonlite_result jsonlite_parser_set_callback(jsonlite_parser parser, const jsonlite_parser_callbacks *cbs);
Josh Hollenbeck 1:807034181e02 722
citrusbyte 0:01a2f8de46c8 723 /** \brief Returns result of last operation.
citrusbyte 0:01a2f8de46c8 724 * @see jsonlite_parser
citrusbyte 0:01a2f8de46c8 725 * @see jsonlite_result
citrusbyte 0:01a2f8de46c8 726 * @param parser the parser object.
citrusbyte 0:01a2f8de46c8 727 * @return jsonlite_result_invalid_argument when parser is NULL; otherwise s result of last operation.
citrusbyte 0:01a2f8de46c8 728 */
citrusbyte 0:01a2f8de46c8 729 jsonlite_result jsonlite_parser_get_result(jsonlite_parser parser);
Josh Hollenbeck 1:807034181e02 730
citrusbyte 0:01a2f8de46c8 731 /** \brief Performs JSON tokenization.
citrusbyte 0:01a2f8de46c8 732 *
citrusbyte 0:01a2f8de46c8 733 * jsonlite is a chunk parser and you can use this function to parser a fragment of JSON.
citrusbyte 0:01a2f8de46c8 734 * @see jsonlite_parser
citrusbyte 0:01a2f8de46c8 735 * @see jsonlite_result
citrusbyte 0:01a2f8de46c8 736 * @param parser the parser object.
citrusbyte 0:01a2f8de46c8 737 * @param buffer the pointer to JSON payload buffer.
citrusbyte 0:01a2f8de46c8 738 * @param size the JSON payload buffer size.
citrusbyte 0:01a2f8de46c8 739 * @return JSON parsing result or jsonlite_result_invalid_argument when some parameter is invalid.
Josh Hollenbeck 1:807034181e02 740 *
citrusbyte 0:01a2f8de46c8 741 * There is an example of JSON validation
citrusbyte 0:01a2f8de46c8 742 * @code{.c}
citrusbyte 0:01a2f8de46c8 743 * char json[] = "{\"key\" : 12345, \"obj\": {}, \"array\":[null, true, false, \"string\"]}";
citrusbyte 0:01a2f8de46c8 744 * jsonlite_parser p = jsonlite_parser_init(16);
citrusbyte 0:01a2f8de46c8 745 * jsonlite_result result = jsonlite_parser_tokenize(p, json, sizeof(json));
citrusbyte 0:01a2f8de46c8 746 * assert(result == jsonlite_result_ok);
citrusbyte 0:01a2f8de46c8 747 * jsonlite_parser_release(p);
citrusbyte 0:01a2f8de46c8 748 * @endcode
citrusbyte 0:01a2f8de46c8 749 *
citrusbyte 0:01a2f8de46c8 750 * There is an another example of JSON chunk parsing.
citrusbyte 0:01a2f8de46c8 751 * @code{.c}
citrusbyte 0:01a2f8de46c8 752 * char chunk1[] = "{\"key\" : 12345, \"obj\": {}, \"arr";
citrusbyte 0:01a2f8de46c8 753 * char chunk2[] = "ay\":[null, true, false, \"string\"]}";
citrusbyte 0:01a2f8de46c8 754 * jsonlite_parser p = jsonlite_parser_init(16);
citrusbyte 0:01a2f8de46c8 755 *
citrusbyte 0:01a2f8de46c8 756 * jsonlite_result result = jsonlite_parser_tokenize(p, chunk1, sizeof(chunk1) - 1);
citrusbyte 0:01a2f8de46c8 757 * assert(result == jsonlite_result_end_of_stream);
citrusbyte 0:01a2f8de46c8 758 * // Now you can release or reuse chunk1 buffer.
citrusbyte 0:01a2f8de46c8 759 *
citrusbyte 0:01a2f8de46c8 760 * result = jsonlite_parser_tokenize(p, chunk2, sizeof(chunk2) - 1);
citrusbyte 0:01a2f8de46c8 761 * assert(result == jsonlite_result_ok);
citrusbyte 0:01a2f8de46c8 762 *
citrusbyte 0:01a2f8de46c8 763 * jsonlite_parser_release(p);
citrusbyte 0:01a2f8de46c8 764 * @endcode
citrusbyte 0:01a2f8de46c8 765 */
citrusbyte 0:01a2f8de46c8 766 jsonlite_result jsonlite_parser_tokenize(jsonlite_parser parser, const void *buffer, size_t size);
Josh Hollenbeck 1:807034181e02 767
citrusbyte 0:01a2f8de46c8 768 /** \brief Resumes JSON tokenization.
citrusbyte 0:01a2f8de46c8 769 * @see jsonlite_parser
citrusbyte 0:01a2f8de46c8 770 * @see jsonlite_result
citrusbyte 0:01a2f8de46c8 771 * @param parser the parser object.
citrusbyte 0:01a2f8de46c8 772 * @return JSON parsing result or jsonlite_result_invalid_argument when parser is NULL.
citrusbyte 0:01a2f8de46c8 773 */
citrusbyte 0:01a2f8de46c8 774 jsonlite_result jsonlite_parser_resume(jsonlite_parser parser);
Josh Hollenbeck 1:807034181e02 775
citrusbyte 0:01a2f8de46c8 776 /** \brief Suspends JSON tokenization.
citrusbyte 0:01a2f8de46c8 777 *
citrusbyte 0:01a2f8de46c8 778 * You can continue tokenization later by calling ::jsonlite_parser_resume.
citrusbyte 0:01a2f8de46c8 779 * @see jsonlite_parser
citrusbyte 0:01a2f8de46c8 780 * @see jsonlite_result
citrusbyte 0:01a2f8de46c8 781 * @param parser the parser object.
Josh Hollenbeck 1:807034181e02 782 * @return jsonlite_result_invalid_argument when parser is NULL;
citrusbyte 0:01a2f8de46c8 783 * jsonlite_result_not_allowed when operation is not allowed;
citrusbyte 0:01a2f8de46c8 784 * otherwise jsonlite_result_ok.
citrusbyte 0:01a2f8de46c8 785 */
citrusbyte 0:01a2f8de46c8 786 jsonlite_result jsonlite_parser_suspend(jsonlite_parser parser);
Josh Hollenbeck 1:807034181e02 787
Josh Hollenbeck 1:807034181e02 788 /** \brief Terminate JSON tokenization.
Josh Hollenbeck 1:807034181e02 789 *
Josh Hollenbeck 1:807034181e02 790 * @see jsonlite_parser
Josh Hollenbeck 1:807034181e02 791 * @see jsonlite_result
Josh Hollenbeck 1:807034181e02 792 * @param parser the parser object.
Josh Hollenbeck 1:807034181e02 793 * @return jsonlite_result_invalid_argument when parser is NULL or result is jsonlite_result_unknown;
Josh Hollenbeck 1:807034181e02 794 * otherwise jsonlite_result_ok.
Josh Hollenbeck 1:807034181e02 795 */
Josh Hollenbeck 1:807034181e02 796 jsonlite_result jsonlite_parser_terminate(jsonlite_parser parser, jsonlite_result result);
Josh Hollenbeck 1:807034181e02 797
citrusbyte 0:01a2f8de46c8 798 /** \brief Releases parser object.
citrusbyte 0:01a2f8de46c8 799 *
citrusbyte 0:01a2f8de46c8 800 * If parser is NULL, jsonlite_parser_release does nothing.
citrusbyte 0:01a2f8de46c8 801 * @see jsonlite_parser
citrusbyte 0:01a2f8de46c8 802 * @param parser the parser object.
citrusbyte 0:01a2f8de46c8 803 */
citrusbyte 0:01a2f8de46c8 804 void jsonlite_parser_release(jsonlite_parser parser);
citrusbyte 0:01a2f8de46c8 805
Josh Hollenbeck 1:807034181e02 806 /** \brief Releases internal resources and states.
Josh Hollenbeck 1:807034181e02 807 *
Josh Hollenbeck 1:807034181e02 808 * If parser is NULL, jsonlite_parser_reset does nothing.
Josh Hollenbeck 1:807034181e02 809 * @see jsonlite_parser
Josh Hollenbeck 1:807034181e02 810 * @param parser the parser object.
Josh Hollenbeck 1:807034181e02 811 */
Josh Hollenbeck 1:807034181e02 812 void jsonlite_parser_cleanup(jsonlite_parser parser);
Josh Hollenbeck 1:807034181e02 813
citrusbyte 0:01a2f8de46c8 814 /** \brief jsonlite_parser_callbacks structure initialized with callbacks that do nothing.
citrusbyte 0:01a2f8de46c8 815 */
citrusbyte 0:01a2f8de46c8 816 extern const jsonlite_parser_callbacks jsonlite_default_callbacks;
Josh Hollenbeck 1:807034181e02 817
citrusbyte 0:01a2f8de46c8 818 #ifdef __cplusplus
citrusbyte 0:01a2f8de46c8 819 }
citrusbyte 0:01a2f8de46c8 820 #endif
citrusbyte 0:01a2f8de46c8 821
citrusbyte 0:01a2f8de46c8 822 #endif
citrusbyte 0:01a2f8de46c8 823
Josh Hollenbeck 1:807034181e02 824 // #include "jsonlite_stream.h"
citrusbyte 0:01a2f8de46c8 825
Josh Hollenbeck 1:807034181e02 826 // #include "jsonlite_token.h"
citrusbyte 0:01a2f8de46c8 827
citrusbyte 0:01a2f8de46c8 828 // #include "jsonlite_token_pool.h"
citrusbyte 0:01a2f8de46c8 829 //
citrusbyte 0:01a2f8de46c8 830 // Copyright 2012-2013, Andrii Mamchur
citrusbyte 0:01a2f8de46c8 831 //
citrusbyte 0:01a2f8de46c8 832 // Licensed under the Apache License, Version 2.0 (the "License");
citrusbyte 0:01a2f8de46c8 833 // you may not use this file except in compliance with the License.
citrusbyte 0:01a2f8de46c8 834 // You may obtain a copy of the License at
citrusbyte 0:01a2f8de46c8 835 //
citrusbyte 0:01a2f8de46c8 836 // http://www.apache.org/licenses/LICENSE-2.0
citrusbyte 0:01a2f8de46c8 837 //
citrusbyte 0:01a2f8de46c8 838 // Unless required by applicable law or agreed to in writing, software
citrusbyte 0:01a2f8de46c8 839 // distributed under the License is distributed on an "AS IS" BASIS,
citrusbyte 0:01a2f8de46c8 840 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
citrusbyte 0:01a2f8de46c8 841 // See the License for the specific language governing permissions and
citrusbyte 0:01a2f8de46c8 842 // limitations under the License
citrusbyte 0:01a2f8de46c8 843
citrusbyte 0:01a2f8de46c8 844 #ifndef JSONLITE_TOKEN_POOL_H
citrusbyte 0:01a2f8de46c8 845 #define JSONLITE_TOKEN_POOL_H
citrusbyte 0:01a2f8de46c8 846
citrusbyte 0:01a2f8de46c8 847 // #include "jsonlite_token.h"
citrusbyte 0:01a2f8de46c8 848
citrusbyte 0:01a2f8de46c8 849
citrusbyte 0:01a2f8de46c8 850 #ifdef __cplusplus
citrusbyte 0:01a2f8de46c8 851 extern "C" {
citrusbyte 0:01a2f8de46c8 852 #endif
Josh Hollenbeck 1:807034181e02 853
citrusbyte 0:01a2f8de46c8 854 typedef void (*jsonlite_token_pool_release_value_fn)(void *);
Josh Hollenbeck 1:807034181e02 855 typedef struct jsonlite_token_pool_struct* jsonlite_token_pool;
Josh Hollenbeck 1:807034181e02 856
citrusbyte 0:01a2f8de46c8 857 typedef struct jsonlite_token_bucket {
citrusbyte 0:01a2f8de46c8 858 const uint8_t *start;
citrusbyte 0:01a2f8de46c8 859 const uint8_t *end;
Josh Hollenbeck 1:807034181e02 860
Josh Hollenbeck 1:807034181e02 861 ptrdiff_t hash;
citrusbyte 0:01a2f8de46c8 862 ptrdiff_t value_hash;
Josh Hollenbeck 1:807034181e02 863
Josh Hollenbeck 1:807034181e02 864 const void *value;
citrusbyte 0:01a2f8de46c8 865 } jsonlite_token_bucket;
Josh Hollenbeck 1:807034181e02 866
citrusbyte 0:01a2f8de46c8 867 jsonlite_token_pool jsonlite_token_pool_create(jsonlite_token_pool_release_value_fn release_fn);
citrusbyte 0:01a2f8de46c8 868 void jsonlite_token_pool_copy_tokens(jsonlite_token_pool pool);
citrusbyte 0:01a2f8de46c8 869 void jsonlite_token_pool_release(jsonlite_token_pool pool);
citrusbyte 0:01a2f8de46c8 870 jsonlite_token_bucket* jsonlite_token_pool_get_bucket(jsonlite_token_pool pool, jsonlite_token *token);
citrusbyte 0:01a2f8de46c8 871
citrusbyte 0:01a2f8de46c8 872 #ifdef __cplusplus
citrusbyte 0:01a2f8de46c8 873 }
citrusbyte 0:01a2f8de46c8 874 #endif
citrusbyte 0:01a2f8de46c8 875
citrusbyte 0:01a2f8de46c8 876 #endif
citrusbyte 0:01a2f8de46c8 877
citrusbyte 0:01a2f8de46c8 878
Josh Hollenbeck 1:807034181e02 879 extern const char *jsonlite_version;
citrusbyte 0:01a2f8de46c8 880
Josh Hollenbeck 1:807034181e02 881 #endif