Grid 0.7.0
JSON_IO.cc
Go to the documentation of this file.
1 /*************************************************************************************
2
3 Grid physics library, www.github.com/paboyle/Grid
4
5 Source file: ./lib/serialisation/JSON_IO.cc
6
7 Copyright (C) 2016
8
9 Author: Guido Cossu<guido.cossu@ed.ac.uk>
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License along
22 with this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24
25 See the full license in the file "LICENSE" in the top level distribution directory
26 *************************************************************************************/
27 /* END LEGAL */
28#include <Grid/Grid.h>
29#ifndef GRID_HIP
30
32
33// Writer implementation ///////////////////////////////////////////////////////
34JSONWriter::JSONWriter(const std::string &fileName)
35: fileName_(fileName), ss_("{ ", std::ostringstream::ate){}
36
38{
39 // close
41 ss_ << "}";
42
43 // write prettified JSON to file
44 std::ofstream os(fileName_);
45 //std::cout << "JSONWriter::~JSONWriter" << std::endl;
46 os << std::setw(2) << json::parse(ss_.str()) << std::endl;
47}
48
49void JSONWriter::push(const std::string &s)
50{
51 // adding a nested object
52 if (s.size())
53 ss_ << " \""<<s<<"\" : {";
54 else
55 ss_ << " {";
56}
57
59{
60 //std::cout << "JSONWriter::pop" << std::endl;
62 ss_ << "},";
63}
64
66{
67 std::string dlast = ss_.str();
68 dlast.pop_back(); // deletes the last comma
69 ss_.str(dlast);
70}
71
72
73// here we are hitting a g++ bug (Bug 56480)
74// compiles fine with clang
75// have to wrap in the Grid namespace
76// annoying, but necessary for TravisCI
77void JSONWriter::writeDefault(const std::string &s, const std::string &x)
78{
79 //std::cout << "JSONWriter::writeDefault(string) : " << s << std::endl;
80 std::ostringstream os;
81 os << std::boolalpha << x;
82 if (s.size())
83 ss_ << "\""<< s << "\" : \"" << os.str() << "\" ," ;
84 else
85 ss_ << "\""<< os.str() << "\" ," ;
86}
87
88// Reader implementation ///////////////////////////////////////////////////////
89JSONReader::JSONReader(const std::string &fileName)
90: fileName_(fileName)
91{
92 std::ifstream file(fileName_);
93 file >> jobject_;
94
95 // test
96 // serialize to standard output
97 //std::cout << "JSONReader::JSONReader : " << jobject_ << endl;
99}
100
101bool JSONReader::push(const std::string &s)
102{
103 if (s.size()){
104 jold_.push_back(jcur_);
105 do_pop.push_back(true);
106 try
107 {
108 jcur_ = jcur_[s];
109 }
110 catch (std::out_of_range& e)
111 {
112 std::cout << "out of range: " << e.what() << '\n';
113 return false;
114 }
115 //cout << "JSONReader::push : " << s << " : "<< jcur_ << endl;
116 }
117 else
118 {
119 do_pop.push_back(false);
120 }
121
122
123 return true;
124}
125
127{
128 if (do_pop.back()){
129 jcur_ = jold_.back();
130 jold_.pop_back();
131 do_pop.pop_back();
132 }
133 else
134 do_pop.pop_back();
135
136 //cout << "JSONReader::pop : " << jcur_ << endl;
137}
138
139bool JSONReader::nextElement(const std::string &s)
140{
141 // Work in progress
142 // JSON dictionaries do not support multiple names
143 // Same name objects must be packed in vectors
144 ++it_;
145
146 //if (it_ == it_end_){
147 // return false;
148 //}
149
150 jcur_ = *it_;
151 //cout << "JSONReader::nextElement(std::string) : " << s << " : "<< jcur_ << endl;
152 //return true;
153
154 return false;
155}
156
157template <>
158void JSONReader::readDefault(const std::string &s, std::string &output)
159{
160 //cout << "JSONReader::readDefault(string) : " << s<< " " << jcur_ << endl;
161 if (s.size()){
162 //std::cout << "String: "<< jcur_[s] << std::endl;
163 output = jcur_[s];
164 }
165 else
166 {
167 //std::cout << "String: "<< jcur_ << std::endl;
168 output = jcur_;
169 }
170}
172#endif
#define NAMESPACE_BEGIN(A)
Definition Namespace.h:35
#define NAMESPACE_END(A)
Definition Namespace.h:36
std::vector< bool > do_pop
Definition JSON_IO.h:124
bool nextElement(const std::string &s)
Definition JSON_IO.cc:139
JSONReader(const std::string &fileName)
Definition JSON_IO.cc:89
void readDefault(const std::string &s, U &output)
Definition JSON_IO.h:205
bool push(const std::string &s)
Definition JSON_IO.cc:101
void pop(void)
Definition JSON_IO.cc:126
std::string fileName_
Definition JSON_IO.h:123
std::vector< json > jold_
Definition JSON_IO.h:122
json::iterator it_
Definition JSON_IO.h:125
void pop(void)
Definition JSON_IO.cc:58
void writeDefault(const std::string &s, const U &x)
Definition JSON_IO.h:141
std::string fileName_
Definition JSON_IO.h:84
std::ostringstream ss_
Definition JSON_IO.h:85
JSONWriter(const std::string &fileName)
Definition JSON_IO.cc:34
void delete_comma()
Definition JSON_IO.cc:65
virtual ~JSONWriter(void)
Definition JSON_IO.cc:37
void push(const std::string &s)
Definition JSON_IO.cc:49