A wrapper class for talking to Axeda from MBED devices. Uses HTTPClient and MbedJSONValue classes.
Dependents: axeda_wrapper_dev MTS_Axeda_Example
AxedaWrapper simplifies pushing data to Axeda's cloud.
Uses HTTPClient and MbedJSONValue libs:
http://mbed.org/users/donatien/code/HTTPClient/
http://mbed.org/users/samux/code/MbedJSONValue/
MbedJSONValue/MbedJSONValue.h@5:dd1e00e3eba5, 2014-01-04 (annotated)
- Committer:
- mfiore
- Date:
- Sat Jan 04 05:07:10 2014 +0000
- Revision:
- 5:dd1e00e3eba5
added modified HTTPClient library; added MbedJSONValue library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfiore | 5:dd1e00e3eba5 | 1 | /** |
mfiore | 5:dd1e00e3eba5 | 2 | * @author Samuel Mokrani |
mfiore | 5:dd1e00e3eba5 | 3 | * |
mfiore | 5:dd1e00e3eba5 | 4 | * @section LICENSE |
mfiore | 5:dd1e00e3eba5 | 5 | * |
mfiore | 5:dd1e00e3eba5 | 6 | * Copyright (c) 2011 mbed |
mfiore | 5:dd1e00e3eba5 | 7 | * |
mfiore | 5:dd1e00e3eba5 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
mfiore | 5:dd1e00e3eba5 | 9 | * of this software and associated documentation files (the "Software"), to deal |
mfiore | 5:dd1e00e3eba5 | 10 | * in the Software without restriction, including without limitation the rights |
mfiore | 5:dd1e00e3eba5 | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
mfiore | 5:dd1e00e3eba5 | 12 | * copies of the Software, and to permit persons to whom the Software is |
mfiore | 5:dd1e00e3eba5 | 13 | * furnished to do so, subject to the following conditions: |
mfiore | 5:dd1e00e3eba5 | 14 | * |
mfiore | 5:dd1e00e3eba5 | 15 | * The above copyright notice and this permission notice shall be included in |
mfiore | 5:dd1e00e3eba5 | 16 | * all copies or substantial portions of the Software. |
mfiore | 5:dd1e00e3eba5 | 17 | * |
mfiore | 5:dd1e00e3eba5 | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
mfiore | 5:dd1e00e3eba5 | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
mfiore | 5:dd1e00e3eba5 | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
mfiore | 5:dd1e00e3eba5 | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
mfiore | 5:dd1e00e3eba5 | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mfiore | 5:dd1e00e3eba5 | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
mfiore | 5:dd1e00e3eba5 | 24 | * THE SOFTWARE. |
mfiore | 5:dd1e00e3eba5 | 25 | * |
mfiore | 5:dd1e00e3eba5 | 26 | * @section DESCRIPTION |
mfiore | 5:dd1e00e3eba5 | 27 | * Types Abstraction. Minimalist JSON serializer and parser (inspired by picojson). Is used by MbedJSONRpc. |
mfiore | 5:dd1e00e3eba5 | 28 | * |
mfiore | 5:dd1e00e3eba5 | 29 | */ |
mfiore | 5:dd1e00e3eba5 | 30 | |
mfiore | 5:dd1e00e3eba5 | 31 | #ifndef _Mbed_RPC_VALUE_H_ |
mfiore | 5:dd1e00e3eba5 | 32 | #define _Mbed_RPC_VALUE_H_ |
mfiore | 5:dd1e00e3eba5 | 33 | |
mfiore | 5:dd1e00e3eba5 | 34 | #define NB_TOKEN 20 |
mfiore | 5:dd1e00e3eba5 | 35 | /*!< Number maximum of MbedJSONValue in an array or an object */ |
mfiore | 5:dd1e00e3eba5 | 36 | |
mfiore | 5:dd1e00e3eba5 | 37 | #include <string> |
mfiore | 5:dd1e00e3eba5 | 38 | #include <stdio.h> |
mfiore | 5:dd1e00e3eba5 | 39 | #include <stdlib.h> |
mfiore | 5:dd1e00e3eba5 | 40 | #include <string.h> |
mfiore | 5:dd1e00e3eba5 | 41 | |
mfiore | 5:dd1e00e3eba5 | 42 | /** MbedJSONValue class |
mfiore | 5:dd1e00e3eba5 | 43 | * |
mfiore | 5:dd1e00e3eba5 | 44 | * Example: |
mfiore | 5:dd1e00e3eba5 | 45 | * - creation of an MbedJSONValue of type TypeObject containing two others MbedJSONValue: |
mfiore | 5:dd1e00e3eba5 | 46 | * -one array of one string and one integer identified by "my_array" |
mfiore | 5:dd1e00e3eba5 | 47 | * -a boolean identified by "my_boolean" |
mfiore | 5:dd1e00e3eba5 | 48 | * - serialization in JSON format of this object |
mfiore | 5:dd1e00e3eba5 | 49 | * @code |
mfiore | 5:dd1e00e3eba5 | 50 | * #include "mbed.h" |
mfiore | 5:dd1e00e3eba5 | 51 | * #include "MbedJSONValue.h" |
mfiore | 5:dd1e00e3eba5 | 52 | * #include <string> |
mfiore | 5:dd1e00e3eba5 | 53 | * |
mfiore | 5:dd1e00e3eba5 | 54 | * int main() { |
mfiore | 5:dd1e00e3eba5 | 55 | * |
mfiore | 5:dd1e00e3eba5 | 56 | * MbedJSONValue demo; |
mfiore | 5:dd1e00e3eba5 | 57 | * std::string s; |
mfiore | 5:dd1e00e3eba5 | 58 | * |
mfiore | 5:dd1e00e3eba5 | 59 | * //fill the object |
mfiore | 5:dd1e00e3eba5 | 60 | * demo["my_array"][0] = "demo_string"; |
mfiore | 5:dd1e00e3eba5 | 61 | * demo["my_array"][1] = 10; |
mfiore | 5:dd1e00e3eba5 | 62 | * demo["my_boolean"] = false; |
mfiore | 5:dd1e00e3eba5 | 63 | * |
mfiore | 5:dd1e00e3eba5 | 64 | * //serialize it into a JSON string |
mfiore | 5:dd1e00e3eba5 | 65 | * s = demo.serialize(); |
mfiore | 5:dd1e00e3eba5 | 66 | * printf("json: %s\r\n", s.c_str()); |
mfiore | 5:dd1e00e3eba5 | 67 | * } |
mfiore | 5:dd1e00e3eba5 | 68 | * |
mfiore | 5:dd1e00e3eba5 | 69 | * @endcode |
mfiore | 5:dd1e00e3eba5 | 70 | * |
mfiore | 5:dd1e00e3eba5 | 71 | * Example: |
mfiore | 5:dd1e00e3eba5 | 72 | * - creation of an MbedJSONValue from a JSON string |
mfiore | 5:dd1e00e3eba5 | 73 | * - extraction of different values from this existing MbedJSONValue |
mfiore | 5:dd1e00e3eba5 | 74 | * @code |
mfiore | 5:dd1e00e3eba5 | 75 | * #include "mbed.h" |
mfiore | 5:dd1e00e3eba5 | 76 | * #include "MbedJSONValue.h" |
mfiore | 5:dd1e00e3eba5 | 77 | * #include <string> |
mfiore | 5:dd1e00e3eba5 | 78 | * |
mfiore | 5:dd1e00e3eba5 | 79 | * int main() { |
mfiore | 5:dd1e00e3eba5 | 80 | * MbedJSONValue demo; |
mfiore | 5:dd1e00e3eba5 | 81 | * |
mfiore | 5:dd1e00e3eba5 | 82 | * const char * json = "{\"my_array\": [\"demo_string\", 10], \"my_boolean\": true}"; |
mfiore | 5:dd1e00e3eba5 | 83 | * |
mfiore | 5:dd1e00e3eba5 | 84 | * //parse the previous string and fill the object demo |
mfiore | 5:dd1e00e3eba5 | 85 | * parse(demo, json); |
mfiore | 5:dd1e00e3eba5 | 86 | * |
mfiore | 5:dd1e00e3eba5 | 87 | * std::string my_str; |
mfiore | 5:dd1e00e3eba5 | 88 | * int my_int; |
mfiore | 5:dd1e00e3eba5 | 89 | * bool my_bool; |
mfiore | 5:dd1e00e3eba5 | 90 | * |
mfiore | 5:dd1e00e3eba5 | 91 | * my_str = demo["my_array"][0].get<std::string>(); |
mfiore | 5:dd1e00e3eba5 | 92 | * my_int = demo["my_array"][1].get<int>(); |
mfiore | 5:dd1e00e3eba5 | 93 | * my_bool = demo["my_boolean"].get<bool>(); |
mfiore | 5:dd1e00e3eba5 | 94 | * |
mfiore | 5:dd1e00e3eba5 | 95 | * printf("my_str: %s\r\n", my_str.c_str()); |
mfiore | 5:dd1e00e3eba5 | 96 | * printf("my_int: %d\r\n", my_int); |
mfiore | 5:dd1e00e3eba5 | 97 | * printf("my_bool: %s\r\n", my_bool ? "true" : "false"); |
mfiore | 5:dd1e00e3eba5 | 98 | * } |
mfiore | 5:dd1e00e3eba5 | 99 | * @endcode |
mfiore | 5:dd1e00e3eba5 | 100 | */ |
mfiore | 5:dd1e00e3eba5 | 101 | class MbedJSONValue { |
mfiore | 5:dd1e00e3eba5 | 102 | public: |
mfiore | 5:dd1e00e3eba5 | 103 | |
mfiore | 5:dd1e00e3eba5 | 104 | /** |
mfiore | 5:dd1e00e3eba5 | 105 | * \enum Type |
mfiore | 5:dd1e00e3eba5 | 106 | * \brief All types which can be used |
mfiore | 5:dd1e00e3eba5 | 107 | */ |
mfiore | 5:dd1e00e3eba5 | 108 | enum Type { |
mfiore | 5:dd1e00e3eba5 | 109 | TypeNull, /*!< Null type */ |
mfiore | 5:dd1e00e3eba5 | 110 | TypeBoolean, /*!< Boolean */ |
mfiore | 5:dd1e00e3eba5 | 111 | TypeInt, /*!< Integer */ |
mfiore | 5:dd1e00e3eba5 | 112 | TypeDouble, /*!< Double */ |
mfiore | 5:dd1e00e3eba5 | 113 | TypeString, /*!< String */ |
mfiore | 5:dd1e00e3eba5 | 114 | TypeArray, /*!< Array (contains MbedJSONValue) */ |
mfiore | 5:dd1e00e3eba5 | 115 | TypeObject /*!< Object (contains MbedJSONValue identified by a name)*/ |
mfiore | 5:dd1e00e3eba5 | 116 | }; |
mfiore | 5:dd1e00e3eba5 | 117 | |
mfiore | 5:dd1e00e3eba5 | 118 | /** |
mfiore | 5:dd1e00e3eba5 | 119 | * MbedJSONValue constructor of type TypeNull |
mfiore | 5:dd1e00e3eba5 | 120 | */ |
mfiore | 5:dd1e00e3eba5 | 121 | MbedJSONValue() : _type(TypeNull), index_array(0), index_token(0) {} |
mfiore | 5:dd1e00e3eba5 | 122 | |
mfiore | 5:dd1e00e3eba5 | 123 | /** |
mfiore | 5:dd1e00e3eba5 | 124 | * MbedJSONValue constructor of type TypeBoolean |
mfiore | 5:dd1e00e3eba5 | 125 | * |
mfiore | 5:dd1e00e3eba5 | 126 | * @param value the object created will be initialized with this boolean |
mfiore | 5:dd1e00e3eba5 | 127 | */ |
mfiore | 5:dd1e00e3eba5 | 128 | MbedJSONValue(bool value) : _type(TypeBoolean), index_array(0), index_token(0) { |
mfiore | 5:dd1e00e3eba5 | 129 | _value.asBool = value; |
mfiore | 5:dd1e00e3eba5 | 130 | } |
mfiore | 5:dd1e00e3eba5 | 131 | |
mfiore | 5:dd1e00e3eba5 | 132 | /** |
mfiore | 5:dd1e00e3eba5 | 133 | * MbedJSONValue constructor of type TypeInt |
mfiore | 5:dd1e00e3eba5 | 134 | * |
mfiore | 5:dd1e00e3eba5 | 135 | * @param value the object created will be initialized with this integer |
mfiore | 5:dd1e00e3eba5 | 136 | */ |
mfiore | 5:dd1e00e3eba5 | 137 | MbedJSONValue(int value) : _type(TypeInt), index_array(0), index_token(0) { |
mfiore | 5:dd1e00e3eba5 | 138 | _value.asInt = value; |
mfiore | 5:dd1e00e3eba5 | 139 | } |
mfiore | 5:dd1e00e3eba5 | 140 | |
mfiore | 5:dd1e00e3eba5 | 141 | /** |
mfiore | 5:dd1e00e3eba5 | 142 | * MbedJSONValue constructor of type TypeDouble |
mfiore | 5:dd1e00e3eba5 | 143 | * |
mfiore | 5:dd1e00e3eba5 | 144 | * @param value the object created will be initialized with this double |
mfiore | 5:dd1e00e3eba5 | 145 | */ |
mfiore | 5:dd1e00e3eba5 | 146 | MbedJSONValue(double value) : _type(TypeDouble), index_array(0), index_token(0) { |
mfiore | 5:dd1e00e3eba5 | 147 | _value.asDouble = value; |
mfiore | 5:dd1e00e3eba5 | 148 | } |
mfiore | 5:dd1e00e3eba5 | 149 | |
mfiore | 5:dd1e00e3eba5 | 150 | /** |
mfiore | 5:dd1e00e3eba5 | 151 | * MbedJSONValue constructor of type TypeString |
mfiore | 5:dd1e00e3eba5 | 152 | * |
mfiore | 5:dd1e00e3eba5 | 153 | * @param value the object created will be initialized with this string |
mfiore | 5:dd1e00e3eba5 | 154 | */ |
mfiore | 5:dd1e00e3eba5 | 155 | MbedJSONValue(std::string const& value) : _type(TypeString), index_array(0), index_token(0) { |
mfiore | 5:dd1e00e3eba5 | 156 | _value.asString = new std::string(value); |
mfiore | 5:dd1e00e3eba5 | 157 | } |
mfiore | 5:dd1e00e3eba5 | 158 | |
mfiore | 5:dd1e00e3eba5 | 159 | /** |
mfiore | 5:dd1e00e3eba5 | 160 | * MbedJSONValue constructor of type TypeString |
mfiore | 5:dd1e00e3eba5 | 161 | * |
mfiore | 5:dd1e00e3eba5 | 162 | * @param value the object created will be initialized with this string |
mfiore | 5:dd1e00e3eba5 | 163 | */ |
mfiore | 5:dd1e00e3eba5 | 164 | MbedJSONValue(const char* value) : _type(TypeString), index_array(0), index_token(0) { |
mfiore | 5:dd1e00e3eba5 | 165 | _value.asString = new std::string(value); |
mfiore | 5:dd1e00e3eba5 | 166 | } |
mfiore | 5:dd1e00e3eba5 | 167 | |
mfiore | 5:dd1e00e3eba5 | 168 | /** |
mfiore | 5:dd1e00e3eba5 | 169 | * Copy constructor |
mfiore | 5:dd1e00e3eba5 | 170 | * |
mfiore | 5:dd1e00e3eba5 | 171 | * @param rhs object which will be copied |
mfiore | 5:dd1e00e3eba5 | 172 | */ |
mfiore | 5:dd1e00e3eba5 | 173 | MbedJSONValue(MbedJSONValue const& rhs) : _type(TypeNull) { *this = rhs; } |
mfiore | 5:dd1e00e3eba5 | 174 | |
mfiore | 5:dd1e00e3eba5 | 175 | /** |
mfiore | 5:dd1e00e3eba5 | 176 | * Destructor |
mfiore | 5:dd1e00e3eba5 | 177 | */ |
mfiore | 5:dd1e00e3eba5 | 178 | ~MbedJSONValue() { clean(); } |
mfiore | 5:dd1e00e3eba5 | 179 | |
mfiore | 5:dd1e00e3eba5 | 180 | /** |
mfiore | 5:dd1e00e3eba5 | 181 | * = Operator overloading for an MbedJSONValue from an MbedJSONValue |
mfiore | 5:dd1e00e3eba5 | 182 | * |
mfiore | 5:dd1e00e3eba5 | 183 | * @param rhs object |
mfiore | 5:dd1e00e3eba5 | 184 | * @return a reference on the MbedJSONValue affected |
mfiore | 5:dd1e00e3eba5 | 185 | */ |
mfiore | 5:dd1e00e3eba5 | 186 | MbedJSONValue& operator=(MbedJSONValue const & rhs); |
mfiore | 5:dd1e00e3eba5 | 187 | |
mfiore | 5:dd1e00e3eba5 | 188 | /** |
mfiore | 5:dd1e00e3eba5 | 189 | * = Operator overloading for an MbedJSONValue from an int |
mfiore | 5:dd1e00e3eba5 | 190 | * |
mfiore | 5:dd1e00e3eba5 | 191 | * @param rhs integer |
mfiore | 5:dd1e00e3eba5 | 192 | * @return a reference on the MbedJSONValue affected |
mfiore | 5:dd1e00e3eba5 | 193 | */ |
mfiore | 5:dd1e00e3eba5 | 194 | MbedJSONValue& operator=(int const& rhs) { return operator=(MbedJSONValue(rhs)); } |
mfiore | 5:dd1e00e3eba5 | 195 | |
mfiore | 5:dd1e00e3eba5 | 196 | /** |
mfiore | 5:dd1e00e3eba5 | 197 | * = Operator overloading for an MbedJSONValue from a boolean |
mfiore | 5:dd1e00e3eba5 | 198 | * |
mfiore | 5:dd1e00e3eba5 | 199 | * @param rhs boolean |
mfiore | 5:dd1e00e3eba5 | 200 | * @return a reference on the MbedJSONValue affected |
mfiore | 5:dd1e00e3eba5 | 201 | */ |
mfiore | 5:dd1e00e3eba5 | 202 | MbedJSONValue& operator=(bool const& rhs) { return operator=(MbedJSONValue(rhs)); } |
mfiore | 5:dd1e00e3eba5 | 203 | |
mfiore | 5:dd1e00e3eba5 | 204 | /** |
mfiore | 5:dd1e00e3eba5 | 205 | * = Operator overloading for an MbedJSONValue from a double |
mfiore | 5:dd1e00e3eba5 | 206 | * |
mfiore | 5:dd1e00e3eba5 | 207 | * @param rhs double |
mfiore | 5:dd1e00e3eba5 | 208 | * @return a reference on the MbedJSONValue affected |
mfiore | 5:dd1e00e3eba5 | 209 | */ |
mfiore | 5:dd1e00e3eba5 | 210 | MbedJSONValue& operator=(double const& rhs) { return operator=(MbedJSONValue(rhs)); } |
mfiore | 5:dd1e00e3eba5 | 211 | |
mfiore | 5:dd1e00e3eba5 | 212 | /** |
mfiore | 5:dd1e00e3eba5 | 213 | * = Operator overloading for an MbedJSONValue from a string |
mfiore | 5:dd1e00e3eba5 | 214 | * |
mfiore | 5:dd1e00e3eba5 | 215 | * @param rhs string |
mfiore | 5:dd1e00e3eba5 | 216 | * @return a reference on the MbedJSONValue affected |
mfiore | 5:dd1e00e3eba5 | 217 | */ |
mfiore | 5:dd1e00e3eba5 | 218 | MbedJSONValue& operator=(const char* rhs) { return operator=(MbedJSONValue(std::string(rhs))); } |
mfiore | 5:dd1e00e3eba5 | 219 | |
mfiore | 5:dd1e00e3eba5 | 220 | |
mfiore | 5:dd1e00e3eba5 | 221 | /** |
mfiore | 5:dd1e00e3eba5 | 222 | * [] Operator overloading for an MbedJSONValue. |
mfiore | 5:dd1e00e3eba5 | 223 | * Each TypeObject object can contain an array of NB_TOKEN MbedJSONValue. |
mfiore | 5:dd1e00e3eba5 | 224 | * This operator is useful to create an array or to retrieve an MbedJSONValue of an existing array. |
mfiore | 5:dd1e00e3eba5 | 225 | * |
mfiore | 5:dd1e00e3eba5 | 226 | * @param i index of the array |
mfiore | 5:dd1e00e3eba5 | 227 | * @return a reference on the MbedJSONValue created or retrieved |
mfiore | 5:dd1e00e3eba5 | 228 | */ |
mfiore | 5:dd1e00e3eba5 | 229 | MbedJSONValue& operator[](int i); |
mfiore | 5:dd1e00e3eba5 | 230 | |
mfiore | 5:dd1e00e3eba5 | 231 | /** |
mfiore | 5:dd1e00e3eba5 | 232 | * [] Operator overloading for an MbedJSONValue. |
mfiore | 5:dd1e00e3eba5 | 233 | * Each TypeObject MbedJSONValue can contain NB_TOKEN MbedJSONValue IDENTIFIED BY A NAME |
mfiore | 5:dd1e00e3eba5 | 234 | * This operator is useful to create a TypeObject MbedJSONValue or to retrieve an MbedJSONValue of an existing TypeObject. |
mfiore | 5:dd1e00e3eba5 | 235 | * |
mfiore | 5:dd1e00e3eba5 | 236 | * |
mfiore | 5:dd1e00e3eba5 | 237 | * @param str identifier of the sub MbedJSONValue |
mfiore | 5:dd1e00e3eba5 | 238 | * @return a reference on the MbedJSONValue created or retrieved |
mfiore | 5:dd1e00e3eba5 | 239 | */ |
mfiore | 5:dd1e00e3eba5 | 240 | MbedJSONValue& operator[](std::string str); |
mfiore | 5:dd1e00e3eba5 | 241 | |
mfiore | 5:dd1e00e3eba5 | 242 | /** |
mfiore | 5:dd1e00e3eba5 | 243 | * Retrieve the value of an MbedJSONValue object. |
mfiore | 5:dd1e00e3eba5 | 244 | * |
mfiore | 5:dd1e00e3eba5 | 245 | * Let's suppose that we have an MbedJSONValue of type TypeString. |
mfiore | 5:dd1e00e3eba5 | 246 | * To retrieve this string, you have to do: |
mfiore | 5:dd1e00e3eba5 | 247 | * my_obj.get<std::string>(); |
mfiore | 5:dd1e00e3eba5 | 248 | * |
mfiore | 5:dd1e00e3eba5 | 249 | * @return A contant reference on the value of the object |
mfiore | 5:dd1e00e3eba5 | 250 | */ |
mfiore | 5:dd1e00e3eba5 | 251 | template <typename T> const T& get() const; |
mfiore | 5:dd1e00e3eba5 | 252 | |
mfiore | 5:dd1e00e3eba5 | 253 | /** |
mfiore | 5:dd1e00e3eba5 | 254 | * Retrieve the value of an MbedJSONValue object. |
mfiore | 5:dd1e00e3eba5 | 255 | * |
mfiore | 5:dd1e00e3eba5 | 256 | * Let's suppose that we have an MbedJSONValue of type TypeInt. |
mfiore | 5:dd1e00e3eba5 | 257 | * To retrieve this integer, you have to do: |
mfiore | 5:dd1e00e3eba5 | 258 | * my_obj.get<int>(); |
mfiore | 5:dd1e00e3eba5 | 259 | * |
mfiore | 5:dd1e00e3eba5 | 260 | * @return A reference on the value of the object |
mfiore | 5:dd1e00e3eba5 | 261 | */ |
mfiore | 5:dd1e00e3eba5 | 262 | template <typename T> T& get(); |
mfiore | 5:dd1e00e3eba5 | 263 | |
mfiore | 5:dd1e00e3eba5 | 264 | |
mfiore | 5:dd1e00e3eba5 | 265 | /** |
mfiore | 5:dd1e00e3eba5 | 266 | * Return the type of the MbedJSONValue object |
mfiore | 5:dd1e00e3eba5 | 267 | * |
mfiore | 5:dd1e00e3eba5 | 268 | * @return type of the MbedJSONValue object |
mfiore | 5:dd1e00e3eba5 | 269 | */ |
mfiore | 5:dd1e00e3eba5 | 270 | Type const &getType() const { |
mfiore | 5:dd1e00e3eba5 | 271 | return _type; |
mfiore | 5:dd1e00e3eba5 | 272 | } |
mfiore | 5:dd1e00e3eba5 | 273 | |
mfiore | 5:dd1e00e3eba5 | 274 | /** |
mfiore | 5:dd1e00e3eba5 | 275 | * Return the size of an MbedJSONValue object (works for TypeString, TypeArray or TypeObject) |
mfiore | 5:dd1e00e3eba5 | 276 | * |
mfiore | 5:dd1e00e3eba5 | 277 | * @return size |
mfiore | 5:dd1e00e3eba5 | 278 | */ |
mfiore | 5:dd1e00e3eba5 | 279 | int size() const; |
mfiore | 5:dd1e00e3eba5 | 280 | |
mfiore | 5:dd1e00e3eba5 | 281 | /** |
mfiore | 5:dd1e00e3eba5 | 282 | * Check for the existence in a TypeObject object of member identified by name |
mfiore | 5:dd1e00e3eba5 | 283 | * |
mfiore | 5:dd1e00e3eba5 | 284 | * @param name Identifier |
mfiore | 5:dd1e00e3eba5 | 285 | * @return true if the object is of type TypeObject AND contains a member named "name", false otherwise |
mfiore | 5:dd1e00e3eba5 | 286 | */ |
mfiore | 5:dd1e00e3eba5 | 287 | bool hasMember(char * name); |
mfiore | 5:dd1e00e3eba5 | 288 | |
mfiore | 5:dd1e00e3eba5 | 289 | /** |
mfiore | 5:dd1e00e3eba5 | 290 | * Convert an MbedJSONValue in a JSON frame |
mfiore | 5:dd1e00e3eba5 | 291 | * |
mfiore | 5:dd1e00e3eba5 | 292 | * @return JSON string |
mfiore | 5:dd1e00e3eba5 | 293 | */ |
mfiore | 5:dd1e00e3eba5 | 294 | std::string serialize(); |
mfiore | 5:dd1e00e3eba5 | 295 | |
mfiore | 5:dd1e00e3eba5 | 296 | protected: |
mfiore | 5:dd1e00e3eba5 | 297 | |
mfiore | 5:dd1e00e3eba5 | 298 | // object type |
mfiore | 5:dd1e00e3eba5 | 299 | Type _type; |
mfiore | 5:dd1e00e3eba5 | 300 | |
mfiore | 5:dd1e00e3eba5 | 301 | //indexes of TypeObject and TypeArray |
mfiore | 5:dd1e00e3eba5 | 302 | int index_array; |
mfiore | 5:dd1e00e3eba5 | 303 | int index_token; |
mfiore | 5:dd1e00e3eba5 | 304 | |
mfiore | 5:dd1e00e3eba5 | 305 | //an object can contain NB_TOKEN tokens. Each token have a name |
mfiore | 5:dd1e00e3eba5 | 306 | MbedJSONValue * token[NB_TOKEN]; |
mfiore | 5:dd1e00e3eba5 | 307 | std::string * token_name[NB_TOKEN]; |
mfiore | 5:dd1e00e3eba5 | 308 | |
mfiore | 5:dd1e00e3eba5 | 309 | //an object can contain an array of NB_TOKEN elements |
mfiore | 5:dd1e00e3eba5 | 310 | MbedJSONValue * array[NB_TOKEN]; |
mfiore | 5:dd1e00e3eba5 | 311 | |
mfiore | 5:dd1e00e3eba5 | 312 | // Clean up |
mfiore | 5:dd1e00e3eba5 | 313 | void clean(); |
mfiore | 5:dd1e00e3eba5 | 314 | |
mfiore | 5:dd1e00e3eba5 | 315 | union { |
mfiore | 5:dd1e00e3eba5 | 316 | bool asBool; |
mfiore | 5:dd1e00e3eba5 | 317 | int asInt; |
mfiore | 5:dd1e00e3eba5 | 318 | double asDouble; |
mfiore | 5:dd1e00e3eba5 | 319 | std::string* asString; |
mfiore | 5:dd1e00e3eba5 | 320 | } _value; |
mfiore | 5:dd1e00e3eba5 | 321 | |
mfiore | 5:dd1e00e3eba5 | 322 | |
mfiore | 5:dd1e00e3eba5 | 323 | MbedJSONValue& operator[](int i) const { return *(array[i]); } |
mfiore | 5:dd1e00e3eba5 | 324 | MbedJSONValue& operator[](std::string k) const; |
mfiore | 5:dd1e00e3eba5 | 325 | |
mfiore | 5:dd1e00e3eba5 | 326 | std::string to_str(); |
mfiore | 5:dd1e00e3eba5 | 327 | void serialize(std::back_insert_iterator<std::string> os); |
mfiore | 5:dd1e00e3eba5 | 328 | |
mfiore | 5:dd1e00e3eba5 | 329 | }; |
mfiore | 5:dd1e00e3eba5 | 330 | |
mfiore | 5:dd1e00e3eba5 | 331 | |
mfiore | 5:dd1e00e3eba5 | 332 | #define GET(ctype, var) \ |
mfiore | 5:dd1e00e3eba5 | 333 | template <> inline const ctype& MbedJSONValue::get<ctype>() const { \ |
mfiore | 5:dd1e00e3eba5 | 334 | return var; \ |
mfiore | 5:dd1e00e3eba5 | 335 | } \ |
mfiore | 5:dd1e00e3eba5 | 336 | template <> inline ctype& MbedJSONValue::get<ctype>() { \ |
mfiore | 5:dd1e00e3eba5 | 337 | return var; \ |
mfiore | 5:dd1e00e3eba5 | 338 | } |
mfiore | 5:dd1e00e3eba5 | 339 | GET(bool, _value.asBool) |
mfiore | 5:dd1e00e3eba5 | 340 | GET(double, _value.asDouble) |
mfiore | 5:dd1e00e3eba5 | 341 | GET(int, _value.asInt) |
mfiore | 5:dd1e00e3eba5 | 342 | GET(std::string, *_value.asString) |
mfiore | 5:dd1e00e3eba5 | 343 | #undef GET |
mfiore | 5:dd1e00e3eba5 | 344 | |
mfiore | 5:dd1e00e3eba5 | 345 | |
mfiore | 5:dd1e00e3eba5 | 346 | //Input class for JSON parser |
mfiore | 5:dd1e00e3eba5 | 347 | class input { |
mfiore | 5:dd1e00e3eba5 | 348 | protected: |
mfiore | 5:dd1e00e3eba5 | 349 | const char * cur_; |
mfiore | 5:dd1e00e3eba5 | 350 | const char * end_; |
mfiore | 5:dd1e00e3eba5 | 351 | int last_ch_; |
mfiore | 5:dd1e00e3eba5 | 352 | bool ungot_; |
mfiore | 5:dd1e00e3eba5 | 353 | int line_; |
mfiore | 5:dd1e00e3eba5 | 354 | public: |
mfiore | 5:dd1e00e3eba5 | 355 | input(const char * first, const char * last) : cur_(first), end_(last), last_ch_(-1), ungot_(false), line_(1) {}; |
mfiore | 5:dd1e00e3eba5 | 356 | |
mfiore | 5:dd1e00e3eba5 | 357 | int getc() { |
mfiore | 5:dd1e00e3eba5 | 358 | if (ungot_) { |
mfiore | 5:dd1e00e3eba5 | 359 | ungot_ = false; |
mfiore | 5:dd1e00e3eba5 | 360 | return last_ch_; |
mfiore | 5:dd1e00e3eba5 | 361 | } |
mfiore | 5:dd1e00e3eba5 | 362 | if (cur_ == end_) { |
mfiore | 5:dd1e00e3eba5 | 363 | last_ch_ = -1; |
mfiore | 5:dd1e00e3eba5 | 364 | return -1; |
mfiore | 5:dd1e00e3eba5 | 365 | } |
mfiore | 5:dd1e00e3eba5 | 366 | if (last_ch_ == '\n') { |
mfiore | 5:dd1e00e3eba5 | 367 | line_++; |
mfiore | 5:dd1e00e3eba5 | 368 | } |
mfiore | 5:dd1e00e3eba5 | 369 | last_ch_ = *cur_++ & 0xff; |
mfiore | 5:dd1e00e3eba5 | 370 | return last_ch_; |
mfiore | 5:dd1e00e3eba5 | 371 | } |
mfiore | 5:dd1e00e3eba5 | 372 | |
mfiore | 5:dd1e00e3eba5 | 373 | void ungetc() { |
mfiore | 5:dd1e00e3eba5 | 374 | if (last_ch_ != -1) { |
mfiore | 5:dd1e00e3eba5 | 375 | ungot_ = true; |
mfiore | 5:dd1e00e3eba5 | 376 | } |
mfiore | 5:dd1e00e3eba5 | 377 | } |
mfiore | 5:dd1e00e3eba5 | 378 | |
mfiore | 5:dd1e00e3eba5 | 379 | const char * cur() const { |
mfiore | 5:dd1e00e3eba5 | 380 | return cur_; |
mfiore | 5:dd1e00e3eba5 | 381 | } |
mfiore | 5:dd1e00e3eba5 | 382 | int line() const { |
mfiore | 5:dd1e00e3eba5 | 383 | return line_; |
mfiore | 5:dd1e00e3eba5 | 384 | } |
mfiore | 5:dd1e00e3eba5 | 385 | void skip_ws() { |
mfiore | 5:dd1e00e3eba5 | 386 | while (1) { |
mfiore | 5:dd1e00e3eba5 | 387 | int ch = getc(); |
mfiore | 5:dd1e00e3eba5 | 388 | if (! (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')) { |
mfiore | 5:dd1e00e3eba5 | 389 | ungetc(); |
mfiore | 5:dd1e00e3eba5 | 390 | break; |
mfiore | 5:dd1e00e3eba5 | 391 | } |
mfiore | 5:dd1e00e3eba5 | 392 | } |
mfiore | 5:dd1e00e3eba5 | 393 | } |
mfiore | 5:dd1e00e3eba5 | 394 | int expect(int expect) { |
mfiore | 5:dd1e00e3eba5 | 395 | skip_ws(); |
mfiore | 5:dd1e00e3eba5 | 396 | if (getc() != expect) { |
mfiore | 5:dd1e00e3eba5 | 397 | ungetc(); |
mfiore | 5:dd1e00e3eba5 | 398 | return false; |
mfiore | 5:dd1e00e3eba5 | 399 | } |
mfiore | 5:dd1e00e3eba5 | 400 | return true; |
mfiore | 5:dd1e00e3eba5 | 401 | } |
mfiore | 5:dd1e00e3eba5 | 402 | bool match(const std::string& pattern) { |
mfiore | 5:dd1e00e3eba5 | 403 | for (std::string::const_iterator pi(pattern.begin()); |
mfiore | 5:dd1e00e3eba5 | 404 | pi != pattern.end(); |
mfiore | 5:dd1e00e3eba5 | 405 | ++pi) { |
mfiore | 5:dd1e00e3eba5 | 406 | if (getc() != *pi) { |
mfiore | 5:dd1e00e3eba5 | 407 | ungetc(); |
mfiore | 5:dd1e00e3eba5 | 408 | return false; |
mfiore | 5:dd1e00e3eba5 | 409 | } |
mfiore | 5:dd1e00e3eba5 | 410 | } |
mfiore | 5:dd1e00e3eba5 | 411 | return true; |
mfiore | 5:dd1e00e3eba5 | 412 | } |
mfiore | 5:dd1e00e3eba5 | 413 | }; |
mfiore | 5:dd1e00e3eba5 | 414 | |
mfiore | 5:dd1e00e3eba5 | 415 | |
mfiore | 5:dd1e00e3eba5 | 416 | |
mfiore | 5:dd1e00e3eba5 | 417 | inline const char * parse(MbedJSONValue& out, const char * first, const char * last, std::string* err); |
mfiore | 5:dd1e00e3eba5 | 418 | |
mfiore | 5:dd1e00e3eba5 | 419 | /** |
mfiore | 5:dd1e00e3eba5 | 420 | * JSON string parser and creation of an MbedJSONValue |
mfiore | 5:dd1e00e3eba5 | 421 | * |
mfiore | 5:dd1e00e3eba5 | 422 | * @param out reference of an MbedJSONValue which will be filled according to the JSON string |
mfiore | 5:dd1e00e3eba5 | 423 | * @param str JSON string |
mfiore | 5:dd1e00e3eba5 | 424 | * @return A non empty string if there is a parsing error |
mfiore | 5:dd1e00e3eba5 | 425 | * |
mfiore | 5:dd1e00e3eba5 | 426 | */ |
mfiore | 5:dd1e00e3eba5 | 427 | |
mfiore | 5:dd1e00e3eba5 | 428 | inline std::string parse(MbedJSONValue& out, const char * str); |
mfiore | 5:dd1e00e3eba5 | 429 | inline bool _parse(MbedJSONValue& out, input& in); |
mfiore | 5:dd1e00e3eba5 | 430 | inline bool _parse_number(MbedJSONValue& out, input& in); |
mfiore | 5:dd1e00e3eba5 | 431 | inline bool _parse_string(MbedJSONValue& out, input& in); |
mfiore | 5:dd1e00e3eba5 | 432 | inline bool _parse_array(MbedJSONValue& out, input& in); |
mfiore | 5:dd1e00e3eba5 | 433 | inline bool _parse_object(MbedJSONValue& out, input& in); |
mfiore | 5:dd1e00e3eba5 | 434 | |
mfiore | 5:dd1e00e3eba5 | 435 | |
mfiore | 5:dd1e00e3eba5 | 436 | inline bool _parse_string(MbedJSONValue& out, input& in) { |
mfiore | 5:dd1e00e3eba5 | 437 | #ifdef DEBUG |
mfiore | 5:dd1e00e3eba5 | 438 | printf("string detected\r\n"); |
mfiore | 5:dd1e00e3eba5 | 439 | #endif |
mfiore | 5:dd1e00e3eba5 | 440 | out = MbedJSONValue(std::string("")); |
mfiore | 5:dd1e00e3eba5 | 441 | std::string& s = out.get<std::string>(); |
mfiore | 5:dd1e00e3eba5 | 442 | while (1) { |
mfiore | 5:dd1e00e3eba5 | 443 | int ch = in.getc(); |
mfiore | 5:dd1e00e3eba5 | 444 | if (ch < ' ') { |
mfiore | 5:dd1e00e3eba5 | 445 | in.ungetc(); |
mfiore | 5:dd1e00e3eba5 | 446 | return false; |
mfiore | 5:dd1e00e3eba5 | 447 | } else if (ch == '"') { |
mfiore | 5:dd1e00e3eba5 | 448 | return true; |
mfiore | 5:dd1e00e3eba5 | 449 | } else if (ch == '\\') { |
mfiore | 5:dd1e00e3eba5 | 450 | if ((ch = in.getc()) == -1) { |
mfiore | 5:dd1e00e3eba5 | 451 | return false; |
mfiore | 5:dd1e00e3eba5 | 452 | } |
mfiore | 5:dd1e00e3eba5 | 453 | switch (ch) { |
mfiore | 5:dd1e00e3eba5 | 454 | #define MAP(sym, val) case sym: s.push_back(val); break |
mfiore | 5:dd1e00e3eba5 | 455 | MAP('"', '\"'); |
mfiore | 5:dd1e00e3eba5 | 456 | MAP('\\', '\\'); |
mfiore | 5:dd1e00e3eba5 | 457 | MAP('/', '/'); |
mfiore | 5:dd1e00e3eba5 | 458 | MAP('b', '\b'); |
mfiore | 5:dd1e00e3eba5 | 459 | MAP('f', '\f'); |
mfiore | 5:dd1e00e3eba5 | 460 | MAP('n', '\n'); |
mfiore | 5:dd1e00e3eba5 | 461 | MAP('r', '\r'); |
mfiore | 5:dd1e00e3eba5 | 462 | MAP('t', '\t'); |
mfiore | 5:dd1e00e3eba5 | 463 | #undef MAP |
mfiore | 5:dd1e00e3eba5 | 464 | default: |
mfiore | 5:dd1e00e3eba5 | 465 | return false; |
mfiore | 5:dd1e00e3eba5 | 466 | } |
mfiore | 5:dd1e00e3eba5 | 467 | } else { |
mfiore | 5:dd1e00e3eba5 | 468 | s.push_back(ch); |
mfiore | 5:dd1e00e3eba5 | 469 | } |
mfiore | 5:dd1e00e3eba5 | 470 | } |
mfiore | 5:dd1e00e3eba5 | 471 | } |
mfiore | 5:dd1e00e3eba5 | 472 | |
mfiore | 5:dd1e00e3eba5 | 473 | inline bool _parse_array(MbedJSONValue& out, input& in) { |
mfiore | 5:dd1e00e3eba5 | 474 | #ifdef DEBUG |
mfiore | 5:dd1e00e3eba5 | 475 | printf("array detected\r\n"); |
mfiore | 5:dd1e00e3eba5 | 476 | #endif |
mfiore | 5:dd1e00e3eba5 | 477 | int i = 0; |
mfiore | 5:dd1e00e3eba5 | 478 | if (in.expect(']')) { |
mfiore | 5:dd1e00e3eba5 | 479 | return true; |
mfiore | 5:dd1e00e3eba5 | 480 | } |
mfiore | 5:dd1e00e3eba5 | 481 | do { |
mfiore | 5:dd1e00e3eba5 | 482 | if (! _parse(out[i], in)) { |
mfiore | 5:dd1e00e3eba5 | 483 | return false; |
mfiore | 5:dd1e00e3eba5 | 484 | } |
mfiore | 5:dd1e00e3eba5 | 485 | i++; |
mfiore | 5:dd1e00e3eba5 | 486 | } while (in.expect(',')); |
mfiore | 5:dd1e00e3eba5 | 487 | return in.expect(']'); |
mfiore | 5:dd1e00e3eba5 | 488 | } |
mfiore | 5:dd1e00e3eba5 | 489 | |
mfiore | 5:dd1e00e3eba5 | 490 | inline bool _parse_object(MbedJSONValue& out, input& in) { |
mfiore | 5:dd1e00e3eba5 | 491 | #ifdef DEBUG |
mfiore | 5:dd1e00e3eba5 | 492 | printf("object detected\r\n"); |
mfiore | 5:dd1e00e3eba5 | 493 | #endif |
mfiore | 5:dd1e00e3eba5 | 494 | if (in.expect('}')) { |
mfiore | 5:dd1e00e3eba5 | 495 | return true; |
mfiore | 5:dd1e00e3eba5 | 496 | } |
mfiore | 5:dd1e00e3eba5 | 497 | do { |
mfiore | 5:dd1e00e3eba5 | 498 | MbedJSONValue key, val; |
mfiore | 5:dd1e00e3eba5 | 499 | if (in.expect('"') && _parse_string(key, in) && in.expect(':') && _parse(val, in)) { |
mfiore | 5:dd1e00e3eba5 | 500 | out[key.get<std::string>().c_str()] = val; |
mfiore | 5:dd1e00e3eba5 | 501 | #ifdef DEBUG |
mfiore | 5:dd1e00e3eba5 | 502 | printf("key: %s \r\n", key.get<std::string>().c_str()); |
mfiore | 5:dd1e00e3eba5 | 503 | #endif |
mfiore | 5:dd1e00e3eba5 | 504 | } else { |
mfiore | 5:dd1e00e3eba5 | 505 | return false; |
mfiore | 5:dd1e00e3eba5 | 506 | } |
mfiore | 5:dd1e00e3eba5 | 507 | } while (in.expect(',')); |
mfiore | 5:dd1e00e3eba5 | 508 | return in.expect('}'); |
mfiore | 5:dd1e00e3eba5 | 509 | } |
mfiore | 5:dd1e00e3eba5 | 510 | |
mfiore | 5:dd1e00e3eba5 | 511 | inline bool _parse_number(MbedJSONValue& out, input& in) { |
mfiore | 5:dd1e00e3eba5 | 512 | #ifdef DEBUG |
mfiore | 5:dd1e00e3eba5 | 513 | printf("number detected\r\n"); |
mfiore | 5:dd1e00e3eba5 | 514 | #endif |
mfiore | 5:dd1e00e3eba5 | 515 | std::string num_str; |
mfiore | 5:dd1e00e3eba5 | 516 | while (1) { |
mfiore | 5:dd1e00e3eba5 | 517 | int ch = in.getc(); |
mfiore | 5:dd1e00e3eba5 | 518 | if (('0' <= ch && ch <= '9') || ch == '+' || ch == '-' || ch == '.' |
mfiore | 5:dd1e00e3eba5 | 519 | || ch == 'e' || ch == 'E') { |
mfiore | 5:dd1e00e3eba5 | 520 | num_str.push_back(ch); |
mfiore | 5:dd1e00e3eba5 | 521 | } else { |
mfiore | 5:dd1e00e3eba5 | 522 | in.ungetc(); |
mfiore | 5:dd1e00e3eba5 | 523 | break; |
mfiore | 5:dd1e00e3eba5 | 524 | } |
mfiore | 5:dd1e00e3eba5 | 525 | } |
mfiore | 5:dd1e00e3eba5 | 526 | char* endp; |
mfiore | 5:dd1e00e3eba5 | 527 | if (strchr(num_str.c_str(), '.') != NULL || strchr(num_str.c_str(), 'e') != NULL || strchr(num_str.c_str(), '+') != NULL) |
mfiore | 5:dd1e00e3eba5 | 528 | out = MbedJSONValue(strtod(num_str.c_str(), &endp)); |
mfiore | 5:dd1e00e3eba5 | 529 | else |
mfiore | 5:dd1e00e3eba5 | 530 | out = MbedJSONValue((int)strtod(num_str.c_str(), &endp)); |
mfiore | 5:dd1e00e3eba5 | 531 | return endp == num_str.c_str() + num_str.size(); |
mfiore | 5:dd1e00e3eba5 | 532 | } |
mfiore | 5:dd1e00e3eba5 | 533 | |
mfiore | 5:dd1e00e3eba5 | 534 | inline bool _parse(MbedJSONValue& out, input& in) { |
mfiore | 5:dd1e00e3eba5 | 535 | in.skip_ws(); |
mfiore | 5:dd1e00e3eba5 | 536 | int ch = in.getc(); |
mfiore | 5:dd1e00e3eba5 | 537 | switch (ch) { |
mfiore | 5:dd1e00e3eba5 | 538 | #define IS(ch, text, val) case ch: \ |
mfiore | 5:dd1e00e3eba5 | 539 | if (in.match(text)) { \ |
mfiore | 5:dd1e00e3eba5 | 540 | out = val; \ |
mfiore | 5:dd1e00e3eba5 | 541 | return true; \ |
mfiore | 5:dd1e00e3eba5 | 542 | } else { \ |
mfiore | 5:dd1e00e3eba5 | 543 | return false; \ |
mfiore | 5:dd1e00e3eba5 | 544 | } |
mfiore | 5:dd1e00e3eba5 | 545 | IS('n', "ull", MbedJSONValue()); |
mfiore | 5:dd1e00e3eba5 | 546 | IS('f', "alse", MbedJSONValue(false)); |
mfiore | 5:dd1e00e3eba5 | 547 | IS('t', "rue", MbedJSONValue(true)); |
mfiore | 5:dd1e00e3eba5 | 548 | #undef IS |
mfiore | 5:dd1e00e3eba5 | 549 | case '"': |
mfiore | 5:dd1e00e3eba5 | 550 | return _parse_string(out, in); |
mfiore | 5:dd1e00e3eba5 | 551 | case '[': |
mfiore | 5:dd1e00e3eba5 | 552 | return _parse_array(out, in); |
mfiore | 5:dd1e00e3eba5 | 553 | case '{': |
mfiore | 5:dd1e00e3eba5 | 554 | return _parse_object(out, in); |
mfiore | 5:dd1e00e3eba5 | 555 | default: |
mfiore | 5:dd1e00e3eba5 | 556 | if (('0' <= ch && ch <= '9') || ch == '-') { |
mfiore | 5:dd1e00e3eba5 | 557 | in.ungetc(); |
mfiore | 5:dd1e00e3eba5 | 558 | return _parse_number(out, in); |
mfiore | 5:dd1e00e3eba5 | 559 | } |
mfiore | 5:dd1e00e3eba5 | 560 | break; |
mfiore | 5:dd1e00e3eba5 | 561 | } |
mfiore | 5:dd1e00e3eba5 | 562 | in.ungetc(); |
mfiore | 5:dd1e00e3eba5 | 563 | return false; |
mfiore | 5:dd1e00e3eba5 | 564 | } |
mfiore | 5:dd1e00e3eba5 | 565 | |
mfiore | 5:dd1e00e3eba5 | 566 | inline std::string parse(MbedJSONValue& out, const char * pos) { |
mfiore | 5:dd1e00e3eba5 | 567 | const char * last = pos + strlen(pos); |
mfiore | 5:dd1e00e3eba5 | 568 | std::string err; |
mfiore | 5:dd1e00e3eba5 | 569 | pos = parse(out, pos, last, &err); |
mfiore | 5:dd1e00e3eba5 | 570 | return err; |
mfiore | 5:dd1e00e3eba5 | 571 | } |
mfiore | 5:dd1e00e3eba5 | 572 | |
mfiore | 5:dd1e00e3eba5 | 573 | inline const char * parse(MbedJSONValue& out, const char * first, const char * last, std::string* err) { |
mfiore | 5:dd1e00e3eba5 | 574 | input in = input(first, last); |
mfiore | 5:dd1e00e3eba5 | 575 | if (! _parse(out, in) && err != NULL) { |
mfiore | 5:dd1e00e3eba5 | 576 | char buf[64]; |
mfiore | 5:dd1e00e3eba5 | 577 | sprintf(buf, "syntax error at line %d near: ", in.line()); |
mfiore | 5:dd1e00e3eba5 | 578 | *err = buf; |
mfiore | 5:dd1e00e3eba5 | 579 | while (1) { |
mfiore | 5:dd1e00e3eba5 | 580 | int ch = in.getc(); |
mfiore | 5:dd1e00e3eba5 | 581 | if (ch == -1 || ch == '\n') { |
mfiore | 5:dd1e00e3eba5 | 582 | break; |
mfiore | 5:dd1e00e3eba5 | 583 | } else if (ch >= ' ') { |
mfiore | 5:dd1e00e3eba5 | 584 | err->push_back(ch); |
mfiore | 5:dd1e00e3eba5 | 585 | } |
mfiore | 5:dd1e00e3eba5 | 586 | } |
mfiore | 5:dd1e00e3eba5 | 587 | } |
mfiore | 5:dd1e00e3eba5 | 588 | return in.cur(); |
mfiore | 5:dd1e00e3eba5 | 589 | } |
mfiore | 5:dd1e00e3eba5 | 590 | |
mfiore | 5:dd1e00e3eba5 | 591 | #endif // _MbedMbedJSONValue_H_ |