hwelltech block chain cpp sdk

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed uniqueCPUID

Fork of bcsdk by Webb Xu

bcsdk 是 block chain sdk 的简写。它是由江苏恒为信息科技有限公司开发的 BlockChain 的 基于 mbed os 的 C++ 版本SDK,它能帮助开发者快速的在支持 mbed os 的芯片上开发 BlockChain 的应用。 bcsdk 的demo文件中包含了BlockChain中的 Key、Account、Asset、Transaction等方面的示例。其中: (1) Key 加密私钥是区块链上的主要授权机制。他们控制资产单位的发行和转让。 资产或帐户将定义发行或转移所需的单个密钥。 在 Key_test.cpp 中,我们实现了: 创建HSM密钥 键入密钥别名的名称(例如'gold','silver','bronze'),密钥别名是用于区分密钥的标签。

(2) Asset 资产是一种可以在区块链上发布的值类型。资产的所有单位均可互换,可以在各方之间直接交易,无需发行人参与。 在 Asset_test.cpp 中,我们实现了: 创建资产 键入资产别名的名称(例如'gold','silver','bronze'),资产别名是用于区分资产的标签。 选择“Key”键以使用现有的HSM密键,此密钥将用于此帐户中资产单位的发放和转移。

(3) Account 加帐户是恒为区块链核心平台中的一个对象,通过创建和跟踪控制程序来跟踪区块链上的资产的所有权。创建帐户时,您提供一个或多个“root”密钥和仲裁。 在 Account_test.cpp 中,我们实现了: 创建帐户 1 输入帐户别名的名称(例如'alice','bob'),帐户别名是用于区分帐户的标签。 2 键入名称以生成新的HSM密钥(例如'alice key','bob key'),此密钥将用于此帐户中资产单位的发放和转移。

(4) Transaction 交易包含一个或多个输入,以及一个或多个输出。恒为区块链核心平台的API允许您使用操作(包括发出,支出和返还)构建交易。 在 Transaction_test.cpp 中,我们实现了: 资产的交易 1 添加“账户支出”操作 2 为资产别名选择“Asset” 3 输入“100”作为金额 4 添加“使用帐户控制”操作 5 为帐户别名选择“Account” 6 为资产别名选择“Asset” 7 输入“100”作为金额 8 “提交”

Committer:
webmaster
Date:
Fri Nov 03 01:07:32 2017 +0000
Revision:
10:aabd720e632c
Parent:
8:f2a567ee3a46
publish v0.1

Who changed what in which revision?

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