[209] | 1 | // Copyright Benoit Blanchon 2014-2016
|
---|
| 2 | // MIT License
|
---|
| 3 | //
|
---|
| 4 | // Arduino JSON library
|
---|
| 5 | // https://github.com/bblanchon/ArduinoJson
|
---|
| 6 | // If you like this project, please add a star!
|
---|
| 7 |
|
---|
| 8 | #pragma once
|
---|
| 9 |
|
---|
| 10 | #include "../Arduino/Print.hpp"
|
---|
| 11 | #include "Encoding.hpp"
|
---|
| 12 | #include "ForceInline.hpp"
|
---|
| 13 | #include "JsonFloat.hpp"
|
---|
| 14 | #include "JsonInteger.hpp"
|
---|
| 15 |
|
---|
| 16 | namespace ArduinoJson {
|
---|
| 17 | namespace Internals {
|
---|
| 18 |
|
---|
| 19 | // Writes the JSON tokens to a Print implementation
|
---|
| 20 | // This class is used by:
|
---|
| 21 | // - JsonArray::writeTo()
|
---|
| 22 | // - JsonObject::writeTo()
|
---|
| 23 | // - JsonVariant::writeTo()
|
---|
| 24 | // Its derived by PrettyJsonWriter that overrides some members to add
|
---|
| 25 | // indentation.
|
---|
| 26 | class JsonWriter {
|
---|
| 27 | public:
|
---|
| 28 | explicit JsonWriter(Print &sink) : _sink(sink), _length(0) {}
|
---|
| 29 |
|
---|
| 30 | // Returns the number of bytes sent to the Print implementation.
|
---|
| 31 | // This is very handy for implementations of printTo() that must return the
|
---|
| 32 | // number of bytes written.
|
---|
| 33 | size_t bytesWritten() const { return _length; }
|
---|
| 34 |
|
---|
| 35 | void beginArray() { write('['); }
|
---|
| 36 | void endArray() { write(']'); }
|
---|
| 37 |
|
---|
| 38 | void beginObject() { write('{'); }
|
---|
| 39 | void endObject() { write('}'); }
|
---|
| 40 |
|
---|
| 41 | void writeColon() { write(':'); }
|
---|
| 42 | void writeComma() { write(','); }
|
---|
| 43 |
|
---|
| 44 | void writeBoolean(bool value) { write(value ? "true" : "false"); }
|
---|
| 45 |
|
---|
| 46 | void writeString(const char *value) {
|
---|
| 47 | if (!value) {
|
---|
| 48 | write("null");
|
---|
| 49 | } else {
|
---|
| 50 | write('\"');
|
---|
| 51 | while (*value) writeChar(*value++);
|
---|
| 52 | write('\"');
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | void writeChar(char c) {
|
---|
| 57 | char specialChar = Encoding::escapeChar(c);
|
---|
| 58 | if (specialChar) {
|
---|
| 59 | write('\\');
|
---|
| 60 | write(specialChar);
|
---|
| 61 | } else {
|
---|
| 62 | write(c);
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | void writeInteger(JsonInteger value) { _length += _sink.print(value); }
|
---|
| 67 |
|
---|
| 68 | void writeFloat(JsonFloat value, uint8_t decimals) {
|
---|
| 69 | _length += _sink.print(value, decimals);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | void writeRaw(const char *s) { return write(s); }
|
---|
| 73 |
|
---|
| 74 | protected:
|
---|
| 75 | void write(char c) { _length += _sink.write(c); }
|
---|
| 76 | FORCE_INLINE void write(const char *s) { _length += _sink.print(s); }
|
---|
| 77 |
|
---|
| 78 | Print &_sink;
|
---|
| 79 | size_t _length;
|
---|
| 80 |
|
---|
| 81 | private:
|
---|
| 82 | JsonWriter &operator=(const JsonWriter &); // cannot be assigned
|
---|
| 83 | };
|
---|
| 84 | }
|
---|
| 85 | }
|
---|