Skip to content

Commit

Permalink
Store parsed token as string and allow conversion between various typ…
Browse files Browse the repository at this point in the history
…es (issues #64, #69, #90, #93)
  • Loading branch information
bblanchon committed Aug 10, 2015
1 parent bce1015 commit ef2641b
Show file tree
Hide file tree
Showing 29 changed files with 685 additions and 273 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ v5.0 (currently in beta)
* Implicitly call `strdup()` for `String` but not for `char*` (issues #84, #87)
* Added support of non standard JSON input (issue #44)
* Added support of comments in JSON input (issue #88)
* Added implicit cast between numerical types (issues #64, #69, #93)
* Added ability to read number values as string (issue #90)
* Redesigned `JsonVariant` to leverage converting constructors instead of assignment operators (issue #66)
* Switched to new the library layout (requires Arduino 1.0.6 or above)

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ It has been written with Arduino in mind, but it isn't linked to Arduino librari
Features
--------

* JSON decoding
* JSON decoding (comments are supported)
* JSON encoding (with optional indentation)
* Elegant API, very easy to use
* Efficient (no malloc, nor copy)
Expand Down Expand Up @@ -80,4 +80,4 @@ From GitHub user `zacsketches`:
---

Found this library useful? [Help me back with a donation!](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=donate%40benoitblanchon%2efr&lc=GB&item_name=Benoit%20Blanchon&item_number=Arduino%20JSON&currency_code=EUR&bn=PP%2dDonationsBF%3abtn_donate_LG%2egif%3aNonHosted) :smile:
Found this library useful? [Help me back with a donation!](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=donate%40benoitblanchon%2efr&lc=GB&item_name=Benoit%20Blanchon&item_number=Arduino%20JSON&currency_code=EUR&bn=PP%2dDonationsBF%3abtn_donate_LG%2egif%3aNonHosted) :smile:
2 changes: 1 addition & 1 deletion include/ArduinoJson/Arduino/Print.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <stddef.h>
#include <stdint.h>

// This class reproduces Arduino's Print
// This class reproduces Arduino's Print class
class Print {
public:
virtual ~Print() {}
Expand Down
15 changes: 14 additions & 1 deletion include/ArduinoJson/Arduino/String.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,20 @@

#include <string>

typedef std::string String;
// This class reproduces Arduino's String class
class String : public std::string {
public:
String(const char *cstr = "") : std::string(cstr) {}
String(const String &str) : std::string(str) {}
explicit String(char c);
explicit String(unsigned char);
explicit String(int);
explicit String(unsigned int);
explicit String(long);
explicit String(unsigned long);
explicit String(float, unsigned char decimalPlaces = 2);
explicit String(double, unsigned char decimalPlaces = 2);
};

#else

Expand Down
29 changes: 29 additions & 0 deletions include/ArduinoJson/Internals/DynamicStringBuilder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson

#pragma once

#include "../Arduino/Print.hpp"
#include "../Arduino/String.hpp"

namespace ArduinoJson {
namespace Internals {

// A Print implementation that allows to write in a String
class DynamicStringBuilder : public Print {
public:
DynamicStringBuilder(String &str) : _str(str) {}

virtual size_t write(uint8_t c) {
_str += c;
return 1;
}

private:
String &_str;
};
}
}
4 changes: 0 additions & 4 deletions include/ArduinoJson/Internals/JsonParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,12 @@ class JsonParser {

private:
bool skip(char charToSkip);
bool skip(const char *wordToSkip);

const char *parseString();
bool parseAnythingTo(JsonVariant *destination);
FORCE_INLINE bool parseAnythingToUnsafe(JsonVariant *destination);

inline bool parseArrayTo(JsonVariant *destination);
inline bool parseBooleanTo(JsonVariant *destination);
inline bool parseNullTo(JsonVariant *destination);
inline bool parseNumberTo(JsonVariant *destination);
inline bool parseObjectTo(JsonVariant *destination);
inline bool parseStringTo(JsonVariant *destination);

Expand Down
26 changes: 18 additions & 8 deletions include/ArduinoJson/Internals/JsonPrintable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
#include "IndentedPrint.hpp"
#include "JsonWriter.hpp"
#include "Prettyfier.hpp"
#include "StringBuilder.hpp"
#include "StaticStringBuilder.hpp"
#include "DynamicStringBuilder.hpp"

#ifdef ARDUINOJSON_ENABLE_STD_STREAM
#include "StreamPrintAdapter.hpp"
Expand All @@ -33,15 +34,20 @@ class JsonPrintable {
}

#ifdef ARDUINOJSON_ENABLE_STD_STREAM
std::ostream& printTo(std::ostream &os) const {
std::ostream &printTo(std::ostream &os) const {
StreamPrintAdapter adapter(os);
printTo(adapter);
return os;
}
#endif
#endif

size_t printTo(char *buffer, size_t bufferSize) const {
StringBuilder sb(buffer, bufferSize);
StaticStringBuilder sb(buffer, bufferSize);
return printTo(sb);
}

size_t printTo(String &str) const {
DynamicStringBuilder sb(str);
return printTo(sb);
}

Expand All @@ -51,7 +57,7 @@ class JsonPrintable {
}

size_t prettyPrintTo(char *buffer, size_t bufferSize) const {
StringBuilder sb(buffer, bufferSize);
StaticStringBuilder sb(buffer, bufferSize);
return prettyPrintTo(sb);
}

Expand All @@ -60,6 +66,11 @@ class JsonPrintable {
return prettyPrintTo(indentedPrint);
}

size_t prettyPrintTo(String &str) const {
DynamicStringBuilder sb(str);
return prettyPrintTo(sb);
}

size_t measureLength() const {
DummyPrint dp;
return printTo(dp);
Expand All @@ -75,11 +86,10 @@ class JsonPrintable {
};

#ifdef ARDUINOJSON_ENABLE_STD_STREAM
template<typename T>
inline std::ostream& operator<<(std::ostream& os, const JsonPrintable<T>& v) {
template <typename T>
inline std::ostream &operator<<(std::ostream &os, const JsonPrintable<T> &v) {
return v.printTo(os);
}
#endif

}
}
8 changes: 1 addition & 7 deletions include/ArduinoJson/Internals/JsonVariantContent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,11 @@ namespace Internals {
// A union that defines the actual content of a JsonVariant.
// The enum JsonVariantType determines which member is in use.
union JsonVariantContent {
bool asBoolean;
double asDouble; // asDouble is also used for float
long asLong; // asLong is also used for char, short and int
long asLong; // asLong is also used for bool, char, short and int
const char* asString; // asString can be null
JsonArray* asArray; // asArray cannot be null
JsonObject* asObject; // asObject cannot be null

template <typename T>
T as() const;
};
}
}

#include "JsonVariantContent.ipp"
96 changes: 0 additions & 96 deletions include/ArduinoJson/Internals/JsonVariantContent.ipp

This file was deleted.

7 changes: 4 additions & 3 deletions include/ArduinoJson/Internals/JsonVariantType.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ namespace Internals {
// The value determines which member of JsonVariantContent is used.
enum JsonVariantType {
JSON_UNDEFINED, // the JsonVariant has not been initialized
JSON_ARRAY, // the JsonVariant stores a pointer to a JsonArray
JSON_OBJECT, // the JsonVariant stores a pointer to a JsonObject
JSON_BOOLEAN, // the JsonVariant stores a bool
JSON_UNPARSED, // the JsonVariant contains an unparsed string
JSON_STRING, // the JsonVariant stores a const char*
JSON_BOOLEAN, // the JsonVariant stores a bool
JSON_LONG, // the JsonVariant stores a long
JSON_ARRAY, // the JsonVariant stores a pointer to a JsonArray
JSON_OBJECT, // the JsonVariant stores a pointer to a JsonObject

// The following values are reserved for double values
// Multiple values are used for double, depending on the number of decimal
Expand Down
8 changes: 4 additions & 4 deletions include/ArduinoJson/Internals/JsonWriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ class JsonWriter {
void writeColon() { write(':'); }
void writeComma() { write(','); }

void writeBoolean(bool value) {
write(value ? "true" : "false");
}

void writeBoolean(bool value) { write(value ? "true" : "false"); }

void writeString(const char *value) {
if (!value) {
write("null");
Expand All @@ -67,6 +65,8 @@ class JsonWriter {
_length += _sink.print(value, decimals);
}

void writeRaw(const char *s) { return write(s); }

protected:
void write(char c) { _length += _sink.write(c); }
void write(const char *s) { _length += _sink.print(s); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace ArduinoJson {
namespace Internals {

// A Print implementation that allows to write in a char[]
class StringBuilder : public Print {
class StaticStringBuilder : public Print {
public:
StringBuilder(char *buf, int size)
StaticStringBuilder(char *buf, int size)
: buffer(buf), capacity(size - 1), length(0) {
buffer[0] = '\0';
}
Expand Down
20 changes: 20 additions & 0 deletions include/ArduinoJson/Internals/Unparsed.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson

#pragma once

namespace ArduinoJson {
namespace Internals {
class Unparsed {
public:
explicit Unparsed(const char* str) : _str(str) {}
operator const char*() const { return _str; }

private:
const char* _str;
};
}
}
12 changes: 12 additions & 0 deletions include/ArduinoJson/JsonArray.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,16 @@ template <>
inline JsonArray const &JsonVariant::invalid<JsonArray const &>() {
return JsonArray::invalid();
}

template <>
inline JsonArray &JsonVariant::as<JsonArray &>() const {
if (_type == Internals::JSON_ARRAY) return *_content.asArray;
return JsonArray::invalid();
}

template <>
inline const JsonArray &JsonVariant::as<const JsonArray &>() const {
if (_type == Internals::JSON_ARRAY) return *_content.asArray;
return JsonArray::invalid();
}
}
12 changes: 12 additions & 0 deletions include/ArduinoJson/JsonObject.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,16 @@ template <>
inline JsonObject &JsonVariant::invalid<JsonObject &>() {
return JsonObject::invalid();
}

template <>
inline JsonObject &JsonVariant::as<JsonObject &>() const {
if (_type == Internals::JSON_OBJECT) return *_content.asObject;
return JsonObject::invalid();
}

template <>
inline const JsonObject &JsonVariant::as<const JsonObject &>() const {
if (_type == Internals::JSON_OBJECT) return *_content.asObject;
return JsonObject::invalid();
}
}
Loading

0 comments on commit ef2641b

Please sign in to comment.