Skip to content
Browse files

Fixed memleak and added keys functionality.

  • Loading branch information...
1 parent bdf02c4 commit c4fcc91ada00cb9bac2c3012d7eef83f116b2e91 @pibara committed Aug 13, 2011
View
4 CMakeLists.txt
@@ -5,9 +5,9 @@ set(prefix ${CMAKE_INSTALL_PREFIX})
set(exec_prefix ${CMAKE_INSTALL_PREFIX})
set(libdir ${CMAKE_INSTALL_PREFIX}/lib)
set(includedir ${CMAKE_INSTALL_PREFIX}/include)
-ADD_DEFINITIONS(-DUSE_GOBJECT_JSON_LIB)
+ADD_DEFINITIONS(-DUSE_GOBJECT_JSON_LIB -ggdb)
ADD_DEFINITIONS( -I/usr/include/json-glib-1.0/ -I/usr/include/glib-2.0/ -I/usr/lib/x86_64-linux-gnu/glib-2.0/include/)
-add_library(jsonme SHARED JsonMeLib.cpp Scalar.cpp Node.cpp ParseError.cpp gobj/GobjectLibImpl.cpp gobj/GobjectImplParser.cpp gobj/GobjectImplFsTopNode.cpp gobj/GobjectImplStringTopNode.cpp gobj/GobjectImplError.cpp gobj/GobjectImplNode.cpp gobj/GobjectImplScalar.cpp)
+add_library(jsonme SHARED JsonMeLib.cpp Scalar.cpp Node.cpp ParseError.cpp gobj/GobjectLibImpl.cpp gobj/GobjectImplParser.cpp gobj/GobjectImplFsTopNode.cpp gobj/GobjectImplStringTopNode.cpp gobj/GobjectImplError.cpp gobj/GobjectImplNode.cpp gobj/GobjectImplScalar.cpp gobj/GobjectImplKeys.cpp)
SET_TARGET_PROPERTIES(jsonme PROPERTIES VERSION 1.0.0 SOVERSION 1)
target_link_libraries(jsonme gobject-2.0 json-glib-1.0)
install_targets(/lib jsonme)
View
2 README
@@ -5,8 +5,6 @@ It has the following issues that I might fix in a later release. Given that I do
work on project like this and given that my priorities lie with different projects, you may consider
it as a wishlist that if you like this library would make me very happy if you could contribute it:
-* Probably need to add a 'keys' method to AbstractNode so the user can find out what members a JSON
- object has. A 'has_key(std::string keyy)' might be usefull to possibly.
* I didn't find a list of the Gtype's that json-glib can produce, there are probably some pieces of
dead code in gobj/GobjectImplScalar.cpp and there might be one missing.
* I didn't write any unit tests. All I wrote was a litle tiny program for a basic API test.
View
3 gobj/GobjectImplFsTopNode.cpp
@@ -30,5 +30,8 @@ namespace jsonme {
GobjectImplFsTopNode::operator Scalar() const {
return (*mRoot);
}
+ AbstractKeys &GobjectImplFsTopNode::keys() {
+ return mRoot->keys();
+ }
}
}
View
3 gobj/GobjectImplFsTopNode.hpp
@@ -8,11 +8,10 @@ namespace jsonme {
class GobjectImplFsTopNode: public jsonme::AbstractNode {
GobjectImplParser mParser;
GobjectImplNode *mRoot;
- NullKeys mNullKeys;
public:
GobjectImplFsTopNode(std::string const & jsonstring);
~GobjectImplFsTopNode() throw();
- AbstractKeys &keys() { return mNullKeys;}
+ AbstractKeys &keys();
jsonme::nodetype nodetype() const;
Node operator[](std::string const & name) const;
size_t size() const;
View
21 gobj/GobjectImplKeys.cpp
@@ -0,0 +1,21 @@
+#include "GobjectImplKeys.hpp"
+namespace jsonme {
+ GobjectImplKeys::GobjectImplKeys(JsonNode* node) {
+ if (json_node_get_node_type(node) == JSON_NODE_OBJECT) {
+ JsonObject *asObject=json_node_get_object(node);
+ if (asObject) {
+ GList *members=json_object_get_members(asObject);
+ if (members) {
+ for (GList *member=members;member;member=member->next) {
+ mKeys.push_back(std::string((char *) member->data));
+ }
+ g_list_free(members);
+ }
+ }
+ }
+ }
+ std::string GobjectImplKeys::operator[](size_t index) const { return mKeys[index];}
+ size_t GobjectImplKeys::size() const{
+ return mKeys.size();
+ }
+}
View
19 gobj/GobjectImplKeys.hpp
@@ -0,0 +1,19 @@
+#ifndef _JSONME_GOBJIMPLKEYS_HPP
+#define _JSONME_GOBJIMPLKEYS_HPP
+#include "../json-me.hpp"
+#include <glib-object.h>
+#include <json-glib/json-glib.h>
+#include <string>
+#include <vector>
+#include <iostream>
+namespace jsonme {
+ class GobjectImplKeys: public AbstractKeys {
+ std::vector<std::string> mKeys;
+ public:
+ GobjectImplKeys(JsonNode* node);
+ ~GobjectImplKeys() throw() {}
+ std::string operator[](size_t index) const;
+ size_t size() const;
+ };
+}
+#endif
View
2 gobj/GobjectImplNode.cpp
@@ -4,7 +4,7 @@
#include "GobjectImplScalar.hpp"
namespace jsonme {
namespace impl {
- GobjectImplNode::GobjectImplNode(JsonNode * const node):mNode(node) {
+ GobjectImplNode::GobjectImplNode(JsonNode * const node):mNode(node),mKeys(node) {
}
jsonme::nodetype GobjectImplNode::nodetype() const {
if (!mNode) {
View
6 gobj/GobjectImplNode.hpp
@@ -3,15 +3,15 @@
#include "../json-me.hpp"
#include <glib-object.h>
#include <json-glib/json-glib.h>
-#include "../NullKeys.hpp"
+#include "GobjectImplKeys.hpp"
namespace jsonme {
namespace impl {
class GobjectImplNode: public AbstractNode {
JsonNode * const mNode;
- NullKeys mNullKeys;
+ GobjectImplKeys mKeys;
public:
GobjectImplNode(JsonNode * const node);
- AbstractKeys &keys() { return mNullKeys;}
+ AbstractKeys &keys() { return mKeys;}
jsonme::nodetype nodetype() const ;
Node operator[](std::string const & name) const ;
size_t size() const;
View
3 gobj/GobjectImplScalar.cpp
@@ -7,6 +7,9 @@ namespace jsonme {
json_node_get_value(mNode,&mValue);
}
}
+ GobjectImplScalar::~GobjectImplScalar() throw() {
+ g_value_unset(&mValue);
+ }
jsonme::scalartype GobjectImplScalar::scalartype() const {
switch (json_node_get_value_type(mNode)) {
case G_TYPE_BOOLEAN :
View
1 gobj/GobjectImplScalar.hpp
@@ -10,6 +10,7 @@ namespace jsonme {
GValue mValue;
public:
GobjectImplScalar(JsonNode * const node);
+ ~GobjectImplScalar() throw();
jsonme::scalartype scalartype() const;
operator long double() const;
operator long long() const;
View
5 gobj/GobjectImplStringTopNode.hpp
@@ -8,12 +8,11 @@ namespace jsonme {
namespace impl {
class GobjectImplStringTopNode: public jsonme::AbstractNode {
GobjectImplParser mParser;
- GobjectImplNode const * mRoot;
- NullKeys mNullKeys;
+ GobjectImplNode * mRoot;
public:
GobjectImplStringTopNode(std::string const & jsonstring);
~GobjectImplStringTopNode() throw();
- AbstractKeys &keys() { return mNullKeys;}
+ AbstractKeys &keys() { return mRoot->keys();}
jsonme::nodetype nodetype() const ;
Node operator[](std::string const & name) const ;
size_t size() const;
View
12 main.cpp
@@ -17,6 +17,18 @@ int main(int argc,char **argv) {
std::cerr << "client net: " << device << std::endl << "\tgroup\t:\t" << group << std::endl <<"\tip\t:\t" << ip << std::endl << "\tnet\t:\t" << net << std::endl;
}
jsonme::Node router=rootnode["devices"]["routers"];
+ if (router.keys().size() < 4) {
+ std::cerr << "No keys returned from routers." << std::endl;;
+ return 1;
+ }
+ for (int keyindex=0;keyindex < router.keys().size() ; keyindex++) {
+ std::cerr << "keys[" << keyindex << "] : " << std::endl;
+ jsonme::AbstractKeys &keys=router.keys();
+ std::string key=keys[keyindex];
+ std::cerr << " key=" << key << std::endl;
+ std::string val=router[key];
+ std::cerr << " val=" << val << std::endl;
+ }
std::string device=router["device"];
std::string group=router["groupname"];
std::string ip= router["ip"];

0 comments on commit c4fcc91

Please sign in to comment.
Something went wrong with that request. Please try again.