ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
adjson.cpp
Go to the documentation of this file.
1 
4 #include <fvar.hpp>
5 #include "adjson.h"
6 #ifndef OPT_LIB
7  #include <cassert>
8  #include <climits>
9 #endif
10 
11 istream& operator>>(istream& input, adjson::json& data)
12 {
13  adjson::value* ret = data.parse(input);
14  data.set(ret);
15  return input;
16 }
17 ostream& operator<<(ostream& output, const adjson::json& data)
18 {
19  output << data.str();
20  return output;
21 }
22 
23 namespace adjson
24 {
25 value* json::parse(istream& input)
26 {
27  value* ret = 0;
28  input >> std::ws;
29  char p = (char)input.peek();
30  if (p != EOF)
31  {
32  char c;
33  if (p == '{')
34  {
35  object* o = new object();
36 
37  if (input.get(c))
38  {
39  while (c != '}')
40  {
41  string* s = (string*)parse(input);
42  input >> std::ws >> c >> std::ws;
43  value* v = parse(input);
44  o->add(s, v);
45  input >> std::ws >> c >> std::ws;
46  }
47  }
48 
49  ret = o;
50  }
51  else if (p == '[')
52  {
53  array* a = new array();
54 
55  if (input.get(c))
56  {
57  while (c != ']')
58  {
59  value* v = parse(input);
60  a->add(v);
61  input >> std::ws >> c >> std::ws;
62  }
63  }
64  ret = a;
65  }
66  else if (p == '\"')
67  {
68  string* s = new string();
69  input.get(c);
70  s->_value.push_back(c);
71  input.get(c);
72  while (c != '\"')
73  {
74  s->_value.push_back(c);
75  input.get(c);
76  if (c == '\\')
77  {
78  s->_value.push_back(c);
79  input.get(c);
80  s->_value.push_back(c);
81  input.get(c);
82  }
83  }
84  s->_value.push_back('\"');
85  ret = s;
86  }
87  else if (p == '-' || std::isdigit(p))
88  {
89  number* n = new number();
90  input >> n->_value;
91  ret = n;
92  }
93  else if (p == 't')
94  {
95  boolean* b = new boolean();
96  char str[6];
97  input.read(str, 4);
98  b->_value = true;
99  ret = b;
100  }
101  else if (p == 'f')
102  {
103  boolean* b = new boolean();
104  char str[7];
105  input.read(str, 5);
106  b->_value = false;
107  ret = b;
108  }
109  else if (p == 'n')
110  {
111  null* b = new null();
112  char str[6];
113  input.read(str, 4);
114  ret = b;
115  }
116  else
117  {
118  std::cerr << "Error: unknown char(" << p << ") in json input.\n";
119  }
120  }
121  return ret;
122 }
123 };
124 
125 istream& dvector::parse(istream& input)
126 {
127  adjson::json data;
128  input >> data;
129  adjson::array* a = (adjson::array*)data.get_value();
130 
131  if (allocated())
132  {
133  deallocate();
134  }
135  size_t size = a->_value.size();
136 #ifndef OPT_LIB
137  assert(size <= INT_MAX);
138 #endif
139  allocate(1, static_cast<int>(size));
140  for (size_t i = 0; i < size; ++i)
141  {
142  adjson::number* n = (adjson::number*)a->_value[i];
143  v[i + 1] = n->_value;
144  }
145 
146  return input;
147 }
double * v
pointer to the data
Definition: dvector.h:53
void add(value *id, value *other)
Definition: adjson.h:91
void set(value *other)
Definition: adjson.h:61
int allocated(void) const
Returns 1 (TRUE) if memory is allocated.
Definition: dvector.h:71
value * parse(istream &input)
Definition: adjson.cpp:25
void add(value *other)
Definition: adjson.h:119
void allocate(void)
Allocate dvector without allocating memory.
Definition: dvector.cpp:495
double _value
Definition: adjson.h:124
std::string _value
Definition: adjson.h:141
vector< value * > _value
Definition: adjson.h:99
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
std::string str() const
Definition: adjson.h:55
unsigned int size() const
Get number of elements in array.
Definition: dvector.h:209
const int output
Definition: fvar.hpp:9505
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
istream & parse(istream &input_json)
Definition: adjson.cpp:125
void deallocate(void)
Called by destructor to deallocate memory for a dvector object.
Definition: dvector.cpp:92