The KPN SenML library helps you create and parse senml documents in both json and cbor format. The library can be used for sending sensor data and receiving actuator commands.
senml_JsonStreamingParser.h@0:a9259748d982, 2018-05-19 (annotated)
- Committer:
- kpniot
- Date:
- Sat May 19 17:35:20 2018 +0000
- Revision:
- 0:a9259748d982
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kpniot | 0:a9259748d982 | 1 | /**The MIT License (MIT) |
kpniot | 0:a9259748d982 | 2 | |
kpniot | 0:a9259748d982 | 3 | Copyright (c) 2015 by Daniel Eichhorn |
kpniot | 0:a9259748d982 | 4 | |
kpniot | 0:a9259748d982 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
kpniot | 0:a9259748d982 | 6 | of this software and associated documentation files (the "Software"), to deal |
kpniot | 0:a9259748d982 | 7 | in the Software without restriction, including without limitation the rights |
kpniot | 0:a9259748d982 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
kpniot | 0:a9259748d982 | 9 | copies of the Software, and to permit persons to whom the Software is |
kpniot | 0:a9259748d982 | 10 | furnished to do so, subject to the following conditions: |
kpniot | 0:a9259748d982 | 11 | |
kpniot | 0:a9259748d982 | 12 | The above copyright notice and this permission notice shall be included in all |
kpniot | 0:a9259748d982 | 13 | copies or substantial portions of the Software. |
kpniot | 0:a9259748d982 | 14 | |
kpniot | 0:a9259748d982 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
kpniot | 0:a9259748d982 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
kpniot | 0:a9259748d982 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
kpniot | 0:a9259748d982 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
kpniot | 0:a9259748d982 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kpniot | 0:a9259748d982 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
kpniot | 0:a9259748d982 | 21 | SOFTWARE. |
kpniot | 0:a9259748d982 | 22 | |
kpniot | 0:a9259748d982 | 23 | See more at http://blog.squix.ch and https://github.com/squix78/json-streaming-parser |
kpniot | 0:a9259748d982 | 24 | */ |
kpniot | 0:a9259748d982 | 25 | |
kpniot | 0:a9259748d982 | 26 | #ifndef SENMLJSONSTREAMINGPARSER |
kpniot | 0:a9259748d982 | 27 | #define SENMLJSONSTREAMINGPARSER |
kpniot | 0:a9259748d982 | 28 | |
kpniot | 0:a9259748d982 | 29 | #ifdef __MBED__ |
kpniot | 0:a9259748d982 | 30 | #include "mbed.h" |
kpniot | 0:a9259748d982 | 31 | #include <string> |
kpniot | 0:a9259748d982 | 32 | using namespace std; |
kpniot | 0:a9259748d982 | 33 | #define String string |
kpniot | 0:a9259748d982 | 34 | #else |
kpniot | 0:a9259748d982 | 35 | #include <Arduino.h> |
kpniot | 0:a9259748d982 | 36 | #endif |
kpniot | 0:a9259748d982 | 37 | #include <senml_JsonListener.h> |
kpniot | 0:a9259748d982 | 38 | |
kpniot | 0:a9259748d982 | 39 | #define STATE_START_DOCUMENT 0 |
kpniot | 0:a9259748d982 | 40 | #define STATE_DONE -1 |
kpniot | 0:a9259748d982 | 41 | #define STATE_IN_ARRAY 1 |
kpniot | 0:a9259748d982 | 42 | #define STATE_IN_OBJECT 2 |
kpniot | 0:a9259748d982 | 43 | #define STATE_END_KEY 3 |
kpniot | 0:a9259748d982 | 44 | #define STATE_AFTER_KEY 4 |
kpniot | 0:a9259748d982 | 45 | #define STATE_IN_STRING 5 |
kpniot | 0:a9259748d982 | 46 | #define STATE_START_ESCAPE 6 |
kpniot | 0:a9259748d982 | 47 | #define STATE_UNICODE 7 |
kpniot | 0:a9259748d982 | 48 | #define STATE_IN_NUMBER 8 |
kpniot | 0:a9259748d982 | 49 | #define STATE_IN_TRUE 9 |
kpniot | 0:a9259748d982 | 50 | #define STATE_IN_FALSE 10 |
kpniot | 0:a9259748d982 | 51 | #define STATE_IN_NULL 11 |
kpniot | 0:a9259748d982 | 52 | #define STATE_AFTER_VALUE 12 |
kpniot | 0:a9259748d982 | 53 | #define STATE_UNICODE_SURROGATE 13 |
kpniot | 0:a9259748d982 | 54 | |
kpniot | 0:a9259748d982 | 55 | #define STACK_OBJECT 0 |
kpniot | 0:a9259748d982 | 56 | #define STACK_ARRAY 1 |
kpniot | 0:a9259748d982 | 57 | #define STACK_KEY 2 |
kpniot | 0:a9259748d982 | 58 | #define STACK_STRING 3 |
kpniot | 0:a9259748d982 | 59 | |
kpniot | 0:a9259748d982 | 60 | #define BUFFER_MAX_LENGTH 512 |
kpniot | 0:a9259748d982 | 61 | |
kpniot | 0:a9259748d982 | 62 | /** |
kpniot | 0:a9259748d982 | 63 | * Internal helper class for parsing json data. |
kpniot | 0:a9259748d982 | 64 | */ |
kpniot | 0:a9259748d982 | 65 | class JsonStreamingParser { |
kpniot | 0:a9259748d982 | 66 | public: |
kpniot | 0:a9259748d982 | 67 | JsonStreamingParser(); |
kpniot | 0:a9259748d982 | 68 | void parse(char c); |
kpniot | 0:a9259748d982 | 69 | void setListener(JsonListener* listener); |
kpniot | 0:a9259748d982 | 70 | void reset(); |
kpniot | 0:a9259748d982 | 71 | |
kpniot | 0:a9259748d982 | 72 | |
kpniot | 0:a9259748d982 | 73 | private: |
kpniot | 0:a9259748d982 | 74 | |
kpniot | 0:a9259748d982 | 75 | int state; |
kpniot | 0:a9259748d982 | 76 | int stack[20]; |
kpniot | 0:a9259748d982 | 77 | int stackPos; |
kpniot | 0:a9259748d982 | 78 | JsonListener* myListener; |
kpniot | 0:a9259748d982 | 79 | |
kpniot | 0:a9259748d982 | 80 | //bool doEmitWhitespace = false; |
kpniot | 0:a9259748d982 | 81 | // fixed length buffer array to prepare for c code |
kpniot | 0:a9259748d982 | 82 | char buffer[BUFFER_MAX_LENGTH]; |
kpniot | 0:a9259748d982 | 83 | int bufferPos; |
kpniot | 0:a9259748d982 | 84 | |
kpniot | 0:a9259748d982 | 85 | char unicodeEscapeBuffer[10]; |
kpniot | 0:a9259748d982 | 86 | int unicodeEscapeBufferPos; |
kpniot | 0:a9259748d982 | 87 | |
kpniot | 0:a9259748d982 | 88 | char unicodeBuffer[10]; |
kpniot | 0:a9259748d982 | 89 | int unicodeBufferPos; |
kpniot | 0:a9259748d982 | 90 | |
kpniot | 0:a9259748d982 | 91 | int characterCounter; |
kpniot | 0:a9259748d982 | 92 | |
kpniot | 0:a9259748d982 | 93 | int unicodeHighSurrogate; |
kpniot | 0:a9259748d982 | 94 | |
kpniot | 0:a9259748d982 | 95 | void increaseBufferPointer(); |
kpniot | 0:a9259748d982 | 96 | |
kpniot | 0:a9259748d982 | 97 | void endString(); |
kpniot | 0:a9259748d982 | 98 | |
kpniot | 0:a9259748d982 | 99 | void endArray(); |
kpniot | 0:a9259748d982 | 100 | |
kpniot | 0:a9259748d982 | 101 | void startValue(char c); |
kpniot | 0:a9259748d982 | 102 | |
kpniot | 0:a9259748d982 | 103 | void startKey(); |
kpniot | 0:a9259748d982 | 104 | |
kpniot | 0:a9259748d982 | 105 | void processEscapeCharacters(char c); |
kpniot | 0:a9259748d982 | 106 | |
kpniot | 0:a9259748d982 | 107 | bool isDigit(char c); |
kpniot | 0:a9259748d982 | 108 | |
kpniot | 0:a9259748d982 | 109 | bool isHexCharacter(char c); |
kpniot | 0:a9259748d982 | 110 | |
kpniot | 0:a9259748d982 | 111 | char convertCodepointToCharacter(int num); |
kpniot | 0:a9259748d982 | 112 | |
kpniot | 0:a9259748d982 | 113 | void endUnicodeCharacter(int codepoint); |
kpniot | 0:a9259748d982 | 114 | |
kpniot | 0:a9259748d982 | 115 | void startNumber(char c); |
kpniot | 0:a9259748d982 | 116 | |
kpniot | 0:a9259748d982 | 117 | void startString(); |
kpniot | 0:a9259748d982 | 118 | |
kpniot | 0:a9259748d982 | 119 | void startObject(); |
kpniot | 0:a9259748d982 | 120 | |
kpniot | 0:a9259748d982 | 121 | void startArray(); |
kpniot | 0:a9259748d982 | 122 | |
kpniot | 0:a9259748d982 | 123 | void endNull(); |
kpniot | 0:a9259748d982 | 124 | |
kpniot | 0:a9259748d982 | 125 | void endFalse(); |
kpniot | 0:a9259748d982 | 126 | |
kpniot | 0:a9259748d982 | 127 | void endTrue(); |
kpniot | 0:a9259748d982 | 128 | |
kpniot | 0:a9259748d982 | 129 | void endDocument(); |
kpniot | 0:a9259748d982 | 130 | |
kpniot | 0:a9259748d982 | 131 | int convertDecimalBufferToInt(char myArray[], int length); |
kpniot | 0:a9259748d982 | 132 | |
kpniot | 0:a9259748d982 | 133 | void endNumber(); |
kpniot | 0:a9259748d982 | 134 | |
kpniot | 0:a9259748d982 | 135 | void endUnicodeSurrogateInterstitial(); |
kpniot | 0:a9259748d982 | 136 | |
kpniot | 0:a9259748d982 | 137 | bool doesCharArrayContain(char myArray[], int length, char c); |
kpniot | 0:a9259748d982 | 138 | |
kpniot | 0:a9259748d982 | 139 | int getHexArrayAsDecimal(char hexArray[], int length); |
kpniot | 0:a9259748d982 | 140 | |
kpniot | 0:a9259748d982 | 141 | void processUnicodeCharacter(char c); |
kpniot | 0:a9259748d982 | 142 | |
kpniot | 0:a9259748d982 | 143 | void endObject(); |
kpniot | 0:a9259748d982 | 144 | }; |
kpniot | 0:a9259748d982 | 145 | |
kpniot | 0:a9259748d982 | 146 | #endif |
kpniot | 0:a9259748d982 | 147 | |
kpniot | 0:a9259748d982 | 148 | |
kpniot | 0:a9259748d982 | 149 | |
kpniot | 0:a9259748d982 | 150 | |
kpniot | 0:a9259748d982 | 151 | |
kpniot | 0:a9259748d982 | 152 | |
kpniot | 0:a9259748d982 | 153 |