Compare commits

..

16 Commits
v4.1 ... v4.3

85 changed files with 689 additions and 223 deletions

View File

@ -9,4 +9,4 @@ before_script:
script:
- make && make test
after_success:
- coveralls --include include --include src --gcov-options '\-lp'
- coveralls --exclude test --exclude third-party --gcov-options '\-lp'

24
ArduinoJson.cpp Normal file
View File

@ -0,0 +1,24 @@
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#ifdef ARDUINO
// This file is here to help the Arduino IDE find the other files.
#include "src/Arduino/Print.cpp"
#include "src/DynamicJsonBuffer.cpp"
#include "src/Internals/IndentedPrint.cpp"
#include "src/Internals/JsonParser.cpp"
#include "src/Internals/List.cpp"
#include "src/Internals/Prettyfier.cpp"
#include "src/Internals/QuotedString.cpp"
#include "src/Internals/StringBuilder.cpp"
#include "src/JsonArray.cpp"
#include "src/JsonBuffer.cpp"
#include "src/JsonObject.cpp"
#include "src/JsonVariant.cpp"
#endif

13
ArduinoJson.h Normal file
View File

@ -0,0 +1,13 @@
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#ifdef ARDUINO
// This file is here to help the Arduino IDE find the other files.
#include "include/ArduinoJson.h"
#endif

View File

@ -1,6 +1,20 @@
Arduino JSON: change log
========================
v4.3
----
* Added `JsonArray::removeAt()` to remove an element of an array (issue #58)
* Fixed stack-overflow in `DynamicJsonBuffer` when parsing huge JSON files (issue #65)
* Fixed wrong return value of `parseArray()` and `parseObject()` when allocation fails (issue #68)
v4.2
----
* Switched back to old library layout (issues #39, #43 and #45)
* Removed global new operator overload (issue #40, #45 and #46)
* Added an example with EthernetServer
v4.1
----

View File

@ -0,0 +1,74 @@
// Sample Arduino Json Web Server
// Created by Benoit Blanchon.
// Heavily inspired by "Web Server" from David A. Mellis and Tom Igoe
#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoJson.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 0, 177);
EthernetServer server(80);
bool readRequest(EthernetClient& client) {
bool currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n' && currentLineIsBlank) {
return true;
} else if (c == '\n') {
currentLineIsBlank = true;
} else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
return false;
}
JsonObject& prepareResponse(JsonBuffer& jsonBuffer) {
JsonObject& root = jsonBuffer.createObject();
JsonArray& analogValues = root.createNestedArray("analog");
for (int pin = 0; pin < 6; pin++) {
int value = analogRead(pin);
analogValues.add(value);
}
JsonArray& digitalValues = root.createNestedArray("digital");
for (int pin = 0; pin < 14; pin++) {
int value = digitalRead(pin);
digitalValues.add(value);
}
return root;
}
void writeResponse(EthernetClient& client, JsonObject& json) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: application/json");
client.println("Connection: close");
client.println();
json.prettyPrintTo(client);
}
void setup() {
Ethernet.begin(mac, ip);
server.begin();
}
void loop() {
EthernetClient client = server.available();
if (client) {
bool success = readRequest(client);
if (success) {
StaticJsonBuffer<500> jsonBuffer;
JsonObject& json = prepareResponse(jsonBuffer);
writeResponse(client, json);
}
delay(1);
client.stop();
}
}

View File

@ -0,0 +1,55 @@
// Send a JSON object on UDP at regular interval
//
// You can easily test this program with netcat:
// $ nc -ulp 8888
//
// by Benoit Blanchon, MIT License 2015
#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoJson.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress localIp(192, 168, 0, 177);
IPAddress remoteIp(192, 168, 0, 109);
unsigned int remotePort = 8888;
unsigned localPort = 8888;
EthernetUDP udp;
JsonObject& buildJson(JsonBuffer& jsonBuffer) {
JsonObject& root = jsonBuffer.createObject();
JsonArray& analogValues = root.createNestedArray("analog");
for (int pin = 0; pin < 6; pin++) {
int value = analogRead(pin);
analogValues.add(value);
}
JsonArray& digitalValues = root.createNestedArray("digital");
for (int pin = 0; pin < 14; pin++) {
int value = digitalRead(pin);
digitalValues.add(value);
}
return root;
}
void sendJson(JsonObject& json) {
udp.beginPacket(remoteIp, remotePort);
json.printTo(udp);
udp.println();
udp.endPacket();
}
void setup() {
Ethernet.begin(mac, localIp);
udp.begin(localPort);
}
void loop() {
delay(1000);
StaticJsonBuffer<300> jsonBuffer;
JsonObject& json = buildJson(jsonBuffer);
sendJson(json);
}

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
@ -8,60 +8,39 @@
#include "JsonBuffer.hpp"
#include <stdlib.h>
namespace ArduinoJson {
// Forward declaration
namespace Internals {
struct DynamicJsonBufferBlock;
}
// Implements a JsonBuffer with dynamic memory allocation.
// You are strongly encouraged to consider using StaticJsonBuffer which is much
// more suitable for embedded systems.
class DynamicJsonBuffer : public JsonBuffer {
public:
DynamicJsonBuffer() : _next(NULL), _size(0) {}
DynamicJsonBuffer();
~DynamicJsonBuffer();
~DynamicJsonBuffer() { delete _next; }
size_t size() const { return _size + (_next ? _next->size() : 0); }
size_t blockCount() const { return 1 + (_next ? _next->blockCount() : 0); }
static const size_t BLOCK_CAPACITY = 32;
size_t size() const;
protected:
virtual void* alloc(size_t bytes) {
if (canAllocInThisBlock(bytes))
return allocInThisBlock(bytes);
else if (canAllocInOtherBlocks(bytes))
return allocInOtherBlocks(bytes);
else
return NULL;
}
virtual void* alloc(size_t bytes);
private:
bool canAllocInThisBlock(size_t bytes) const {
return _size + bytes <= BLOCK_CAPACITY;
}
typedef Internals::DynamicJsonBufferBlock Block;
void* allocInThisBlock(size_t bytes) {
void* p = _buffer + _size;
_size += bytes;
return p;
}
static const size_t FIRST_BLOCK_CAPACITY = 32;
bool canAllocInOtherBlocks(size_t bytes) const {
// by design a DynamicJsonBuffer can't alloc a block bigger than
// BLOCK_CAPACITY
return bytes <= BLOCK_CAPACITY;
}
static Block* createBlock(size_t capacity);
void* allocInOtherBlocks(size_t bytes) {
if (!_next) {
_next = new DynamicJsonBuffer();
if (!_next) return NULL;
}
return _next->alloc(bytes);
}
inline bool canAllocInHead(size_t bytes) const;
inline void* allocInHead(size_t bytes);
inline void addNewBlock();
DynamicJsonBuffer* _next;
size_t _size;
uint8_t _buffer[BLOCK_CAPACITY];
Block* _head;
};
}

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -0,0 +1,23 @@
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#pragma once
#include "../JsonBuffer.hpp"
namespace ArduinoJson {
namespace Internals {
class JsonBufferAllocated {
public:
void *operator new(size_t n, JsonBuffer *jsonBuffer) throw() {
return jsonBuffer->alloc(n);
}
void operator delete(void *, JsonBuffer *) throw() {}
};
}
}

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
@ -9,7 +9,6 @@
#include "../JsonBuffer.hpp"
#include "ListConstIterator.hpp"
#include "ListIterator.hpp"
#include "PlacementNew.hpp"
namespace ArduinoJson {
namespace Internals {
@ -51,8 +50,7 @@ class List {
protected:
node_type *createNode() {
if (!_buffer) return NULL;
void *ptr = _buffer->alloc(sizeof(node_type));
return ptr ? new (ptr) node_type() : NULL;
return new (_buffer) node_type();
}
void addNode(node_type *nodeToAdd) {

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
@ -8,16 +8,18 @@
#include <stddef.h> // for NULL
#include "JsonBufferAllocated.hpp"
namespace ArduinoJson {
namespace Internals {
// A node for a singly-linked list.
// Used by List<T> and its iterators.
template <typename T>
struct ListNode {
struct ListNode : public Internals::JsonBufferAllocated {
ListNode() : next(NULL) {}
ListNode<T>* next;
ListNode<T> *next;
T content;
};
}

View File

@ -1,19 +0,0 @@
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#pragma once
#ifdef ARDUINO
// Declares the placement new as in <new>.
// This is required for Arduino IDE because it doesn't include the <new> header.
inline void *operator new(size_t, void *p) throw() { return p; }
#else
#include <new>
#endif

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
@ -6,6 +6,7 @@
#pragma once
#include "Internals/JsonBufferAllocated.hpp"
#include "Internals/JsonPrintable.hpp"
#include "Internals/List.hpp"
#include "Internals/ReferenceType.hpp"
@ -30,7 +31,8 @@ class JsonBuffer;
// It can also be deserialized from a JSON string via JsonBuffer::parseArray().
class JsonArray : public Internals::JsonPrintable<JsonArray>,
public Internals::ReferenceType,
public Internals::List<JsonVariant> {
public Internals::List<JsonVariant>,
public Internals::JsonBufferAllocated {
// JsonBuffer is a friend because it needs to call the private constructor.
friend class JsonBuffer;
@ -69,6 +71,9 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
// It's a shortcut for JsonBuffer::createObject() and JsonArray::add()
JsonObject &createNestedObject();
// Removes element at specified index.
void removeAt(int index);
// Returns a reference an invalid JsonArray.
// This object is meant to replace a NULL pointer.
// This is used when memory allocation or JSON parsing fail.
@ -82,6 +87,8 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
explicit JsonArray(JsonBuffer *buffer)
: Internals::List<JsonVariant>(buffer) {}
node_type *getNodeAt(int index) const;
// The instance returned by JsonArray::invalid()
static JsonArray _invalid;
};

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
@ -9,6 +9,12 @@
#include <stddef.h> // for size_t
#include <stdint.h> // for uint8_t
#if defined(__clang__)
#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
#elif defined(__GNUC__)
#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
#endif
namespace ArduinoJson {
class JsonArray;
class JsonObject;

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
@ -6,6 +6,7 @@
#pragma once
#include "Internals/JsonBufferAllocated.hpp"
#include "Internals/JsonPrintable.hpp"
#include "Internals/List.hpp"
#include "Internals/ReferenceType.hpp"
@ -30,7 +31,8 @@ class JsonBuffer;
// It can also be deserialized from a JSON string via JsonBuffer::parseObject().
class JsonObject : public Internals::JsonPrintable<JsonObject>,
public Internals::ReferenceType,
public Internals::List<JsonPair> {
public Internals::List<JsonPair>,
public Internals::JsonBufferAllocated {
// JsonBuffer is a friend because it needs to call the private constructor.
friend class JsonBuffer;

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,8 +0,0 @@
name=ArduinoJson
version=4.0
author=Benoit Blanchon <http://blog.benoitblanchon.fr/>
maintainer=Benoit Blanchon <http://blog.benoitblanchon.fr/>
sentence=An efficient and elegant JSON library for Arduino
paragraph=Supports JSON parsing and formatting. Uses fixed memory allocation.
url=https://github.com/bblanchon/ArduinoJson
architectures=*

View File

@ -15,7 +15,9 @@ rm -f $OUTPUT
ArduinoJson/examples \
ArduinoJson/include \
ArduinoJson/keywords.txt \
ArduinoJson/library.properties \
ArduinoJson/LICENSE.md \
ArduinoJson/README.md \
ArduinoJson/src
ArduinoJson/src \
ArduinoJson/ArduinoJson.h \
ArduinoJson/ArduinoJson.cpp \
-x!ArduinoJson/src/CMakeLists.txt

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
@ -8,7 +8,8 @@
#include "../../include/ArduinoJson/Arduino/Print.hpp"
#include <stdio.h> // for sprintf
#include <math.h> // for isnan() and isinf()
#include <stdio.h> // for sprintf()
size_t Print::print(const char s[]) {
size_t n = 0;
@ -19,8 +20,24 @@ size_t Print::print(const char s[]) {
}
size_t Print::print(double value, int digits) {
// https://github.com/arduino/Arduino/blob/db8cbf24c99dc930b9ccff1a43d018c81f178535/hardware/arduino/sam/cores/arduino/Print.cpp#L218
if (isnan(value)) return print("nan");
if (isinf(value)) return print("inf");
char tmp[32];
sprintf(tmp, "%.*f", digits, value);
// https://github.com/arduino/Arduino/blob/db8cbf24c99dc930b9ccff1a43d018c81f178535/hardware/arduino/sam/cores/arduino/Print.cpp#L220
bool isBigDouble = value > 4294967040.0 || value < -4294967040.0;
if (isBigDouble) {
// Arduino's implementation prints "ovf"
// We prefer trying to use scientific notation, since we have sprintf
sprintf(tmp, "%g", value);
} else {
// Here we have the exact same output as Arduino's implementation
sprintf(tmp, "%.*f", digits, value);
}
return print(tmp);
}

View File

@ -1,13 +0,0 @@
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
// About this file
// ---------------
// This file is here to please the Arduino IDE. It must be present in the src/
// for the IDE to find it. Feel free to ignore this file if your working in
// another environment
#include "../include/ArduinoJson.h"

View File

@ -20,6 +20,7 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang)")
-Wno-sign-conversion
-Wno-unused
-Wno-variadic-macros
-Wnon-virtual-dtor
-Wold-style-cast
-Woverloaded-virtual
-Wredundant-decls

79
src/DynamicJsonBuffer.cpp Normal file
View File

@ -0,0 +1,79 @@
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#include "../include/ArduinoJson/DynamicJsonBuffer.hpp"
namespace ArduinoJson {
namespace Internals {
struct DynamicJsonBufferBlockWithoutData {
DynamicJsonBufferBlock* next;
size_t capacity;
size_t size;
};
struct DynamicJsonBufferBlock : DynamicJsonBufferBlockWithoutData {
uint8_t data[1];
};
}
}
using namespace ArduinoJson;
using namespace ArduinoJson::Internals;
DynamicJsonBuffer::DynamicJsonBuffer() {
_head = createBlock(FIRST_BLOCK_CAPACITY);
}
DynamicJsonBuffer::~DynamicJsonBuffer() {
Block* currentBlock = _head;
while (currentBlock != NULL) {
Block* nextBlock = currentBlock->next;
free(currentBlock);
currentBlock = nextBlock;
}
}
size_t DynamicJsonBuffer::size() const {
size_t total = 0;
for (const Block* b = _head; b != NULL; b = b->next) {
total += b->size;
}
return total;
}
void* DynamicJsonBuffer::alloc(size_t bytes) {
if (!canAllocInHead(bytes)) addNewBlock();
return allocInHead(bytes);
}
bool DynamicJsonBuffer::canAllocInHead(size_t bytes) const {
return _head->size + bytes <= _head->capacity;
}
void* DynamicJsonBuffer::allocInHead(size_t bytes) {
void* p = _head->data + _head->size;
_head->size += bytes;
return p;
}
void DynamicJsonBuffer::addNewBlock() {
Block* block = createBlock(_head->capacity * 2);
block->next = _head;
_head = block;
}
DynamicJsonBuffer::Block* DynamicJsonBuffer::createBlock(size_t capacity) {
size_t blkSize = sizeof(DynamicJsonBufferBlockWithoutData) + capacity;
Block* block = static_cast<Block*>(malloc(blkSize));
block->capacity = capacity;
block->size = 0;
block->next = NULL;
return block;
}

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
@ -6,7 +6,6 @@
#include "../../include/ArduinoJson/Internals/List.hpp"
#include "../../include/ArduinoJson/Internals/PlacementNew.hpp"
#include "../../include/ArduinoJson/JsonPair.hpp"
#include "../../include/ArduinoJson/JsonVariant.hpp"

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
@ -8,16 +8,21 @@
using namespace ArduinoJson::Internals;
// How to escape special chars:
// specialChars[2*i+1] => the special char
// specialChars[2*i] => the char to use instead
static const char specialChars[] = "\"\"\\\\b\bf\fn\nr\rt\t";
static inline char getSpecialChar(char c) {
// Optimized for code size on a 8-bit AVR
const char *p = "\"\"\\\\\bb\ff\nn\rr\tt\0";
const char *p = specialChars;
while (p[0] && p[0] != c) {
while (p[0] && p[1] != c) {
p += 2;
}
return p[1];
return p[0];
}
static inline size_t printCharTo(char c, Print &p) {
@ -41,7 +46,7 @@ size_t QuotedString::printTo(const char *s, Print &p) {
static char unescapeChar(char c) {
// Optimized for code size on a 8-bit AVR
const char *p = "b\bf\fn\nr\rt\t";
const char *p = specialChars + 4;
for (;;) {
if (p[0] == '\0') return c;

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
@ -15,8 +15,7 @@ using namespace ArduinoJson::Internals;
JsonArray JsonArray::_invalid(NULL);
JsonVariant &JsonArray::at(int index) const {
node_type *node = _firstNode;
while (node && index--) node = node->next;
node_type *node = getNodeAt(index);
return node ? node->content : JsonVariant::invalid();
}
@ -43,6 +42,14 @@ JsonObject &JsonArray::createNestedObject() {
return object;
}
JsonArray::node_type *JsonArray::getNodeAt(int index) const {
node_type *node = _firstNode;
while (node && index--) node = node->next;
return node;
}
void JsonArray::removeAt(int index) { removeNode(getNodeAt(index)); }
void JsonArray::writeTo(JsonWriter &writer) const {
writer.beginArray();

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
@ -7,7 +7,6 @@
#include "../include/ArduinoJson/JsonBuffer.hpp"
#include "../include/ArduinoJson/Internals/JsonParser.hpp"
#include "../include/ArduinoJson/Internals/PlacementNew.hpp"
#include "../include/ArduinoJson/JsonArray.hpp"
#include "../include/ArduinoJson/JsonObject.hpp"
@ -15,15 +14,13 @@ using namespace ArduinoJson;
using namespace ArduinoJson::Internals;
JsonArray &JsonBuffer::createArray() {
void *ptr = alloc(sizeof(JsonArray));
if (ptr) return *new (ptr) JsonArray(this);
return JsonArray::invalid();
JsonArray *ptr = new (this) JsonArray(this);
return ptr ? *ptr : JsonArray::invalid();
}
JsonObject &JsonBuffer::createObject() {
void *ptr = alloc(sizeof(JsonObject));
if (ptr) return *new (ptr) JsonObject(this);
return JsonObject::invalid();
JsonObject *ptr = new (this) JsonObject(this);
return ptr ? *ptr : JsonObject::invalid();
}
JsonArray &JsonBuffer::parseArray(char *json, uint8_t nestingLimit) {

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
@ -8,7 +8,6 @@
#include <string.h> // for strcmp
#include "../include/ArduinoJson/Internals/PlacementNew.hpp"
#include "../include/ArduinoJson/Internals/StringBuilder.hpp"
#include "../include/ArduinoJson/JsonArray.hpp"
#include "../include/ArduinoJson/JsonBuffer.hpp"

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
@ -64,13 +64,13 @@ void JsonVariant::set(long value) {
void JsonVariant::set(JsonArray &array) {
if (_type == JSON_INVALID) return;
_type = JSON_ARRAY;
_type = array.success() ? JSON_ARRAY : JSON_INVALID;
_content.asArray = &array;
}
void JsonVariant::set(JsonObject &object) {
if (_type == JSON_INVALID) return;
_type = JSON_OBJECT;
_type = object.success() ? JSON_OBJECT : JSON_INVALID;
_content.asObject = &object;
}

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
@ -20,40 +20,11 @@ TEST_F(DynamicJsonBuffer_Basic_Tests, InitialSizeIsZero) {
ASSERT_EQ(0, buffer.size());
}
TEST_F(DynamicJsonBuffer_Basic_Tests, InitialBlockCountIsOne) {
ASSERT_EQ(1, buffer.blockCount());
}
TEST_F(DynamicJsonBuffer_Basic_Tests, SizeIncreasesAfterAlloc) {
buffer.alloc(1);
ASSERT_EQ(1, buffer.size());
buffer.alloc(1);
ASSERT_EQ(2, buffer.size());
buffer.alloc(DynamicJsonBuffer::BLOCK_CAPACITY);
ASSERT_EQ(2 + DynamicJsonBuffer::BLOCK_CAPACITY, buffer.size());
}
TEST_F(DynamicJsonBuffer_Basic_Tests, BlockCountDoesntChangeWhenNotFull) {
buffer.alloc(DynamicJsonBuffer::BLOCK_CAPACITY);
ASSERT_EQ(1, buffer.blockCount());
}
TEST_F(DynamicJsonBuffer_Basic_Tests, BlockCountChangesWhenFull) {
buffer.alloc(DynamicJsonBuffer::BLOCK_CAPACITY);
buffer.alloc(1);
ASSERT_EQ(2, buffer.blockCount());
}
TEST_F(DynamicJsonBuffer_Basic_Tests, CanAllocLessThanBlockCapacity) {
void* p1 = buffer.alloc(DynamicJsonBuffer::BLOCK_CAPACITY);
ASSERT_TRUE(p1);
void* p2 = buffer.alloc(DynamicJsonBuffer::BLOCK_CAPACITY);
ASSERT_TRUE(p2);
}
TEST_F(DynamicJsonBuffer_Basic_Tests, CantAllocMoreThanBlockCapacity) {
void* p = buffer.alloc(DynamicJsonBuffer::BLOCK_CAPACITY + 1);
ASSERT_FALSE(p);
}
TEST_F(DynamicJsonBuffer_Basic_Tests, ReturnDifferentPointer) {

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

47
test/Issue67.cpp Normal file
View File

@ -0,0 +1,47 @@
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#include <gtest/gtest.h>
#include <ArduinoJson.h>
class Issue67 : public testing::Test {
public:
void whenInputIs(double value) { _variant = value; }
void outputMustBe(const char* expected) {
char buffer[1024];
_variant.printTo(buffer, sizeof(buffer));
ASSERT_STREQ(expected, buffer);
}
private:
JsonVariant _variant;
};
TEST_F(Issue67, BigPositiveDouble) {
whenInputIs(1e100);
outputMustBe("1e+100");
}
TEST_F(Issue67, BigNegativeDouble) {
whenInputIs(-1e100);
outputMustBe("-1e+100");
}
TEST_F(Issue67, Zero) {
whenInputIs(0.0);
outputMustBe("0.00");
}
TEST_F(Issue67, SmallPositiveDouble) {
whenInputIs(111.111);
outputMustBe("111.11");
}
TEST_F(Issue67, SmallNegativeDouble) {
whenInputIs(-111.111);
outputMustBe("-111.11");
}

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
@ -48,6 +48,11 @@ class JsonArray_Container_Tests : public ::testing::Test {
}
};
template <>
void JsonArray_Container_Tests::itemMustEqual(int index, const char* expected) {
EXPECT_STREQ(expected, _array[index].asString());
}
TEST_F(JsonArray_Container_Tests, SuccessIsTrue) {
EXPECT_TRUE(_array.success());
}
@ -87,14 +92,11 @@ TEST_F(JsonArray_Container_Tests, CanStoreBooleans) {
}
TEST_F(JsonArray_Container_Tests, CanStoreStrings) {
const char* firstString = "h3110";
const char* secondString = "w0r1d";
_array.add("hello");
_array.add("world");
_array.add(firstString);
_array.add(secondString);
firstMustEqual(firstString);
secondMustEqual(secondString);
firstMustEqual("hello");
secondMustEqual("world");
}
TEST_F(JsonArray_Container_Tests, CanStoreNestedArrays) {
@ -134,3 +136,39 @@ TEST_F(JsonArray_Container_Tests, CanCreateNestedObjects) {
firstMustReference(innerObject1);
secondMustReference(innerObject2);
}
TEST_F(JsonArray_Container_Tests, RemoveFirstElement) {
_array.add("one");
_array.add("two");
_array.add("three");
_array.removeAt(0);
sizeMustBe(2);
firstMustEqual("two");
secondMustEqual("three");
}
TEST_F(JsonArray_Container_Tests, RemoveMiddleElement) {
_array.add("one");
_array.add("two");
_array.add("three");
_array.removeAt(1);
sizeMustBe(2);
firstMustEqual("one");
secondMustEqual("three");
}
TEST_F(JsonArray_Container_Tests, RemoveLastElement) {
_array.add("one");
_array.add("two");
_array.add("three");
_array.removeAt(2);
sizeMustBe(2);
firstMustEqual("one");
secondMustEqual("two");
}

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
@ -62,11 +62,13 @@ TEST_F(JsonVariant_Undefined_Tests, CanBeSetToBool) {
}
TEST_F(JsonVariant_Undefined_Tests, CanBeSetToArray) {
variant = JsonArray::invalid();
DynamicJsonBuffer jsonBuffer;
variant = jsonBuffer.createArray();
EXPECT_TRUE(variant.success());
}
TEST_F(JsonVariant_Undefined_Tests, CanBeSetToObject) {
variant = JsonObject::invalid();
DynamicJsonBuffer jsonBuffer;
variant = jsonBuffer.createObject();
EXPECT_TRUE(variant.success());
}

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
@ -7,7 +7,7 @@
#include <gtest/gtest.h>
#include <ArduinoJson.h>
TEST(StaticJsonBuffer_Array_Tests, GrowsWithArray) {
TEST(StaticJsonBuffer_CreateArray_Tests, GrowsWithArray) {
StaticJsonBuffer<JSON_ARRAY_SIZE(2)> json;
JsonArray &array = json.createArray();
@ -20,21 +20,21 @@ TEST(StaticJsonBuffer_Array_Tests, GrowsWithArray) {
ASSERT_EQ(JSON_ARRAY_SIZE(2), json.size());
}
TEST(StaticJsonBuffer_Array_Tests, SucceedWhenBigEnough) {
TEST(StaticJsonBuffer_CreateArray_Tests, SucceedWhenBigEnough) {
StaticJsonBuffer<JSON_ARRAY_SIZE(0)> json;
JsonArray &array = json.createArray();
ASSERT_TRUE(array.success());
}
TEST(StaticJsonBuffer_Array_Tests, FailsWhenTooSmall) {
TEST(StaticJsonBuffer_CreateArray_Tests, FailsWhenTooSmall) {
StaticJsonBuffer<JSON_ARRAY_SIZE(0) - 1> json;
JsonArray &array = json.createArray();
ASSERT_FALSE(array.success());
}
TEST(StaticJsonBuffer_Array_Tests, ArrayDoesntGrowWhenFull) {
TEST(StaticJsonBuffer_CreateArray_Tests, ArrayDoesntGrowWhenFull) {
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> json;
JsonArray &array = json.createArray();

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
@ -7,7 +7,7 @@
#include <gtest/gtest.h>
#include <ArduinoJson.h>
TEST(StaticJsonBuffer_Object_Tests, GrowsWithObject) {
TEST(StaticJsonBuffer_CreateObject_Tests, GrowsWithObject) {
StaticJsonBuffer<JSON_OBJECT_SIZE(3)> json;
JsonObject &obj = json.createObject();
@ -23,21 +23,21 @@ TEST(StaticJsonBuffer_Object_Tests, GrowsWithObject) {
ASSERT_EQ(JSON_OBJECT_SIZE(2), json.size());
}
TEST(StaticJsonBuffer_Object_Tests, SucceedWhenBigEnough) {
TEST(StaticJsonBuffer_CreateObject_Tests, SucceedWhenBigEnough) {
StaticJsonBuffer<JSON_OBJECT_SIZE(0)> json;
JsonObject &object = json.createObject();
ASSERT_TRUE(object.success());
}
TEST(StaticJsonBuffer_Object_Tests, FailsWhenTooSmall) {
TEST(StaticJsonBuffer_CreateObject_Tests, FailsWhenTooSmall) {
StaticJsonBuffer<JSON_OBJECT_SIZE(0) - 1> json;
JsonObject &object = json.createObject();
ASSERT_FALSE(object.success());
}
TEST(StaticJsonBuffer_Object_Tests, ObjectDoesntGrowWhenFull) {
TEST(StaticJsonBuffer_CreateObject_Tests, ObjectDoesntGrowWhenFull) {
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> json;
JsonObject &obj = json.createObject();

View File

@ -0,0 +1,72 @@
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#include <gtest/gtest.h>
#include <ArduinoJson.h>
class StaticJsonBuffer_ParseArray_Tests : public testing::Test {
protected:
void with(JsonBuffer& jsonBuffer) { _jsonBuffer = &jsonBuffer; }
void whenInputIs(const char* json) { strcpy(_jsonString, json); }
void parseMustSucceed() {
EXPECT_TRUE(_jsonBuffer->parseArray(_jsonString).success());
}
void parseMustFail() {
EXPECT_FALSE(_jsonBuffer->parseArray(_jsonString).success());
}
private:
JsonBuffer* _jsonBuffer;
char _jsonString[256];
};
TEST_F(StaticJsonBuffer_ParseArray_Tests, TooSmallBufferForEmptyArray) {
StaticJsonBuffer<JSON_ARRAY_SIZE(0) - 1> bufferTooSmall;
with(bufferTooSmall);
whenInputIs("[]");
parseMustFail();
}
TEST_F(StaticJsonBuffer_ParseArray_Tests, BufferOfTheRightSizeForEmptyArray) {
StaticJsonBuffer<JSON_ARRAY_SIZE(0)> bufferOfRightSize;
with(bufferOfRightSize);
whenInputIs("[]");
parseMustSucceed();
}
TEST_F(StaticJsonBuffer_ParseArray_Tests, TooSmallBufferForArrayWithOneValue) {
StaticJsonBuffer<JSON_ARRAY_SIZE(1) - 1> bufferTooSmall;
with(bufferTooSmall);
whenInputIs("[1]");
parseMustFail();
}
TEST_F(StaticJsonBuffer_ParseArray_Tests,
BufferOfTheRightSizeForArrayWithOneValue) {
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> bufferOfRightSize;
with(bufferOfRightSize);
whenInputIs("[1]");
parseMustSucceed();
}
TEST_F(StaticJsonBuffer_ParseArray_Tests,
TooSmallBufferForArrayWithNestedObject) {
StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0) - 1> bufferTooSmall;
with(bufferTooSmall);
whenInputIs("[{}]");
parseMustFail();
}
TEST_F(StaticJsonBuffer_ParseArray_Tests,
BufferOfTheRightSizeForArrayWithNestedObject) {
StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0)> bufferOfRightSize;
with(bufferOfRightSize);
whenInputIs("[{}]");
parseMustSucceed();
}

View File

@ -0,0 +1,73 @@
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#include <gtest/gtest.h>
#include <ArduinoJson.h>
class StaticJsonBuffer_ParseObject_Tests : public testing::Test {
protected:
void with(JsonBuffer& jsonBuffer) { _jsonBuffer = &jsonBuffer; }
void whenInputIs(const char* json) { strcpy(_jsonString, json); }
void parseMustSucceed() {
EXPECT_TRUE(_jsonBuffer->parseObject(_jsonString).success());
}
void parseMustFail() {
EXPECT_FALSE(_jsonBuffer->parseObject(_jsonString).success());
}
private:
JsonBuffer* _jsonBuffer;
char _jsonString[256];
};
TEST_F(StaticJsonBuffer_ParseObject_Tests, TooSmallBufferForEmptyObject) {
StaticJsonBuffer<JSON_OBJECT_SIZE(0) - 1> bufferTooSmall;
with(bufferTooSmall);
whenInputIs("{}");
parseMustFail();
}
TEST_F(StaticJsonBuffer_ParseObject_Tests, BufferOfTheRightSizeForEmptyObject) {
StaticJsonBuffer<JSON_OBJECT_SIZE(0)> bufferOfRightSize;
with(bufferOfRightSize);
whenInputIs("{}");
parseMustSucceed();
}
TEST_F(StaticJsonBuffer_ParseObject_Tests,
TooSmallBufferForObjectWithOneValue) {
StaticJsonBuffer<JSON_OBJECT_SIZE(1) - 1> bufferTooSmall;
with(bufferTooSmall);
whenInputIs("{\"a\":1}");
parseMustFail();
}
TEST_F(StaticJsonBuffer_ParseObject_Tests,
BufferOfTheRightSizeForObjectWithOneValue) {
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> bufferOfRightSize;
with(bufferOfRightSize);
whenInputIs("{\"a\":1}");
parseMustSucceed();
}
TEST_F(StaticJsonBuffer_ParseObject_Tests,
TooSmallBufferForObjectWithNestedObject) {
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0) - 1> bufferTooSmall;
with(bufferTooSmall);
whenInputIs("{\"a\":[]}");
parseMustFail();
}
TEST_F(StaticJsonBuffer_ParseObject_Tests,
BufferOfTheRightSizeForObjectWithNestedObject) {
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0)> bufferOfRightSize;
with(bufferOfRightSize);
whenInputIs("{\"a\":[]}");
parseMustSucceed();
}

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library