ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
adjson.h
Go to the documentation of this file.
1 #ifndef __ADJSON_H__
8 #define __ADJSON_H__
9 
10 #include <iostream>
11 #include <string>
12 #include <cctype>
13 #include <sstream>
14 #include <vector>
15 using std::istream;
16 using std::ostream;
17 using std::ostringstream;
18 using std::vector;
19 
20 namespace adjson
21 {
22 enum class values
23 {
24  _object,
25  _array,
26  _number,
27  _string,
28  _boolean,
29  _null
30 };
31 struct value
32 {
34 
35  virtual ~value() {}
36  virtual std::string str() const = 0;
37  values get_type() const { return _type; }
38 };
39 class json
40 {
42 
43 public:
44  json(): _value(0) { }
45  json(const json& other) { }
46  virtual ~json()
47  {
48  if (_value)
49  {
50  delete _value;
51  _value = 0;
52  }
53  }
54 public:
55  std::string str() const
56  { return _value->str(); }
57 
58  value* get_value() const
59  { return _value; }
60 
61  void set(value* other)
62  { _value = other; }
63 
64  value* parse(istream& input);
65 };
66 struct object: value
67 {
68  vector<value*> _value;
69 
71  { _type = values::_object; }
72 
73  std::string str() const
74  {
75  std::string ret;
76  ret = "{";
77  size_t size = _value.size() - 2;
78  for (size_t i = 0; i < size; i += 2)
79  {
80  ret += _value[i]->str();
81  ret += ":";
82  ret += _value[i + 1]->str();
83  ret += ", ";
84  }
85  ret += _value[size]->str();
86  ret += ":";
87  ret += _value[size + 1]->str();
88  ret += "}";
89  return ret;
90  }
91  void add(value* id, value* other)
92  {
93  _value.push_back(id);
94  _value.push_back(other);
95  }
96 };
97 struct array: value
98 {
99  vector<value*> _value;
100 
102  { _type = values::_array; }
103 
104  std::string str() const
105  {
106  std::string ret;
107  ret = "[";
108  size_t size = _value.size() - 1;
109  for (size_t i = 0; i < size; ++i)
110  {
111  ret += _value[i]->str();
112  ret += ", ";
113  }
114  ret += _value[size]->str();
115  ret += "]";
116  return ret;
117  }
118 
119  void add(value* other)
120  { _value.push_back(other); }
121 };
122 struct number: value
123 {
124  double _value;
125 
127  { _type = values::_number; }
128 
129  double get_value() const
130  { return _value; }
131 
132  std::string str() const
133  {
134  ostringstream output;
135  output << _value;
136  return output.str();
137  }
138 };
139 struct string: value
140 {
141  std::string _value;
142 
144  { _type = values::_string; }
145 
146  std::string str() const
147  { return _value; }
148 
149  std::string get_value() const
150  { return str(); }
151 };
152 struct boolean: value
153 {
154  bool _value;
155 
156  boolean(): _value(false)
157  { _type = values::_boolean; }
158 
159  std::string str() const
160  { return _value ? "true" : "false"; }
161 
162  bool get_value() const
163  { return _value; }
164 };
165 struct null: value
166 {
168  { _type = values::_null; }
169 
170  std::string str() const
171  { return "null"; }
172 
173  value* get_value() const
174  { return 0; }
175 };
176 };
177 istream& operator>>(istream& input, adjson::json& data);
178 ostream& operator<<(ostream& output, const adjson::json& data);
179 #endif
void add(value *id, value *other)
Definition: adjson.h:91
bool get_value() const
Definition: adjson.h:162
void set(value *other)
Definition: adjson.h:61
virtual ~value()
Definition: adjson.h:35
value * get_value() const
Definition: adjson.h:173
json(const json &other)
Definition: adjson.h:45
virtual std::string str() const =0
value * parse(istream &input)
Definition: adjson.cpp:25
void add(value *other)
Definition: adjson.h:119
values get_type() const
Definition: adjson.h:37
double _value
Definition: adjson.h:124
std::string str() const
Definition: adjson.h:159
virtual ~json()
Definition: adjson.h:46
std::string _value
Definition: adjson.h:141
values _type
Definition: adjson.h:33
vector< value * > _value
Definition: adjson.h:99
value * get_value() const
Definition: adjson.h:58
std::string str() const
Definition: adjson.h:55
values
Definition: adjson.h:22
std::string str() const
Definition: adjson.h:104
double get_value() const
Definition: adjson.h:129
std::string str() const
Definition: adjson.h:170
const int output
Definition: fvar.hpp:9505
value * _value
Definition: adjson.h:41
istream & operator>>(const istream &input, const d3_array &arr3)
Read values from input stream into arr3.
Definition: d3_io.cpp:60
ostream & operator<<(const ostream &_s, preshowpoint p)
Description not yet available.
Definition: admanip.cpp:48
std::string str() const
Definition: adjson.h:146
vector< value * > _value
Definition: adjson.h:68
std::string str() const
Definition: adjson.h:132
std::string str() const
Definition: adjson.h:73
std::string get_value() const
Definition: adjson.h:149