diff -r 209d6fa3805c6cba93dbefa51a42f02fed6dd82a -r 3005c9d543da92f1428adc00606f5f6f6d4cdf3f include/ptypes.h --- a/include/ptypes.h Sat May 11 21:57:33 2013 -0500 +++ b/include/ptypes.h Sun May 12 16:17:02 2013 -0500 @@ -1053,6 +1053,10 @@ virtual ~evariant(); }; +/* +Added by Nathan Adams - the following is not apart of the original ptypes +*/ + enum { ARR_ASSOCIATIVE, ARR_LIST, @@ -1129,6 +1133,66 @@ inline bool islist(const tparray& t) { return t.tag == ARR_LIST; } inline bool isnull(const tparray& t) { return t.tag == ARR_NULL; } + +//http://stackoverflow.com/a/7936901/195722 +template class ByRef { +public: + ByRef() { + } + + ByRef(const T value) : mValue(value) { + } + + operator T&() const { + return((T&)mValue); + } + +private: + T mValue; +}; + + +class json +{ + + protected: + string ParseString(const string & json, int & index, bool & success); + large ParseNumber(const string & json, int & index, bool & success); +#ifndef USE_VARIANT + tparray * ParseObject(const string & json, int & index, bool & success); + tparray * ParseArray(const string & json, int & index, bool & success); +#else + variant ParseObject(const string & json, int & index, bool & success); + variant ParseArray(const string & json, int & index, bool & success); +#endif + int GetLastIndexOfNumber(const string & json, int index); + variant ParseValue(const string & json, int & index, bool & success); + int LookAhead(const string & json, int index); + void EatWhiteSpace(const string & json, int & index); + int NextToken(const string & json, int & index); + bool SerializeValue(const variant & value, string & builder); + bool SerializeString(const string & aString, string & builder); + bool SerializeObject(const tparray & anObject, string & builder); + bool SerializeArray(const tparray & anArray, string & builder); + bool SerializeNumber(large number, string & builder); + public: + enum Token { TOKEN_NONE, + TOKEN_CURLY_OPEN, + TOKEN_CURLY_CLOSE, + TOKEN_SQUARED_OPEN, + TOKEN_SQUARED_CLOSE, + TOKEN_COLON, + TOKEN_COMMA, + TOKEN_STRING, + TOKEN_NUMBER, + TOKEN_TRUE, + TOKEN_FALSE, + TOKEN_NULL }; + variant JsonDecode(const string & json, bool & success = ByRef(true)); + string JsonEncode(const variant & v); + +}; + // -------------------------------------------------------------------- // // --- pre-2.0 compatibility declarations ----------------------------- // // -------------------------------------------------------------------- // diff -r 209d6fa3805c6cba93dbefa51a42f02fed6dd82a -r 3005c9d543da92f1428adc00606f5f6f6d4cdf3f src/ptparray.cxx --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ptparray.cxx Sun May 12 16:17:02 2013 -0500 @@ -0,0 +1,98 @@ +#include +#include +#include + +#include "ptypes.h" +#include "pstreams.h" + + +PTYPES_BEGIN + +bool ptdecl anext(const tparray& a, int& i, variant& val) +{ + //if (a._vals.size() == i) + //if (a._vals.get_count() == i) + if (alength(a._vals) == i) + { + return false; + } + else + { + val = a._vals[(large)i]; + i++; + return true; + } +} + +bool ptdecl anext(const tparray& a, int& i, variant& val, variant & key) +{ + //if (a._keys.size() == i) + //if (a._keys.get_count() == i) + if (alength(a._keys) == i) + { + return false; + } + else + { + val = a._vals[(large)i]; + key = a._keys[(large)i]; + i++; + return true; + } +} + +void ptdecl add(tparray & t, const variant & val) +{ + if (t.tag == ARR_NULL || t.tag == ARR_LIST) + { + t.tag = ARR_LIST; + //pout.put("here1"); + //t._vals.push_back(val); + add(t._vals, val); + //t._vals.add(val); + //pout.put("here2"); + } + + //else drop it +} + +void ptdecl add(tparray & t, const variant & key, const variant & val) +{ + if (t.tag == ARR_NULL || t.tag == ARR_ASSOCIATIVE) + { + t.tag = ARR_ASSOCIATIVE; + add(t._vals, val); + add(t._keys, key); + //t._vals.add(val); + //t._keys.add(key); + //t._vals.push_back(val); + //t._keys.push_back(key); + + } +} + +variant ptdecl get(tparray & t, const variant & val) +{ + //for(int i = 0; i < t._keys.size(); i++) + //for(int i = 0; i < t._keys.get_count(); i++) + for(int i = 0; i < alength(t._keys); i++) + { + if (t._keys[(large)i] == val) + { + return t._vals[(large)i]; + } + } + return NULL; +} + +variant ptdecl at(tparray & t, large index) +{ + return t._vals[index]; +} + +variant ptdecl keyat(tparray & t, large index) +{ + return t._keys[index]; +} + +PTYPES_END \ No newline at end of file