Permalink
Browse files

Added LGPL comments to the code, added comment and made a few robustn…

…ess fixes.
  • Loading branch information...
1 parent beb09f9 commit 7bf4398b274fd2f13589db54da571719e9b8434e @pibara committed Aug 20, 2011
View
@@ -1,23 +1,46 @@
+//JsonMe++ C++ JSON parsing (wrapper) library.
+//Copyright (C) Rob J Meijer 2011 <jsonme@polacanthus.net>
+//
+//This library is free software; you can redistribute it and/or
+//modify it under the terms of the GNU Lesser General Public
+//License as published by the Free Software Foundation; either
+//version 2.1 of the License, or (at your option) any later version.
+//
+//This library is distributed in the hope that it will be useful,
+//but WITHOUT ANY WARRANTY; without even the implied warranty of
+//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+//Lesser General Public License for more details.
+//
+//You should have received a copy of the GNU Lesser General Public
+//License along with this library; if not, write to the Free Software
+//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "json-me.hpp"
+//Curently the glib implementation is the only one implemented, so USE_GOBJECT_JSON_LIB better be defined.
#ifdef USE_GOBJECT_JSON_LIB
#define _JSON_LIB_DEFINED_
#include "gobj/impl.hpp"
namespace jsonme {
typedef jsonme::impl::GobjectLibImpl LibImpl;
}
#endif
+//Maybe we can create an second build for jsonme using an application based on boost::property_tree
+//For now we don't have such an implementation yet.
#ifdef USE_BOOST_JSON_LIB
#error Boost implementation not yet implemented.
#endif
#ifndef _JSON_LIB_DEFINED_
#error low level lib undefined
#endif
namespace jsonme {
+ //Constructor creates a wrapper for a dynamicaly allocated library implementation object.
JsonMeLib::JsonMeLib():mLibImpl(new LibImpl()) {}
+ //Destructor
JsonMeLib::~JsonMeLib() throw() {}
+ //The pare method gets forwarded to the implementation.
Node JsonMeLib::parse(std::string const & jsonstring) const {
return mLibImpl->parse(jsonstring);
}
+ //The parseFile gets forwarded to the implementation.
Node JsonMeLib::parseFile(std::string const & path) const {
return mLibImpl->parseFile( path);
}
View
@@ -1,8 +1,25 @@
+//JsonMe++ C++ JSON parsing (wrapper) library.
+//Copyright (C) Rob J Meijer 2011 <jsonme@polacanthus.net>
+//
+//This library is free software; you can redistribute it and/or
+//modify it under the terms of the GNU Lesser General Public
+//License as published by the Free Software Foundation; either
+//version 2.1 of the License, or (at your option) any later version.
+//
+//This library is distributed in the hope that it will be useful,
+//but WITHOUT ANY WARRANTY; without even the implied warranty of
+//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+//Lesser General Public License for more details.
+//
+//You should have received a copy of the GNU Lesser General Public
+//License along with this library; if not, write to the Free Software
+//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "json-me.hpp"
#include "NullKeys.hpp"
namespace jsonme {
+ //Implementing the NullObject pattern for the AbstractNode interface.
class NullNode: public AbstractNode {
- NullKeys mNullKeys;
+ NullKeys mNullKeys; //If we need to return an AbstractKeys object reference, a NullKeys object is needed.
public:
NullNode():mNullKeys(){}
AbstractKeys & keys() { return mNullKeys;}
@@ -12,59 +29,75 @@ namespace jsonme {
Node operator[](size_t index) const { return Node();}
operator Scalar() const { return Scalar(); }
};
+ //The default constructor creates a node wrapping a NullNode.
Node::Node():mNode(new NullNode()){}
+ //A second constructor wraps some kind of AbstractNode subclass.
Node::Node(AbstractNode *node):mNode(node){}
+ //Simply forward method to the implementation.
jsonme::nodetype Node::nodetype() const {
return mNode->nodetype();
}
+ //Simply forward method to the implementation.
AbstractKeys & Node::keys() {
return mNode->keys();
}
+ //Simply forward method to the implementation.
Node Node::operator[](std::string const & name) const {
return (*mNode)[name];
}
+ //Forwarder with cstring to std::string conversion.
Node Node::operator[](char const * const name) const {
return (*mNode)[std::string(name)];
}
+ //Simply forward method to the implementation.
size_t Node::size() const {
return mNode->size();
}
+ //Simply forward method to the implementation.
Node Node::operator[](size_t index) const {
return (*mNode)[index];
}
+ //Simpe trick to forward this method to the implementation.
Node::operator Scalar() const {
return (*mNode);
}
+ //Two step forwarder: 1) cast node to scalar. 2) forward method.
jsonme::scalartype Node::scalartype() const{
Scalar s(*mNode);
return s.scalartype();
}
+ //Two step forwarder: 1) cast node to scalar. 2) forward method.
Node::operator long double() const {
Scalar s(*mNode);
return s;
}
+ //Two step forwarder: 1) cast node to scalar. 2) forward method.
Node::operator long long() const {
Scalar s(*mNode);
return s;
}
+ //Two step forwarder: 1) cast node to scalar. 2) forward method.
Node::operator std::string() const {
Scalar s(*mNode);
return s;
}
+ //Node type dependant casting to boolean.
Node::operator bool() const {
switch (mNode->nodetype()) {
- case INVALID:
- return false;
- case OBJECT:
+ case INVALID: //Invalid nodes are always false.
+ return false;
+ case OBJECT: //Objects are always true.
return true;
- case ARRAY:
+ case ARRAY: //Empty arrays are false, non empty arrays are true.
return (mNode->size() != 0);
- case SCALAR:
+ case SCALAR: //Scalars take care of their own casting to boolean.
Scalar s(*mNode);
return s;
}
+ //This code should not get reached, just in case lets return false.
return false;
}
+ //Two step forwarder: 1) cast node to scalar. 2) forward method.
bool Node::isNull() const {
Scalar s(*mNode);
return s.isNull();
View
@@ -1,6 +1,23 @@
+//JsonMe++ C++ JSON parsing (wrapper) library.
+//Copyright (C) Rob J Meijer 2011 <jsonme@polacanthus.net>
+//
+//This library is free software; you can redistribute it and/or
+//modify it under the terms of the GNU Lesser General Public
+//License as published by the Free Software Foundation; either
+//version 2.1 of the License, or (at your option) any later version.
+//
+//This library is distributed in the hope that it will be useful,
+//but WITHOUT ANY WARRANTY; without even the implied warranty of
+//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+//Lesser General Public License for more details.
+//
+//You should have received a copy of the GNU Lesser General Public
+//License along with this library; if not, write to the Free Software
+//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#ifndef _JSONME_NULLKEYS_HPP
#define _JSONME_NULLKEYS_HPP
namespace jsonme {
+ //Implementing the NullObject pattern for the AbstractKeys interface.
class NullKeys: public AbstractKeys {
public:
std::string operator[](size_t index) const { return "";}
View
@@ -1,3 +1,19 @@
+//JsonMe++ C++ JSON parsing (wrapper) library.
+//Copyright (C) Rob J Meijer 2011 <jsonme@polacanthus.net>
+//
+//This library is free software; you can redistribute it and/or
+//modify it under the terms of the GNU Lesser General Public
+//License as published by the Free Software Foundation; either
+//version 2.1 of the License, or (at your option) any later version.
+//
+//This library is distributed in the hope that it will be useful,
+//but WITHOUT ANY WARRANTY; without even the implied warranty of
+//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+//Lesser General Public License for more details.
+//
+//You should have received a copy of the GNU Lesser General Public
+//License along with this library; if not, write to the Free Software
+//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "json-me.hpp"
namespace jsonme {
ParseError::ParseError(std::string msg):mMessage(msg){}
View
@@ -1,32 +1,58 @@
+//JsonMe++ C++ JSON parsing (wrapper) library.
+//Copyright (C) Rob J Meijer 2011 <jsonme@polacanthus.net>
+//
+//This library is free software; you can redistribute it and/or
+//modify it under the terms of the GNU Lesser General Public
+//License as published by the Free Software Foundation; either
+//version 2.1 of the License, or (at your option) any later version.
+//
+//This library is distributed in the hope that it will be useful,
+//but WITHOUT ANY WARRANTY; without even the implied warranty of
+//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+//Lesser General Public License for more details.
+//
+//You should have received a copy of the GNU Lesser General Public
+//License along with this library; if not, write to the Free Software
+//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
#include "json-me.hpp"
namespace jsonme {
+ //This implements the NullObject design pattern for the AbstractScalar interface.
class NullScalar: public AbstractScalar {
public:
jsonme::scalartype scalartype() const { return NULLVAL;}
operator long double() const { return 0;}
operator long long() const { return 0;}
operator std::string() const { return "";}
- operator bool() const { return false;}
+ operator bool() const { return false;}
bool isNull() const { return true;}
};
+ //The default constructor wraps a NullScalar.
Scalar::Scalar():mScalar(new NullScalar()){}
+ //Alternative constructor wraps a dynamically allocated implementation of the AbstractScalar interface.
Scalar::Scalar(AbstractScalar *scalar):mScalar(scalar){}
+ //Simple method forwarding.
jsonme::scalartype Scalar::scalartype() const {
return mScalar->scalartype();
}
+ //Simple forwarding of the real cast.
Scalar::operator long double() const {
return (*mScalar);
}
+ //Simple forwarding of the real cast.
Scalar::operator long long() const {
return (*mScalar);
}
+ //Simple forwarding of the real cast.
Scalar::operator std::string() const {
return (*mScalar);
}
+ //Simple forwarding of the real cast.
Scalar::operator bool() const {
return (*mScalar);
}
+ //Simple forwarding of the real cast.
bool Scalar::isNull() const {
return mScalar->isNull();
}
View
@@ -1,9 +1,26 @@
+//JsonMe++ C++ JSON parsing (wrapper) library.
+//Copyright (C) Rob J Meijer 2011 <jsonme@polacanthus.net>
+//
+//This library is free software; you can redistribute it and/or
+//modify it under the terms of the GNU Lesser General Public
+//License as published by the Free Software Foundation; either
+//version 2.1 of the License, or (at your option) any later version.
+//
+//This library is distributed in the hope that it will be useful,
+//but WITHOUT ANY WARRANTY; without even the implied warranty of
+//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+//Lesser General Public License for more details.
+//
+//You should have received a copy of the GNU Lesser General Public
+//License along with this library; if not, write to the Free Software
+//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "GobjectImplError.hpp"
namespace jsonme {
namespace impl {
GobjectImplError::GobjectImplError():mError(0){}
GobjectImplError::~GobjectImplError(){
+ //If needed invoke g_error_free to avoid resource leaks.
if (mError) {
g_error_free(mError);
mError=0;
View
@@ -1,12 +1,29 @@
+//JsonMe++ C++ JSON parsing (wrapper) library.
+//Copyright (C) Rob J Meijer 2011 <jsonme@polacanthus.net>
+//
+//This library is free software; you can redistribute it and/or
+//modify it under the terms of the GNU Lesser General Public
+//License as published by the Free Software Foundation; either
+//version 2.1 of the License, or (at your option) any later version.
+//
+//This library is distributed in the hope that it will be useful,
+//but WITHOUT ANY WARRANTY; without even the implied warranty of
+//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+//Lesser General Public License for more details.
+//
+//You should have received a copy of the GNU Lesser General Public
+//License along with this library; if not, write to the Free Software
+//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#ifndef _JSONME_GOBJECTIMP_ERROR_HPP_
#define _JSONME_GOBJECTIMP_ERROR_HPP_
#include <glib-object.h>
namespace jsonme {
namespace impl {
+ //Simple RAII wke rapper for a GError.
class GobjectImplError {
GError *mError;
- GobjectImplError(const GobjectImplError&);
- GobjectImplError &operator=(const GobjectImplError&);
+ GobjectImplError(const GobjectImplError&); //Prohibit copying.
+ GobjectImplError &operator=(const GobjectImplError&); //Prohibit assignment.
public:
GobjectImplError();
~GobjectImplError();
@@ -1,35 +1,59 @@
+//JsonMe++ C++ JSON parsing (wrapper) library.
+//Copyright (C) Rob J Meijer 2011 <jsonme@polacanthus.net>
+//
+//This library is free software; you can redistribute it and/or
+//modify it under the terms of the GNU Lesser General Public
+//License as published by the Free Software Foundation; either
+//version 2.1 of the License, or (at your option) any later version.
+//
+//This library is distributed in the hope that it will be useful,
+//but WITHOUT ANY WARRANTY; without even the implied warranty of
+//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+//Lesser General Public License for more details.
+//
+//You should have received a copy of the GNU Lesser General Public
+//License along with this library; if not, write to the Free Software
+//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "GobjectImplFsTopNode.hpp"
#include "GobjectImplError.hpp"
#include "GobjectImplNode.hpp"
#include "GobjectImplScalar.hpp"
namespace jsonme {
namespace impl {
+ //Constructor.
GobjectImplFsTopNode::GobjectImplFsTopNode(std::string const & path):mParser(),mRoot(0) {
- GobjectImplError lerror;
+ GobjectImplError lerror; //Use a RAII like object for holding our error state.
json_parser_load_from_file (mParser,path.c_str(), lerror.errorp());
if (lerror.error()) {
throw jsonme::ParseError(lerror.error()->message);
}
- mRoot=new GobjectImplNode(json_parser_get_root(mParser));
+ mRoot=new GobjectImplNode(json_parser_get_root(mParser)); // Wrap the root node from the file.
}
+ //Destructor, destroy the node that we wrap.
GobjectImplFsTopNode::~GobjectImplFsTopNode() throw() {
- delete mRoot;
+ delete mRoot;
}
+ //Simple method forwarding.
jsonme::nodetype GobjectImplFsTopNode::nodetype() const {
return mRoot->nodetype();
}
+ //Simple method forwarding.
Node GobjectImplFsTopNode::operator[](std::string const & name) const {
return (*mRoot)[name];
}
+ //Simple method forwarding.
size_t GobjectImplFsTopNode::size() const {
return mRoot->size();
}
+ //Simple method forwarding.
Node GobjectImplFsTopNode::operator[](size_t index) const {
return (*mRoot)[index];
}
+ //Simple method forwarding.
GobjectImplFsTopNode::operator Scalar() const {
return (*mRoot);
}
+ //Simple method forwarding.
AbstractKeys &GobjectImplFsTopNode::keys() {
return mRoot->keys();
}
Oops, something went wrong.

0 comments on commit 7bf4398

Please sign in to comment.