CUGL 1.3
Cornell University Game Library
CUJsonReader.h
1 //
2 // CUJsonReader.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides extends the basic TextReader to support JSON decoding.
6 // It does not require that the entire file conform to JSON standards; it can
7 // read 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 MIT 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_READER_H__
50 #define __CU_JSON_READER_H__
51 #include <cugl/io/CUTextReader.h>
52 #include <cugl/assets/CUJsonValue.h>
53 
54 namespace cugl {
55 
70 class JsonReader : public TextReader {
71 
72 #pragma mark -
73 #pragma mark Static Constructors
74 public:
90  static std::shared_ptr<JsonReader> alloc(const std::string& file) {
91  std::shared_ptr<JsonReader> result = std::make_shared<JsonReader>();
92  return (result->init(file) ? result : nullptr);
93  }
94 
110  static std::shared_ptr<JsonReader> alloc(const char* file) {
111  std::shared_ptr<JsonReader> result = std::make_shared<JsonReader>();
112  return (result->init(file) ? result : nullptr);
113  }
114 
130  static std::shared_ptr<JsonReader> alloc(const Pathname& file) {
131  std::shared_ptr<JsonReader> result = std::make_shared<JsonReader>();
132  return (result->init(file) ? result : nullptr);
133  }
134 
148  static std::shared_ptr<JsonReader> alloc(const std::string& file, unsigned int capacity) {
149  std::shared_ptr<JsonReader> result = std::make_shared<JsonReader>();
150  return (result->init(file,capacity) ? result : nullptr);
151  }
152 
166  static std::shared_ptr<JsonReader> alloc(const char* file, unsigned int capacity) {
167  std::shared_ptr<JsonReader> result = std::make_shared<JsonReader>();
168  return (result->init(file,capacity) ? result : nullptr);
169  }
170 
184  static std::shared_ptr<JsonReader> alloc(const Pathname& file, unsigned int capacity) {
185  std::shared_ptr<JsonReader> result = std::make_shared<JsonReader>();
186  return (result->init(file,capacity) ? result : nullptr);
187  }
188 
203  static std::shared_ptr<JsonReader> allocWithAsset(const std::string& file) {
204  std::shared_ptr<JsonReader> result = std::make_shared<JsonReader>();
205  return (result->initWithAsset(file) ? result : nullptr);
206  }
207 
222  static std::shared_ptr<JsonReader> allocWithAsset(const char* file) {
223  std::shared_ptr<JsonReader> result = std::make_shared<JsonReader>();
224  return (result->initWithAsset(file) ? result : nullptr);
225  }
226 
240  static std::shared_ptr<JsonReader> allocWithAsset(const std::string& file, unsigned int capacity) {
241  std::shared_ptr<JsonReader> result = std::make_shared<JsonReader>();
242  return (result->initWithAsset(file,capacity) ? result : nullptr);
243  }
244 
258  static std::shared_ptr<JsonReader> allocWithAsset(const char* file, unsigned int capacity) {
259  std::shared_ptr<JsonReader> result = std::make_shared<JsonReader>();
260  return (result->initWithAsset(file,capacity) ? result : nullptr);
261  }
262 
263 
264 #pragma mark -
265 #pragma mark Read Methods
266 
279  std::string readJsonString();
280 
293  std::shared_ptr<JsonValue> readJson();
294 
295 };
296 
297 }
298 #endif /* __CU_JSON_READER_H__ */
cugl::JsonReader
Definition: CUJsonReader.h:70
cugl::JsonReader::alloc
static std::shared_ptr< JsonReader > alloc(const Pathname &file, unsigned int capacity)
Definition: CUJsonReader.h:184
cugl::Pathname
Definition: CUPathname.h:85
cugl::JsonReader::alloc
static std::shared_ptr< JsonReader > alloc(const std::string &file)
Definition: CUJsonReader.h:90
cugl::JsonReader::alloc
static std::shared_ptr< JsonReader > alloc(const char *file)
Definition: CUJsonReader.h:110
cugl::JsonReader::readJsonString
std::string readJsonString()
cugl::JsonReader::allocWithAsset
static std::shared_ptr< JsonReader > allocWithAsset(const std::string &file)
Definition: CUJsonReader.h:203
cugl::JsonReader::allocWithAsset
static std::shared_ptr< JsonReader > allocWithAsset(const char *file)
Definition: CUJsonReader.h:222
cugl::JsonReader::alloc
static std::shared_ptr< JsonReader > alloc(const char *file, unsigned int capacity)
Definition: CUJsonReader.h:166
cugl::JsonReader::alloc
static std::shared_ptr< JsonReader > alloc(const std::string &file, unsigned int capacity)
Definition: CUJsonReader.h:148
cugl::JsonReader::alloc
static std::shared_ptr< JsonReader > alloc(const Pathname &file)
Definition: CUJsonReader.h:130
cugl::JsonReader::allocWithAsset
static std::shared_ptr< JsonReader > allocWithAsset(const char *file, unsigned int capacity)
Definition: CUJsonReader.h:258
cugl::JsonReader::allocWithAsset
static std::shared_ptr< JsonReader > allocWithAsset(const std::string &file, unsigned int capacity)
Definition: CUJsonReader.h:240
cugl::JsonReader::readJson
std::shared_ptr< JsonValue > readJson()
cugl::TextReader
Definition: CUTextReader.h:75