-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathwrite_objects_to_json.cpp
More file actions
149 lines (114 loc) · 4.88 KB
/
Copy pathwrite_objects_to_json.cpp
File metadata and controls
149 lines (114 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* This example shows how to work with json stored objects
* RCDB uses json library
* https://github.com/nlohmann/json
*/
#include <string>
#include <iostream>
#include "RCDB/WritingConnection.h"
#include "RCDB/StringUtils.h"
#include <tao/json.hpp>
using namespace tao;
int main(int argc, char *argv[]) {
using namespace std;
// Get a connection string from arguments
if (argc != 2) {
cout << "This example writes objects as j for a specified run" << endl;
cout<<"rcnd --create json_cnd --type j --description \"JSON serialized values\""<<endl;
cout << "usage: " << argv[0] << " <connection_string>" << endl;
cout << "exmpl: " << argv[0] << " mysql://rcdb@localhost/rcdb" << endl;
cout << "exmpl: " << argv[0] << " mysql://rcdb:password@clondb1/rcdb" << endl;
return 1;
}
string connection_str(argv[1]);
// Create DB connection
rcdb::WritingConnection connection(connection_str);
struct tm start_time;
start_time.tm_year = 2016 - 1900;
start_time.tm_mon = 1;
start_time.tm_mday = 4;
start_time.tm_hour = 02;
start_time.tm_min = 30;
start_time.tm_sec = 38;
start_time.tm_isdst = 0;
struct tm end_time;
end_time.tm_year = 2016 - 1900;
end_time.tm_mon = 1;
end_time.tm_mday = 4;
end_time.tm_hour = 04;
end_time.tm_min = 25;
end_time.tm_sec = 10;
end_time.tm_isdst = 0;
connection.AddRun(999);
connection.AddRunStartTime(999, start_time);
connection.AddRunEndTime(999, end_time);
/*
id name value_type created description
1 event_count int 2017-03-15 14:18:55 Number of events in run
2 events_rate float 2017-03-15 14:18:55 Daq events rate
3 temperature int 2017-03-15 14:18:55 Temperature of the Sun
4 beam_energy float 2017-03-15 18:57:22 Beam Energy
5 test float 2017-03-15 18:58:00 Beam test
6 beam_current float 2017-03-15 19:02:10 Beam current
7 torus_scale float 2017-03-15 19:02:10 Torus scale factor
8 daq_trigger string 2017-03-15 19:02:10 Trigger file
9 target_position float 2017-03-15 19:02:10 Target position
10 daq_comment string 2017-03-15 19:02:10 DAQ comment
11 run_start_time time 2017-04-21 10:28:49 Run start time
12 run_end_time time 2017-04-21 10:29:02 Run end time
13 is_valid_run_end bool 2017-04-21 10:48:35 True if a run has valid run-end record. False mean...
14 status int 2017-05-17 13:30:34 Run Status
*/
int32_t event_count = 12345;
float events_rate = 98765.32;
int32_t temperature = 77;
float beam_energy = 6.095;
float test = 1234567890.123;
string daq_trigger = "trigger_file_name";
string daq_comment = "this run is junk";
std::vector<int> c_vector{1, 2, 3, 4};
// P A R T 1 - w r i t i n g a r r a y
// You can create j a way, which looks very similar to the JSON itself)
json::value j({
{"event_count", event_count},
{"events_rate", events_rate},
{"temperature", temperature},
{"beam_energy", beam_energy},
{"daq_trigger", daq_trigger},
{"test", test},
{"list", {json::value::array({1, 0, true})} },
{"object", {{"currency", "USD"}, {"value", 42.99}} },
{"start_time", StringUtils::GetFormattedTime(start_time)}
});
// copy array to json
auto c_vector_json = json::value::array({});
for(auto& item: c_vector) c_vector_json.emplace_back(item);
// + operator adds one json to another
j += {{"c_vector", c_vector_json }};
// or act as a dictionary
// add a number that is stored as double (note the implicit conversion of j to an object)
j["pi"] = 3.141;
// Pretty formatted j
cout << tao::json::to_string(j, 4) << endl;
// compact formatted j
cout << tao::json::to_string(j) << endl;
// Add condition
connection.AddCondition(999, "json_cnd", tao::json::to_string(j));
// P A R T 2 - r e a d i n g a r r a y
auto cnd = connection.GetCondition(999, "json_cnd");
auto data = tao::json::from_string(cnd->ToString());
// iterate underlying map<name, value>
auto &object = data.get_object();
//for ( auto it= object.begin(); it != object.end(); ++it) {
//std::cout << *it <<" "<< *it << std::endl;
//}
// range-based for
for (auto& element : object) {
std::cout << element.first << " " << element.first << std::endl;
}
int32_t ev_cnt = object["event_count"].get_unsigned();
// explicit data conversion
auto trigger = data["daq_trigger"].get_string();
cout<<"event_count= "<<ev_cnt<<" trigger= "<<trigger<<" start_time="<<data["start_time"]<<endl;
return 0;
}