IOSS  2.0
json-forwards.h
Go to the documentation of this file.
1 /*
2  * Copyright(C) 2010-2017 National Technology & Engineering Solutions
3  * of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
4  * NTESS, the U.S. Government retains certain rights in this software.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  *
18  * * Neither the name of NTESS nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 //////////////////////////////////////////////////////////////////////
35 // End of content of file: include/json/assertions.h
36 // //////////////////////////////////////////////////////////////////////
37 
38 #endif // ifndef JSON_AMALGATED_H_INCLUDED
39 cause an exception.
40  - This is a security issue (seg-faults caused by deeply nested JSON),
41  so the default is low.
42  - `"failIfExtra": false or true`
43  - If true, `parse()` returns false when extra non-whitespace trails
44  the JSON value in the input string.
45  - `"rejectDupKeys": false or true`
46  - If true, `parse()` returns false when a key is duplicated within an object.
47 
48  You can examine 'settings_` yourself
49  to see the defaults. You can also write and read them just like any
50  JSON Value.
51  \sa setDefaults()
52  */
53  Json::Value settings_;
54 
55 CharReaderBuilder();
56 virtual ~CharReaderBuilder();
57 
58 virtual CharReader *newCharReader() const;
59 
60 /** \return true if 'settings' are legal and consistent;
61  * otherwise, indicate bad settings via 'invalid'.
62  */
63 bool validate(Json::Value *invalid) const;
64 
65 /** A simple way to update a specific setting.
66  */
67 Value &operator[](std::string key);
68 
69 /** Called by ctor, but you can use this to reset settings_.
70  * \pre 'settings' != NULL (but Json::null is fine)
71  * \remark Defaults:
72  * \snippet src/lib_json/json_reader.cpp CharReaderBuilderStrictMode
73  */
74 static void setDefaults(Json::Value *settings);
75 /** Same as old Features::strictMode().
76  * \pre 'settings' != NULL (but Json::null is fine)
77  * \remark Defaults:
78  * \snippet src/lib_json/json_reader.cpp CharReaderBuilderDefaults
79  */
80 static void strictMode(Json::Value *settings);
81 }
82 ;
83 
84 /** Consume entire stream and use its begin/end.
85  * Someday we might have a real StreamReader, but for now this
86  * is convenient.
87  */
88 bool JSON_API parseFromStream(CharReader::Factory const &, std::istream &, Value *root,
89  std::string *errs);
90 
91 /** \brief Read from 'sin' into 'root'.
92 
93  Always keep comments from the input JSON.
94 
95  This can be used to read a file into a particular sub-object.
96  For example:
97  \code
98  Json::Value root;
99  cin >> root["dir"]["file"];
100  cout << root;
101  \endcode
102  Result:
103  \verbatim
104  {
105  "dir": {
106  "file": {
107  // The input stream JSON would be nested here.
108  }
109  }
110  }
111  \endverbatim
112  \throw std::exception on parse error.
113  \see Json::operator<<()
114 */
115 JSON_API std::istream &operator>>(std::istream &, Value &);
116 
117 } // namespace Json
118 
119 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
120 #pragma warning(pop)
121 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
122 
123 #endif // CPPTL_JSON_READER_H_INCLUDED
124 
125 // //////////////////////////////////////////////////////////////////////
126 // End of content of file: include/json/reader.h
127 // //////////////////////////////////////////////////////////////////////
128 
129 // //////////////////////////////////////////////////////////////////////
130 // Beginning of content of file: include/json/writer.h
131 // //////////////////////////////////////////////////////////////////////
132 
133 // Copyright 2007-2010 Baptiste Lepilleur
134 // Distributed under MIT license, or public domain if desired and
135 // recognized in your jurisdiction.
136 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
137 
138 #ifndef JSON_WRITER_H_INCLUDED
139 #define JSON_WRITER_H_INCLUDED
140 
141 #if !defined(JSON_IS_AMALGAMATION)
142 #include "value.h"
143 #endif // if !defined(JSON_IS_AMALGAMATION)
144 #include <ostream>
145 #include <string>
146 #include <vector>
147 
148 // Disable warning C4251: <data member>: <type> needs to have dll-interface to
149 // be used by...
150 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
151 #pragma warning(push)
152 #pragma warning(disable : 4251)
153 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
154 
155 namespace Json {
156 
157  class Value;
158 
159  /**
160 
161  Usage:
162  \code
163  using namespace Json;
164  void writeToStdout(StreamWriter::Factory const& factory, Value const& value) {
165  std::unique_ptr<StreamWriter> const writer(
166  factory.newStreamWriter());
167  writer->write(value, &std::cout);
168  std::cout << std::endl; // add lf and flush
169  }
170  \endcode
171  */
172  class JSON_API StreamWriter
173  {
174  protected:
175  std::ostream *sout_; // not owned; will not delete
176  public:
177  StreamWriter();
178  virtual ~StreamWriter();
179  /** Write Value into document as configured in sub-class.
180  Do not take ownership of sout, but maintain a reference during function.
181  \pre sout != NULL
182  \return zero on success (For now, we always return zero, so check the
183  stream instead.)
184  \throw std::exception possibly, depending on configuration
185  */
186  virtual int write(Value const &root, std::ostream *sout) = 0;
187 
188  /** \brief A simple abstract factory.
189  */
190  class JSON_API Factory
191  {
192  public:
193  virtual ~Factory();
194  /** \brief Allocate a CharReader via operator new().
195  * \throw std::exception if something goes wrong (e.g. invalid settings)
196  */
197  virtual StreamWriter *newStreamWriter() const = 0;
198  }; // Factory
199  }; // StreamWriter
200 
201  /** \brief Write into stringstream, then return string, for convenience.
202  * A StreamWriter will be created from the factory, used, and then deleted.
203  */
204  std::string JSON_API writeString(StreamWriter::Factory const &factory, Value const &root);
205 
206  /** \brief Build a StreamWriter implementation.
207 
208  Usage:
209  \code
210  using namespace Json;
211  Value value = ...;
212  StreamWriterBuilder builder;
213  builder["commentStyle"] = "None";
214  builder["indentation"] = " "; // or whatever you like
215  std::unique_ptr<Json::StreamWriter> writer(
216  builder.newStreamWriter());
217  writer->write(value, &std::cout);
218  std::cout << std::endl; // add lf and flush
219  \endcode
220  */
221  class JSON_API StreamWriterBuilder : public StreamWriter::Factory
222  {
223  public:
224  // Note: We use a Json::Value so that we can add data-members to this class
225  // without a major version bump.
226  /** Configuration of this builder.
227  Available settings (case-sensitive):
228  - "commentStyle": "None" or "All"
229  - "indentation": "<anything>"
230  - "enableYAMLCompatibility": false or true
231  - slightly change the whitespace around colons
232  - "dropNullPlaceholders": false or true
233  - Drop the "null" string from the writer's output for nullValues.
234  Strictly speaking, this is not valid JSON. But when the output is being
235  fed to a browser's Javascript, it makes for smaller output and the
236  browser can handle the output just fine.
237 
238  You can examine 'settings_` yourself
239  to see the defaults. You can also write and read them just like any
240  JSON Value.
241  \sa setDefaults()
242  */
243  Json::Value settings_;
244 
245  StreamWriterBuilder();
246  virtual ~StreamWriterBuilder();
247 
248  /**
249  * \throw std::exception if something goes wrong (e.g. invalid settings)
250  */
251  virtual StreamWriter *newStreamWriter() const;
252 
253  /** \return true if 'settings' are legal and consistent;
254  * otherwise, indicate bad settings via 'invalid'.
255  */
256  bool validate(Json::Value *invalid) const;
257  /** A simple way to update a specific setting.
258  */
259  Value &operator[](std::string key);
260 
261  /** Called by ctor, but you can use this to reset settings_.
262  * \pre 'settings' != NULL (but Json::null is fine)
263  * \remark Defaults:
264  * \snippet src/lib_json/json_writer.cpp StreamWriterBuilderDefaults
265  */
266  static void setDefaults(Json::Value *settings);
267  };
268 
269  /** \brief Abstract class for writers.
270  * \deprecated Use StreamWriter. (And really, this is an implementation detail.)
271  */
272  class JSON_API Writer
273  {
274  public:
275  virtual ~Writer();
276 
277  virtual std::string write(const Value &root) = 0;
278  };
279 
280  /** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format
281  *without formatting (not human friendly).
282  *
283  * The JSON document is written in a single line. It is not intended for 'human'
284  *consumption,
285  * but may be useful to support feature such as RPC where bandwidth is limited.
286  * \sa Reader, Value
287  * \deprecated Use StreamWriterBuilder.
288  */
289  class JSON_API FastWriter : public Writer
290  {
291 
292  public:
293  FastWriter();
294  virtual ~FastWriter() {}
295 
296  void enableYAMLCompatibility();
297 
298  /** \brief Drop the "null" string from the writer's output for nullValues.
299  * Strictly speaking, this is not valid JSON. But when the output is being
300  * fed to a browser's Javascript, it makes for smaller output and the
301  * browser can handle the output just fine.
302  */
303  void dropNullPlaceholders();
304 
305  void omitEndingLineFeed();
306 
307  public: // overridden from Writer
308  virtual std::string write(const Value &root);
309 
310  private:
311  void writeValue(const Value &value);
312 
313  std::string document_;
314  bool yamlCompatiblityEnabled_;
315  bool dropNullPlaceholders_;
316  bool omitEndingLineFeed_;
317  };
318 
319  /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
320  *human friendly way.
321  *
322  * The rules for line break and indent are as follow:
323  * - Object value:
324  * - if empty then print {} without indent and line break
325  * - if not empty the print '{', line break & indent, print one value per
326 
cause an exception This is a security issue(seg-faults caused by deeply nested JSON)