Minimalist JSON parser and serializer (inspired by picojson). Used by MbedJSONRpc. (Original by Samuel Mokrani)
Dependents: City_Game_JSON City_Game_JSON
Fork of MbedJSONValue by
Revision 7:a2fabe322eff, committed 2016-12-14
- Comitter:
- Nico De Witte
- Date:
- Wed Dec 14 17:46:06 2016 +0100
- Parent:
- 6:c21b9ffd9628
- Commit message:
- Change hasMember to take const char * instead of char *. Should be const and also gets rid of some warnings.
Changed in this revision
MbedJSONValue.cpp | Show annotated file Show diff for this revision Revisions of this file |
MbedJSONValue.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/MbedJSONValue.cpp Thu Nov 24 20:25:42 2016 +0000 +++ b/MbedJSONValue.cpp Wed Dec 14 17:46:06 2016 +0100 @@ -28,7 +28,7 @@ _type = TypeNull; } -bool MbedJSONValue::hasMember(char * name) +bool MbedJSONValue::hasMember(const char * name) { for(int i = 0; i < index_token; i++) if( !strcmp(name, (*(token_name[i])).c_str() )) @@ -190,7 +190,7 @@ return *(token[i]); } } - + //if the user is not doing something wrong, this code is never executed!! return *(new MbedJSONValue()); } @@ -242,4 +242,3 @@ } return 0; // 0 is better than -1 for for-loops } -
--- a/MbedJSONValue.h Thu Nov 24 20:25:42 2016 +0000 +++ b/MbedJSONValue.h Wed Dec 14 17:46:06 2016 +0100 @@ -42,7 +42,7 @@ /** MbedJSONValue class * * Example: - * - creation of an MbedJSONValue of type TypeObject containing two others MbedJSONValue: + * - creation of an MbedJSONValue of type TypeObject containing two others MbedJSONValue: * -one array of one string and one integer identified by "my_array" * -a boolean identified by "my_boolean" * - serialization in JSON format of this object @@ -51,7 +51,7 @@ * #include "MbedJSONValue.h" * #include <string> * - * int main() { + * int main() { * * MbedJSONValue demo; * std::string s; @@ -65,7 +65,7 @@ * s = demo.serialize(); * printf("json: %s\r\n", s.c_str()); * } - * + * * @endcode * * Example: @@ -76,7 +76,7 @@ * #include "MbedJSONValue.h" * #include <string> * - * int main() { + * int main() { * MbedJSONValue demo; * * const char * json = "{\"my_array\": [\"demo_string\", 10], \"my_boolean\": true}"; @@ -91,7 +91,7 @@ * my_str = demo["my_array"][0].get<std::string>(); * my_int = demo["my_array"][1].get<int>(); * my_bool = demo["my_boolean"].get<bool>(); - * + * * printf("my_str: %s\r\n", my_str.c_str()); * printf("my_int: %d\r\n", my_int); * printf("my_bool: %s\r\n", my_bool ? "true" : "false"); @@ -119,7 +119,7 @@ * MbedJSONValue constructor of type TypeNull */ MbedJSONValue() : _type(TypeNull), index_array(0), index_token(0) {} - + /** * MbedJSONValue constructor of type TypeBoolean * @@ -128,7 +128,7 @@ MbedJSONValue(bool value) : _type(TypeBoolean), index_array(0), index_token(0) { _value.asBool = value; } - + /** * MbedJSONValue constructor of type TypeInt * @@ -137,7 +137,7 @@ MbedJSONValue(int value) : _type(TypeInt), index_array(0), index_token(0) { _value.asInt = value; } - + /** * MbedJSONValue constructor of type TypeDouble * @@ -184,7 +184,7 @@ * @return a reference on the MbedJSONValue affected */ MbedJSONValue& operator=(MbedJSONValue const & rhs); - + /** * = Operator overloading for an MbedJSONValue from an int * @@ -192,7 +192,7 @@ * @return a reference on the MbedJSONValue affected */ MbedJSONValue& operator=(int const& rhs) { return operator=(MbedJSONValue(rhs)); } - + /** * = Operator overloading for an MbedJSONValue from a boolean * @@ -200,7 +200,7 @@ * @return a reference on the MbedJSONValue affected */ MbedJSONValue& operator=(bool const& rhs) { return operator=(MbedJSONValue(rhs)); } - + /** * = Operator overloading for an MbedJSONValue from a double * @@ -208,7 +208,7 @@ * @return a reference on the MbedJSONValue affected */ MbedJSONValue& operator=(double const& rhs) { return operator=(MbedJSONValue(rhs)); } - + /** * = Operator overloading for an MbedJSONValue from a string * @@ -216,21 +216,21 @@ * @return a reference on the MbedJSONValue affected */ MbedJSONValue& operator=(const char* rhs) { return operator=(MbedJSONValue(std::string(rhs))); } - - + + /** * [] Operator overloading for an MbedJSONValue. - * Each TypeObject object can contain an array of NB_TOKEN MbedJSONValue. + * Each TypeObject object can contain an array of NB_TOKEN MbedJSONValue. * This operator is useful to create an array or to retrieve an MbedJSONValue of an existing array. * * @param i index of the array * @return a reference on the MbedJSONValue created or retrieved */ MbedJSONValue& operator[](int i); - + /** * [] Operator overloading for an MbedJSONValue. - * Each TypeObject MbedJSONValue can contain NB_TOKEN MbedJSONValue IDENTIFIED BY A NAME + * Each TypeObject MbedJSONValue can contain NB_TOKEN MbedJSONValue IDENTIFIED BY A NAME * This operator is useful to create a TypeObject MbedJSONValue or to retrieve an MbedJSONValue of an existing TypeObject. * * @@ -249,7 +249,7 @@ * @return A contant reference on the value of the object */ template <typename T> const T& get() const; - + /** * Retrieve the value of an MbedJSONValue object. * @@ -272,7 +272,7 @@ } /** - * Return the size of an MbedJSONValue object (works for TypeString, TypeArray or TypeObject) + * Return the size of an MbedJSONValue object (works for TypeString, TypeArray or TypeObject) * * @return size */ @@ -284,7 +284,7 @@ * @param name Identifier * @return true if the object is of type TypeObject AND contains a member named "name", false otherwise */ - bool hasMember(char * name); + bool hasMember(const char * name); /** * Convert an MbedJSONValue in a JSON frame @@ -297,7 +297,7 @@ // object type Type _type; - + //indexes of TypeObject and TypeArray int index_array; int index_token; @@ -322,7 +322,7 @@ MbedJSONValue& operator[](int i) const { return *(array[i]); } MbedJSONValue& operator[](std::string k) const; - + std::string to_str(); void serialize(std::back_insert_iterator<std::string> os);