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
46
47namespace sce
48{
49
50namespace Json
51{
52
56typedef 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{
74public:
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
125
130{
131public:
134
142 int initialize(const InitParameter* initParams);
149};
150
151class Value;
155class Array
156{
157public:
162 {
163 public:
165 iterator(const iterator& iter);
167
171 void advance(SceSize adv);
172
180 void operator++(int adv);
190 Value& operator*() const;
198 bool operator!=(iterator iter) const;
203 private:
204 void* implData;
205 };
206
208 Array(const Array& arr);
210
222 iterator end() const;
231 iterator insert(const iterator& pos, const Value& val);
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
278private:
279 void* implData;
280};
281
286{
287public:
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;
450 const char* c_str() const;
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
499private:
500 void* implData;
501};
502
503class Object;
507class Value
508{
509public:
517 typedef int (*SerializeCallback)(String& str, void* userData);
529 typedef Value const&(*NullAccessCallback)(ValueType type, const Value* parent, void* userData);
530
536 Value(double 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
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
844private:
847 union
848 {
857 char unused[4];
859};
860
865{
866public:
870 class Pair
871 {
872 public:
874 Pair(const String& name, const Value& value);
876
878 char unused[4];
880 };
885 {
886 public:
888 iterator(const iterator& iter);
890
896 void advance(SceSize adv);
897
914 Pair& operator*() const;
920 bool operator==(iterator iter) const;
926 bool operator!=(iterator iter) const;
930 Pair* operator->() const;
931
936
937 private:
938 void* implData;
939 };
940
942 Object(const Object& obj);
944
956 iterator end() const;
972 iterator find(const String& key) const;
976 void clear();
982 void erase(const String& str);
983
987 SceSize size() const;
993 bool empty() const;
994
1001
1006
1007private:
1008 void* implData;
1009};
1010
1015{
1016public:
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.
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.
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.
Value & back() const
Returns a reference to the last element in the Array.
const Object & getObject() const
Returns a constant reference to the object.
SceBool boolean
Definition json.h:849
static int parse(Value &val, const char *buf, SceSize size)
Parses a string buffer.
ValueType type
The type of the value.
Definition json.h:858
SceInt64 * referInteger()
Returns a pointer to the value.
void swap(Value &val)
Swaps the value with another Value.
Pair & operator*() const
Dereference the iterator.
bool empty() const
Returns whether the string is empty or not.
SceDouble * referReal()
Returns a pointer to the value.
void set(SceInt64 value)
Set the value to a long integer(64 bits)
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 & operator=(const iterator &iter)
Assignment Operator.
iterator end() const
Get an iterator pointing to after the end of the Array.
SceSize count() const
Returns number of children.
const Array & getArray() const
Returns a constant reference to the array.
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)
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)
iterator & operator=(const iterator &iter)
Assignment operator.
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
const SceDouble & getReal() const
Returns a constant reference to the value.
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.
String & append(const String &str)
Append string str.
const SceUInt64 & getUInteger() const
Returns a constant reference to the value.
Value & operator*() const
Dereference the iterator.
Object(const Object &obj)
Value(const Value &value)
const Value & getValue(SceSize pos) const
Returns a constant reference to the value.
Array(const Array &arr)
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)
const SceInt64 & getInteger() const
Returns a constant reference to the value.
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.
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
const SceBool & getBoolean() const
Returns a constant reference to the value.
Value(const Object &value)
const Value & operator[](SceSize pos) const
Returns constant reference of the Value at pos.
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.
bool operator==(const String &str) const
Compares the string to str.
Value(SceInt64 value)
const Value & operator[](const String &key) const
Returns constant reference of the Value of key.
String(const String &str)
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.
const Value & operator[](const char *key) const
Returns constant reference of the Value of key.
Value * referValue(SceSize pos)
Returns a pointer to the value.
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 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...
SceBool * referBoolean()
Returns a pointer to the value.
NullAccessCallback cb
The value's NullAccessCallback.
Definition json.h:846
SceUInt64 * referUInteger()
Returns a pointer to the value.
const String & getString() const
Returns a constant reference to the string.
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.
String & operator+=(const char *s)
Appends s to the string.
void set(const Object &value)
Set the value to an Object.
void clear()
Empty 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.
Value & operator[](const String &str)
Searches for a pair with a key matching str.
SceSize size() const
Returns the length of the String in bytes.
static int parse(Value &val, const char *path)
Parse a JSON file.
Value * referValue(const String &key)
Returns a pointer to the value.
virtual void freeMemory(void *ptr, void *userData)=0
Virtual Function for memory deallocation.
Value(const Array &value)
int initialize(const InitParameter *initParams)
Initialise the library.
iterator(const iterator &iter)
iterator & operator++(int adv)
Increment the iterator.
Value & getRoot() const
Returns a reference to the root Value.
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.
Array & operator=(const Array &arr)
Assignment Operator.
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 & append(const char *s, SceSize pos)
Append string s to position pos.
bool empty() const
Is the object empty?
void toString(String &s) const
Sets s to be a string representation of the value.
Pair * operator->() const
Access the Pair the iterator is pointing to.
String * referString()
Returns a pointer to the string.
ValueType getType() const
Returns the ValueType of the value.
virtual void notifyError(int errorCode, SceSize size, void *userData)
The base class definition prints an error to sceClibPrintf.
Object * referObject()
Returns a pointer to the object.
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.
Value & operator=(const Value &value)
Assignment Operator.
void operator++(int adv)
Advance the iterator by adv.
bool compare(const char *s) const
Compares the string to s.
const char * c_str() const
Returns a C style string buffer of the String.
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
iterator & operator++()
Increment the iterator.
virtual void * allocateMemory(SceSize size, void *userData)=0
Virtual Function for memory allocation.
char at(SceSize pos) const
Returns the character found at pos.
Value * operator->() const
Access the Value being pointed to.
const Value & getValue(const String &key) const
Returns a constant reference to the value.
union sce::Json::Value::@8 value
Union of different value types.
Value const &(* NullAccessCallback)(ValueType type, const Value *parent, void *userData)
Callback for NullValue access.
Definition json.h:529
SceJsonErrorCode
Enumerator of different errors in this library.
Definition json.h:20
Object & operator=(const Object &obj)
Assignment Operator.
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.
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)
String & append(const char *s)
Append string s to the end.
String & operator=(const String &str)
Assignment Operator.
iterator find(const String &key) const
Finds a Pair with a matching key.
Object * object
Definition json.h:855
String & operator+=(unsigned char c)
Appends c to the string.
Array * referArray()
Returns a pointer to the array.
@ 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