TPIE

2362a60
jsonprint.h
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2016, The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 #ifndef __TPIE_JSONPRINT_H__
20 #define __TPIE_JSONPRINT_H__
21 
22 #include <string>
23 #include <cstdint>
24 #include <iosfwd>
25 namespace tpie {
26 
27 // predeclare reflection
28 template <typename R, typename T, typename ... TT>
29 bool reflect(R & r, T && v, TT && ... vs);
30 
31 // JSONReflector pimpl
32 class JSONReflectorP;
33 
37 struct JSONReflector {
38 public:
39  static constexpr bool write = false;
40  static constexpr bool arithmetic = true;
41  static constexpr bool string = true;
42  static constexpr bool trivialSerializable = false;
43 
44  JSONReflector(std::ostream & o, bool pretty);
45  ~JSONReflector();
46 
47  void begin(const char *);
48  void end();
49  void beginArray(size_t);
50  void endArray();
51  void name(const char * name);
52 
53  void beginStaticArray(size_t x) {beginArray(x);}
54  void endStaticArray() {endArray();}
55  bool operator()(const uint8_t & v) {writeUint(v); return true;}
56  bool operator()(const uint16_t & v) {writeUint(v); return true;}
57  bool operator()(const uint32_t & v) {writeUint(v); return true;}
58  bool operator()(const uint64_t & v) {writeUint(v); return true;}
59  bool operator()(const int8_t & v) {writeInt(v); return true;}
60  bool operator()(const int16_t & v) {writeInt(v); return true;}
61  bool operator()(const int32_t & v) {writeInt(v); return true;}
62  bool operator()(const int64_t & v) {writeInt(v); return true;}
63  bool operator()(const float & v) {writeDouble(v); return true;}
64  bool operator()(const double & v) {writeDouble(v); return true;}
65  bool operator()(const std::string & v) {writeString(v); return true;}
66 private:
67  void writeUint(uint64_t v);
68  void writeInt(int64_t v);
69  void writeDouble(double v);
70  void writeString(const std::string & v);
71 
72  JSONReflectorP * p;
73 };
74 
78 template <typename T>
79 class JSONPrinter {
80 public:
81  JSONPrinter(const T & t, bool pretty): t(t), pretty(pretty) {}
82 
83  friend std::ostream & operator << (std::ostream & o, const JSONPrinter & p) {
84  JSONReflector jr(o, p.pretty);
85  reflect(jr, p.t);
86  return o;
87  }
88 private:
89  const T & t;
90  bool pretty;
91 };
92 
99 template <typename T>
100 inline JSONPrinter<T> json_printer(const T & t, bool pretty=true) {return JSONPrinter<T>(t, pretty);}
101 
102 } //namespace tpie
103 
104 #endif //__TPIE_JSONPRINT_H__
refletor for json printing to an ostream
Definition: jsonprint.h:37
helper struct for printing
Definition: jsonprint.h:79
JSONPrinter< T > json_printer(const T &t, bool pretty=true)
create a json print wrapper around t
Definition: jsonprint.h:100