KPN IoT / senml

Fork of kpn_senml by KPN IoT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers senml_JsonStreamingParser.h Source File

senml_JsonStreamingParser.h

00001 /**The MIT License (MIT)
00002 
00003 Copyright (c) 2015 by Daniel Eichhorn
00004 
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011 
00012 The above copyright notice and this permission notice shall be included in all
00013 copies or substantial portions of the Software.
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00021 SOFTWARE.
00022 
00023 See more at http://blog.squix.ch and https://github.com/squix78/json-streaming-parser
00024 */
00025 
00026 #ifndef SENMLJSONSTREAMINGPARSER
00027 #define SENMLJSONSTREAMINGPARSER
00028 
00029 #ifdef __MBED__
00030     #include "mbed.h"
00031     #include <string> 
00032     using namespace std;
00033     #define String string
00034 #else
00035     #include <Arduino.h>
00036 #endif
00037 #include <senml_JsonListener.h>
00038 
00039 #define STATE_START_DOCUMENT     0
00040 #define STATE_DONE               -1
00041 #define STATE_IN_ARRAY           1
00042 #define STATE_IN_OBJECT          2
00043 #define STATE_END_KEY            3
00044 #define STATE_AFTER_KEY          4
00045 #define STATE_IN_STRING          5
00046 #define STATE_START_ESCAPE       6
00047 #define STATE_UNICODE            7
00048 #define STATE_IN_NUMBER          8
00049 #define STATE_IN_TRUE            9
00050 #define STATE_IN_FALSE           10
00051 #define STATE_IN_NULL            11
00052 #define STATE_AFTER_VALUE        12
00053 #define STATE_UNICODE_SURROGATE  13
00054 
00055 #define STACK_OBJECT             0
00056 #define STACK_ARRAY              1
00057 #define STACK_KEY                2
00058 #define STACK_STRING             3
00059 
00060 #define BUFFER_MAX_LENGTH  512
00061 
00062 /**
00063  * Internal helper class for parsing json data.
00064  */
00065 class JsonStreamingParser {
00066     public:
00067         JsonStreamingParser();
00068         void parse(char c);
00069         void setListener(JsonListener* listener);
00070         void reset();
00071 
00072 
00073     private:
00074 
00075         int state;
00076         int stack[20];
00077         int stackPos;
00078         JsonListener* myListener;
00079 
00080         //bool doEmitWhitespace = false;
00081         // fixed length buffer array to prepare for c code
00082         char buffer[BUFFER_MAX_LENGTH];
00083         int bufferPos;
00084 
00085         char unicodeEscapeBuffer[10];
00086         int unicodeEscapeBufferPos;
00087 
00088         char unicodeBuffer[10];
00089         int unicodeBufferPos;
00090 
00091         int characterCounter;
00092 
00093         int unicodeHighSurrogate;
00094 
00095         void increaseBufferPointer();
00096 
00097         void endString();
00098 
00099         void endArray();
00100 
00101         void startValue(char c);
00102 
00103         void startKey();
00104 
00105         void processEscapeCharacters(char c);
00106 
00107         bool isDigit(char c);
00108 
00109         bool isHexCharacter(char c);
00110 
00111         char convertCodepointToCharacter(int num);
00112 
00113         void endUnicodeCharacter(int codepoint);
00114 
00115         void startNumber(char c);
00116 
00117         void startString();
00118 
00119         void startObject();
00120 
00121         void startArray();
00122 
00123         void endNull();
00124 
00125         void endFalse();
00126 
00127         void endTrue();
00128 
00129         void endDocument();
00130 
00131         int convertDecimalBufferToInt(char myArray[], int length);
00132 
00133         void endNumber();
00134 
00135         void endUnicodeSurrogateInterstitial();
00136 
00137         bool doesCharArrayContain(char myArray[], int length, char c);
00138 
00139         int getHexArrayAsDecimal(char hexArray[], int length);
00140 
00141         void processUnicodeCharacter(char c);
00142 
00143         void endObject();
00144 };
00145 
00146 #endif
00147 
00148 
00149 
00150 
00151 
00152 
00153