Added comments

This commit is contained in:
Benoit Blanchon
2014-11-06 13:42:16 +01:00
parent a3425a6306
commit 79bfe731af
6 changed files with 43 additions and 13 deletions

View File

@ -10,6 +10,7 @@
namespace ArduinoJson { namespace ArduinoJson {
namespace Internals { namespace Internals {
// Decorator on top of Print to allow indented output. // Decorator on top of Print to allow indented output.
// This class is used by JsonPrintable::prettyPrintTo() but can also be used // This class is used by JsonPrintable::prettyPrintTo() but can also be used
// for your own purpose, like logging. // for your own purpose, like logging.

View File

@ -11,6 +11,9 @@
namespace ArduinoJson { namespace ArduinoJson {
namespace Internals { namespace Internals {
// Parse JSON string to create JsonArrays and JsonObjects
// This internal class is not indended to be used directly.
// Instead, use JsonBuffer.parseArray() or .parseObject()
class JsonParser { class JsonParser {
public: public:
JsonParser(JsonBuffer *buffer, char *json, uint8_t nestingLimit) JsonParser(JsonBuffer *buffer, char *json, uint8_t nestingLimit)

View File

@ -13,6 +13,10 @@
namespace ArduinoJson { namespace ArduinoJson {
namespace Internals { namespace Internals {
// Implements all the overloads of printTo() and prettyPrintTo()
// Caution: this class use a template parameter to avoid virtual methods.
// This is a bit curious but allows to reduce the size of JsonVariant, JsonArray
// and JsonObject.
template <typename T> template <typename T>
class JsonPrintable { class JsonPrintable {
public: public:
@ -21,6 +25,7 @@ class JsonPrintable {
downcast().writeTo(writer); downcast().writeTo(writer);
return writer.bytesWritten(); return writer.bytesWritten();
} }
size_t printTo(char *buffer, size_t bufferSize) const { size_t printTo(char *buffer, size_t bufferSize) const {
StringBuilder sb(buffer, bufferSize); StringBuilder sb(buffer, bufferSize);
return printTo(sb); return printTo(sb);
@ -36,6 +41,7 @@ class JsonPrintable {
StringBuilder sb(buffer, bufferSize); StringBuilder sb(buffer, bufferSize);
return prettyPrintTo(sb); return prettyPrintTo(sb);
} }
size_t prettyPrintTo(Print &print) const { size_t prettyPrintTo(Print &print) const {
IndentedPrint indentedPrint = IndentedPrint(print); IndentedPrint indentedPrint = IndentedPrint(print);
return prettyPrintTo(indentedPrint); return prettyPrintTo(indentedPrint);

View File

@ -8,18 +8,21 @@
namespace ArduinoJson { namespace ArduinoJson {
// Forward declarations
class JsonArray; class JsonArray;
class JsonObject; class JsonObject;
namespace Internals { namespace Internals {
// A union that defines the actual content of a JsonVariant.
// The enum JsonVariantType determines which member is in use.
union JsonVariantContent { union JsonVariantContent {
bool asBoolean; bool asBoolean;
double asDouble; double asDouble; // asDouble is also used for float
long asLong; long asLong; // asLong is also used for char, short and int
const char* asString; const char* asString; // asString can be null
JsonArray* asArray; JsonArray* asArray; // asArray cannot be null
JsonObject* asObject; JsonObject* asObject; // asObject cannot be null
}; };
} }
} }

View File

@ -9,18 +9,25 @@
namespace ArduinoJson { namespace ArduinoJson {
namespace Internals { namespace Internals {
// Enumerated type to know the current type of a JsonVariant.
// The value determines which member of JsonVariantContent is used.
enum JsonVariantType { enum JsonVariantType {
JSON_UNDEFINED, JSON_INVALID, // a special state for JsonVariant::invalid()
JSON_INVALID, JSON_UNDEFINED, // the JsonVariant has not been initialized
JSON_ARRAY, JSON_ARRAY, // the JsonVariant stores a pointer to a JsonArray
JSON_OBJECT, JSON_OBJECT, // the JsonVariant stores a pointer to a JsonObject
JSON_BOOLEAN, JSON_BOOLEAN, // the JsonVariant stores a bool
JSON_STRING, JSON_STRING, // the JsonVariant stores a const char*
JSON_LONG, 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
// digits that must be printed in the JSON output.
// This little trick allow to save one extra member in JsonVariant
JSON_DOUBLE_0_DECIMALS JSON_DOUBLE_0_DECIMALS
// JSON_DOUBLE_1_DECIMAL // JSON_DOUBLE_1_DECIMAL
// JSON_DOUBLE_2_DECIMALS // JSON_DOUBLE_2_DECIMALS
// etc. // ...
}; };
} }
} }

View File

@ -11,10 +11,20 @@
namespace ArduinoJson { namespace ArduinoJson {
namespace Internals { namespace Internals {
// Writes the JSON tokens to a Print implementation
// This class is used by:
// - JsonArray::writeTo()
// - JsonObject::writeTo()
// - JsonVariant::writeTo()
// Its derived by PrettyJsonWriter that overrides some members to add
// indentation.
class JsonWriter { class JsonWriter {
public: public:
explicit JsonWriter(Print *sink) : _sink(sink), _length(0) {} explicit JsonWriter(Print *sink) : _sink(sink), _length(0) {}
// Returns the number of bytes sent to the Print implementation.
// This is very handy for implementations of printTo() that must return the
// number of bytes written.
size_t bytesWritten() { return _length; } size_t bytesWritten() { return _length; }
void beginArray() { _length += _sink->write('['); } void beginArray() { _length += _sink->write('['); }