Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 103 lines (99 sloc) 3.57 kb
94baac2 Rob J Meijer Initial commit
authored
1 #ifndef _JSONME_HPP
2 #define _JSONME_HPP
3 #include <string>
4 #include <boost/shared_ptr.hpp>
5 //The json-me library is a simple wrapper library for making the parsing of json input
6 //with a known structure easy in C++. The interface used to wrap the low level json library
7 //is aimed at making no assumptions on the way the low level library is implemented in order
8 //to allow multiple bindings to exist for any json library desired. This file holds the generic
9 //interface that you as a user should use.
10 namespace jsonme {
11 class ParseError: public std::exception {
12 std::string mMessage;
13 public:
14 ParseError(std::string msg):mMessage(msg){}
15 ~ParseError() throw() {}
16 const char * what() const throw() {
17 return mMessage.c_str();
18 }
19 };
20 //A scalar can be a number (float or integer), a string, a bool or a null value.
21 typedef enum {FLOAT,INTEGER,STRING,BOOL,NULLVAL} scalartype;
22 //The scalar has a type and can be cast to that type. Casting to any other type shall
23 //result in a lexical conversion if possible or a default null value if not.
24 class AbstractScalar {
25 public:
26 virtual ~AbstractScalar(){}
27 virtual jsonme::scalartype scalartype()=0;
28 virtual operator long double()=0;
29 virtual operator long long()=0;
30 virtual operator std::string()=0;
31 virtual operator bool()=0;
32 virtual bool isNull()=0;
33 };
34 //A value semantics proxy to the implementation specific scalar.
35 class Scalar: public AbstractScalar {
36 boost::shared_ptr<AbstractScalar> mScalar;
37 public:
38 Scalar(AbstractScalar *scalar);
39 jsonme::scalartype scalartype();
40 operator long double();
41 operator long long();
42 operator std::string();
43 operator bool();
44 bool isNull();
45 };
46 //A json node is either an object, an array or a scalar.
47 typedef enum {OBJECT,ARRAY,SCALAR,INVALID} nodetype;
48
49 class Node;
50 //The node has a type, and depending on its type can:
51 //* SCALAR: be cast to a Scalar
52 //* ARRAY: have its size queried and have access to indexed sub node's.
53 //* OBJECT: have its members looked up by name.
54 class AbstractNode {
55 public:
56 virtual ~AbstractNode(){}
57 virtual jsonme::nodetype nodetype()=0;
58 virtual Node operator[](std::string name)=0;
59 virtual size_t size()=0;
60 virtual Node operator[](size_t index)=0;
61 virtual operator Scalar()=0;
62 };
63 //A value semantics proxy to the implementation specific node.
64 class Node: public AbstractNode, public AbstractScalar {
65 boost::shared_ptr<AbstractNode> mNode;
66 public:
67 Node(AbstractNode *node);
68 jsonme::nodetype nodetype();
69 Node operator[](std::string name);
70 Node operator[](const char *);
71 size_t size();
72 Node operator[](size_t index);
73 jsonme::scalartype scalartype();
74 operator Scalar();
75 operator long double();
76 operator long long();
77 operator std::string();
78 operator bool();
79 bool isNull();
80 };
81 //The impl namespace should be non of your concern.
82 namespace impl {
83 class AbstractLibImpl {
84 public:
85 virtual ~AbstractLibImpl(){}
86 virtual AbstractNode *parse(std::string jsonstring)=0;
87 virtual AbstractNode *parseFile(std::string jsonstring)=0;
88 };
89 };
90 //Wrapper class for an available low level json library.
91 class JsonMeLib {
92 jsonme::impl::AbstractLibImpl *mLibImpl;
93 public:
94 JsonMeLib();
95 ~JsonMeLib();
96 Node parse(std::string jsonstring);
97 Node parseFile(std::string path);
98 };
99 }
100
101
102 #endif
Something went wrong with that request. Please try again.