[DEV] some interface adaptation and documentation update

This commit is contained in:
Edouard DUPIN 2013-12-31 21:14:54 +01:00
parent fbf39d9ac2
commit 8cca9d9ce2
14 changed files with 607 additions and 40 deletions

32
doc/index.bb Normal file
View File

@ -0,0 +1,32 @@
== [center]E-json library[/center] ==
__________________________________________________
===What is EJSON, and how can I use it?===
EJSON, or Ewol json file interface, is a multi-platform library for creating and reading json file.
===What languages are supported?===
EJSON is written in C++.
===Are there any licensing restrictions?===
EJSON is [b]FREE software[/b].
That allow you to use it for every program you want, including those developing proprietary software, without any license fees or royalties.
==== License (DSB) ====
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
:** Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
:** Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
:** The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
==== Tutorials : ====
:** [tutorial[000_Build | Build example]]
:** [tutorial[001_Read | Read a json file]]
:** [tutorial[002_Write | Write a json file]]

41
doc/tutorial/000_Build.bb Normal file
View File

@ -0,0 +1,41 @@
=?=E-json extract and build examples=?=
__________________________________________________
[right][tutorial[001_Read | Next: Read a file]][/right]
All developpement software will start by getting the dependency and the sources.
=== Linux dependency packages ===
[code style=shell]
sudo apt-get install g++ zlib1g-dev
# if you want to compile with clang :
sudo apt-get install clang
[/code]
=== Download instructions ===
Download the software :
[code style=shell]
# create a working directory path
mkdir your_workspace_path
cd your_workspace_path
# clone ewol and all sub-library
git clone http://github.com/HeeroYui/ewol.git
cd ewol
git submodule init
git submodule update
cd ..
# download examples
git clone http://github.com/HeeroYui/example.git
[/code]
[note]
The full build tool documentation is availlable here : [[http://heeroyui.github.io/lutin/ | lutin]]
[/note]
=== Common build instructions ===
Compile software in debug for the curent platform :
[code style=shell]
./ewol/build/lutin.py -mdebug ejson*
[/code]

282
doc/tutorial/001_Read.bb Normal file
View File

@ -0,0 +1,282 @@
=?=Read a json file=?=
__________________________________________________
[left][tutorial[000_Build | Previous: Build example]][/left] [right][tutorial[002_Write | Next: Write a file]][/right]
A json file is like a xml, but in a simplest way.
It is in the minimum constituated of:
[code style=json]
{}
[/code]
This is the simplest json code.
for example we will use the next json file :
[code style=json]
{
"element1":25622.53,
"element2":"a string...",
"is active":false,
"NULL element":null,
"exampleObject":{
"a string":"my super example of string",
"a null element":null,
"an array element":[
12, 25, 65, 654
],
"a boolean Element":true,
"a simple sumber"=156156.343,
"an other object":{
"plop": 1,
"plop2": 2
}
},
"exampleArray":[
12,
25,
65,
654,
{
"plup": true,
"plup2": false
},
true,
null,
[ 22, 23, 24, 25]
}
}
[/code]
== Open the file ==
The first step to open a file is to create the json document:
[code style=c++]
#include <ejson/ejson.h>
int main() {
// declare document
ejson::Document doc;
...
}
[/code]
=== Load a stored file ===
It is important to remember that the input file is manage by etk,
then the naming form is describe by the class: [class[etk::FSNode]]
[code style=c++]
// read file
if (doc.load("DATA:example.json") == false) {
APPL_ERROR("An error occured when reading the file...");
// TODO : STANDARDIZE ERROR....
return -1;
}
[/code]
=== Load a file stored in memory ===
This step is easyest has a reading in a file.
In the first step, declare a string containing the json description:
[code style=c++]
std:string myJson = "{ \"element1\":25622.53, \"element2\":\"a string...\" }";
[/code]
Now we just need to load the string:
[code style=c++]
if (doc.parse(myJson) == false) {
APPL_ERROR("An error occured when parsing the string");
return -1;
}
[/code]
it could be interesting to add some \n in the string to find some error in the json string
== Access on the data ==
now we have the data stored in the doc instance, it could be interesting to access on it.
Despite of XML interface that is not designed to be keep in memory but just parsed and drop,
the json element is many time use as a simple interface to acces on the data.
This is the reason for this json interfce is designed for simple acces and use.
=== The simple way ===
Read a number value in the doc:
[code style=c++]
double myValue = doc.getNumberValue("element1", 49);
APPL_INFO("Get the element value:" << myValue);
[/code]
Note that we had a return value, in case of the element is not present or in the wrong form.
We have the same interface for boolean, string and number:
:** [methode[ejson::Object::getNumberValue]]
:** [methode[ejson::Object::getStringValue]]
:** [methode[ejson::Object::getBooleanValue]]
These interface methode are availlable for [class[ejson::Document]], [class[ejson::Object]].
The similar interface are availlable on [class[ejson:Array]]:
:** [methode[ejson::Array::getNumberValue]]
:** [methode[ejson::Array::getStringValue]]
:** [methode[ejson::Array::getBooleanValue]]
It could be interesting to remember that the maain node of a json file in an object,
this is the reason that [class[ejson::Document]] herited of [class[ejson::Object]].
=== The generic way ===
The classical way to read a json file is to parse it like a xml:
==== Object ====
We are now reading all node in an object:
[code style=c++]
ejson::Object* obj = doc.getObject("exampleObject");
// note that the obj is NULL if it not an "Object"
if (obj == NULL) {
APPL_ERROR("Can not get the object 'exampleObject' in the json file");
return -1;
}
[/code]
Note at this point we can parse an object in 2 way:
1: The fastest but not the best:
[code style=c++]
for (size_t iii=0; iii < obj->size(); ++iii) {
std::string key = obj->getKey(iii);
ejson::Value* val = obj[iii];
// note that error can appear, then check result...
if (val == NULL) {
APPL_ERROR("Can not read the object id=" << iii);
continue;
}
switch(val->getType()) {
case typeArray: {
ejson::Array* myArray = val->toArray();
APPL_INFO("Find an Array @" << key);
} break;
case typeString: {
ejson::String* myString = val->toString();
APPL_INFO("Find a String @" << key << " value='" << myString->get() << "'");
} break;
case typeNumber: {
ejson::Number* myNumber = val->toNumber();
APPL_INFO("Find a Number @" << key << " value='" << myNumber->get() << "'");
} break;
case typeBoolean: {
ejson::Boolean* myBoolean = val->toBoolean();
APPL_INFO("Find a Boolean @" << key << " value='" << myBoolean->get() << "'");
} break;
case typeNull:
APPL_INFO("Find a null @" << key);
break;
case typeObject: {
ejson::Object* myObject = val->toObject();
APPL_INFO("Find an Object @" << key);
} break;
}
}
[/code]
2: A more generic way to acces on the elemnts:
[code style=c++]
stk::vector<std::string> keys = obj->getKeys();
for (auto key in keys) {
ejson::Value* val = obj[key];
// note that error can appear, then check result...
if (val == NULL) {
APPL_ERROR("Can not read the object key=" << key);
continue;
}
switch(val->getType()) {
case typeArray: {
ejson::Array* myArray = val->toArray();
APPL_INFO("Find an Array @" << key);
} break;
case typeString: {
ejson::String* myString = val->toString();
APPL_INFO("Find a String @" << key << " value='" << myString->get() << "'");
} break;
case typeNumber: {
ejson::Number* myNumber = val->toNumber();
APPL_INFO("Find a Number @" << key << " value='" << myNumber->get() << "'");
} break;
case typeBoolean: {
ejson::Boolean* myBoolean = val->toBoolean();
APPL_INFO("Find a Boolean @" << key << " value='" << myBoolean->get() << "'");
} break;
case typeNull:
APPL_INFO("Find a null @" << key);
break;
case typeObject: {
ejson::Object* myObject = val->toObject();
APPL_INFO("Find an Object @" << key);
} break;
}
}
[/code]
==== Array ====
We are now reading all node in an Array:
[code style=c++]
ejson::Array* obj = doc.getArray("exampleArray");
// note that the obj is NULL if it not an "Array"
if (obj == NULL) {
APPL_ERROR("Can not get the array 'exampleArray' in the json file");
return -1;
}
[/code]
Note for an array we have only one methode to parse the data :
[code style=c++]
for (size_t iii=0; iii < obj->size(); ++iii) {
ejson::Value* val = obj[iii];
// or ejson::Value* val = obj->get(iii);
// note that error can appear, then check result...
if (val == NULL) {
APPL_ERROR("Can not read the object id=" << iii);
continue;
}
switch(val->getType()) {
case typeArray: {
ejson::Array* myArray = val->toArray();
APPL_INFO("Find an Array @" << key);
} break;
case typeString: {
ejson::String* myString = val->toString();
APPL_INFO("Find a String @" << key << " value='" << myString->get() << "'");
} break;
case typeNumber: {
ejson::Number* myNumber = val->toNumber();
APPL_INFO("Find a Number @" << key << " value='" << myNumber->get() << "'");
} break;
case typeBoolean: {
ejson::Boolean* myBoolean = val->toBoolean();
APPL_INFO("Find a Boolean @" << key << " value='" << myBoolean->get() << "'");
} break;
case typeNull:
APPL_INFO("Find a null @" << key);
break;
case typeObject: {
ejson::Object* myObject = val->toObject();
APPL_INFO("Find an Object @" << key);
} break;
}
}
[/code]
It is important to note that many time the user know what type will appear in a list or in an object , then you can directly use:
:** [methode[ejson::Array::getNumber]]
:** [methode[ejson::Array::getNull]]
:** [methode[ejson::Array::getArray]]
:** [methode[ejson::Array::getObject]]
:** [methode[ejson::Array::getBoolean]]
:** [methode[ejson::Array::getstring]]
These fuction automatly cast the resut in the good form (if it is the real one)

View File

@ -0,0 +1,3 @@
=?=Write a json file=?=
__________________________________________________
[left][tutorial[001_Read | Previous: Read a file]][/left]

View File

@ -257,6 +257,13 @@ ejson::Object* ejson::Array::getObject(size_t _id) {
}
return tmpElement->toObject();
}
const ejson::Object* ejson::Array::getObject(size_t _id) const {
const ejson::Value* tmpElement = m_value[_id];
if (NULL == tmpElement) {
return NULL;
}
return tmpElement->toObject();
}
ejson::String* ejson::Array::getString(size_t _id) {
ejson::Value* tmpElement = m_value[_id];
@ -266,6 +273,14 @@ ejson::String* ejson::Array::getString(size_t _id) {
return tmpElement->toString();
}
const ejson::String* ejson::Array::getString(size_t _id) const {
const ejson::Value* tmpElement = m_value[_id];
if (NULL == tmpElement) {
return NULL;
}
return tmpElement->toString();
}
ejson::Array* ejson::Array::getArray(size_t _id) {
ejson::Value* tmpElement = m_value[_id];
if (NULL == tmpElement) {
@ -274,6 +289,14 @@ ejson::Array* ejson::Array::getArray(size_t _id) {
return tmpElement->toArray();
}
const ejson::Array* ejson::Array::getArray(size_t _id) const {
const ejson::Value* tmpElement = m_value[_id];
if (NULL == tmpElement) {
return NULL;
}
return tmpElement->toArray();
}
ejson::Null* ejson::Array::getNull(size_t _id) {
ejson::Value* tmpElement = m_value[_id];
if (NULL == tmpElement) {
@ -282,6 +305,14 @@ ejson::Null* ejson::Array::getNull(size_t _id) {
return tmpElement->toNull();
}
const ejson::Null* ejson::Array::getNull(size_t _id) const {
const ejson::Value* tmpElement = m_value[_id];
if (NULL == tmpElement) {
return NULL;
}
return tmpElement->toNull();
}
ejson::Number* ejson::Array::getNumber(size_t _id) {
ejson::Value* tmpElement = m_value[_id];
if (NULL == tmpElement) {
@ -290,6 +321,14 @@ ejson::Number* ejson::Array::getNumber(size_t _id) {
return tmpElement->toNumber();
}
const ejson::Number* ejson::Array::getNumber(size_t _id) const {
const ejson::Value* tmpElement = m_value[_id];
if (NULL == tmpElement) {
return NULL;
}
return tmpElement->toNumber();
}
ejson::Boolean* ejson::Array::getBoolean(size_t _id) {
ejson::Value* tmpElement = m_value[_id];
if (NULL == tmpElement) {
@ -298,33 +337,49 @@ ejson::Boolean* ejson::Array::getBoolean(size_t _id) {
return tmpElement->toBoolean();
}
const std::string& ejson::Array::getStringValue(size_t _id) {
static const std::string errorValue("");
const ejson::Boolean* ejson::Array::getBoolean(size_t _id) const {
const ejson::Value* tmpElement = m_value[_id];
if (NULL == tmpElement) {
return NULL;
}
return tmpElement->toBoolean();
}
std::string ejson::Array::getStringValue(size_t _id) {
ejson::String* tmpElement = getString(_id);
if (NULL == tmpElement) {
return "";
}
return tmpElement->get();
}
const std::string& ejson::Array::getStringValue(size_t _id) const {
static const std::string errorValue("");
const ejson::String* tmpElement = getString(_id);
if (NULL == tmpElement) {
return errorValue;
}
return tmpElement->get();
}
std::string ejson::Array::getStringValue(size_t _id, const std::string& _errorValue) {
ejson::String* tmpElement = getString(_id);
std::string ejson::Array::getStringValue(size_t _id, const std::string& _errorValue) const {
const ejson::String* tmpElement = getString(_id);
if (NULL == tmpElement) {
return _errorValue;
}
return tmpElement->get();
}
double ejson::Array::getNumberValue(size_t _id, double _errorValue) {
ejson::Number* tmpElement = getNumber(_id);
double ejson::Array::getNumberValue(size_t _id, double _errorValue) const {
const ejson::Number* tmpElement = getNumber(_id);
if (NULL == tmpElement) {
return _errorValue;
}
return tmpElement->get();
}
bool ejson::Array::getBooleanValue(size_t _id, bool _errorValue) {
ejson::Boolean* tmpElement = getBoolean(_id);
bool ejson::Array::getBooleanValue(size_t _id, bool _errorValue) const {
const ejson::Boolean* tmpElement = getBoolean(_id);
if (NULL == tmpElement) {
return _errorValue;
}

View File

@ -20,15 +20,11 @@ namespace ejson {
/**
* @brief basic element of a xml structure
*/
Array(void) {
};
Array(void) { };
/**
* @brief destructor
*/
virtual ~Array(void) {
};
virtual ~Array(void) { };
private:
std::vector<ejson::Value*> m_value; //!< vector of sub elements
public:
@ -44,75 +40,98 @@ namespace ejson {
* @param[in] _id Id of the element.
* @return NULL if the element does not exist.
*/
const ejson::Value* get(size_t _id) const {
return m_value[_id];
};
ejson::Value* get(size_t _id) {
return m_value[_id];
};
//! @previous
const ejson::Value* get(size_t _id) const{
return m_value[_id];
};
//! @previous
ejson::Value* operator[] (size_t _id) {
return m_value[_id];
}
//! @previous
const ejson::Value* operator[] (size_t _id) const {
return m_value[_id];
}
/**
* @brief get the pointer on an element reference with his ID (casted in Object if it is an object).
* @param[in] _id Id of the element.
* @return NULL if the element does not exist.
*/
ejson::Object* getObject(size_t _id);
//! @previous
const ejson::Object* getObject(size_t _id) const;
/**
* @brief get the pointer on an element reference with his ID (casted in String if it is an String).
* @param[in] _id Id of the element.
* @return NULL if the element does not exist.
*/
ejson::String* getString(size_t _id);
//! @previous
const ejson::String* getString(size_t _id) const;
/**
* @brief get the value of the string element (if not a string return "")
* @param[in] _id Id of the element.
* @return value of the element.
*/
const std::string& getStringValue(size_t _id);
std::string getStringValue(size_t _id);
//! @previous
const std::string& getStringValue(size_t _id) const;
/**
* @brief get the value of the string element
* @param[in] _id Id of the element.
* @param[in] _errorValue The return value if an error occured.
* @return value of the element, or the _errorValue.
*/
std::string getStringValue(size_t _id, const std::string& _errorValue);
std::string getStringValue(size_t _id, const std::string& _errorValue) const;
/**
* @brief get the pointer on an element reference with his ID (casted in Array if it is an Array).
* @param[in] _id Id of the element.
* @return NULL if the element does not exist.
*/
ejson::Array* getArray(size_t _id);
//! @previous
const ejson::Array* getArray(size_t _id) const;
/**
* @brief get the pointer on an element reference with his ID (casted in Null if it is an Null).
* @param[in] _id Id of the element.
* @return NULL if the element does not exist.
*/
ejson::Null* getNull(size_t _id);
//! @previous
const ejson::Null* getNull(size_t _id) const;
/**
* @brief get the pointer on an element reference with his ID (casted in Number if it is an Number).
* @param[in] _id Id of the element.
* @return NULL if the element does not exist.
*/
ejson::Number* getNumber(size_t _id);
//! @previous
const ejson::Number* getNumber(size_t _id) const;
/**
* @brief get the value of the Number element
* @param[in] _id Id of the element.
* @param[in] _errorValue The return value if an error occured.
* @return value of the element, or the _errorValue.
*/
double getNumberValue(size_t _id, double _errorValue);
double getNumberValue(size_t _id, double _errorValue) const;
/**
* @brief get the pointer on an element reference with his ID (casted in Boolean if it is an Boolean).
* @param[in] _id Id of the element.
* @return NULL if the element does not exist.
*/
ejson::Boolean* getBoolean(size_t _id);
//! @previous
const ejson::Boolean* getBoolean(size_t _id) const;
/**
* @brief get the value of the Boolean element
* @param[in] _id Id of the element.
* @param[in] _errorValue The return value if an error occured.
* @return value of the element, or the _errorValue.
*/
bool getBooleanValue(size_t _id, bool _errorValue);
bool getBooleanValue(size_t _id, bool _errorValue) const;
/**
* @brief add an element on the array.
* @param[in] _element element to add.
@ -152,6 +171,7 @@ namespace ejson {
virtual ejson::Array* toArray(void) {
return this;
};
//! @previous
virtual const ejson::Array* toArray(void) const {
return this;
};

View File

@ -56,6 +56,7 @@ namespace ejson {
virtual ejson::Boolean* toBoolean(void) {
return this;
};
//! @previous
virtual const ejson::Boolean* toBoolean(void) const{
return this;
};

View File

@ -34,6 +34,7 @@ namespace ejson {
virtual ejson::Null* toNull(void) {
return this;
};
//! @previous
virtual const ejson::Null* toNull(void) const{
return this;
};

View File

@ -68,6 +68,7 @@ namespace ejson {
virtual ejson::Number* toNumber(void) {
return this;
};
//! @previous
virtual const ejson::Number* toNumber(void) const{
return this;
};

View File

@ -267,14 +267,21 @@ bool ejson::Object::exist(const std::string& _name) const {
return m_value.exist(_name);
}
ejson::Value* ejson::Object::get(const std::string& _name) const {
ejson::Value* ejson::Object::get(const std::string& _name) {
if (false == m_value.exist(_name)) {
return NULL;
}
return m_value[_name];
}
ejson::Object* ejson::Object::getObject(const std::string& _name) const {
const ejson::Value* ejson::Object::get(const std::string& _name) const {
if (false == m_value.exist(_name)) {
return NULL;
}
return m_value[_name];
}
ejson::Object* ejson::Object::getObject(const std::string& _name) {
ejson::Value* tmp = get(_name);
if (NULL == tmp) {
return NULL;
@ -282,7 +289,15 @@ ejson::Object* ejson::Object::getObject(const std::string& _name) const {
return tmp->toObject();
}
ejson::Array* ejson::Object::getArray(const std::string& _name) const {
const ejson::Object* ejson::Object::getObject(const std::string& _name) const {
const ejson::Value* tmp = get(_name);
if (NULL == tmp) {
return NULL;
}
return tmp->toObject();
}
ejson::Array* ejson::Object::getArray(const std::string& _name) {
ejson::Value* tmp = get(_name);
if (NULL == tmp) {
return NULL;
@ -290,7 +305,15 @@ ejson::Array* ejson::Object::getArray(const std::string& _name) const {
return tmp->toArray();
}
ejson::Null* ejson::Object::getNull(const std::string& _name) const {
const ejson::Array* ejson::Object::getArray(const std::string& _name) const {
const ejson::Value* tmp = get(_name);
if (NULL == tmp) {
return NULL;
}
return tmp->toArray();
}
ejson::Null* ejson::Object::getNull(const std::string& _name) {
ejson::Value* tmp = get(_name);
if (NULL == tmp) {
return NULL;
@ -298,7 +321,15 @@ ejson::Null* ejson::Object::getNull(const std::string& _name) const {
return tmp->toNull();
}
ejson::String* ejson::Object::getString(const std::string& _name) const {
const ejson::Null* ejson::Object::getNull(const std::string& _name) const {
const ejson::Value* tmp = get(_name);
if (NULL == tmp) {
return NULL;
}
return tmp->toNull();
}
ejson::String* ejson::Object::getString(const std::string& _name) {
ejson::Value* tmp = get(_name);
if (NULL == tmp) {
return NULL;
@ -306,9 +337,17 @@ ejson::String* ejson::Object::getString(const std::string& _name) const {
return tmp->toString();
}
const ejson::String* ejson::Object::getString(const std::string& _name) const {
const ejson::Value* tmp = get(_name);
if (NULL == tmp) {
return NULL;
}
return tmp->toString();
}
const std::string& ejson::Object::getStringValue(const std::string& _name) const {
static const std::string errorString("");
ejson::String* tmpp = getString(_name);
const ejson::String* tmpp = getString(_name);
if (NULL == tmpp) {
return errorString;
}
@ -316,14 +355,14 @@ const std::string& ejson::Object::getStringValue(const std::string& _name) const
}
std::string ejson::Object::getStringValue(const std::string& _name, const std::string& _errorValue) const {
ejson::String* tmpp = getString(_name);
const ejson::String* tmpp = getString(_name);
if (NULL == tmpp) {
return _errorValue;
}
return tmpp->get();
}
ejson::Boolean* ejson::Object::getBoolean(const std::string& _name) const {
ejson::Boolean* ejson::Object::getBoolean(const std::string& _name) {
ejson::Value* tmp = get(_name);
if (NULL == tmp) {
return NULL;
@ -331,15 +370,23 @@ ejson::Boolean* ejson::Object::getBoolean(const std::string& _name) const {
return tmp->toBoolean();
}
const ejson::Boolean* ejson::Object::getBoolean(const std::string& _name) const {
const ejson::Value* tmp = get(_name);
if (NULL == tmp) {
return NULL;
}
return tmp->toBoolean();
}
bool ejson::Object::getBooleanValue(const std::string& _name, bool _errorValue) const {
ejson::Boolean* tmpp = getBoolean(_name);
const ejson::Boolean* tmpp = getBoolean(_name);
if (NULL == tmpp) {
return _errorValue;
}
return tmpp->get();
}
ejson::Number* ejson::Object::getNumber(const std::string& _name) const {
ejson::Number* ejson::Object::getNumber(const std::string& _name) {
ejson::Value* tmp = get(_name);
if (NULL == tmp) {
return NULL;
@ -347,8 +394,16 @@ ejson::Number* ejson::Object::getNumber(const std::string& _name) const {
return tmp->toNumber();
}
const ejson::Number* ejson::Object::getNumber(const std::string& _name) const {
const ejson::Value* tmp = get(_name);
if (NULL == tmp) {
return NULL;
}
return tmp->toNumber();
}
double ejson::Object::getNumberValue(const std::string& _name, double _errorValue) const {
ejson::Number* tmpp = getNumber(_name);
const ejson::Number* tmpp = getNumber(_name);
if (NULL == tmpp) {
return _errorValue;
}

View File

@ -29,6 +29,7 @@ namespace ejson {
protected:
etk::Hash<ejson::Value*> m_value; //!< value of the node (for element this is the name, for text it is the inside text ...)
public:
// TODO : add direct id access....
/**
* @brief check if an element exist.
* @param[in] _name name of the object.
@ -40,31 +41,91 @@ namespace ejson {
* @param[in] _name name of the object
* @return pointer on the element requested or NULL if it not the corect type or does not existed
*/
ejson::Value* get(const std::string& _name) const;
ejson::Value* get(const std::string& _name);
//! @previous
const ejson::Value* get(const std::string& _name) const;
//! @previous
ejson::Value* operator[] (const std::string& _name) {
return get(_name);
}
//! @previous
const ejson::Value* operator[] (const std::string& _name) const {
return get(_name);
}
/**
* @brief Get all the element name (keys).
* @return a vector of all name (key).
*/
std::vector<std::string> getKeys(void) const {
return m_value.getKeys();
}
/**
* @brief get the number of sub element in the current one
* @return the Number of stored element
*/
size_t size(void) const {
return m_value.size();
};
/**
* @brief get the pointer on an element reference with his ID.
* @param[in] _id Id of the element.
* @return NULL if the element does not exist.
*/
ejson::Value* get(size_t _id) {
return m_value[_id];
};
//! @previous
const ejson::Value* get(size_t _id) const{
return m_value[_id];
};
//! @previous
ejson::Value* operator[] (size_t _id) {
return m_value[_id];
}
//! @previous
const ejson::Value* operator[] (size_t _id) const {
return m_value[_id];
}
/**
* @brief Get the element name (key).
* @param[in] _id Id of the element.
* @return The name (key).
*/
std::string getKey(size_t _id) const {
return m_value.getKey(_id);
}
/**
* @brief get the sub element with his name (Casted as Object if it is possible)
* @param[in] _name name of the object
* @return pointer on the element requested or NULL if it not the corect type or does not existed
*/
ejson::Object* getObject(const std::string& _name) const;
ejson::Object* getObject(const std::string& _name);
//! @previous
const ejson::Object* getObject(const std::string& _name) const;
/**
* @brief get the sub element with his name (Casted as Array if it is possible)
* @param[in] _name name of the object
* @return pointer on the element requested or NULL if it not the corect type or does not existed
*/
ejson::Array* getArray(const std::string& _name) const;
ejson::Array* getArray(const std::string& _name);
//! @previous
const ejson::Array* getArray(const std::string& _name) const;
/**
* @brief get the sub element with his name (Casted as Null if it is possible)
* @param[in] _name name of the object
* @return pointer on the element requested or NULL if it not the corect type or does not existed
*/
ejson::Null* getNull(const std::string& _name) const;
ejson::Null* getNull(const std::string& _name);
//! @previous
const ejson::Null* getNull(const std::string& _name) const;
/**
* @brief get the sub element with his name (Casted as String if it is possible)
* @param[in] _name name of the object
* @return pointer on the element requested or NULL if it not the corect type or does not existed
*/
ejson::String* getString(const std::string& _name) const;
ejson::String* getString(const std::string& _name);
//! @previous
const ejson::String* getString(const std::string& _name) const;
/**
* @brief get the sub string value of the requested element
* @param[in] _name name of the object
@ -83,7 +144,9 @@ namespace ejson {
* @param[in] _name name of the object
* @return pointer on the element requested or NULL if it not the corect type or does not existed
*/
ejson::Boolean* getBoolean(const std::string& _name) const;
ejson::Boolean* getBoolean(const std::string& _name);
//! @previous
const ejson::Boolean* getBoolean(const std::string& _name) const;
/**
* @brief get the sub boolean value of the requested element.
* @param[in] _name name of the object.
@ -96,7 +159,9 @@ namespace ejson {
* @param[in] _name name of the object
* @return pointer on the element requested or NULL if it not the corect type or does not existed
*/
ejson::Number* getNumber(const std::string& _name) const;
ejson::Number* getNumber(const std::string& _name);
//! @previous
const ejson::Number* getNumber(const std::string& _name) const;
/**
* @brief get the sub Number value of the requested element.
* @param[in] _name name of the object.
@ -148,6 +213,7 @@ namespace ejson {
virtual ejson::Object* toObject(void) {
return this;
};
//! @previous
virtual const ejson::Object* toObject(void) const{
return this;
};

View File

@ -54,6 +54,7 @@ namespace ejson {
virtual ejson::String* toString(void) {
return this;
};
//! @previous
virtual const ejson::String* toString(void) const {
return this;
};

View File

@ -44,7 +44,7 @@ namespace ejson {
typeNull, //!< the null element
typeObject, //!< the { ... }
};
//! @not-in-doc
class filePos {
private:
size_t m_col;
@ -192,6 +192,7 @@ namespace ejson {
virtual ejson::Value* toValue(void) {
return this;
};
//! @previous
virtual const ejson::Value* toValue(void) const {
return this;
};
@ -202,6 +203,7 @@ namespace ejson {
virtual ejson::Document* toDocument(void) {
return NULL;
};
//! @previous
virtual const ejson::Document* toDocument(void) const {
return NULL;
};
@ -212,6 +214,7 @@ namespace ejson {
virtual ejson::Array* toArray(void) {
return NULL;
};
//! @previous
virtual const ejson::Array* toArray(void) const{
return NULL;
};
@ -222,6 +225,7 @@ namespace ejson {
virtual ejson::Object* toObject(void) {
return NULL;
};
//! @previous
virtual const ejson::Object* toObject(void) const{
return NULL;
};
@ -232,6 +236,7 @@ namespace ejson {
virtual ejson::String* toString(void) {
return NULL;
};
//! @previous
virtual const ejson::String* toString(void) const{
return NULL;
};
@ -242,6 +247,7 @@ namespace ejson {
virtual ejson::Number* toNumber(void) {
return NULL;
};
//! @previous
virtual const ejson::Number* toNumber(void) const{
return NULL;
};
@ -252,6 +258,7 @@ namespace ejson {
virtual ejson::Boolean* toBoolean(void) {
return NULL;
};
//! @previous
virtual const ejson::Boolean* toBoolean(void) const{
return NULL;
};
@ -262,6 +269,7 @@ namespace ejson {
virtual ejson::Null* toNull(void) {
return NULL;
};
//! @previous
virtual const ejson::Null* toNull(void) const{
return NULL;
};

View File

@ -12,6 +12,7 @@ def create():
myModule.set_website("http://heeroyui.github.io/ejson/")
myModule.set_website_sources("http://github.com/heeroyui/ejson/")
myModule.set_path(tools.get_current_path(__file__) + "/ejson/")
myModule.set_path_general_doc(tools.get_current_path(__file__) + "/doc/")
# add the currrent module at the
return myModule