Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor patch to print buffer size #58

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions aJSON.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
//how much digits after . for float
#define FLOAT_PRECISION 5

#define AJASON_PRINT_BUFFER_MAX_SIZE_BYTES 384

bool
aJsonStream::available()
Expand Down Expand Up @@ -208,10 +209,10 @@ aJsonClass::deleteItem(aJsonObject *c)
}

// Parse the input text to generate a number, and populate the result into item.
int
ajson_int_t
aJsonStream::parseNumber(aJsonObject *item)
{
int i = 0;
ajson_int_t i = 0;
char sign = 1;

int in = this->getch();
Expand Down Expand Up @@ -241,7 +242,16 @@ aJsonStream::parseNumber(aJsonObject *item)
//end of integer part � or isn't it?
if (!(in == '.' || in == 'e' || in == 'E'))
{
item->valueint = i * (int) sign;
#ifdef AJSON_UNSIGNED_LONG_INT
if (sign != 1)
{
/* Can't store signed value in unsigned variable */
return EOF;
}
item->valueint = i;
#else
item->valueint = i * (ajson_int_t) sign;
#endif
item->type = aJson_Int;
}
//ok it seems to be a double
Expand Down Expand Up @@ -566,12 +576,12 @@ aJsonClass::print(aJsonObject* item, aJsonStream* stream)
char*
aJsonClass::print(aJsonObject* item)
{
char* outBuf = (char*) malloc(256); /* XXX: Dynamic size. */
char* outBuf = (char*) malloc(AJASON_PRINT_BUFFER_MAX_SIZE_BYTES);
if (outBuf == NULL)
{
return NULL;
}
aJsonStringStream stringStream(NULL, outBuf, 256);
aJsonStringStream stringStream(NULL, outBuf, AJASON_PRINT_BUFFER_MAX_SIZE_BYTES);
print(item, &stringStream);
return outBuf;
}
Expand Down Expand Up @@ -1125,13 +1135,13 @@ aJsonClass::createItem(char b)
}

aJsonObject*
aJsonClass::createItem(int num)
aJsonClass::createItem(ajson_int_t num)
{
aJsonObject *item = newItem();
if (item)
{
item->type = aJson_Int;
item->valueint = (int) num;
item->valueint = num;
}
return item;
}
Expand Down Expand Up @@ -1179,7 +1189,7 @@ aJsonClass::createObject()

// Create Arrays:
aJsonObject*
aJsonClass::createIntArray(int *numbers, unsigned char count)
aJsonClass::createIntArray(ajson_int_t *numbers, unsigned char count)
{
unsigned char i;
aJsonObject *n = 0, *p = 0, *a = createArray();
Expand Down Expand Up @@ -1271,7 +1281,7 @@ aJsonClass::addFalseToObject(aJsonObject* object, const char* name)
}

void
aJsonClass::addNumberToObject(aJsonObject* object, const char* name, int n)
aJsonClass::addNumberToObject(aJsonObject* object, const char* name, ajson_int_t n)
{
addItemToObject(object, name, createItem(n));
}
Expand Down
19 changes: 14 additions & 5 deletions aJSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
#include <Client.h>
#include <Arduino.h> // To get access to the Arduino millis() function

// Use an unsigned long for integer values
#define AJSON_UNSIGNED_LONG_INT

/******************************************************************************
* Definitions
******************************************************************************/
Expand All @@ -49,6 +52,12 @@
#define EOF -1
#endif

#ifdef AJSON_UNSIGNED_LONG_INT
typedef unsigned long ajson_int_t;
#else
typedef int ajson_int_t;
#endif

// 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.
Expand All @@ -60,7 +69,7 @@ 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 number, if type==aJson_Number
ajson_int_t valueint; // The item's number, if type==aJson_Number
double valuefloat; // The item's number, if type==aJson_Number
};
} aJsonObject;
Expand All @@ -79,7 +88,7 @@ class aJsonStream : public Print {
* skips separating whitespace if you use this method. */
virtual bool available();

int parseNumber(aJsonObject *item);
ajson_int_t parseNumber(aJsonObject *item);
int printInt(aJsonObject *item);
int printFloat(aJsonObject *item);

Expand Down Expand Up @@ -199,14 +208,14 @@ class aJsonClass {
aJsonObject* createTrue();
aJsonObject* createFalse();
aJsonObject* createItem(char b);
aJsonObject* createItem(int num);
aJsonObject* createItem(ajson_int_t 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(ajson_int_t *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);
Expand Down Expand Up @@ -236,7 +245,7 @@ class aJsonClass {
void addBooleanToObject(aJsonObject* object, const char* name, bool b);
void addTrueToObject(aJsonObject* object, const char* name);
void addFalseToObject(aJsonObject* object, const char* name);
void addNumberToObject(aJsonObject* object, const char* name, int n);
void addNumberToObject(aJsonObject* object, const char* name, ajson_int_t n);
void addNumberToObject(aJsonObject* object, const char* name, double n);
void addStringToObject(aJsonObject* object, const char* name,
const char* s);
Expand Down