Store parsed token as string and allow conversion between various types (issues #64, #69, #90, #93)

This commit is contained in:
Benoit Blanchon
2015-08-10 17:22:22 +02:00
parent bce101578d
commit ef2641b49b
29 changed files with 686 additions and 274 deletions

View File

@@ -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;
};
}
}

View File

@@ -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);

View File

@@ -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"
@@ -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);
}
@@ -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);
}
@@ -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);
@@ -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
}
}

View File

@@ -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"

View File

@@ -1,96 +0,0 @@
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#pragma once
namespace ArduinoJson {
// Forward declarations
class JsonArray;
class JsonObject;
namespace Internals {
template <>
inline bool JsonVariantContent::as<bool>() const {
return asBoolean;
}
template <>
inline char const* JsonVariantContent::as<char const*>() const {
return asString;
}
template <>
inline double JsonVariantContent::as<double>() const {
return asDouble;
}
template <>
inline float JsonVariantContent::as<float>() const {
return static_cast<float>(asDouble);
}
template <>
inline JsonArray& JsonVariantContent::as<JsonArray&>() const {
return *asArray;
}
template <>
inline const JsonArray& JsonVariantContent::as<JsonArray const&>() const {
return *asArray;
}
template <>
inline JsonObject& JsonVariantContent::as<JsonObject&>() const {
return *asObject;
}
template <>
inline const JsonObject& JsonVariantContent::as<JsonObject const&>() const {
return *asObject;
}
template <>
inline signed char JsonVariantContent::as<signed char>() const {
return static_cast<signed char>(asLong);
}
template <>
inline signed int JsonVariantContent::as<signed int>() const {
return static_cast<signed int>(asLong);
}
template <>
inline signed long JsonVariantContent::as<signed long>() const {
return static_cast<signed long>(asLong);
}
template <>
inline signed short JsonVariantContent::as<signed short>() const {
return static_cast<signed short>(asLong);
}
template <>
inline unsigned char JsonVariantContent::as<unsigned char>() const {
return static_cast<unsigned char>(asLong);
}
template <>
inline unsigned int JsonVariantContent::as<unsigned int>() const {
return static_cast<unsigned int>(asLong);
}
template <>
inline unsigned long JsonVariantContent::as<unsigned long>() const {
return static_cast<unsigned long>(asLong);
}
template <>
inline unsigned short JsonVariantContent::as<unsigned short>() const {
return static_cast<unsigned short>(asLong);
}
}
}

View File

@@ -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_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
JSON_BOOLEAN, // the JsonVariant stores a bool
JSON_STRING, // the JsonVariant stores a const char*
JSON_LONG, // the JsonVariant stores a long
// The following values are reserved for double values
// Multiple values are used for double, depending on the number of decimal

View File

@@ -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");
@@ -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); }

View File

@@ -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';
}

View File

@@ -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;
};
}
}