test_json_bzip2.cxx
Go to the documentation of this file.
1 #include <random>
2 
3 #include <json/json.h>
4 
5 #include <iostream>
6 #include <boost/iostreams/copy.hpp>
7 #include <boost/iostreams/filter/bzip2.hpp>
8 #include <boost/iostreams/device/file.hpp>
9 #include <boost/iostreams/filtering_stream.hpp>
10 #include <string>
11 
12 #include <fstream>
13 using namespace std;
14 
15 
16 int main(int argc, char* argv[])
17 {
18  string name = argv[0];
19 
20  std::default_random_engine generator;
21  std::uniform_real_distribution<double> distribution(0.0,1.0);
22  auto uni = std::bind(distribution, generator);
23 
24  cout << "Generating data\n";
25  const int nbins = 500;
26  const int narrays = 100;
27  double data[narrays][nbins];
28  Json::Value jroot;
29  for (int ind=0; ind<narrays; ++ind) {
30  Json::Value jarr;
31  for (int ibin=0; ibin<nbins; ++ibin) {
32  auto number = uni();
33  data[ind][ibin] = number;
34  jarr.append(number);
35  }
36  jroot[ind] = jarr;
37  }
38 
39 
40  {
41  string fname = name+".bin";
42  cout << "writing " << fname << endl;
43  fstream myfile(fname.c_str(), ios::out | ios::binary);
44  myfile.write((char*)data, nbins*narrays*sizeof(double));
45  myfile.close();
46  }
47 
48  {
49  string fname = name+".json";
50  cout << "writing " << fname << endl;
51  ofstream jout(fname.c_str());
52  Json::FastWriter jwriter;
53  jout << jwriter.write(jroot);
54  }
55 
56  {
57  string fname = name+".json.bz2";
58  cout << "writing " << fname << endl;
59  std::fstream jbz2file(fname.c_str(), std::ios::binary|std::ios::out);
60  boost::iostreams::filtering_stream<boost::iostreams::output> outfilt;
61  outfilt.push(boost::iostreams::bzip2_compressor());
62  outfilt.push(jbz2file);
63  outfilt << jroot;
64  }
65 
66  Json::Value jroot2;
67  {
68  string fname = name+".json.bz2";
69  cout << "reading " << fname << endl;
70  std::fstream jbz2file(fname.c_str(), std::ios::binary|std::ios::in);
71  boost::iostreams::filtering_stream<boost::iostreams::input> infilt;
72  infilt.push(boost::iostreams::bzip2_decompressor());
73  infilt.push(jbz2file);
74  infilt >> jroot2;
75  }
76 
77  {
78  string fname = name + "2.json";
79  cout << "writing " << fname << endl;
80  ofstream jout2(fname.c_str());
81  Json::FastWriter jwriter2;
82  jout2 << jwriter2.write(jroot2);
83  }
84 
85  return 0;
86 }
static QCString name
Definition: declinfo.cpp:673
STL namespace.
basic_data data
Definition: format.h:764
int main(int argc, char *argv[])
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition: document.h:2106
generator
Definition: train.py:468
QTextStream & endl(QTextStream &s)