|
1 | 1 | /* |
2 | | - * coreJSON v2.0.0 |
| 2 | + * coreJSON v3.0.0 |
3 | 3 | * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
4 | 4 | * |
5 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of |
@@ -173,17 +173,18 @@ JSONStatus_t JSON_Validate( const char * buf, |
173 | 173 |
|
174 | 174 | /** |
175 | 175 | * @ingroup json_enum_types |
176 | | - * @brief Return codes from coreJSON library functions. |
| 176 | + * @brief Value types from the JSON standard. |
177 | 177 | */ |
178 | 178 | typedef enum |
179 | 179 | { |
180 | | - JSONString = 0, /**< @brief A quote delimited sequence of Unicode characters. */ |
181 | | - JSONNumber, /**< @brief A rational number. */ |
182 | | - JSONTrue, /**< @brief The literal value true. */ |
183 | | - JSONFalse, /**< @brief The literal value false. */ |
184 | | - JSONNull, /**< @brief The literal value null. */ |
185 | | - JSONObject, /**< @brief A collection of zero or more key-value pairs. */ |
186 | | - JSONArray /**< @brief A collection of zero or more values. */ |
| 180 | + JSONInvalid = 0, /**< @brief Not a valid JSON type. */ |
| 181 | + JSONString, /**< @brief A quote delimited sequence of Unicode characters. */ |
| 182 | + JSONNumber, /**< @brief A rational number. */ |
| 183 | + JSONTrue, /**< @brief The literal value true. */ |
| 184 | + JSONFalse, /**< @brief The literal value false. */ |
| 185 | + JSONNull, /**< @brief The literal value null. */ |
| 186 | + JSONObject, /**< @brief A collection of zero or more key-value pairs. */ |
| 187 | + JSONArray /**< @brief A collection of zero or more values. */ |
187 | 188 | } JSONTypes_t; |
188 | 189 |
|
189 | 190 | /** |
@@ -231,4 +232,93 @@ JSONStatus_t JSON_SearchConst( const char * buf, |
231 | 232 | size_t * outValueLength, |
232 | 233 | JSONTypes_t * outType ); |
233 | 234 | /* @[declare_json_searchconst] */ |
| 235 | + |
| 236 | +/** |
| 237 | + * @ingroup json_struct_types |
| 238 | + * @brief Structure to represent a key-value pair. |
| 239 | + */ |
| 240 | +typedef struct |
| 241 | +{ |
| 242 | + const char * key; /**< @brief Pointer to the code point sequence for key. */ |
| 243 | + size_t keyLength; /**< @brief Length of the code point sequence for key. */ |
| 244 | + const char * value; /**< @brief Pointer to the code point sequence for value. */ |
| 245 | + size_t valueLength; /**< @brief Length of the code point sequence for value. */ |
| 246 | + JSONTypes_t jsonType; /**< @brief JSON-specific type of the value. */ |
| 247 | +} JSONPair_t; |
| 248 | + |
| 249 | +/** |
| 250 | + * @brief Output the next key-value pair or value from a collection. |
| 251 | + * |
| 252 | + * This function may be used in a loop to output each key-value pair from an object, |
| 253 | + * or each value from an array. For the first invocation, the integers pointed to by |
| 254 | + * start and next should be initialized to 0. These will be updated by the function. |
| 255 | + * If another key-value pair or value is present, the output structure is populated |
| 256 | + * and #JSONSuccess is returned; otherwise the structure is unchanged and #JSONNotFound |
| 257 | + * is returned. |
| 258 | + * |
| 259 | + * @param[in] buf The buffer to search. |
| 260 | + * @param[in] max size of the buffer. |
| 261 | + * @param[in,out] start The index at which the collection begins. |
| 262 | + * @param[in,out] next The index at which to seek the next value. |
| 263 | + * @param[out] outPair A pointer to receive the next key-value pair. |
| 264 | + * |
| 265 | + * @note This function expects a valid JSON document; run JSON_Validate() first. |
| 266 | + * |
| 267 | + * @note For an object, the outPair structure will reference a key and its value. |
| 268 | + * For an array, only the value will be referenced (i.e., outPair.key will be NULL). |
| 269 | + * |
| 270 | + * @return #JSONSuccess if a value is output; |
| 271 | + * #JSONIllegalDocument if the buffer does not contain a collection; |
| 272 | + * #JSONNotFound if there are no further values in the collection. |
| 273 | + * |
| 274 | + * <b>Example</b> |
| 275 | + * @code{c} |
| 276 | + * // Variables used in this example. |
| 277 | + * static char * json_types[] = |
| 278 | + * { |
| 279 | + * "invalid", |
| 280 | + * "string", |
| 281 | + * "number", |
| 282 | + * "true", |
| 283 | + * "false", |
| 284 | + * "null", |
| 285 | + * "object", |
| 286 | + * "array" |
| 287 | + * }; |
| 288 | + * |
| 289 | + * void show( const char * json, |
| 290 | + * size_t length ) |
| 291 | + * { |
| 292 | + * size_t start = 0, next = 0; |
| 293 | + * JSONPair_t pair = { 0 }; |
| 294 | + * JSONStatus_t result; |
| 295 | + * |
| 296 | + * result = JSON_Validate( json, length ); |
| 297 | + * if( result == JSONSuccess ) |
| 298 | + * { |
| 299 | + * result = JSON_Iterate( json, length, &start, &next, &pair ); |
| 300 | + * } |
| 301 | + * |
| 302 | + * while( result == JSONSuccess ) |
| 303 | + * { |
| 304 | + * if( pair.key != NULL ) |
| 305 | + * { |
| 306 | + * printf( "key: %.*s\t", ( int ) pair.keyLength, pair.key ); |
| 307 | + * } |
| 308 | + * |
| 309 | + * printf( "value: (%s) %.*s\n", json_types[ pair.jsonType ], |
| 310 | + * ( int ) pair.valueLength, pair.value ); |
| 311 | + * |
| 312 | + * result = JSON_Iterate( json, length, &start, &next, &pair ); |
| 313 | + * } |
| 314 | + * } |
| 315 | + * @endcode |
| 316 | + */ |
| 317 | +/* @[declare_json_iterate] */ |
| 318 | +JSONStatus_t JSON_Iterate( const char * buf, |
| 319 | + size_t max, |
| 320 | + size_t * start, |
| 321 | + size_t * next, |
| 322 | + JSONPair_t * outPair ); |
| 323 | +/* @[declare_json_iterate] */ |
234 | 324 | #endif /* ifndef CORE_JSON_H_ */ |
0 commit comments