The Pubnub C-core library. It's home is on https://github.com/pubnub/c_core, this is a copy
Dependents: Pubnub_c_core_mbed2_pal Pubnub_c_core_mbed2_pal Pubnub_c_core_mbed2_pal2
pubnub_json_parse.h@2:d85e42c1125d, 2016-11-22 (annotated)
- Committer:
- sveljko
- Date:
- Tue Nov 22 22:21:39 2016 +0000
- Revision:
- 2:d85e42c1125d
- Parent:
- 0:d13755cfb705
Added `pubnub_helper` module
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sveljko | 0:d13755cfb705 | 1 | /* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */ |
sveljko | 0:d13755cfb705 | 2 | #if !defined INC_PUBNUB_JSON_PARSE |
sveljko | 0:d13755cfb705 | 3 | #define INC_PUBNUB_JSON_PARSE |
sveljko | 0:d13755cfb705 | 4 | |
sveljko | 0:d13755cfb705 | 5 | #include <stdbool.h> |
sveljko | 0:d13755cfb705 | 6 | |
sveljko | 0:d13755cfb705 | 7 | |
sveljko | 0:d13755cfb705 | 8 | /** @file pubnub_json_parse.h |
sveljko | 0:d13755cfb705 | 9 | |
sveljko | 0:d13755cfb705 | 10 | A bunch of functions for parsing JSON. These are designed for |
sveljko | 0:d13755cfb705 | 11 | internal use by the Pubnub client, but, since they are rather |
sveljko | 0:d13755cfb705 | 12 | generic, the user can use them, too, though their availability and |
sveljko | 0:d13755cfb705 | 13 | interface are not guaranteed to survive Pubnub client version |
sveljko | 0:d13755cfb705 | 14 | changes. |
sveljko | 0:d13755cfb705 | 15 | */ |
sveljko | 0:d13755cfb705 | 16 | |
sveljko | 0:d13755cfb705 | 17 | /** A representation of a JSON element. */ |
sveljko | 0:d13755cfb705 | 18 | struct pbjson_elem { |
sveljko | 0:d13755cfb705 | 19 | /** The start of the element - pointer to the first |
sveljko | 0:d13755cfb705 | 20 | character. */ |
sveljko | 0:d13755cfb705 | 21 | char const *start; |
sveljko | 0:d13755cfb705 | 22 | /** The end of the element - pointer to the character |
sveljko | 0:d13755cfb705 | 23 | _after_ the last character */ |
sveljko | 0:d13755cfb705 | 24 | char const *end; |
sveljko | 0:d13755cfb705 | 25 | }; |
sveljko | 0:d13755cfb705 | 26 | |
sveljko | 0:d13755cfb705 | 27 | |
sveljko | 0:d13755cfb705 | 28 | /** Results of parsing a JSON (object) definition */ |
sveljko | 0:d13755cfb705 | 29 | enum pbjson_object_name_parse_result { |
sveljko | 0:d13755cfb705 | 30 | /** No starting curly brace `{` */ |
sveljko | 0:d13755cfb705 | 31 | jonmpNoStartCurly, |
sveljko | 0:d13755cfb705 | 32 | /** Key is missing from a JSON object definition */ |
sveljko | 0:d13755cfb705 | 33 | jonmpKeyMissing, |
sveljko | 0:d13755cfb705 | 34 | /** Key is not a string (this forbidden by JSON) */ |
sveljko | 0:d13755cfb705 | 35 | jonmpKeyNotString, |
sveljko | 0:d13755cfb705 | 36 | /** String not terminated (no end `"`) */ |
sveljko | 0:d13755cfb705 | 37 | jonmpStringNotTerminated, |
sveljko | 0:d13755cfb705 | 38 | /** Colon (`:`) is missing in JSON object definition */ |
sveljko | 0:d13755cfb705 | 39 | jonmpMissingColon, |
sveljko | 0:d13755cfb705 | 40 | /** Ending curly brace `}` is missing */ |
sveljko | 0:d13755cfb705 | 41 | jonmpObjectIncomplete, |
sveljko | 0:d13755cfb705 | 42 | /** A comma `,`, delimiting key-value pairs in a JSON |
sveljko | 0:d13755cfb705 | 43 | object, is missing.*/ |
sveljko | 0:d13755cfb705 | 44 | jonmpMissingValueSeparator, |
sveljko | 0:d13755cfb705 | 45 | /** The key was not found in the JSON object definition */ |
sveljko | 0:d13755cfb705 | 46 | jonmpKeyNotFound, |
sveljko | 0:d13755cfb705 | 47 | /** The name of the key is empty or otherwise not valid */ |
sveljko | 0:d13755cfb705 | 48 | jonmpInvalidKeyName, |
sveljko | 0:d13755cfb705 | 49 | /** Parsed OK, JSON (object) is valid */ |
sveljko | 0:d13755cfb705 | 50 | jonmpOK |
sveljko | 0:d13755cfb705 | 51 | }; |
sveljko | 0:d13755cfb705 | 52 | |
sveljko | 0:d13755cfb705 | 53 | |
sveljko | 0:d13755cfb705 | 54 | /** Skips whitespace starting from @p start, until @p end. |
sveljko | 0:d13755cfb705 | 55 | Interprets whitespace as JSON does - that should be |
sveljko | 0:d13755cfb705 | 56 | compatible with a lot of other specifications. |
sveljko | 0:d13755cfb705 | 57 | |
sveljko | 0:d13755cfb705 | 58 | @return Pointer to the first character that is not whitespace. |
sveljko | 0:d13755cfb705 | 59 | It is == @p end if the whole input is skipped. |
sveljko | 0:d13755cfb705 | 60 | */ |
sveljko | 0:d13755cfb705 | 61 | char const* pbjson_skip_whitespace(char const *start, char const *end); |
sveljko | 0:d13755cfb705 | 62 | |
sveljko | 0:d13755cfb705 | 63 | |
sveljko | 0:d13755cfb705 | 64 | /** Finds the end of the string starting from @p start, until @p end. |
sveljko | 0:d13755cfb705 | 65 | Interprets string as JSON does (starting and ending with |
sveljko | 0:d13755cfb705 | 66 | double-quotation and allowing escape characters with backslash) - |
sveljko | 0:d13755cfb705 | 67 | that should be compatible with a lot of other specifications. |
sveljko | 0:d13755cfb705 | 68 | |
sveljko | 0:d13755cfb705 | 69 | Assumes that @p start points to the first character of the string |
sveljko | 0:d13755cfb705 | 70 | (that is, one past the opening double-quote). |
sveljko | 0:d13755cfb705 | 71 | |
sveljko | 0:d13755cfb705 | 72 | @return Pointer to the double-quotation character that is the end |
sveljko | 0:d13755cfb705 | 73 | of string from input. It is == @p end if the end of the string |
sveljko | 0:d13755cfb705 | 74 | was not found in input. |
sveljko | 0:d13755cfb705 | 75 | */ |
sveljko | 0:d13755cfb705 | 76 | |
sveljko | 0:d13755cfb705 | 77 | char const* pbjson_find_end_string(char const *start, char const *end); |
sveljko | 0:d13755cfb705 | 78 | |
sveljko | 0:d13755cfb705 | 79 | |
sveljko | 0:d13755cfb705 | 80 | /** Finds the end of the "primitive" value starting from @p start, |
sveljko | 0:d13755cfb705 | 81 | until @p end. Interprets "primitive value" a little more broadly |
sveljko | 0:d13755cfb705 | 82 | than JSON does (basically, we allow anything that is not a JSON |
sveljko | 0:d13755cfb705 | 83 | string, array or object) - that should be compatible with a lot of |
sveljko | 0:d13755cfb705 | 84 | other specifications. |
sveljko | 0:d13755cfb705 | 85 | |
sveljko | 0:d13755cfb705 | 86 | Assumes that @p start points to the first character of the |
sveljko | 0:d13755cfb705 | 87 | primitive. |
sveljko | 0:d13755cfb705 | 88 | |
sveljko | 0:d13755cfb705 | 89 | @return Pointer to the character that is the end of the primitive. |
sveljko | 0:d13755cfb705 | 90 | It is == @p end if the end of the primitive was not found in |
sveljko | 0:d13755cfb705 | 91 | input. |
sveljko | 0:d13755cfb705 | 92 | */ |
sveljko | 0:d13755cfb705 | 93 | char const *pbjson_find_end_primitive(char const *start, char const *end); |
sveljko | 0:d13755cfb705 | 94 | |
sveljko | 0:d13755cfb705 | 95 | |
sveljko | 0:d13755cfb705 | 96 | /** Finds the end of the "complex" value starting from @p start, until |
sveljko | 0:d13755cfb705 | 97 | @p end. Interprets "complex value" as a JSON object or array - |
sveljko | 0:d13755cfb705 | 98 | that should be compatible with a lot of other specifications. |
sveljko | 0:d13755cfb705 | 99 | |
sveljko | 0:d13755cfb705 | 100 | Assumes that @p start points to the first character of the |
sveljko | 0:d13755cfb705 | 101 | complex object (opening curly brace or square bracket). |
sveljko | 0:d13755cfb705 | 102 | |
sveljko | 0:d13755cfb705 | 103 | @return Pointer to the character that is the end of the complex. |
sveljko | 0:d13755cfb705 | 104 | It is == @p end if the end of the complex was not found in |
sveljko | 0:d13755cfb705 | 105 | input. |
sveljko | 0:d13755cfb705 | 106 | */ |
sveljko | 0:d13755cfb705 | 107 | char const *pbjson_find_end_complex(char const *start, char const *end); |
sveljko | 0:d13755cfb705 | 108 | |
sveljko | 0:d13755cfb705 | 109 | |
sveljko | 0:d13755cfb705 | 110 | /** Finds the end of the JSON element starting from @p start, until |
sveljko | 0:d13755cfb705 | 111 | @p end. Interprets element as JSON does (primitive, object or array) - |
sveljko | 0:d13755cfb705 | 112 | that should be compatible with a lot of other specifications. |
sveljko | 0:d13755cfb705 | 113 | |
sveljko | 0:d13755cfb705 | 114 | Assumes that @p start points to the first character of the |
sveljko | 0:d13755cfb705 | 115 | element. |
sveljko | 0:d13755cfb705 | 116 | |
sveljko | 0:d13755cfb705 | 117 | @return Pointer to the character that is the end of the element. |
sveljko | 0:d13755cfb705 | 118 | It is == @p end if the end of the element was not found in |
sveljko | 0:d13755cfb705 | 119 | input. |
sveljko | 0:d13755cfb705 | 120 | */ |
sveljko | 0:d13755cfb705 | 121 | char const *pbjson_find_end_element(char const *start, char const *end); |
sveljko | 0:d13755cfb705 | 122 | |
sveljko | 0:d13755cfb705 | 123 | |
sveljko | 0:d13755cfb705 | 124 | /** Gets the value from a JSON object from @p p, with the key @p name |
sveljko | 0:d13755cfb705 | 125 | and puts it to @p parsed o success, returning jonmpOK. On failure, |
sveljko | 0:d13755cfb705 | 126 | returns the error code and the effects on @p parsed are not |
sveljko | 0:d13755cfb705 | 127 | defined. |
sveljko | 0:d13755cfb705 | 128 | */ |
sveljko | 0:d13755cfb705 | 129 | enum pbjson_object_name_parse_result pbjson_get_object_value(struct pbjson_elem const *p, char const *name, struct pbjson_elem *parsed); |
sveljko | 0:d13755cfb705 | 130 | |
sveljko | 0:d13755cfb705 | 131 | |
sveljko | 0:d13755cfb705 | 132 | /** Helper function, returns whether string @p s is equal to the |
sveljko | 0:d13755cfb705 | 133 | contents of the JSON element @p e. |
sveljko | 0:d13755cfb705 | 134 | */ |
sveljko | 0:d13755cfb705 | 135 | bool pbjson_elem_equals_string(struct pbjson_elem const *e, char const *s); |
sveljko | 0:d13755cfb705 | 136 | |
sveljko | 0:d13755cfb705 | 137 | /** Helper function, returns a string describing an enum for |
sveljko | 0:d13755cfb705 | 138 | the JSON (object) parse result. |
sveljko | 0:d13755cfb705 | 139 | */ |
sveljko | 0:d13755cfb705 | 140 | char const *pbjson_object_name_parse_result_2_string(enum pbjson_object_name_parse_result e); |
sveljko | 0:d13755cfb705 | 141 | |
sveljko | 0:d13755cfb705 | 142 | |
sveljko | 0:d13755cfb705 | 143 | #endif /* !defined INC_PUBNUB_JSON_PARSE */ |