Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Saver.h
1 /*
2  * json::Saver class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2016, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_JSON_SAVER_H_
22 #define ELM_JSON_SAVER_H_
23 
24 #include <elm/data/Vector.h>
25 #include <elm/io.h>
26 #include <elm/io/BufferedOutStream.h>
27 #include <elm/io/StructuredOutput.h>
28 #include <elm/string/utf8.h>
29 #include <elm/sys/Path.h>
30 #include "common.h"
31 
32 namespace elm { namespace json {
33 
34 class Saver: public io::StructuredOutput {
35 public:
37  Saver(StringBuffer& buf);
38  Saver(sys::Path& path);
39  ~Saver(void);
40  void close(void);
41 
42  inline bool isReadable(void) const { return readable; }
43  inline void setReadable(bool read) { readable = read; }
44  inline string getIndent(void) const { return indent; }
45  inline void setIndent(string i) { indent = i; }
46 
47  // deprecated
48  inline void beginObject(void) { beginMap(); }
49  inline void endObject(void) { endMap(); }
50  inline void beginArray(void) { beginList(); }
51  inline void endArray(void) { endList(); }
52  inline void addField(string id) { key(id); }
53 
54  // deprecated
55  void put(void);
56  inline void put(const char *val) { put(cstring(val)); }
57  inline void put(cstring val) { write(val); }
58  inline void put(string val) { write(val); }
59  inline void put(t::uint64 val) { write(val); }
60  inline void put(t::int64 val) { write(val); }
61  inline void put(int val) { put(t::int64(val)); }
62  inline void put(double val) { write(val); }
63  inline void put(bool val) { write(val); }
64 
65  // StructuredOutput interface
66  virtual void write(bool x) override;
67  virtual void write(char c) override;
68  virtual void write(signed char x) override;
69  virtual void write(unsigned char x) override;
70  virtual void write(short x) override;
71  virtual void write(unsigned short x) override;
72  virtual void write(int x) override;
73  virtual void write(unsigned int x) override;
74  virtual void write(long x) override;
75  virtual void write(unsigned long x) override;
76  virtual void write(long long int x) override;
77  virtual void write(long long unsigned int x) override;
78  virtual void write(float x) override;
79  virtual void write(double x) override;
80  virtual void write(long double x) override;
81  virtual void write(const char *s) override;
82  virtual void write(cstring x) override;
83  virtual void write(const string& x) override;
84  virtual void key(cstring x) override;
85  virtual void key(const string& x) override;
86  virtual void beginMap() override;
87  virtual void endMap() override;
88  virtual void beginList() override;
89  virtual void endList() override;
90 
91 private:
92  typedef enum {
93  BEGIN,
94  OBJECT,
95  IN_OBJECT,
96  FIELD,
97  ARRAY,
98  IN_ARRAY,
99  END
100  } state_t;
101 
102  void doIndent(bool close = false);
103  static state_t next(state_t s);
104  static bool isObject(state_t s);
105  static bool isArray(state_t s);
106  void escape(utf8::char_t c);
107  inline void nextByValue(void);
108 
109  state_t state;
110  Vector<state_t> stack;
111  io::Output _out;
112  bool readable;
113  string indent;
115  io::OutStream *str;
116 };
117 
118 } } // elm::json
119 
120 #endif /* ELM_JSON_SAVER_H_ */
elm::t::out
typename type_info< T >::out_t out
Definition: type_info.h:284
elm::json::Saver::beginObject
void beginObject(void)
Definition: Saver.h:48
elm::json::Saver::endObject
void endObject(void)
Definition: Saver.h:49
elm::json::Saver::put
void put(void)
Definition: json.cpp:307
elm::json::Saver::write
virtual void write(bool x) override
Definition: json.cpp:320
elm::json::Saver::endArray
void endArray(void)
Definition: Saver.h:51
elm::sys::Path
Definition: Path.h:33
elm::StringBuffer
Definition: StringBuffer.h:18
elm::io::out
sys::SystemOutStream & out
Definition: system_SystemIO.cpp:122
elm::json::Saver::put
void put(const char *val)
Definition: Saver.h:56
elm::json::Saver::beginList
virtual void beginList() override
Definition: json.cpp:213
elm::t::int64
long int64
Definition: arch.h:32
elm::json::Saver::put
void put(double val)
Definition: Saver.h:62
elm::cstring
CString cstring
Definition: CString.h:62
elm::json::Saver::endList
virtual void endList() override
Definition: json.cpp:226
elm::CString
Definition: CString.h:17
elm::json::Saver::setReadable
void setReadable(bool read)
Definition: Saver.h:43
elm::json::Saver::getIndent
string getIndent(void) const
Definition: Saver.h:44
elm::json::Saver::put
void put(t::uint64 val)
Definition: Saver.h:59
elm::json::Saver::endMap
virtual void endMap() override
Definition: json.cpp:200
elm::io::read
FileInput read(sys::Path path)
Definition: io.h:26
elm::json::Saver::put
void put(bool val)
Definition: Saver.h:63
elm::json::Saver::put
void put(cstring val)
Definition: Saver.h:57
elm::json::Saver::~Saver
~Saver(void)
Definition: json.cpp:71
elm::json::Saver::isReadable
bool isReadable(void) const
Definition: Saver.h:42
elm
Definition: adapter.h:26
elm::json::Saver::put
void put(int val)
Definition: Saver.h:61
elm::json::Saver::addField
void addField(string id)
Definition: Saver.h:52
elm::Vector< state_t >
elm::t::uint64
unsigned long uint64
Definition: arch.h:33
elm::io::BufferedOutStream
Definition: BufferedOutStream.h:31
elm::json::Saver::setIndent
void setIndent(string i)
Definition: Saver.h:45
elm::io::OutStream
Definition: OutStream.h:30
elm::io::StructuredOutput
Definition: StructuredOutput.h:28
elm::json::Saver::put
void put(string val)
Definition: Saver.h:58
elm::json::Saver::key
virtual void key(cstring x) override
Definition: json.cpp:261
elm::json::Saver::beginMap
virtual void beginMap() override
Definition: json.cpp:187
elm::json::Saver
Definition: Saver.h:34
elm::json::Saver::close
void close(void)
Definition: json.cpp:108
elm::json::Saver::put
void put(t::int64 val)
Definition: Saver.h:60
elm::json::Saver::beginArray
void beginArray(void)
Definition: Saver.h:50
elm::io::Output
Definition: Output.h:179
elm::utf8::char_t
t::uint32 char_t
Definition: utf8.h:31
elm::json::Saver::Saver
Saver(io::OutStream &out=io::out)
Definition: json.cpp:47