forked from naturalog/tauchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_object.h
177 lines (158 loc) · 4.42 KB
/
json_object.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#ifndef __OBJ_H__
#define __OBJ_H__
#include <map>
#include <vector>
#include <memory>
#include <string>
#include <stdio.h>
#include <signal.h>
#include <stdio.h>
#include <signal.h>
#include <execinfo.h>
#include <ostream>
#include "strings.h"
#include <iostream>
#include <list>
extern bool deref, shorten;
typedef std::nullptr_t null;
typedef std::shared_ptr<bool> pbool;
string indent();
using std::endl;
#ifdef DEBUG
#define TRACE(x) if (_indent + (int)proc.size() < level) { dout << indent(); x; }
#else
#define TRACE(X)
#endif
extern std::wostream& dout;
extern std::wostream& derr;
#ifdef JSON
#ifdef DEBUG
//logger _logger;
extern bool autobt, _pause;
void bt();
void dopause();
#define trace(x) std::wclog<<__FILE__<<':'<<__LINE__<<tab<<x; if (_pause) dopause()
#else
void bt();
#define trace(x)
#endif
class obj {
protected:
obj() {}
public:
typedef std::shared_ptr<obj> pobj;
typedef std::map<string, pobj> somap;
typedef std::vector<pobj> olist;
typedef std::shared_ptr<somap> psomap;
virtual std::shared_ptr<uint64_t> UINT() {
return 0;
}
virtual std::shared_ptr<int64_t> INT() {
return 0;
}
virtual std::shared_ptr<bool> BOOL() {
return 0;
}
virtual std::shared_ptr<string> STR() {
return 0;
}
virtual std::shared_ptr<somap> MAP() {
return 0;
}
virtual std::shared_ptr<olist> LIST() {
return 0;
}
virtual std::shared_ptr<double> DOUBLE() {
return 0;
}
virtual std::shared_ptr<null> Null() {
return 0;
}
size_t size();
bool STR ( const string& x );
std::shared_ptr<obj> MAP ( const string& k );
bool map_and_has ( const string& k );
bool map_and_has_null ( const string& k );
virtual std::string type_str() const = 0;
virtual bool equals ( const obj& o ) const = 0;
virtual pobj clone() const = 0;
string toString ( );
};
#define OBJ_IMPL(type, getter) \
class type##_obj : public obj { \
std::shared_ptr<type> data; \
public: \
type##_obj(const type& o = type()) { data = std::make_shared<type>(); *data = o; } \
type##_obj(const std::shared_ptr<type> o) : data(o) { } \
virtual std::shared_ptr<type> getter() { \
return data; } \
virtual std::string type_str() const { return #type; } \
virtual bool equals(const obj& o) const { \
if ( type_str() != o.type_str() ) return false; \
auto od = ((const type##_obj&)o).data; \
if ( !data || !od) return data == od; \
return *data == *od; \
}\
virtual pobj clone() const { return std::make_shared<type##_obj>(*data); }\
};typedef std::shared_ptr<type##_obj> p##type##_obj
OBJ_IMPL ( int64_t, INT );
OBJ_IMPL ( uint64_t, UINT );
OBJ_IMPL ( bool, BOOL );
OBJ_IMPL ( double, DOUBLE );
OBJ_IMPL ( string, STR );
OBJ_IMPL ( somap, MAP );
OBJ_IMPL ( olist, LIST );
OBJ_IMPL ( null, Null );
typedef obj::pobj pobj;
typedef obj::somap somap;
typedef obj::olist olist;
typedef std::shared_ptr<somap> psomap;
typedef std::shared_ptr<olist> polist;
typedef std::map<string, bool> defined_t;
typedef std::shared_ptr<defined_t> pdefined_t;
template<typename T> inline pstring_obj mk_str_obj ( T t ) {
return std::make_shared<string_obj> ( t );
}
template<typename T> inline psomap_obj mk_somap_obj ( T t ) {
return std::make_shared<somap_obj> ( t );
}
template<typename T> inline polist_obj mk_olist_obj ( T t ) {
return std::make_shared<olist_obj> ( t );
}
template<typename T> inline polist mk_olist ( T t ) {
return std::make_shared<olist> ( t );
}
pstring_obj mk_str_obj();
psomap_obj mk_somap_obj();
polist_obj mk_olist_obj();
polist mk_olist();
bool has ( const defined_t& c, const string& k );
bool has ( pdefined_t c, const string& k );
bool has ( const somap& c, const string& k );
bool has ( psomap c, const string& k );
bool has ( psomap c, pstring k );
bool has ( pobj o, string s );
bool has ( pobj o, pstring s );
struct jsonld_options {
jsonld_options() {
}
jsonld_options ( pstring base_ ) : base ( base_ ) {}
jsonld_options ( string base_ ) :
base ( pstr ( base_ ) ) {
}
pstring base = 0;//pstr ( "http://tauchain.org/" );
pbool compactArrays = std::make_shared<bool> ( true );
std::shared_ptr<class obj> expandContext = 0;
pstring processingMode = pstr ( L"json-ld-1.0" );
pbool embed = 0;
pbool isexplicit = std::make_shared<bool> ( true );
pbool omitDefault = 0;
pbool useRdfType = std::make_shared<bool> ( true );
pbool useNativeTypes = std::make_shared<bool> ( true );
pbool produceGeneralizedRdf = std::make_shared<bool> ( true );
pstring format = 0;
pbool useNamespaces = std::make_shared<bool> ( true );
pstring outputForm = 0;
};
#endif
#endif