CUGL
Cornell University Game Library
CUJsonWriter.h
1 //
2 // CUJsonWriter.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides extends the basic TextWriter to support JSON encoding.
6 // It does not require that the entire file conform to JSON standards; it can
7 // write a JSON string embedded in a larger text file.
8 //
9 // By default, this module (and every module in the io package) accesses the
10 // application save directory. If you want to access another directory, you
11 // will need to specify an absolute path for the file name. Keep in mind that
12 // absolute paths are very dangerous on mobile devices, because they do not
13 // have proper file systems. You should confine all files to either the asset
14 // or the save directory.
15 //
16 // This class uses our standard shared-pointer architecture.
17 //
18 // 1. The constructor does not perform any initialization; it just sets all
19 // attributes to their defaults.
20 //
21 // 2. All initialization takes place via init methods, which can fail if an
22 // object is initialized more than once.
23 //
24 // 3. All allocation takes place via static constructors which return a shared
25 // pointer.
26 //
27 // CUGL zlib License:
28 // This software is provided 'as-is', without any express or implied
29 // warranty. In no event will the authors be held liable for any damages
30 // arising from the use of this software.
31 //
32 // Permission is granted to anyone to use this software for any purpose,
33 // including commercial applications, and to alter it and redistribute it
34 // freely, subject to the following restrictions:
35 //
36 // 1. The origin of this software must not be misrepresented; you must not
37 // claim that you wrote the original software. If you use this software
38 // in a product, an acknowledgment in the product documentation would be
39 // appreciated but is not required.
40 //
41 // 2. Altered source versions must be plainly marked as such, and must not
42 // be misrepresented as being the original software.
43 //
44 // 3. This notice may not be removed or altered from any source distribution.
45 //
46 // Author: Walker White
47 // Version: 11/28/16
48 //
49 #ifndef __CU_JSON_WRITER_H__
50 #define __CU_JSON_WRITER_H__
51 #include <cugl/io/CUTextWriter.h>
52 #include <cugl/assets/CUJsonValue.h>
53 
54 namespace cugl {
55 
70 class JsonWriter : public TextWriter {
71 #pragma mark -
72 #pragma mark Static Constructors
73 public:
90  static std::shared_ptr<JsonWriter> alloc(const std::string& file) {
91  std::shared_ptr<JsonWriter> result = std::make_shared<JsonWriter>();
92  return (result->init(file) ? result : nullptr);
93  }
94 
111  static std::shared_ptr<JsonWriter> alloc(const char* file) {
112  std::shared_ptr<JsonWriter> result = std::make_shared<JsonWriter>();
113  return (result->init(file) ? result : nullptr);
114  }
115 
132  static std::shared_ptr<JsonWriter> alloc(const Pathname& file) {
133  std::shared_ptr<JsonWriter> result = std::make_shared<JsonWriter>();
134  return (result->init(file) ? result : nullptr);
135  }
136 
151  static std::shared_ptr<JsonWriter> alloc(const std::string& file, unsigned int capacity) {
152  std::shared_ptr<JsonWriter> result = std::make_shared<JsonWriter>();
153  return (result->init(file,capacity) ? result : nullptr);
154  }
155 
170  static std::shared_ptr<JsonWriter> alloc(const char* file, unsigned int capacity) {
171  std::shared_ptr<JsonWriter> result = std::make_shared<JsonWriter>();
172  return (result->init(file,capacity) ? result : nullptr);
173  }
174 
189  static std::shared_ptr<JsonWriter> alloc(const Pathname& file, unsigned int capacity) {
190  std::shared_ptr<JsonWriter> result = std::make_shared<JsonWriter>();
191  return (result->init(file,capacity) ? result : nullptr);
192  }
193 
194 
195 #pragma mark -
196 #pragma mark Write Methods
197 
208  void writeJson(const std::shared_ptr<JsonValue>& json, bool format=true) {
209  writeJson(json.get(),format);
210  }
211 
223  void writeJson(const JsonValue* json, bool format=true);
224 };
225 
226 }
227 
228 
229 #endif /* __CU_JSON_WRITER_H__ */
static std::shared_ptr< JsonWriter > alloc(const Pathname &file)
Definition: CUJsonWriter.h:132
void writeJson(const std::shared_ptr< JsonValue > &json, bool format=true)
Definition: CUJsonWriter.h:208
Definition: CUTextWriter.h:72
static std::shared_ptr< JsonWriter > alloc(const char *file, unsigned int capacity)
Definition: CUJsonWriter.h:170
static std::shared_ptr< JsonWriter > alloc(const char *file)
Definition: CUJsonWriter.h:111
static std::shared_ptr< JsonWriter > alloc(const std::string &file, unsigned int capacity)
Definition: CUJsonWriter.h:151
static std::shared_ptr< JsonWriter > alloc(const Pathname &file, unsigned int capacity)
Definition: CUJsonWriter.h:189
Definition: CUJsonValue.h:68
static std::shared_ptr< JsonWriter > alloc(const std::string &file)
Definition: CUJsonWriter.h:90
Definition: CUAnimationNode.h:52
Definition: CUPathname.h:85
Definition: CUJsonWriter.h:70