1 #ifndef STAN__IO__JSON__JSON_DATA_HPP
2 #define STAN__IO__JSON__JSON_DATA_HPP
11 #include <boost/throw_exception.hpp>
12 #include <boost/lexical_cast.hpp>
51 std::vector<double>
const empty_vec_r_;
52 std::vector<int>
const empty_vec_i_;
53 std::vector<size_t>
const empty_vec_ui_;
64 bool contains_r_only(
const std::string& name)
const {
65 return vars_r_.find(name) != vars_r_.end();
78 json_data(std::istream& in) : vars_r_(), vars_i_() {
92 return contains_r_only(name) ||
contains_i(name);
104 return vars_i_.find(name) != vars_i_.end();
114 std::vector<double>
vals_r(
const std::string& name)
const {
115 if (contains_r_only(name)) {
116 return (vars_r_.find(name)->second).first;
118 std::vector<int> vec_int = (vars_i_.find(name)->second).first;
119 std::vector<double> vec_r(vec_int.size());
120 for (
size_t ii = 0; ii < vec_int.size(); ii++) {
121 vec_r[ii] = vec_int[ii];
135 std::vector<size_t>
dims_r(
const std::string& name)
const {
136 if (contains_r_only(name)) {
137 return (vars_r_.find(name)->second).second;
139 return (vars_i_.find(name)->second).second;
141 return empty_vec_ui_;
151 std::vector<int>
vals_i(
const std::string& name)
const {
153 return (vars_i_.find(name)->second).first;
165 std::vector<size_t>
dims_i(
const std::string& name)
const {
167 return (vars_i_.find(name)->second).second;
169 return empty_vec_ui_;
178 virtual void names_r(std::vector<std::string>& names)
const {
180 for (vars_map_r::const_iterator it = vars_r_.begin();
181 it != vars_r_.end(); ++it)
182 names.push_back((*it).first);
191 virtual void names_i(std::vector<std::string>& names)
const {
193 for (vars_map_i::const_iterator it = vars_i_.begin();
194 it != vars_i_.end(); ++it)
195 names.push_back((*it).first);
205 bool remove(
const std::string& name) {
206 return (vars_i_.erase(name) > 0)
207 || (vars_r_.erase(name) > 0);
std::vector< double > vals_r(const std::string &name) const
Return the double values for the variable with the specified name or null.
bool contains_r(const std::string &name) const
Return true if this json_data contains the specified variable name.
std::vector< int > vals_i(const std::string &name) const
Return the integer values for the variable with the specified name.
A json_data_handler is an implementation of a json_handler that restricts the allowed JSON text a set...
bool contains_i(const std::string &name) const
Return true if this json_data contains an integer valued array with the specified name...
json_data(std::istream &in)
Construct a json_data object from the specified input stream.
A var_reader reads array variables of integer and floating point type by name and dimension...
virtual void names_i(std::vector< std::string > &names) const
Return a list of the names of the integer variables in the json_data.
virtual void names_r(std::vector< std::string > &names) const
Return a list of the names of the floating point variables in the json_data.
std::vector< size_t > dims_i(const std::string &name) const
Return the dimensions for the integer variable with the specified name.
void parse(std::istream &in, Handler &handler)
Parse the JSON text represented by the specified input stream, sending events to the specified handle...
std::vector< size_t > dims_r(const std::string &name) const
Return the dimensions for the variable with the specified name.
A json_data is a var_context object that represents a set of named values which are typed to either d...