Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pibara committed Jul 4, 2011
0 parents commit 94baac2
Show file tree
Hide file tree
Showing 23 changed files with 758 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
@@ -0,0 +1,11 @@
*.o
*.swp
CMakeCache.txt
CMakeFiles
CTestTestfile.cmake
Makefile
Testing
apitest
cmake_install.cmake
*.so
*.so.*
17 changes: 17 additions & 0 deletions CMakeLists.txt
@@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 2.6)
enable_testing()
project(jsonme)
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( -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 gobj/GobjectLibImpl.cpp gobj/GobjectImplParser.cpp gobj/GobjectImplFsTopNode.cpp gobj/GobjectImplStringTopNode.cpp gobj/GobjectImplError.cpp gobj/GobjectImplNode.cpp gobj/GobjectImplScalar.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)
install(FILES json-me.hpp DESTINATION include)
add_executable(apitest main.cpp)
target_link_libraries(apitest jsonme)
add_test(tests.api apitest)
27 changes: 27 additions & 0 deletions JsonMeLib.cpp
@@ -0,0 +1,27 @@
#include "json-me.hpp"
#ifdef USE_GOBJECT_JSON_LIB
#define _JSON_LIB_DEFINED_
#include "gobj/impl.hpp"
namespace jsonme {
typedef jsonme::impl::GobjectLibImpl LibImpl;
}
#endif
#ifdef USE_BOOST_JSON_LIB
#error Boost implementation not yet implemented.
#endif
#ifndef _JSON_LIB_DEFINED_
#error low level lib undefined
#endif
namespace jsonme {
JsonMeLib::JsonMeLib():mLibImpl(new LibImpl()) {}
JsonMeLib::~JsonMeLib() {
delete mLibImpl;
}
Node JsonMeLib::parse(std::string jsonstring){
return mLibImpl->parse(jsonstring);
}
Node JsonMeLib::parseFile(std::string path) {
return mLibImpl->parseFile( path);
}
}

47 changes: 47 additions & 0 deletions Node.cpp
@@ -0,0 +1,47 @@
#include "json-me.hpp"

namespace jsonme {
Node::Node(AbstractNode *node):mNode(node){}
jsonme::nodetype Node::nodetype() {
return mNode->nodetype();
}
Node Node::operator[](std::string name){
return (*mNode)[name];
}
Node Node::operator[](const char *name) {
return (*mNode)[std::string(name)];
}
size_t Node::size(){
return mNode->size();
}
Node Node::operator[](size_t index){
return (*mNode)[index];
}
Node::operator Scalar() {
return (*mNode);
}
jsonme::scalartype Node::scalartype(){
Scalar s(*mNode);
return s.scalartype();
}
Node::operator long double() {
Scalar s(*mNode);
return s;
}
Node::operator long long() {
Scalar s(*mNode);
return s;
}
Node::operator std::string() {
Scalar s(*mNode);
return s;
}
Node::operator bool() {
Scalar s(*mNode);
return s;
}
bool Node::isNull() {
Scalar s(*mNode);
return s.isNull();
}
}
32 changes: 32 additions & 0 deletions README
@@ -0,0 +1,32 @@
This library is a wrapper for the json-glib library that aims tp provide the user with a trivial alternative API to the API provided by the base json-glib library.

This is the first version of the json-me (JSON made easy) that I created as a weekend project.
It has the following issues that I might fix in a later release. Given that I don't have much time to
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:

* There is probably a lot of stuff that I should have defined as const, didn't spent time
to check for that yet.
* 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.
Given the limited test coverage, there may still be some bugs in this library.
* The CMakeLists.txt doesn't look for its dependencies and just adds -I flags that happen to be right
on Ubuntu 11.4. Should do some real checking.
* I didn't create non of the operator<< stuff for making usage ostream usage easy. Tried to for half
an hour and kept getting useless messages from my compiler. Should make the library even more
friendly if we could fix this.

Seeing the above list, if you feel like fixing any of these problems, please feel free to do so and send me your patches if you do.

To build jsonme, take the following steps:

cd build

cmake ..

make

make test

sudo make install
23 changes: 23 additions & 0 deletions Scalar.cpp
@@ -0,0 +1,23 @@
#include "json-me.hpp"

namespace jsonme {
Scalar::Scalar(AbstractScalar *scalar):mScalar(scalar){}
jsonme::scalartype Scalar::scalartype() {
return mScalar->scalartype();
}
Scalar::operator long double() {
return (*mScalar);
}
Scalar::operator long long() {
return (*mScalar);
}
Scalar::operator std::string() {
return (*mScalar);
}
Scalar::operator bool() {
return (*mScalar);
}
bool Scalar::isNull() {
return mScalar->isNull();
}
}
67 changes: 67 additions & 0 deletions build/example.json
@@ -0,0 +1,67 @@
{
"devices": {
"clients": [
{
"device": "eth0",
"groupname": "wankers",
"ip": "192.168.122.1",
"net": "192.168.122.0/24"
},
{
"device": "eth1",
"groupname": "tossers",
"ip": "192.168.56.1",
"net": "192.168.56.0/24"
}
],
"routers": {
"device": "eth2",
"groupname": "loop",
"ip": "192.168.1.56",
"net": "192.168.1.0/24"
}
},
"disabled": 0,
"gateways": [
{
"allowedgroups": [
"wankers",
"tossers"
],
"ip": "192.168.1.254",
"name": "gateway1",
"tableno": 1
},
{
"allowedgroups": [
"wankers",
"tossers"
],
"ip": "192.168.1.5",
"name": "parkip",
"tableno": 2
}
],
"localdns": {
"10.IN-ADDR.ARPA": {
"myip": "192.168.1.56",
"serverip": "192.168.1.6"
},
"16.172.IN-ADDR.ARPA": {
"myip": "192.168.1.56",
"serverip": "192.168.1.6"
},
"168.192.IN-ADDR.ARPA": {
"myip": "192.168.1.56",
"serverip": "192.168.1.6"
},
"LOCAL": {
"myip": "192.168.1.56",
"serverip": "192.168.1.6"
},
"VPN": {
"myip": "192.168.1.56",
"serverip": "192.168.1.6"
}
}
}
19 changes: 19 additions & 0 deletions gobj/GobjectImplError.cpp
@@ -0,0 +1,19 @@
#include "GobjectImplError.hpp"

namespace jsonme {
namespace impl {
GobjectImplError::GobjectImplError():mError(0){}
GobjectImplError::~GobjectImplError(){
if (mError) {
g_error_free(mError);
mError=0;
}
}
GError * GobjectImplError::error() {
return mError;
}
GError ** GobjectImplError::errorp(){
return &mError;
}
}
}
16 changes: 16 additions & 0 deletions gobj/GobjectImplError.hpp
@@ -0,0 +1,16 @@
#ifndef _JSONME_GOBJECTIMP_ERROR_HPP_
#define _JSONME_GOBJECTIMP_ERROR_HPP_
#include <glib-object.h>
namespace jsonme {
namespace impl {
class GobjectImplError {
GError *mError;
public:
GobjectImplError();
~GobjectImplError();
GError * error();
GError ** errorp();
};
}
}
#endif
34 changes: 34 additions & 0 deletions gobj/GobjectImplFsTopNode.cpp
@@ -0,0 +1,34 @@
#include "GobjectImplFsTopNode.hpp"
#include "GobjectImplError.hpp"
#include "GobjectImplNode.hpp"
#include "GobjectImplScalar.hpp"
namespace jsonme {
namespace impl {
GobjectImplFsTopNode::GobjectImplFsTopNode(std::string path):mRoot(0) {
GobjectImplError lerror;
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));
}
GobjectImplFsTopNode::~GobjectImplFsTopNode() {
delete mRoot;
}
jsonme::nodetype GobjectImplFsTopNode::nodetype() {
return mRoot->nodetype();
}
Node GobjectImplFsTopNode::operator[](std::string name) {
return (*mRoot)[name];
}
size_t GobjectImplFsTopNode::size() {
return mRoot->size();
}
Node GobjectImplFsTopNode::operator[](size_t index) {
return (*mRoot)[index];
}
GobjectImplFsTopNode::operator Scalar() {
return (*mRoot);
}
}
}
22 changes: 22 additions & 0 deletions gobj/GobjectImplFsTopNode.hpp
@@ -0,0 +1,22 @@
#ifndef _JSONME_GOBJECTIMPL_FSTOPNODE_HPP
#define _JSONME_GOBJECTIMPL_FSTOPNODE_HPP
#include "../json-me.hpp"
#include "GobjectImplParser.hpp"
#include "GobjectImplNode.hpp"
namespace jsonme {
namespace impl {
class GobjectImplFsTopNode: public jsonme::AbstractNode {
GobjectImplParser mParser;
GobjectImplNode *mRoot;
public:
GobjectImplFsTopNode(std::string jsonstring);
~GobjectImplFsTopNode();
jsonme::nodetype nodetype();
Node operator[](std::string name);
size_t size();
Node operator[](size_t index);
operator Scalar();
};
}
}
#endif
45 changes: 45 additions & 0 deletions gobj/GobjectImplNode.cpp
@@ -0,0 +1,45 @@
#include "GobjectImplFsTopNode.hpp"
#include "GobjectImplError.hpp"
#include "GobjectImplNode.hpp"
#include "GobjectImplScalar.hpp"
namespace jsonme {
namespace impl {
GobjectImplNode::GobjectImplNode(JsonNode *node):mNode(node) {
}
jsonme::nodetype GobjectImplNode::nodetype() {
if (!mNode) {
return jsonme::INVALID;
}
switch (json_node_get_node_type(mNode)) {
JSON_NODE_OBJECT: return jsonme::OBJECT;
JSON_NODE_ARRAY: return jsonme::ARRAY;
JSON_NODE_VALUE: return jsonme::SCALAR;
}
return jsonme::INVALID;
}
Node GobjectImplNode::operator[](std::string name) {
if (!mNode) {
return Node(new GobjectImplNode(mNode));
}
JsonObject *asobject=json_node_get_object(mNode);
return Node(new GobjectImplNode(json_object_get_member(asobject,name.c_str())));
}
size_t GobjectImplNode::size() {
if (!mNode) {
return 0;
}
JsonArray *asarray=json_node_get_array(mNode);
return json_array_get_length(asarray);
}
Node GobjectImplNode::operator[](size_t index) {
if (!mNode) {
return Node(new GobjectImplNode(mNode));
}
JsonArray *asarray=json_node_get_array(mNode);
return Node(new GobjectImplNode(json_array_get_element(asarray,index)));
}
GobjectImplNode::operator Scalar() {
return Scalar(new GobjectImplScalar(mNode));
}
}
}
20 changes: 20 additions & 0 deletions gobj/GobjectImplNode.hpp
@@ -0,0 +1,20 @@
#ifndef _JSONME_GOBFECTIMPL_NODE_
#define _JSONME_GOBFECTIMPL_NODE_
#include "../json-me.hpp"
#include <glib-object.h>
#include <json-glib/json-glib.h>
namespace jsonme {
namespace impl {
class GobjectImplNode: public AbstractNode {
JsonNode *mNode;
public:
GobjectImplNode(JsonNode *node);
jsonme::nodetype nodetype();
Node operator[](std::string name);
size_t size();
Node operator[](size_t index);
operator Scalar();
};
}
}
#endif

0 comments on commit 94baac2

Please sign in to comment.