1 #ifndef STAN_IO_JSON_JSON_DATA_HPP
2 #define STAN_IO_JSON_JSON_DATA_HPP
4 #include <boost/throw_exception.hpp>
5 #include <boost/lexical_cast.hpp>
49 std::vector<double>
const empty_vec_r_;
50 std::vector<int>
const empty_vec_i_;
51 std::vector<size_t>
const empty_vec_ui_;
62 bool contains_r_only(
const std::string& name)
const {
63 return vars_r_.find(name) != vars_r_.end();
75 explicit json_data(std::istream& in) : vars_r_(), vars_i_() {
89 return contains_r_only(name) ||
contains_i(name);
101 return vars_i_.find(name) != vars_i_.end();
111 std::vector<double>
vals_r(
const std::string& name)
const {
112 if (contains_r_only(name)) {
113 return (vars_r_.find(name)->second).first;
115 std::vector<int> vec_int = (vars_i_.find(name)->second).first;
116 std::vector<double> vec_r(vec_int.size());
117 for (
size_t ii = 0; ii < vec_int.size(); ii++) {
118 vec_r[ii] = vec_int[ii];
132 std::vector<size_t>
dims_r(
const std::string& name)
const {
133 if (contains_r_only(name)) {
134 return (vars_r_.find(name)->second).second;
136 return (vars_i_.find(name)->second).second;
138 return empty_vec_ui_;
148 std::vector<int>
vals_i(
const std::string& name)
const {
150 return (vars_i_.find(name)->second).first;
162 std::vector<size_t>
dims_i(
const std::string& name)
const {
164 return (vars_i_.find(name)->second).second;
166 return empty_vec_ui_;
175 virtual void names_r(std::vector<std::string>& names)
const {
177 for (vars_map_r::const_iterator it = vars_r_.begin();
178 it != vars_r_.end(); ++it)
179 names.push_back((*it).first);
188 virtual void names_i(std::vector<std::string>& names)
const {
190 for (vars_map_i::const_iterator it = vars_i_.begin();
191 it != vars_i_.end(); ++it)
192 names.push_back((*it).first);
202 bool remove(
const std::string& name) {
203 return (vars_i_.erase(name) > 0)
204 || (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.
Probability, optimization and sampling library.
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...