Functions
test_json_bzip2.cxx File Reference
#include <random>
#include <json/json.h>
#include <iostream>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <string>
#include <fstream>

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 16 of file test_json_bzip2.cxx.

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
basic_data data
Definition: format.h:764
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition: document.h:2106
generator
Definition: train.py:468
QTextStream & endl(QTextStream &s)