Scarab  v3.4.0
Project 8 C++ Utility Library
param_json.hh
Go to the documentation of this file.
1 /*
2  * param_json.hh
3  *
4  * Created on: March 27, 2015
5  * Author: nsoblath, bhlaroque
6  */
7 
8 #ifndef SCARAB_PARAM_JSON_HH_
9 #define SCARAB_PARAM_JSON_HH_
10 
11 #include "param_codec.hh"
12 
13 #include "logger.hh"
14 #include "param.hh"
15 
16 #include "rapidjson/fwd.h"
17 #include "rapidjson/prettywriter.h"
18 #include "rapidjson/writer.h"
19 
20 #include <deque>
21 #include <map>
22 #include <sstream>
23 #include <string>
24 
25 
26 namespace scarab
27 {
28  LOGGER( dlog_param_json, "param_json" );
29 
30  //***************************************
31  //************** INPUT ******************
32  //***************************************
33 
44  {
45  public:
47  virtual ~param_input_json();
48 
49  virtual param_ptr_t read_file( const std::string& a_filename, const param_node& a_options = param_node() );
50  virtual param_ptr_t read_string( const std::string& a_json_str, const param_node& a_options = param_node() );
51  param_ptr_t read_document( const rapidjson::Document& a_document );
52  param_ptr_t read_value( const rapidjson::Value& a_value );
53  };
54 
55  //***************************************
56  //************** OUTPUT *****************
57  //***************************************
58 
72  {
73  public:
74  typedef rapidjson::Writer< rapidjson::FileWriteStream > rj_file_writer;
75  typedef rapidjson::PrettyWriter< rapidjson::FileWriteStream > rj_pretty_file_writer;
76  typedef rapidjson::Writer< rapidjson::StringBuffer > rj_string_writer;
77  typedef rapidjson::PrettyWriter< rapidjson::StringBuffer > rj_pretty_string_writer;
78 
80  {
81  k_compact = 0,
82  k_pretty = 1
83  };
84 
85  public:
87  virtual ~param_output_json();
88 
89  virtual bool write_file( const param& a_to_write, const std::string& a_filename, const param_node& a_options = param_node() );
90  virtual bool write_string( const param& a_to_write, std::string& a_string, const param_node& a_options = param_node() );
91 
92  template< class XWriter >
93  bool write_param( const param& a_to_write, XWriter* a_writer );
94  template< class XWriter >
95  bool write_param_null( const param& a_to_write, XWriter* a_writer );
96  template< class XWriter >
97  bool write_param_value( const param_value& a_to_write, XWriter* a_writer );
98  template< class XWriter >
99  bool write_param_array( const param_array& a_to_write, XWriter* a_writer );
100  template< class XWriter >
101  bool write_param_node( const param_node& a_to_write, XWriter* a_writer );
102 
103  };
104 
105  template< class XWriter >
106  bool param_output_json::write_param( const param& a_to_write, XWriter* a_writer )
107  {
108  if( a_to_write.is_null() )
109  {
110  return param_output_json::write_param_null( a_to_write, a_writer );
111  }
112  if( a_to_write.is_value() )
113  {
114  return param_output_json::write_param_value( a_to_write.as_value(), a_writer );
115  }
116  if( a_to_write.is_array() )
117  {
118  return param_output_json::write_param_array( a_to_write.as_array(), a_writer );
119  }
120  if( a_to_write.is_node() )
121  {
122  return param_output_json::write_param_node( a_to_write.as_node(), a_writer );
123  }
124  LWARN( dlog_param_json, "parameter not written: <" << a_to_write << ">" );
125  return false;
126  }
127  template< class XWriter >
128  bool param_output_json::write_param_null( const param& /*a_to_write*/, XWriter* a_writer )
129  {
130  //LWARN( dlog_param_json, "writing null" );
131  a_writer->Null();
132  return true;
133  }
134  template< class XWriter >
135  bool param_output_json::write_param_value( const param_value& a_to_write, XWriter* a_writer )
136  {
137  //LWARN( dlog_param_json, "writing value" );
138  if( a_to_write.is_string() )
139  {
140  a_writer->String( a_to_write.as_string().c_str() );
141  //LWARN( dlog_param_json, "writing string to json: " << a_to_write.as_string() );
142  }
143  else if( a_to_write.is_bool() )
144  {
145  a_writer->Bool( a_to_write.as_bool() );
146  //LWARN( dlog_param_json, "writing bool to json: " << a_to_write.as_bool() );
147  }
148  else if( a_to_write.is_int() )
149  {
150  a_writer->Int64( a_to_write.as_int() );
151  //LWARN( dlog_param_json, "writing int to json: " << a_to_write.as_int() );
152  }
153  else if( a_to_write.is_uint() )
154  {
155  a_writer->Uint64( a_to_write.as_uint() );
156  //LWARN( dlog_param_json, "writing uint to json: " << a_to_write.as_uint() );
157  }
158  else if( a_to_write.is_double() )
159  {
160  a_writer->Double( a_to_write.as_double() );
161  //LWARN( dlog_param_json, "writing double to json: " << a_to_write.as_double() );
162  }
163  return true;
164  }
165  template< class XWriter >
166  bool param_output_json::write_param_array( const param_array& a_to_write, XWriter* a_writer )
167  {
168  //LWARN( dlog_param_json, "writing array" );
169  a_writer->StartArray();
170  for( param_array::const_iterator it = a_to_write.begin(); it != a_to_write.end(); ++it )
171  {
172  if( ! param_output_json::write_param( *it, a_writer ) )
173  {
174  LERROR( dlog_param_json, "Error while writing parameter array" );
175  return false;
176  }
177  }
178  a_writer->EndArray();
179  return true;
180  }
181  template< class XWriter >
182  bool param_output_json::write_param_node( const param_node& a_to_write, XWriter* a_writer )
183  {
184  //LWARN( dlog_param_json, "writing node" );
185  a_writer->StartObject();
186  for( param_node::const_iterator it = a_to_write.begin(); it != a_to_write.end(); ++it )
187  {
188  a_writer->String( it.name().c_str() );
189  if( ! param_output_json::write_param( *it, a_writer ) )
190  {
191  LERROR( dlog_param_json, "Error while writing parameter node" );
192  return false;
193  }
194  }
195  a_writer->EndObject();
196  return true;
197  }
198 
199 
200 } /* namespace scarab */
201 
202 #endif /* SCARAB_PARAM_JSON_HH_ */
int64_t as_int() const
Definition: param_value.hh:524
bool is_bool() const
Definition: param_value.hh:489
#define LWARN(...)
Definition: logger.hh:381
virtual bool is_value() const
bool write_param(const param &a_to_write, XWriter *a_writer)
Definition: param_json.hh:106
bool write_param_array(const param_array &a_to_write, XWriter *a_writer)
Definition: param_json.hh:166
iterator begin()
Definition: param_node.hh:328
#define SCARAB_API
Definition: scarab_api.hh:24
bool write_param_value(const param_value &a_to_write, XWriter *a_writer)
Definition: param_json.hh:135
LOGGER(mtlog, "authentication")
rapidjson::PrettyWriter< rapidjson::StringBuffer > rj_pretty_string_writer
Definition: param_json.hh:77
bool write_param_null(const param &a_to_write, XWriter *a_writer)
Definition: param_json.hh:128
bool is_uint() const
Definition: param_value.hh:494
#define LERROR(...)
Definition: logger.hh:382
double as_double() const
Definition: param_value.hh:529
Contains the logger class and macros, based on Kasper&#39;s KLogger class.
rapidjson::PrettyWriter< rapidjson::FileWriteStream > rj_pretty_file_writer
Definition: param_json.hh:75
virtual bool is_null() const
bool as_bool() const
Definition: param_value.hh:514
param_value & as_value()
Convert JSON to Param.
Definition: param_json.hh:43
Convert Param to JSON.
Definition: param_json.hh:71
bool write_param_node(const param_node &a_to_write, XWriter *a_writer)
Definition: param_json.hh:182
virtual bool is_array() const
bool is_double() const
Definition: param_value.hh:504
virtual bool is_node() const
std::unique_ptr< param > param_ptr_t
Definition: param_base.hh:23
bool is_string() const
Definition: param_value.hh:509
param_node & as_node()
uint64_t as_uint() const
Definition: param_value.hh:519
rapidjson::Writer< rapidjson::FileWriteStream > rj_file_writer
Definition: param_json.hh:74
param_array & as_array()
param_array_const_iterator const_iterator
Definition: param_array.hh:36
bool is_int() const
Definition: param_value.hh:499
std::string as_string() const
Definition: param_value.hh:534
rapidjson::Writer< rapidjson::StringBuffer > rj_string_writer
Definition: param_json.hh:76