From 9bdd6e55bce62929e42f42c998a4ac368d9f373a Mon Sep 17 00:00:00 2001 From: anklimov Date: Wed, 5 Oct 2016 21:19:32 +0300 Subject: [PATCH 01/28] Adding the new class --- aJSON.cpp | 18 ++++++++++++++++++ aJSON.h | 14 ++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/aJSON.cpp b/aJSON.cpp index 1bb9320..eadac28 100644 --- a/aJSON.cpp +++ b/aJSON.cpp @@ -43,6 +43,7 @@ #endif #include "aJSON.h" #include "utility/stringbuffer.h" +#include /****************************************************************************** * Definitions @@ -81,6 +82,7 @@ aJsonStream::getch() bucket = EOF; return ret; } + // In case input was malformed - can happen, this is the // real world, we can end up in a situation where the parser // would expect another character and end up stuck on @@ -96,6 +98,22 @@ aJsonStream::ungetch(char ch) bucket = ch; } + +int +aJsonFileStream::getch() +{ + if (bucket != EOF) + { + int ret = bucket; + bucket = EOF; + return ret; + } + return fgetc(fl); +} + + + + size_t aJsonStream::write(uint8_t ch) { diff --git a/aJSON.h b/aJSON.h index a409a92..dc4063b 100644 --- a/aJSON.h +++ b/aJSON.h @@ -167,6 +167,20 @@ class aJsonStringStream : public aJsonStream { size_t inbuf_len, outbuf_len; }; +class aJsonFileStream : public aJsonStream { +public: + aJsonFileStream(FILE* _fl) + : aJsonStream(NULL) + { + fl=_fl; + } + + +private: + virtual int getch(); + FILE* fl; +}; + class aJsonClass { /****************************************************************************** * Constructors From cbaa8498abb59790c6c4eb292203de163acfe154 Mon Sep 17 00:00:00 2001 From: Andrey Klimov Date: Fri, 7 Oct 2016 21:40:10 +0300 Subject: [PATCH 02/28] Adding EEPROM Stream class void loadConfig (char* tokens) { Serial.println("loading Config"); aJsonEEPROMStream as=aJsonEEPROMStream(0); aJson.deleteItem(root); root = aJson.parse(&as); Serial.println(); if (!root) { Serial.println("load failed"); return; } Serial.println("Loaded"); items = aJson.getObjectItem(root,"items"); dmxArr= aJson.getObjectItem(root, "dmxin"); } void saveConfig(char* tokens) { aJsonEEPROMStream es=aJsonEEPROMStream(0); Serial.println("Saving config.."); aJson.print(root,&es); es.putEOF(); Serial.println("Saved"); } --- aJSON.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++++--- aJSON.h | 24 ++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/aJSON.cpp b/aJSON.cpp index eadac28..529db15 100644 --- a/aJSON.cpp +++ b/aJSON.cpp @@ -44,7 +44,7 @@ #include "aJSON.h" #include "utility/stringbuffer.h" #include - +#include /****************************************************************************** * Definitions ******************************************************************************/ @@ -112,11 +112,54 @@ aJsonFileStream::getch() } +int +aJsonEEPROMStream::getch() +{ char c; + if (bucket != EOF) + { + int ret = bucket; + bucket = EOF; + return ret; + } + EEPROM.get(addr+offset++,c); + return c; +} + +bool +aJsonEEPROMStream::available() +{ + if (bucket != EOF) + return true; + while (addr+offsetgetch(); + + if (ch > 32) + { + this->ungetch(ch); + return true; + } + } + return false; + } + + +size_t +aJsonEEPROMStream::write(uint8_t ch) +{ + if (addr+offset>=EEPROM.length()) + { + return 0; + } +EEPROM.update(addr+offset++,(char)ch); + return 1; +} size_t aJsonStream::write(uint8_t ch) -{ +{ return stream()->write(ch); } @@ -192,7 +235,6 @@ aJsonStringStream::write(uint8_t ch) return 1; } - // Internal constructor. aJsonObject* aJsonClass::newItem() diff --git a/aJSON.h b/aJSON.h index dc4063b..7c4e074 100644 --- a/aJSON.h +++ b/aJSON.h @@ -181,6 +181,30 @@ class aJsonFileStream : public aJsonStream { FILE* fl; }; +class aJsonEEPROMStream : public aJsonStream { +public: + aJsonEEPROMStream(int _addr) + : aJsonStream(NULL) + { + addr=_addr; + offset=0; + } + + int putEOF(void) + { + return write(EOF); + } + virtual bool available(); + +private: + virtual int getch(); + virtual size_t write(uint8_t ch); + + int addr; + int offset; +}; + + class aJsonClass { /****************************************************************************** * Constructors From cc5d7c9b26c558e133dbf3646f16a75b8b1b3501 Mon Sep 17 00:00:00 2001 From: Andrey Klimov Date: Mon, 7 Aug 2017 09:15:00 +0300 Subject: [PATCH 03/28] Update README.md --- README.md | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/README.md b/README.md index 2e718a2..5c7b562 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ Copyright (c) 2010, Interactive Matter, Marcus Nowotny Based on the cJSON Library, Copyright (C) 2009 Dave Gamble + Updated by anklimov - changelog on the bottom of readme Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -295,3 +296,61 @@ As soon as you call aJson.print(), it renders the structure to text. Have Fun! + +New in this fork: +adding class "aJsonFileStream" inherited from aJsonStream in order to backward- compatibility with HTTPClient library +Now is possible to operate directly with *FILE like streams, returned by HttpClient library and avoid intermediate buffering + +Code example: + + +int getConfig() +{ + FILE* result; + int returnCode ; + + HTTPClient hclient("192.168.88.2",hserver,80); + result = hclient.getURI( FEED_URI); + returnCode = hclient.getLastReturnCode(); + + if (result!=NULL) { + if (returnCode==200) { + + Serial.println("got Config :"); + aJsonFileStream as=aJsonFileStream(result); + root = aJson.parse(&as); + + hclient.closeStream(result); // this is very important -- be sure to close the STREAM + + if (!root) + { + Serial.println("parseObject() failed"); + return -11; + } else + + { + + char * outstr=aJson.print(root); + Serial.println(outstr); + items = aJson.getObjectItem(root,"items"); + } + + } + else { + Serial.print("ERROR: Server returned "); + Serial.println(returnCode); + return -11; + + } + + } + else { + Serial.println("failed to connect"); + Serial.println(" try again in 5 seconds"); + return -11; + } + + + + return 2; +} From 8ced52a8ccce668288bb9c1699e205571188d54d Mon Sep 17 00:00:00 2001 From: Andrey Klimov Date: Mon, 7 Aug 2017 09:17:00 +0300 Subject: [PATCH 04/28] Update README.md --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index 5c7b562..9921a7d 100644 --- a/README.md +++ b/README.md @@ -349,8 +349,5 @@ int getConfig() Serial.println(" try again in 5 seconds"); return -11; } - - - return 2; } From fb8f10cdad8195cb8856eeeee9cafded74230a13 Mon Sep 17 00:00:00 2001 From: Andrey Klimov Date: Sun, 29 Oct 2017 14:55:27 +0300 Subject: [PATCH 05/28] Update aJSON.cpp Float precession decreased to conserve string memory usage --- aJSON.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aJSON.cpp b/aJSON.cpp index 529db15..0b1700b 100644 --- a/aJSON.cpp +++ b/aJSON.cpp @@ -52,7 +52,7 @@ #define BUFFER_DEFAULT_SIZE 4 //how much digits after . for float -#define FLOAT_PRECISION 5 +#define FLOAT_PRECISION 1 bool From b834e6bb1521c8b7ffc6d2e18f9024906af0fe4a Mon Sep 17 00:00:00 2001 From: Andrey Klimov Date: Fri, 17 Nov 2017 01:43:11 +0300 Subject: [PATCH 06/28] flush method changed in order of DUE libs compatibility --- aJSON.cpp | 26 ++++++++++++++++++++------ aJSON.h | 6 +++--- 2 files changed, 23 insertions(+), 9 deletions(-) mode change 100644 => 100755 aJSON.cpp mode change 100644 => 100755 aJSON.h diff --git a/aJSON.cpp b/aJSON.cpp old mode 100644 new mode 100755 index 0b1700b..c7644a1 --- a/aJSON.cpp +++ b/aJSON.cpp @@ -39,12 +39,17 @@ #ifdef __AVR__ #include #else -#include +//#include #endif #include "aJSON.h" #include "utility/stringbuffer.h" #include +#if defined(__SAM3X8E__) +#include +DueFlashStorage EEPROM; +#else #include +#endif /****************************************************************************** * Definitions ******************************************************************************/ @@ -121,7 +126,7 @@ aJsonEEPROMStream::getch() bucket = EOF; return ret; } - EEPROM.get(addr+offset++,c); + c=EEPROM.read(addr+offset++); return c; } @@ -130,7 +135,11 @@ aJsonEEPROMStream::available() { if (bucket != EOF) return true; +#if defined(__SAM3X8E__) + while (1) ///fix it +#else while (addr+offsetgetch(); @@ -148,11 +157,16 @@ aJsonEEPROMStream::available() size_t aJsonEEPROMStream::write(uint8_t ch) { - if (addr+offset>=EEPROM.length()) +#if defined(__SAM3X8E__) + while (1) ///fix it +#else + while (addr+offsetgetch(); @@ -578,7 +592,7 @@ aJsonStream::flush() { in = this->getch(); } - return EOF; + return;// EOF; } diff --git a/aJSON.h b/aJSON.h old mode 100644 new mode 100755 index 7c4e074..1004926 --- a/aJSON.h +++ b/aJSON.h @@ -61,8 +61,8 @@ typedef struct aJsonObject { union { 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 - double valuefloat; // The item's value, if type==aJson_Float + long int valueint; // The item's value, if type==aJson_Int + float valuefloat; // The item's value, if type==aJson_Float }; } aJsonObject; @@ -89,7 +89,7 @@ class aJsonStream : public Print { int printString(aJsonObject *item); int skip(); - int flush(); + void flush(); int parseValue(aJsonObject *item, char** filter); int printValue(aJsonObject *item); From badb84fa8daf9c9c2819087cc152de23fc3272b7 Mon Sep 17 00:00:00 2001 From: Andrey Klimov Date: Fri, 17 Nov 2017 01:56:44 +0300 Subject: [PATCH 07/28] some ARM compatibility added --- sam/pgmspace.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 sam/pgmspace.h diff --git a/sam/pgmspace.h b/sam/pgmspace.h new file mode 100644 index 0000000..b4e8df0 --- /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 From 60e5d57998b8a74b0d6f57336c7c060661bc42b8 Mon Sep 17 00:00:00 2001 From: Andrey Klimov Date: Mon, 15 Jan 2018 21:30:58 +0300 Subject: [PATCH 08/28] Ported to ARM, SAM Flash write issue fixed --- aJSON.cpp | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/aJSON.cpp b/aJSON.cpp index c7644a1..db930c6 100755 --- a/aJSON.cpp +++ b/aJSON.cpp @@ -36,17 +36,19 @@ #include #include #include -#ifdef __AVR__ +#ifdef __AVR__ #include +#elif defined(__arm__) +#include #else -//#include +#include #endif #include "aJSON.h" #include "utility/stringbuffer.h" #include #if defined(__SAM3X8E__) #include -DueFlashStorage EEPROM; +extern DueFlashStorage EEPROM; #else #include #endif @@ -133,16 +135,19 @@ aJsonEEPROMStream::getch() bool aJsonEEPROMStream::available() { + int ch =0; if (bucket != EOF) return true; + #if defined(__SAM3X8E__) - while (1) ///fix it + while ((ch!=EOF) && (offset<32000)) ///fix it #else while (addr+offsetgetch(); + ch = this->getch(); if (ch > 32) { @@ -157,17 +162,8 @@ aJsonEEPROMStream::available() size_t aJsonEEPROMStream::write(uint8_t ch) { -#if defined(__SAM3X8E__) - while (1) ///fix it -#else - while (addr+offset Date: Mon, 5 Nov 2018 18:19:45 +0300 Subject: [PATCH 09/28] fix compilation error on STM32F1 arch --- aJSON.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aJSON.cpp b/aJSON.cpp index db930c6..140442f 100755 --- a/aJSON.cpp +++ b/aJSON.cpp @@ -139,7 +139,7 @@ aJsonEEPROMStream::available() if (bucket != EOF) return true; -#if defined(__SAM3X8E__) +#if defined(__SAM3X8E__) or defined(ARDUINO_ARCH_STM32F1) while ((ch!=EOF) && (offset<32000)) ///fix it #else while (addr+offset Date: Sun, 3 Feb 2019 03:39:30 +0300 Subject: [PATCH 10/28] NRF --- aJSON.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/aJSON.cpp b/aJSON.cpp index 140442f..721765a 100755 --- a/aJSON.cpp +++ b/aJSON.cpp @@ -49,6 +49,9 @@ #if defined(__SAM3X8E__) #include extern DueFlashStorage EEPROM; +#elif defined(NRF5) +#include +extern NRFFlashStorage EEPROM; #else #include #endif From d31073d1aaecc04dd682bb1390368f4e76bbb1df Mon Sep 17 00:00:00 2001 From: Andrey Klimov Date: Sun, 3 Feb 2019 04:16:23 +0300 Subject: [PATCH 11/28] NRF --- aJSON.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aJSON.cpp b/aJSON.cpp index 721765a..8c8c0f7 100755 --- a/aJSON.cpp +++ b/aJSON.cpp @@ -142,7 +142,7 @@ aJsonEEPROMStream::available() if (bucket != EOF) return true; -#if defined(__SAM3X8E__) or defined(ARDUINO_ARCH_STM32F1) +#if defined(__SAM3X8E__) or defined(ARDUINO_ARCH_STM32F1) or defined (NRF5) while ((ch!=EOF) && (offset<32000)) ///fix it #else while (addr+offset Date: Sat, 23 Mar 2019 19:35:18 +0300 Subject: [PATCH 12/28] EEPROM stream for ESP8266 added --- aJSON.cpp | 17 +++++++++++++++-- aJSON.h | 5 +---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/aJSON.cpp b/aJSON.cpp index 8c8c0f7..f00fb24 100755 --- a/aJSON.cpp +++ b/aJSON.cpp @@ -49,9 +49,11 @@ #if defined(__SAM3X8E__) #include extern DueFlashStorage EEPROM; -#elif defined(NRF5) +#elif defined(NRF5) || defined (ARDUINO_ARCH_ESP32) #include extern NRFFlashStorage EEPROM; +#elif defined(ARDUINO_ARCH_ESP8266) +#include #else #include #endif @@ -142,7 +144,7 @@ aJsonEEPROMStream::available() if (bucket != EOF) return true; -#if defined(__SAM3X8E__) or defined(ARDUINO_ARCH_STM32F1) or defined (NRF5) +#if defined(__SAM3X8E__) or defined(ARDUINO_ARCH_STM32F1) or defined (NRF5) or defined (ARDUINO_ARCH_ESP32) while ((ch!=EOF) && (offset<32000)) ///fix it #else while (addr+offset Date: Sat, 23 Mar 2019 21:53:13 +0300 Subject: [PATCH 13/28] EEPROM for STM32 added --- aJSON.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aJSON.cpp b/aJSON.cpp index f00fb24..2f09beb 100755 --- a/aJSON.cpp +++ b/aJSON.cpp @@ -49,7 +49,7 @@ #if defined(__SAM3X8E__) #include extern DueFlashStorage EEPROM; -#elif defined(NRF5) || defined (ARDUINO_ARCH_ESP32) +#elif defined(NRF5) || defined (ARDUINO_ARCH_ESP32) || defined (ARDUINO_ARCH_STM32) #include extern NRFFlashStorage EEPROM; #elif defined(ARDUINO_ARCH_ESP8266) @@ -144,7 +144,7 @@ aJsonEEPROMStream::available() if (bucket != EOF) return true; -#if defined(__SAM3X8E__) or defined(ARDUINO_ARCH_STM32F1) or defined (NRF5) or defined (ARDUINO_ARCH_ESP32) +#if defined(__SAM3X8E__) or defined(ARDUINO_ARCH_STM32F1) or defined (NRF5) or defined (ARDUINO_ARCH_ESP32) or defined (ARDUINO_ARCH_STM32) while ((ch!=EOF) && (offset<32000)) ///fix it #else while (addr+offset Date: Sun, 24 Mar 2019 02:57:19 +0300 Subject: [PATCH 14/28] EEPROM for ESP8266 fix --- aJSON.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/aJSON.cpp b/aJSON.cpp index 2f09beb..369f812 100755 --- a/aJSON.cpp +++ b/aJSON.cpp @@ -144,7 +144,7 @@ aJsonEEPROMStream::available() if (bucket != EOF) return true; -#if defined(__SAM3X8E__) or defined(ARDUINO_ARCH_STM32F1) or defined (NRF5) or defined (ARDUINO_ARCH_ESP32) or defined (ARDUINO_ARCH_STM32) +#if defined(__SAM3X8E__) or defined(ARDUINO_ARCH_STM32) or defined (NRF5) or defined (ARDUINO_ARCH_ESP32) or defined (ARDUINO_ARCH_STM32) while ((ch!=EOF) && (offset<32000)) ///fix it #else while (addr+offset Date: Sun, 17 May 2020 22:31:16 +0300 Subject: [PATCH 15/28] added subtype field (4 bits) to aJSON Object --- aJSON.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/aJSON.h b/aJSON.h index 5b3673c..b3ecb76 100755 --- a/aJSON.h +++ b/aJSON.h @@ -42,7 +42,8 @@ #define aJson_Array 5 #define aJson_Object 6 -#define aJson_IsReference 128 +////#define aJson_IsReference 128 +#define aJson_IsReference 8 #ifndef EOF #define EOF -1 @@ -56,8 +57,8 @@ typedef struct aJsonObject { struct aJsonObject *next, *prev; // next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem struct aJsonObject *child; // An array or object item will have a child pointer pointing to a chain of the items in the array/object. - char type; // The type of the item, as above. - + char type:4; // The type of the item, as above. + char subtype:4; // User defined field union { char *valuestring; // The item's string, if type==aJson_String char valuebool; //the items value for true & false From 8aa73b1c4fb05562d6844e514a04c3b8b6603d07 Mon Sep 17 00:00:00 2001 From: Andrey Klimov Date: Sun, 29 Nov 2020 22:36:59 +0300 Subject: [PATCH 16/28] program API for int -> to long int modified --- aJSON.cpp | 10 +++++----- aJSON.h | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/aJSON.cpp b/aJSON.cpp index 369f812..b903251 100755 --- a/aJSON.cpp +++ b/aJSON.cpp @@ -300,7 +300,7 @@ aJsonClass::deleteItem(aJsonObject *c) int aJsonStream::parseNumber(aJsonObject *item) { - int i = 0; + long int i = 0; int sign = 1; int in = this->getch(); @@ -1206,13 +1206,13 @@ aJsonClass::createItem(char b) } aJsonObject* -aJsonClass::createItem(int num) +aJsonClass::createItem(long int num) { aJsonObject *item = newItem(); if (item) { item->type = aJson_Int; - item->valueint = (int) num; + item->valueint = (long int) num; } return item; } @@ -1260,7 +1260,7 @@ aJsonClass::createObject() // Create Arrays: aJsonObject* -aJsonClass::createIntArray(int *numbers, unsigned char count) +aJsonClass::createIntArray(long int *numbers, unsigned char count) { unsigned char i; aJsonObject *n = 0, *p = 0, *a = createArray(); @@ -1340,7 +1340,7 @@ aJsonClass::addBooleanToObject(aJsonObject* object, const char* name, bool b) } void -aJsonClass::addNumberToObject(aJsonObject* object, const char* name, int n) +aJsonClass::addNumberToObject(aJsonObject* object, const char* name, long int n) { addItemToObject(object, name, createItem(n)); } diff --git a/aJSON.h b/aJSON.h index b3ecb76..bca60e1 100755 --- a/aJSON.h +++ b/aJSON.h @@ -235,14 +235,14 @@ class aJsonClass { aJsonObject* createNull(); aJsonObject* createItem(bool b); aJsonObject* createItem(char b); - aJsonObject* createItem(int num); + aJsonObject* createItem(long int num); aJsonObject* createItem(double num); aJsonObject* createItem(const char *string); aJsonObject* createArray(); aJsonObject* createObject(); // These utilities create an Array of count items. - aJsonObject* createIntArray(int *numbers, unsigned char count); + aJsonObject* createIntArray(long int *numbers, unsigned char count); aJsonObject* createFloatArray(double *numbers, unsigned char count); aJsonObject* createDoubleArray(double *numbers, unsigned char count); aJsonObject* createStringArray(const char **strings, unsigned char count); @@ -270,7 +270,7 @@ 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, long int n); void addNumberToObject(aJsonObject* object, const char* name, double n); void addStringToObject(aJsonObject* object, const char* name, const char* s); From b063de5fb8041e039aaff06b21790edb12c22fdf Mon Sep 17 00:00:00 2001 From: Andrey Klimov Date: Sat, 6 Mar 2021 15:10:22 +0300 Subject: [PATCH 17/28] slim global-buffer for AVR --- utility/stringbuffer.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/utility/stringbuffer.c b/utility/stringbuffer.c index 87eda53..3c37742 100644 --- a/utility/stringbuffer.c +++ b/utility/stringbuffer.c @@ -26,7 +26,11 @@ #include "stringbuffer.h" //Default buffer size for strings +#if defined(ARDUINO_ARCH_AVR) +#define BUFFER_SIZE 64 +#else #define BUFFER_SIZE 256 +#endif //there is a static buffer allocated, which is used to decode strings to //strings cannot be longer than the buffer char global_buffer[BUFFER_SIZE]; From a369d071c98b95b75791d6e5ef89ad8346970cce Mon Sep 17 00:00:00 2001 From: Andrey Klimov Date: Mon, 5 Jul 2021 11:29:15 +0300 Subject: [PATCH 18/28] EEPROM dependancy removed back --- aJSON.cpp | 6 ++++-- aJSON.h | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/aJSON.cpp b/aJSON.cpp index b903251..ef4bb27 100755 --- a/aJSON.cpp +++ b/aJSON.cpp @@ -46,6 +46,7 @@ #include "aJSON.h" #include "utility/stringbuffer.h" #include +/* #if defined(__SAM3X8E__) #include extern DueFlashStorage EEPROM; @@ -57,6 +58,7 @@ extern NRFFlashStorage EEPROM; #else #include #endif +*/ /****************************************************************************** * Definitions ******************************************************************************/ @@ -123,7 +125,7 @@ aJsonFileStream::getch() return fgetc(fl); } - +/* int aJsonEEPROMStream::getch() { char c; @@ -151,7 +153,6 @@ aJsonEEPROMStream::available() #endif { - /* Make an effort to skip whitespace. */ ch = this->getch(); if (ch > 32) @@ -182,6 +183,7 @@ int aJsonEEPROMStream::putEOF(void) #endif. return res; } +*/ size_t aJsonStream::write(uint8_t ch) diff --git a/aJSON.h b/aJSON.h index bca60e1..7a97dea 100755 --- a/aJSON.h +++ b/aJSON.h @@ -181,7 +181,7 @@ class aJsonFileStream : public aJsonStream { virtual int getch(); FILE* fl; }; - +/* class aJsonEEPROMStream : public aJsonStream { public: aJsonEEPROMStream(int _addr) @@ -202,6 +202,7 @@ class aJsonEEPROMStream : public aJsonStream { int offset; }; +*/ class aJsonClass { /****************************************************************************** From 2713118230ab9593b72d7a9528bc423154a4646b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=BB=D0=B8=D0=BC=D0=BE=D0=B2=20=D0=90=D0=BD=D0=B4?= =?UTF-8?q?=D1=80=D0=B5=D0=B9=20=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B5?= =?UTF-8?q?=D0=B2=D0=B8=D1=87?= Date: Mon, 27 Mar 2023 18:27:52 +0300 Subject: [PATCH 19/28] added safety with NULL pointers to objects --- aJSON.cpp | 81 +++++++------------------------------------------------ 1 file changed, 9 insertions(+), 72 deletions(-) diff --git a/aJSON.cpp b/aJSON.cpp index ef4bb27..becf68b 100755 --- a/aJSON.cpp +++ b/aJSON.cpp @@ -46,19 +46,7 @@ #include "aJSON.h" #include "utility/stringbuffer.h" #include -/* -#if defined(__SAM3X8E__) -#include -extern DueFlashStorage EEPROM; -#elif defined(NRF5) || defined (ARDUINO_ARCH_ESP32) || defined (ARDUINO_ARCH_STM32) -#include -extern NRFFlashStorage EEPROM; -#elif defined(ARDUINO_ARCH_ESP8266) -#include -#else -#include -#endif -*/ + /****************************************************************************** * Definitions ******************************************************************************/ @@ -125,65 +113,6 @@ aJsonFileStream::getch() return fgetc(fl); } -/* -int -aJsonEEPROMStream::getch() -{ char c; - if (bucket != EOF) - { - int ret = bucket; - bucket = EOF; - return ret; - } - c=EEPROM.read(addr+offset++); - return c; -} - -bool -aJsonEEPROMStream::available() -{ - int ch =0; - if (bucket != EOF) - return true; - -#if defined(__SAM3X8E__) or defined(ARDUINO_ARCH_STM32) or defined (NRF5) or defined (ARDUINO_ARCH_ESP32) or defined (ARDUINO_ARCH_STM32) - while ((ch!=EOF) && (offset<32000)) ///fix it -#else - while (addr+offsetgetch(); - - if (ch > 32) - { - this->ungetch(ch); - return true; - } - } - return false; - } - - -size_t -aJsonEEPROMStream::write(uint8_t ch) -{ -EEPROM.write(addr+offset++,(char)ch); -return 1; -} - -int aJsonEEPROMStream::putEOF(void) - { - int res; - res = write(EOF); - #if defined(ARDUINO_ARCH_ESP8266) - // write the data to EEPROM - res = EEPROM.commitReset(); - Serial.println((res) ? "Commit OK" : "Commit failed"); - #endif. - return res; - } -*/ size_t aJsonStream::write(uint8_t ch) @@ -277,6 +206,7 @@ aJsonClass::newItem() void aJsonClass::deleteItem(aJsonObject *c) { + if (!c) return; aJsonObject *next; while (c) { @@ -1013,6 +943,7 @@ aJsonStream::printObject(aJsonObject *item) unsigned char aJsonClass::getArraySize(aJsonObject *array) { + if (!array) return 0; aJsonObject *c = array->child; unsigned char i = 0; while (c) @@ -1022,6 +953,7 @@ aJsonClass::getArraySize(aJsonObject *array) aJsonObject* aJsonClass::getArrayItem(aJsonObject *array, unsigned char item) { + if (!array) return NULL; aJsonObject *c = array->child; while (c && item > 0) item--, c = c->next; @@ -1030,6 +962,7 @@ aJsonClass::getArrayItem(aJsonObject *array, unsigned char item) aJsonObject* aJsonClass::getObjectItem(aJsonObject *object, const char *string) { + if (!object) return NULL; aJsonObject *c = object->child; while (c && strcasecmp(c->name, string)) c = c->next; @@ -1061,6 +994,7 @@ aJsonClass::createReference(aJsonObject *item) void aJsonClass::addItemToArray(aJsonObject *array, aJsonObject *item) { + if (!array) return; aJsonObject *c = array->child; if (!item) return; @@ -1101,6 +1035,7 @@ aJsonClass::addItemReferenceToObject(aJsonObject *object, const char *string, aJsonObject* aJsonClass::detachItemFromArray(aJsonObject *array, unsigned char which) { + if (!array) return NULL; aJsonObject *c = array->child; while (c && which > 0) c = c->next, which--; @@ -1124,6 +1059,7 @@ aJsonObject* aJsonClass::detachItemFromObject(aJsonObject *object, const char *string) { unsigned char i = 0; + if (!object) return NULL; aJsonObject *c = object->child; while (c && strcasecmp(c->name, string)) i++, c = c->next; @@ -1142,6 +1078,7 @@ void aJsonClass::replaceItemInArray(aJsonObject *array, unsigned char which, aJsonObject *newitem) { + if (!array) return; aJsonObject *c = array->child; while (c && which > 0) c = c->next, which--; From be2b268abd0b62db8587ba03c35ba3f2d5aee3da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=BB=D0=B8=D0=BC=D0=BE=D0=B2=20=D0=90=D0=BD=D0=B4?= =?UTF-8?q?=D1=80=D0=B5=D0=B9=20=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B5?= =?UTF-8?q?=D0=B2=D0=B8=D1=87?= Date: Sun, 9 Apr 2023 02:49:57 +0300 Subject: [PATCH 20/28] RAM saving by remove strings duplicates --- aJSON.cpp | 15 +++--- aJSON.h | 9 ++++ utility/stringbuffer.c | 104 ++++++++++++++++++++++++++++++++++++++++- utility/stringbuffer.h | 22 ++++++++- 4 files changed, 140 insertions(+), 10 deletions(-) diff --git a/aJSON.cpp b/aJSON.cpp index becf68b..ad994ec 100755 --- a/aJSON.cpp +++ b/aJSON.cpp @@ -217,11 +217,11 @@ aJsonClass::deleteItem(aJsonObject *c) } if ((c->type == aJson_String) && c->valuestring) { - free(c->valuestring); + freeString(c->valuestring); } if (c->name) { - free(c->name); + freeString(c->name); } free(c); c = next; @@ -1016,8 +1016,8 @@ aJsonClass::addItemToObject(aJsonObject *object, const char *string, if (!item) return; if (item->name) - free(item->name); - item->name = strdup(string); + freeString(item->name); + item->name = newString(string); addItemToArray(object, item); } void @@ -1105,7 +1105,7 @@ aJsonClass::replaceItemInObject(aJsonObject *object, const char *string, i++, c = c->next; if (c) { - newitem->name = strdup(string); + newitem->name = newString(string); replaceItemInArray(object, i, newitem); } } @@ -1175,7 +1175,8 @@ aJsonClass::createItem(const char *string) if (item) { item->type = aJson_String; - item->valuestring = strdup(string); + /// item->valuestring = strdup(string); + item->valuestring = newString(string); } return item; } @@ -1298,5 +1299,7 @@ aJsonClass::addStringToObject(aJsonObject* object, const char* name, } //TODO conversion routines btw. float & int types? +void debugPrint(const char* s){Serial.print(s);} aJsonClass aJson; + diff --git a/aJSON.h b/aJSON.h index 7a97dea..c091495 100755 --- a/aJSON.h +++ b/aJSON.h @@ -42,6 +42,7 @@ #define aJson_Array 5 #define aJson_Object 6 + ////#define aJson_IsReference 128 #define aJson_IsReference 8 @@ -288,4 +289,12 @@ class aJsonClass { extern aJsonClass aJson; +#ifdef __cplusplus +extern "C" +{ +#endif +void debugPrint(const char* s); +#ifdef __cplusplus +} +#endif #endif diff --git a/utility/stringbuffer.c b/utility/stringbuffer.c index 3c37742..caf2ea1 100644 --- a/utility/stringbuffer.c +++ b/utility/stringbuffer.c @@ -24,6 +24,9 @@ #include #include #include "stringbuffer.h" +#include +//#include + //Default buffer size for strings #if defined(ARDUINO_ARCH_AVR) @@ -99,12 +102,19 @@ stringBufferToString(string_buffer* buffer) buffer->string=NULL; free(buffer); return string;*/ - char* result = malloc(buffer->string_length * sizeof(char)); + + ///char* result = malloc(buffer->string_length * sizeof(char)); + + char * result = newString(global_buffer); if (result == NULL) { return NULL; } - strcpy(result, global_buffer); + + ///strcpy(result, global_buffer); + + + buffer->string = NULL; free(buffer); return result; @@ -127,3 +137,93 @@ stringBufferFree(string_buffer* buffer) free(buffer); } +static string_card stringLib ={NULL,0,NULL}; + + string_card *findString(char * str) + { + string_card * card = &stringLib; + if (!str) return NULL; + do + { + if (card->string && !strcmp(str,card->string)) return card; + card = card->next; + } while (card); + return NULL; + } + + char *newString(const char * str) + { + //debugPrint(str); + string_card * card = findString(str); + string_card * prevCard; + if (card) + { + // debugPrint(":reused; "); + card->used++; + return card->string; + } + else + { + card = &stringLib; + do + { + if(!card->string) + { + card->string = strdup(str); + card->used=1; + // debugPrint(":added/replaced; "); + return card->string; + } + prevCard = card; + card = card->next; + } while (card); + + card = malloc (sizeof(string_card)); + if (card) + { + prevCard->next = card; + card->string = strdup(str); + card->used=1; + card->next=NULL; + // debugPrint(":added "); + return card->string; + } + } + return NULL; + } + + void freeString(char * str) + { + string_card * card = findString(str); + if (!card) return; + card->used--; + if (!card->used) + { + // debugPrint(str); + free(card->string); + card->string = NULL; + // debugPrint(":removed "); + compressList(); + } + } + + void compressList() + { + string_card * prevPtr = &stringLib; + string_card * nextPtr = prevPtr->next; + while (nextPtr) + { + if (!nextPtr->string) + { + //removing card + prevPtr->next=nextPtr->next; + free (nextPtr); + nextPtr=prevPtr->next; + } + else + { + prevPtr=nextPtr; + nextPtr=nextPtr->next; + } + } + } \ No newline at end of file diff --git a/utility/stringbuffer.h b/utility/stringbuffer.h index b0a8181..6c9778a 100644 --- a/utility/stringbuffer.h +++ b/utility/stringbuffer.h @@ -24,7 +24,7 @@ #ifndef STRINGBUFFER_H_ #define STRINGBUFFER_H_ - +#include typedef struct { char* string; @@ -32,11 +32,12 @@ typedef struct unsigned int string_length; } string_buffer; + #ifdef __cplusplus extern "C" { #endif - +extern void debugPrint(const char*); string_buffer* stringBufferCreate(void); @@ -49,7 +50,24 @@ extern "C" void stringBufferFree(string_buffer* buffer); +typedef struct string_card string_card; +struct string_card +{ + char* string; + uint8_t used; + string_card * next; +}; + + + + string_card *findString(char *); + char *newString(const char *); + void freeString(char *); + void compressList(); + + #ifdef __cplusplus } #endif + #endif /* STRINGBUFFER_H_ */ From 0c23b799722dc505a41e05de3d60491b8b471f76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=BB=D0=B8=D0=BC=D0=BE=D0=B2=20=D0=90=D0=BD=D0=B4?= =?UTF-8?q?=D1=80=D0=B5=D0=B9=20=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B5?= =?UTF-8?q?=D0=B2=D0=B8=D1=87?= Date: Sun, 9 Apr 2023 16:24:39 +0300 Subject: [PATCH 21/28] Garbage stream parsing tolerance --- aJSON.cpp | 11 ++++++++--- utility/stringbuffer.c | 14 ++++++++------ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/aJSON.cpp b/aJSON.cpp index ad994ec..0294b70 100755 --- a/aJSON.cpp +++ b/aJSON.cpp @@ -511,13 +511,14 @@ aJsonStream::printString(aJsonObject *item) // Utility to jump whitespace and cr/lf int aJsonStream::skip() -{ +{ + int skipCounter=256; int in = this->getch(); - while (in != EOF && (in <= 32)) + while (in != EOF && (in <= 32) && skipCounter--) { in = this->getch(); } - if (in != EOF) + if ((in != EOF) && skipCounter) { this->ungetch(in); return 0; @@ -566,11 +567,15 @@ aJsonClass::parse(aJsonStream* stream, char** filter) } aJsonObject *c = newItem(); if (!c) + { + //debugPrint("new item fail\n"); return NULL; /* memory fail */ + } stream->skip(); if (stream->parseValue(c, filter) == EOF) { + //debugPrint("del item\n"); deleteItem(c); return NULL; } diff --git a/utility/stringbuffer.c b/utility/stringbuffer.c index caf2ea1..08249b4 100644 --- a/utility/stringbuffer.c +++ b/utility/stringbuffer.c @@ -62,7 +62,7 @@ stringBufferCreate(void) char stringBufferAdd(char value, string_buffer* buffer) { - if (buffer->string_length >= buffer->memory) + if (buffer->string_length >= buffer->memory-1) { //this has to be enabled after realloc works /*char* new_string = (char*) realloc((void*) buffer->string, (buffer->memory @@ -108,6 +108,7 @@ stringBufferToString(string_buffer* buffer) char * result = newString(global_buffer); if (result == NULL) { + // debugPrint(("String allocation error\n")); return NULL; } @@ -158,7 +159,7 @@ static string_card stringLib ={NULL,0,NULL}; string_card * prevCard; if (card) { - // debugPrint(":reused; "); + //debugPrint(":reused; "); card->used++; return card->string; } @@ -171,7 +172,7 @@ static string_card stringLib ={NULL,0,NULL}; { card->string = strdup(str); card->used=1; - // debugPrint(":added/replaced; "); + //debugPrint(":added/replaced; "); return card->string; } prevCard = card; @@ -185,7 +186,7 @@ static string_card stringLib ={NULL,0,NULL}; card->string = strdup(str); card->used=1; card->next=NULL; - // debugPrint(":added "); + //debugPrint(":added "); return card->string; } } @@ -199,10 +200,10 @@ static string_card stringLib ={NULL,0,NULL}; card->used--; if (!card->used) { - // debugPrint(str); + //debugPrint(str); free(card->string); card->string = NULL; - // debugPrint(":removed "); + //debugPrint(":removed "); compressList(); } } @@ -226,4 +227,5 @@ static string_card stringLib ={NULL,0,NULL}; nextPtr=nextPtr->next; } } + //debugPrint(("Compressed\n")); } \ No newline at end of file From 6bbe201ffc6ac4d6d24e3657a7bd2aa52a96e02d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=BB=D0=B8=D0=BC=D0=BE=D0=B2=20=D0=90=D0=BD=D0=B4?= =?UTF-8?q?=D1=80=D0=B5=D0=B9=20=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B5?= =?UTF-8?q?=D0=B2=D0=B8=D1=87?= Date: Sun, 8 Oct 2023 15:52:26 +0300 Subject: [PATCH 22/28] 1 bit more for subitem --- aJSON.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/aJSON.h b/aJSON.h index c091495..dc30f01 100755 --- a/aJSON.h +++ b/aJSON.h @@ -44,7 +44,7 @@ ////#define aJson_IsReference 128 -#define aJson_IsReference 8 +#define aJson_IsReference 7 #ifndef EOF #define EOF -1 @@ -54,12 +54,12 @@ // The aJson structure: typedef struct aJsonObject { - char *name; // The item's name string, if this item is the child of, or is in the list of subitems of an object. + char *name; // The item's name string, if this item is the child of, or is in the list of subitems of an object. struct aJsonObject *next, *prev; // next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem struct aJsonObject *child; // An array or object item will have a child pointer pointing to a chain of the items in the array/object. - char type:4; // The type of the item, as above. - char subtype:4; // User defined field + unsigned char type:3; // The type of the item, as above. + unsigned char subtype:5; // User defined field union { char *valuestring; // The item's string, if type==aJson_String char valuebool; //the items value for true & false From 9f17e2241f0e57317ac63f9b9fd0b67d6ea01284 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=BB=D0=B8=D0=BC=D0=BE=D0=B2=20=D0=90=D0=BD=D0=B4?= =?UTF-8?q?=D1=80=D0=B5=D0=B9=20=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B5?= =?UTF-8?q?=D0=B2=D0=B8=D1=87?= Date: Mon, 23 Oct 2023 20:59:52 +0300 Subject: [PATCH 23/28] rollback previous change --- aJSON.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/aJSON.h b/aJSON.h index dc30f01..61bc429 100755 --- a/aJSON.h +++ b/aJSON.h @@ -41,10 +41,11 @@ #define aJson_String 4 #define aJson_Array 5 #define aJson_Object 6 +#define aJson_Reserved 7 ////#define aJson_IsReference 128 -#define aJson_IsReference 7 +#define aJson_IsReference 8 #ifndef EOF #define EOF -1 @@ -58,8 +59,8 @@ typedef struct aJsonObject { struct aJsonObject *next, *prev; // next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem struct aJsonObject *child; // An array or object item will have a child pointer pointing to a chain of the items in the array/object. - unsigned char type:3; // The type of the item, as above. - unsigned char subtype:5; // User defined field + unsigned char type:4; // The type of the item, as above. + unsigned char subtype:4; // User defined field union { char *valuestring; // The item's string, if type==aJson_String char valuebool; //the items value for true & false From d0562f14beca7a8628b6332d53285db369e0c0d3 Mon Sep 17 00:00:00 2001 From: Andrey Date: Sun, 12 Nov 2023 00:23:26 +0300 Subject: [PATCH 24/28] cleaning subtype --- aJSON.cpp | 16 ++++++++++++++++ aJSON.h | 22 ---------------------- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/aJSON.cpp b/aJSON.cpp index 0294b70..fc6f33c 100755 --- a/aJSON.cpp +++ b/aJSON.cpp @@ -264,6 +264,7 @@ aJsonStream::parseNumber(aJsonObject *item) { item->valueint = i * (int) sign; item->type = aJson_Int; + item->subtype = 0; } //ok it seems to be a double else @@ -306,6 +307,7 @@ aJsonStream::parseNumber(aJsonObject *item) item->valuefloat = n; item->type = aJson_Float; + item->subtype = 0; } //preserve the last character for the next routine this->ungetch(in); @@ -371,6 +373,7 @@ aJsonStream::parseString(aJsonObject *item) return EOF; // not a string! } item->type = aJson_String; + item->subtype = 0; //allocate a buffer & track how long it is and how much we have read string_buffer* buffer = stringBufferCreate(); if (buffer == NULL) @@ -646,6 +649,7 @@ aJsonStream::parseValue(aJsonObject *item, char** filter) if (!strncmp(buffer, "null", 4)) { item->type = aJson_NULL; + item->subtype = 0; return 0; } else @@ -665,6 +669,7 @@ aJsonStream::parseValue(aJsonObject *item, char** filter) if (!strncmp(buffer, "false", 5)) { item->type = aJson_Boolean; + item->subtype = 0; item->valuebool = false; return 0; } @@ -681,6 +686,7 @@ aJsonStream::parseValue(aJsonObject *item, char** filter) if (!strncmp(buffer, "true", 4)) { item->type = aJson_Boolean; + item->subtype = 0; item->valuebool = true; return 0; } @@ -742,6 +748,7 @@ aJsonStream::parseArray(aJsonObject *item, char** filter) } item->type = aJson_Array; + item->subtype = 0; this->skip(); in = this->getch(); //check for empty array @@ -836,6 +843,7 @@ aJsonStream::parseObject(aJsonObject *item, char** filter) } item->type = aJson_Object; + item->subtype = 0; this->skip(); //check for an empty object in = this->getch(); @@ -1122,6 +1130,7 @@ aJsonClass::createNull() aJsonObject *item = newItem(); if (item) item->type = aJson_NULL; + item->subtype = 0; return item; } @@ -1131,6 +1140,7 @@ aJsonClass::createItem(bool b) aJsonObject *item = newItem(); if (item){ item->type = aJson_Boolean; + item->subtype = 0; item->valuebool = b; } @@ -1144,6 +1154,7 @@ aJsonClass::createItem(char b) if (item) { item->type = aJson_Boolean; + item->subtype = 0; item->valuebool = b ? -1 : 0; } return item; @@ -1156,6 +1167,7 @@ aJsonClass::createItem(long int num) if (item) { item->type = aJson_Int; + item->subtype = 0; item->valueint = (long int) num; } return item; @@ -1168,6 +1180,7 @@ aJsonClass::createItem(double num) if (item) { item->type = aJson_Float; + item->subtype = 0; item->valuefloat = num; } return item; @@ -1180,6 +1193,7 @@ aJsonClass::createItem(const char *string) if (item) { item->type = aJson_String; + item->subtype = 0; /// item->valuestring = strdup(string); item->valuestring = newString(string); } @@ -1192,6 +1206,7 @@ aJsonClass::createArray() aJsonObject *item = newItem(); if (item) item->type = aJson_Array; + item->subtype = 0; return item; } aJsonObject* @@ -1200,6 +1215,7 @@ aJsonClass::createObject() aJsonObject *item = newItem(); if (item) item->type = aJson_Object; + item->subtype = 0; return item; } diff --git a/aJSON.h b/aJSON.h index 61bc429..55eda5a 100755 --- a/aJSON.h +++ b/aJSON.h @@ -183,28 +183,6 @@ class aJsonFileStream : public aJsonStream { virtual int getch(); FILE* fl; }; -/* -class aJsonEEPROMStream : public aJsonStream { -public: - aJsonEEPROMStream(int _addr) - : aJsonStream(NULL) - { - addr=_addr; - offset=0; - } - - int putEOF(void); - virtual bool available(); - -private: - virtual int getch(); - virtual size_t write(uint8_t ch); - - int addr; - int offset; -}; - -*/ class aJsonClass { /****************************************************************************** From eba2e06cff5879ea02245fe3395b16679fd8d59e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=BB=D0=B8=D0=BC=D0=BE=D0=B2=20=D0=90=D0=BD=D0=B4?= =?UTF-8?q?=D1=80=D0=B5=D0=B9=20=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B5?= =?UTF-8?q?=D0=B2=D0=B8=D1=87?= Date: Sat, 9 Dec 2023 22:21:52 +0300 Subject: [PATCH 25/28] FLOAT_PRECISION 2 --- aJSON.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aJSON.cpp b/aJSON.cpp index fc6f33c..322c60c 100755 --- a/aJSON.cpp +++ b/aJSON.cpp @@ -54,7 +54,7 @@ #define BUFFER_DEFAULT_SIZE 4 //how much digits after . for float -#define FLOAT_PRECISION 1 +#define FLOAT_PRECISION 2 bool From feb3a37e314e113af9153bd901bfd0d517ec1831 Mon Sep 17 00:00:00 2001 From: Andrey Date: Tue, 20 Aug 2024 20:22:30 +0300 Subject: [PATCH 26/28] print_hidden parameter to print functions --- aJSON.cpp | 27 +++++++++++++++------------ aJSON.h | 10 +++++----- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/aJSON.cpp b/aJSON.cpp index 322c60c..9a3b989 100755 --- a/aJSON.cpp +++ b/aJSON.cpp @@ -587,13 +587,13 @@ aJsonClass::parse(aJsonStream* stream, char** filter) // Render a aJsonObject item/entity/structure to text. int -aJsonClass::print(aJsonObject* item, aJsonStream* stream) +aJsonClass::print(aJsonObject* item, aJsonStream* stream, bool print_hidden) { - return stream->printValue(item); + return stream->printValue(item, print_hidden); } char* -aJsonClass::print(aJsonObject* item) +aJsonClass::print(aJsonObject* item, bool print_hidden) { char* outBuf = (char*) malloc(PRINT_BUFFER_LEN); /* XXX: Dynamic size. */ if (outBuf == NULL) @@ -601,7 +601,7 @@ aJsonClass::print(aJsonObject* item) return NULL; } aJsonStringStream stringStream(NULL, outBuf, PRINT_BUFFER_LEN); - print(item, &stringStream); + print(item, &stringStream, print_hidden); return outBuf; } @@ -697,7 +697,7 @@ aJsonStream::parseValue(aJsonObject *item, char** filter) // Render a value to text. int -aJsonStream::printValue(aJsonObject *item) +aJsonStream::printValue(aJsonObject *item, bool print_hidden) { int result = 0; if (item == NULL) @@ -728,10 +728,10 @@ aJsonStream::printValue(aJsonObject *item) result = this->printString(item); break; case aJson_Array: - result = this->printArray(item); + result = this->printArray(item, print_hidden); break; case aJson_Object: - result = this->printObject(item); + result = this->printObject(item, print_hidden); break; } return result; @@ -798,7 +798,7 @@ aJsonStream::parseArray(aJsonObject *item, char** filter) // Render an array to text int -aJsonStream::printArray(aJsonObject *item) +aJsonStream::printArray(aJsonObject *item, bool print_hidden) { if (item == NULL) { @@ -812,7 +812,7 @@ aJsonStream::printArray(aJsonObject *item) } while (child) { - if (this->printValue(child) == EOF) + if (this->printValue(child, print_hidden) == EOF) { return EOF; } @@ -910,7 +910,7 @@ aJsonStream::parseObject(aJsonObject *item, char** filter) // Render an object to text. int -aJsonStream::printObject(aJsonObject *item) +aJsonStream::printObject(aJsonObject *item, bool print_hidden) { if (item == NULL) { @@ -924,6 +924,8 @@ aJsonStream::printObject(aJsonObject *item) } while (child) { + if (print_hidden || (*child->name != '@')) /// + { if (this->printStringPtr(child->name) == EOF) { return EOF; @@ -932,12 +934,13 @@ aJsonStream::printObject(aJsonObject *item) { return EOF; } - if (this->printValue(child) == EOF) + if (this->printValue(child, print_hidden) == EOF) { return EOF; } + } //// child = child->next; - if (child) + if (child && (print_hidden || (*child->name != '@') )) { if (this->print(',') == EOF) { diff --git a/aJSON.h b/aJSON.h index 55eda5a..e20e838 100755 --- a/aJSON.h +++ b/aJSON.h @@ -95,13 +95,13 @@ class aJsonStream : public Print { void flush(); int parseValue(aJsonObject *item, char** filter); - int printValue(aJsonObject *item); + int printValue(aJsonObject *item, bool print_hidden = true); int parseArray(aJsonObject *item, char** filter); - int printArray(aJsonObject *item); + int printArray(aJsonObject *item, bool print_hidden = true); int parseObject(aJsonObject *item, char** filter); - int printObject(aJsonObject *item); + int printObject(aJsonObject *item, bool print_hidden = true); protected: /* Blocking load of character, returning EOF if the stream @@ -198,8 +198,8 @@ class aJsonClass { aJsonObject* parse(aJsonStream* stream,char** filter_values); //Read from a file, but only return values include in the char* array filter_values aJsonObject* parse(char *value); //Reads from a string // Render a aJsonObject entity to text for transfer/storage. Free the char* when finished. - int print(aJsonObject *item, aJsonStream* stream); - char* print(aJsonObject* item); + int print(aJsonObject *item, aJsonStream* stream, bool print_hidden = true); + char* print(aJsonObject* item , bool print_hidden = true); //Renders a aJsonObject directly to a output stream char stream(aJsonObject *item, aJsonStream* stream); // Delete a aJsonObject entity and all sub-entities. From 263b2fec9a60b9bb3fa4e5ed268fdde27e9305bb Mon Sep 17 00:00:00 2001 From: Andrey Date: Tue, 20 Aug 2024 23:15:22 +0300 Subject: [PATCH 27/28] remove ambiques declaration --- aJSON.cpp | 4 ++-- aJSON.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/aJSON.cpp b/aJSON.cpp index 9a3b989..13c7e38 100755 --- a/aJSON.cpp +++ b/aJSON.cpp @@ -593,7 +593,7 @@ aJsonClass::print(aJsonObject* item, aJsonStream* stream, bool print_hidden) } char* -aJsonClass::print(aJsonObject* item, bool print_hidden) +aJsonClass::print(aJsonObject* item) { char* outBuf = (char*) malloc(PRINT_BUFFER_LEN); /* XXX: Dynamic size. */ if (outBuf == NULL) @@ -601,7 +601,7 @@ aJsonClass::print(aJsonObject* item, bool print_hidden) return NULL; } aJsonStringStream stringStream(NULL, outBuf, PRINT_BUFFER_LEN); - print(item, &stringStream, print_hidden); + print(item, &stringStream); return outBuf; } diff --git a/aJSON.h b/aJSON.h index e20e838..333866f 100755 --- a/aJSON.h +++ b/aJSON.h @@ -198,8 +198,8 @@ class aJsonClass { aJsonObject* parse(aJsonStream* stream,char** filter_values); //Read from a file, but only return values include in the char* array filter_values aJsonObject* parse(char *value); //Reads from a string // Render a aJsonObject entity to text for transfer/storage. Free the char* when finished. - int print(aJsonObject *item, aJsonStream* stream, bool print_hidden = true); - char* print(aJsonObject* item , bool print_hidden = true); + int print(aJsonObject *item, aJsonStream* stream, bool print_hidden=true); + char* print(aJsonObject* item); //Renders a aJsonObject directly to a output stream char stream(aJsonObject *item, aJsonStream* stream); // Delete a aJsonObject entity and all sub-entities. From 4d0da32d7ce85c04af106b9f03a58988836086ec Mon Sep 17 00:00:00 2001 From: Andrey Date: Sat, 7 Sep 2024 15:45:25 +0300 Subject: [PATCH 28/28] print for aJson_Reserved --- aJSON.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/aJSON.cpp b/aJSON.cpp index 13c7e38..74114ab 100755 --- a/aJSON.cpp +++ b/aJSON.cpp @@ -719,6 +719,7 @@ aJsonStream::printValue(aJsonObject *item, bool print_hidden) } break; case aJson_Int: + case aJson_Reserved: result = this->printInt(item); break; case aJson_Float: