diff --git a/README.md b/README.md index 2e718a2..9c7615a 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,8 @@ To render the object back to a string you can simply call ```c char *json_String=aJson.print(jsonObject); + Serial.println(aJsonPtr); + free(aJsonPtr); // To free memory allocated by aJSON.print() ``` Finished? Delete the root (this takes care of everything else). diff --git a/aJSON.cpp b/aJSON.cpp index 1bb9320..fb88732 100644 --- a/aJSON.cpp +++ b/aJSON.cpp @@ -38,6 +38,8 @@ #include #ifdef __AVR__ #include +#elif defined(__arm__) +#include #else #include #endif @@ -306,6 +308,17 @@ aJsonStream::printInt(aJsonObject *item) return 0; } +int +aJsonStream::printLong(aJsonObject *item) +{ + if (item != NULL) + { + return this->print(item->valuelong, DEC); + } + //printing nothing is ok + return 0; +} + int aJsonStream::printFloat(aJsonObject *item) { @@ -510,7 +523,7 @@ aJsonStream::skip() // Utility to flush our buffer in case it contains garbage // since the parser will return the buffer untouched if it // cannot understand it. -int +void aJsonStream::flush() { int in = this->getch(); @@ -518,7 +531,7 @@ aJsonStream::flush() { in = this->getch(); } - return EOF; + //return EOF; } @@ -692,6 +705,9 @@ aJsonStream::printValue(aJsonObject *item) case aJson_Int: result = this->printInt(item); break; + case aJson_Long: + result = this->printLong(item); + break; case aJson_Float: result = this->printFloat(item); break; @@ -1131,6 +1147,19 @@ aJsonClass::createItem(int num) return item; } +aJsonObject* +aJsonClass::createItem(unsigned long num) +{ + aJsonObject *item = newItem(); + if (item) + { + item->type = aJson_Long; + item->valueint = (unsigned long)num; + } + return item; +} + + aJsonObject* aJsonClass::createItem(double num) { @@ -1259,6 +1288,11 @@ aJsonClass::addNumberToObject(aJsonObject* object, const char* name, int n) addItemToObject(object, name, createItem(n)); } +void +aJsonClass::addNumberToObject(aJsonObject* object, const char* name, unsigned long n) +{ + addItemToObject(object, name, createItem(n)); +} void aJsonClass::addNumberToObject(aJsonObject* object, const char* name, double n) { diff --git a/aJSON.h b/aJSON.h index a409a92..5dab6f6 100644 --- a/aJSON.h +++ b/aJSON.h @@ -37,10 +37,11 @@ #define aJson_NULL 0 #define aJson_Boolean 1 #define aJson_Int 2 -#define aJson_Float 3 -#define aJson_String 4 -#define aJson_Array 5 -#define aJson_Object 6 +#define aJson_Long 3 +#define aJson_Float 4 +#define aJson_String 5 +#define aJson_Array 6 +#define aJson_Object 7 #define aJson_IsReference 128 @@ -62,6 +63,7 @@ typedef struct aJsonObject { char *valuestring; // The item's string, if type==aJson_String char valuebool; //the items value for true & false int valueint; // The item's value, if type==aJson_Int + long valuelong; // The item's value, if type==aJson_Int double valuefloat; // The item's value, if type==aJson_Float }; } aJsonObject; @@ -82,6 +84,7 @@ class aJsonStream : public Print { int parseNumber(aJsonObject *item); int printInt(aJsonObject *item); + int printLong(aJsonObject *item); int printFloat(aJsonObject *item); int parseString(aJsonObject *item); @@ -89,7 +92,8 @@ class aJsonStream : public Print { int printString(aJsonObject *item); int skip(); - int flush(); + void flush(); + //virtual void flush(); int parseValue(aJsonObject *item, char** filter); int printValue(aJsonObject *item); @@ -200,6 +204,7 @@ class aJsonClass { aJsonObject* createItem(bool b); aJsonObject* createItem(char b); aJsonObject* createItem(int num); + aJsonObject* createItem(unsigned long num); aJsonObject* createItem(double num); aJsonObject* createItem(const char *string); aJsonObject* createArray(); @@ -235,7 +240,8 @@ class aJsonClass { void addNullToObject(aJsonObject* object, const char* name); void addBooleanToObject(aJsonObject* object, const char* name, bool b); void addNumberToObject(aJsonObject* object, const char* name, int n); - void addNumberToObject(aJsonObject* object, const char* name, double n); + void addNumberToObject(aJsonObject* object, const char* name, unsigned long n); + void addNumberToObject(aJsonObject* object, const char* name, double n); void addStringToObject(aJsonObject* object, const char* name, const char* s); diff --git a/sam/pgmspace.h b/sam/pgmspace.h new file mode 100644 index 0000000..fbf7027 --- /dev/null +++ b/sam/pgmspace.h @@ -0,0 +1,38 @@ +#ifndef __PGMSPACE_H_ +#define __PGMSPACE_H_ 1 + +#include + +#define PROGMEM +#define PGM_P const char * +#define PSTR(str) (str) + +typedef void prog_void; +typedef char prog_char; +typedef unsigned char prog_uchar; +typedef int8_t prog_int8_t; +typedef uint8_t prog_uint8_t; +typedef int16_t prog_int16_t; +typedef uint16_t prog_uint16_t; +typedef int32_t prog_int32_t; +typedef uint32_t prog_uint32_t; + +#define strcpy_P(dest, src) strcpy((dest), (src)) +#define strcat_P(dest, src) strcat((dest), (src)) +#define strcmp_P(a, b) strcmp((a), (b)) + +#define pgm_read_byte(addr) (*(const unsigned char *)(addr)) +#define pgm_read_word(addr) (*(const unsigned short *)(addr)) +#define pgm_read_dword(addr) (*(const unsigned long *)(addr)) +#define pgm_read_float(addr) (*(const float *)(addr)) + +#define pgm_read_byte_near(addr) pgm_read_byte(addr) +#define pgm_read_word_near(addr) pgm_read_word(addr) +#define pgm_read_dword_near(addr) pgm_read_dword(addr) +#define pgm_read_float_near(addr) pgm_read_float(addr) +#define pgm_read_byte_far(addr) pgm_read_byte(addr) +#define pgm_read_word_far(addr) pgm_read_word(addr) +#define pgm_read_dword_far(addr) pgm_read_dword(addr) +#define pgm_read_float_far(addr) pgm_read_float(addr) + +#endif \ No newline at end of file