vitasdk
Documentation of the vitasdk
json.h
Go to the documentation of this file.
1 
9 #ifndef _PSP2_JSON_H_
10 #define _PSP2_JSON_H_
11 
12 #ifdef __cplusplus
13 
14 #include <psp2/types.h>
15 
19 typedef enum SceJsonErrorCode
20 {
28  SCE_JSON_ERROR_LOADED = 0x80920111,
46 
47 namespace sce
48 {
49 
50 namespace Json
51 {
52 
56 typedef enum class ValueType
57 {
58  NullValue = 0,
59  BoolValue = 1,
60  IntValue = 2,
61  UIntValue = 3,
62  RealValue = 4,
63  StringValue = 5,
64  ArrayValue = 6,
65  ObjectValue = 7
66 } ValueType;
67 
73 {
74 public:
76  virtual ~MemAllocator();
77 
86  virtual void* allocateMemory(SceSize size, void* userData) = 0;
93  virtual void freeMemory(void* ptr, void* userData) = 0;
94 
98  virtual void notifyError(int errorCode, SceSize size, void* userData);
99 };
100 
105 {
106 public:
115  void* userData;
124 };
125 
130 {
131 public:
134 
142  int initialize(const InitParameter* initParams);
148  int terminate();
149 };
150 
151 class Value;
155 class Array
156 {
157 public:
161  class iterator
162  {
163  public:
165  iterator(const iterator& iter);
167 
171  void advance(SceSize adv);
172 
176  iterator& operator=(const iterator& iter);
180  void operator++(int adv);
184  void operator++();
190  Value& operator*() const;
198  bool operator!=(iterator iter) const;
202  Value* operator->() const;
203  private:
204  void* implData;
205  };
206 
207  Array();
208  Array(const Array& arr);
210 
216  iterator begin() const;
222  iterator end() const;
231  iterator insert(const iterator& pos, const Value& val);
239  iterator erase(const iterator& pos);
245  Value& back() const;
246 
250  void clear();
254  void push_back(const Value& val);
258  void pop_back();
259 
265  SceSize size() const;
271  bool empty() const;
272 
276  Array& operator=(const Array& arr);
277 
278 private:
279  void* implData;
280 };
281 
285 class String
286 {
287 public:
289  String(const char* s);
290  String(const String& str);
292 
293  const static SceSize npos = -1;
294 
302  String& append(const char* s);
311  String& append(const char* s, SceSize pos);
319  String& append(const String& str);
320 
330  String substr(SceSize pos = 0, SceSize len = npos) const;
338  char at(SceSize pos) const;
339 
349  SceSize find(const char* s, SceSize pos) const;
360  SceSize find(const char* s, SceSize pos, SceSize n) const;
370  SceSize find(const String& str, SceSize pos) const;
380  SceSize find(char c, SceSize pos) const;
381 
391  SceSize rfind(const char* s, SceSize pos) const;
402  SceSize rfind(const char* s, SceSize pos, SceSize n) const;
412  SceSize rfind(const String& str, SceSize pos) const;
421  SceSize rfind(char c, SceSize pos) const;
422 
426  void clear();
432  bool empty() const;
438  SceSize size() const;
444  SceSize length() const;
450  const char* c_str() const;
454  void resize(SceSize n);
455 
461  bool compare(const char* s) const;
467  bool compare(const String& str) const;
468 
474  String& operator+=(const char* s);
480  String& operator+=(unsigned char c);
486  bool operator==(const char* s) const;
492  bool operator==(const String& str) const;
493 
497  String& operator=(const String& str);
498 
499 private:
500  void* implData;
501 };
502 
503 class Object;
507 class Value
508 {
509 public:
517  typedef int (*SerializeCallback)(String& str, void* userData);
529  typedef Value const&(*NullAccessCallback)(ValueType type, const Value* parent, void* userData);
530 
531  Value();
536  Value(double value);
537  Value(const String& value);
538  Value(const Array& value);
539  Value(const Object& value);
540  Value(const Value& value);
542 
548  void toString(String& s) const;
549 
555  void swap(Value& val);
559  void clear();
560 
566  SceSize count() const;
567 
574 
604  void set(double value);
610  void set(const String& value);
616  void set(const Array& value);
622  void set(const Object& value);
628  void set(const Value& value);
629 
639 
645  Value& getRoot() const;
646 
653  const SceBool& getBoolean() const;
660  const SceInt64& getInteger() const;
667  const SceUInt64& getUInteger() const;
674  const SceDouble& getReal() const;
683  const String& getString() const;
692  const Array& getArray() const;
701  const Object& getObject() const;
702 
712  const Value& getValue(SceSize pos) const;
723  const Value& getValue(const String& key) const;
724 
777 
795  Value* referValue(const String& key);
796 
804  int serialize(String& s);
816  int serialize(String& str, SerializeCallback cb, void* userData);
817 
823  const Value& operator[](const String& key) const;
829  const Value& operator[](const char* key) const;
835  const Value& operator[](SceSize pos) const;
836 
841 
842  operator bool() const;
843 
844 private:
847  union
848  {
856  } value;
857  char unused[4];
859 };
860 
864 class Object
865 {
866 public:
870  class Pair
871  {
872  public:
873  Pair();
874  Pair(const String& name, const Value& value);
875  ~Pair();
876 
878  char unused[4];
880  };
884  class iterator
885  {
886  public:
888  iterator(const iterator& iter);
890 
896  void advance(SceSize adv);
897 
903  iterator& operator++(int adv);
914  Pair& operator*() const;
920  bool operator==(iterator iter) const;
926  bool operator!=(iterator iter) const;
930  Pair* operator->() const;
931 
935  iterator& operator=(const iterator& iter);
936 
937  private:
938  void* implData;
939  };
940 
942  Object(const Object& obj);
944 
950  iterator begin() const;
956  iterator end() const;
964  iterator insert(const Pair& p);
972  iterator find(const String& key);
976  void clear();
982  void erase(const String& str);
983 
987  SceSize size() const;
993  bool empty() const;
994 
1000  Value& operator[](const String& str);
1001 
1005  Object& operator=(const Object& obj);
1006 
1007 private:
1008  void* implData;
1009 };
1010 
1014 class Parser
1015 {
1016 public:
1045  typedef int (*ParseCallback)(char& ch, void* userData);
1046 
1056  static int parse(Value& val, ParseCallback cb, void* userData);
1057 
1067  static int parse(Value& val, const char* buf, SceSize size);
1068 
1077  static int parse(Value& val, const char* path);
1078 };
1079 
1080 } /* namespace Json */
1081 } /* namespace sce */
1082 
1083 #endif /* __cplusplus */
1084 
1085 #endif /* _PSP2_JSON_H_ */
SceSize size
struct size(variable size)
Definition: debug.h:0
Class for JSON arrays.
Definition: json.h:156
Class for iterating over Array members.
Definition: json.h:162
Utility Data Structure to pass on initialisation parameters.
Definition: json.h:105
Utility class for initialising and terminating the library.
Definition: json.h:130
Base class for reimplementation of memory allocation.
Definition: json.h:73
Class for JSON objects.
Definition: json.h:865
Key-Value Pair of JSON Object property.
Definition: json.h:871
Class for iterating over Object members.
Definition: json.h:885
Static Class for parsing.
Definition: json.h:1015
Class for JSON strings.
Definition: json.h:286
Class for value of JSON property.
Definition: json.h:508
bool compare(const String &str) const
Compares the string to str.
iterator & operator++(int adv)
Increment the iterator.
Pair * operator->() const
Access the Pair the iterator is pointing to.
bool operator==(iterator iter) const
Compare to another iterator.
SceSize rfind(const String &str, SceSize pos) const
Searches the string for the last instance of a sequence.
Array * referArray()
Returns a pointer to the array.
bool operator==(const char *s) const
Compares the string to s.
SceInt64 integer
Definition: json.h:850
void advance(SceSize adv)
Advance the iterator by adv.
void erase(const String &str)
Remove the Pair with the key str.
SceBool boolean
Definition: json.h:849
static int parse(Value &val, const char *buf, SceSize size)
Parses a string buffer.
Value & operator=(const Value &value)
Assignment Operator.
ValueType type
The type of the value.
Definition: json.h:858
const Value & operator[](SceSize pos) const
Returns constant reference of the Value at pos.
void swap(Value &val)
Swaps the value with another Value.
Value * referValue(const String &key)
Returns a pointer to the value.
bool empty() const
Returns whether the string is empty or not.
void set(SceInt64 value)
Set the value to a long integer(64 bits)
Value const &(* NullAccessCallback)(ValueType type, const Value *parent, void *userData)
Callback for NullValue access.
Definition: json.h:529
void * implData
Pointer to internal implementation data.
Definition: json.h:500
iterator begin() const
Returns an iterator to the first child element.
void * implData
Pointer to internal implementation data.
Definition: json.h:938
iterator end() const
Get an iterator pointing to after the end of the Array.
SceSize count() const
Returns number of children.
void * implData
Pointer to internal implementation data.
Definition: json.h:279
char unused[4]
Unused.
Definition: json.h:878
Pair(const String &name, const Value &value)
Pair & operator*() const
Dereference the iterator.
void pop_back()
Removes the last child value.
int(* SerializeCallback)(String &str, void *userData)
Callback for serialization.
Definition: json.h:517
String(const char *s)
iterator insert(const iterator &pos, const Value &val)
Insert a Value at pos.
Value(double value)
String key
Name assigned to the property.
Definition: json.h:877
char unused[4]
Unused.
Definition: json.h:857
SceDouble real
Definition: json.h:852
Value(SceUInt64 value)
void * implData
Pointer to internal implementation data.
Definition: json.h:204
SceSize rfind(char c, SceSize pos) const
Searches the string for the last instance of a sequence.
iterator end() const
Returns an iterator to after the last child element.
Value(SceBool value)
iterator insert(const Pair &p)
Inserts a pair into the Object.
Value & operator*() const
Dereference the iterator.
String & operator+=(const char *s)
Appends s to the string.
Object(const Object &obj)
Value(const Value &value)
Array(const Array &arr)
iterator find(const String &key)
Finds a Pair with a matching key.
String * string
Definition: json.h:853
int(* ParseCallback)(char &ch, void *userData)
Callback for outputing a character to parse.
Definition: json.h:1045
void * userData
User defined data sent to overriden MemAllocator functions.
Definition: json.h:115
ValueType
Strongly typed Enumerator of different types of Values.
Definition: json.h:57
Value(ValueType type)
SceUInt64 uinteger
Definition: json.h:851
SceSize size() const
Get the size of the array.
Value(const String &value)
SceSize find(const char *s, SceSize pos, SceSize n) const
Searches the string for the first instance of a sequence.
Value & back() const
Returns a reference to the last element in the Array.
Array * array
Definition: json.h:854
iterator erase(const iterator &pos)
Erase the Value at pos.
void clear()
Clears the value by setting to null.
Value * parent
Pointer to the value's parent.
Definition: json.h:845
Value(const Object &value)
void set(ValueType type)
Set the type.
iterator(const iterator &iter)
iterator begin() const
Get an iterator pointing to the start of the Array.
bool operator!=(iterator iter) const
Compare to another iterator.
String & append(const String &str)
Append string str.
String * referString()
Returns a pointer to the string.
const Value & operator[](const char *key) const
Returns constant reference of the Value of key.
bool operator==(const String &str) const
Compares the string to str.
Value(SceInt64 value)
Object * referObject()
Returns a pointer to the object.
String(const String &str)
const SceBool & getBoolean() const
Returns a constant reference to the value.
SceSize find(char c, SceSize pos) const
Searches the string for the first instance of a sequence.
int setNullAccessCallback(NullAccessCallback cb, void *userData)
Set the Value's NullAccessCallback.
Array & operator=(const Array &arr)
Assignment Operator.
SceSize find(const String &str, SceSize pos) const
Searches the string for the first instance of a sequence.
void set(const String &value)
Set the value to a String.
void set(SceBool value)
Set the value to a boolean.
static const SceSize npos
Definition: json.h:293
String & append(const char *s, SceSize pos)
Append string s to position pos.
SceInt64 * referInteger()
Returns a pointer to the value.
String substr(SceSize pos=0, SceSize len=npos) const
Returns a newly constructed string object with its value initialized to a copy of a substring of this...
Value & operator[](const String &str)
Searches for a pair with a key matching str.
NullAccessCallback cb
The value's NullAccessCallback.
Definition: json.h:846
Value * operator->() const
Access the Value being pointed to.
MemAllocator * allocator
Pointer to a MemAllocator object for internal memory allocations.
Definition: json.h:111
void push_back(const Value &val)
Adds val to the end of the Array.
void advance(SceSize adv)
Advance the iterator.
void operator++()
Increment the iterator.
const String & getString() const
Returns a constant reference to the string.
void set(const Object &value)
Set the value to an Object.
void clear()
Empty the object.
const Object & getObject() const
Returns a constant reference to the object.
void * implData
Pointer to internal implementation data.
Definition: json.h:1008
int serialize(String &str, SerializeCallback cb, void *userData)
Serializes the values into a string in JSON format.
int terminate()
Terminate the library.
SceSize size() const
Returns the length of the String in bytes.
static int parse(Value &val, const char *path)
Parse a JSON file.
virtual void * allocateMemory(SceSize size, void *userData)=0
Virtual Function for memory allocation.
String & operator=(const String &str)
Assignment Operator.
virtual void freeMemory(void *ptr, void *userData)=0
Virtual Function for memory deallocation.
Object & operator=(const Object &obj)
Assignment Operator.
Value(const Array &value)
int initialize(const InitParameter *initParams)
Initialise the library.
iterator(const iterator &iter)
void set(double value)
Set the value to a double precision float.
bool empty() const
Is the array empty.
static int parse(Value &val, ParseCallback cb, void *userData)
Parses user defined data using characters from a user defined callback function.
const Value & operator[](const String &key) const
Returns constant reference of the Value of key.
bool operator!=(iterator iter) const
Compare to another iterator.
void clear()
Clears the string's contents.
SceSize length() const
Returns the length of the String in bytes.
String & operator+=(unsigned char c)
Appends c to the string.
const SceUInt64 & getUInteger() const
Returns a constant reference to the value.
SceDouble * referReal()
Returns a pointer to the value.
iterator & operator=(const iterator &iter)
Assignment operator.
bool empty() const
Is the object empty?
void toString(String &s) const
Sets s to be a string representation of the value.
ValueType getType() const
Returns the ValueType of the value.
const Value & getValue(const String &key) const
Returns a constant reference to the value.
virtual void notifyError(int errorCode, SceSize size, void *userData)
The base class definition prints an error to sceClibPrintf.
SceSize size() const
Gets the number of child Pairs.
Value value
Value assigned to the property.
Definition: json.h:879
void clear()
Will empty the Array, removing all child valus.
void operator++(int adv)
Advance the iterator by adv.
bool compare(const char *s) const
Compares the string to s.
const SceInt64 & getInteger() const
Returns a constant reference to the value.
String & append(const char *s)
Append string s to the end.
int serialize(String &s)
Serializes the data into a string in JSON format.
SceSize bufSize
Size of the buffer used for reading JSON file data in Parser::parse(Value&, const char*).
Definition: json.h:123
Value * referValue(SceSize pos)
Returns a pointer to the value.
char at(SceSize pos) const
Returns the character found at pos.
SceUInt64 * referUInteger()
Returns a pointer to the value.
union sce::Json::Value::@8 value
Union of different value types.
iterator & operator++()
Increment the iterator.
SceJsonErrorCode
Enumerator of different errors in this library.
Definition: json.h:20
const Array & getArray() const
Returns a constant reference to the array.
iterator & operator=(const iterator &iter)
Assignment Operator.
const SceDouble & getReal() const
Returns a constant reference to the value.
SceSize rfind(const char *s, SceSize pos) const
Searches the string for the last instance of a sequence.
void resize(SceSize n)
Resizes the string to a length of n characters.
SceSize find(const char *s, SceSize pos) const
Searches the string for the first instance of a sequence.
void set(const Value &value)
Set the value.
Value & getRoot() const
Returns a reference to the root Value.
SceSize rfind(const char *s, SceSize pos, SceSize n) const
Searches the string for the last instance of a sequence.
void set(const Array &value)
Set the value to an Array.
void set(SceUInt64 value)
Set the value to an unsigned long integer(64 bits)
SceBool * referBoolean()
Returns a pointer to the value.
Object * object
Definition: json.h:855
const char * c_str() const
Returns a C style string buffer of the String.
const Value & getValue(SceSize pos) const
Returns a constant reference to the value.
@ SCE_JSON_ERROR_LOADED
The module has already been initialised.
Definition: json.h:28
@ SCE_JSON_ERROR_MEM_ALLOC
Internal Memory Allocation failure(returned nullptr).
Definition: json.h:32
@ SCE_JSON_PARSER_ERROR_EMPTY_BUF
InitParameter.bufSize is 0.
Definition: json.h:36
@ SCE_JSON_PARSER_ERROR_INVALID_TOKEN
Invalid character in the parsed data.
Definition: json.h:44
@ SCE_JSON_PARSER_ERROR_FILE_LOAD
Parser failed to load file.
Definition: json.h:40
@ SCE_JSON_ERROR_UNLOADED
The module has not been initialised.
Definition: json.h:24
int SceBool
Definition: types.h:59
unsigned int SceSize
Definition: types.h:56
double SceDouble
Definition: types.h:68
int64_t SceInt64
Definition: types.h:50
uint64_t SceUInt64
Definition: types.h:51
Definition: json.h:48