/src/arduinojson/src/ArduinoJson/Json/PrettyJsonSerializer.hpp
Line | Count | Source |
1 | | // ArduinoJson - https://arduinojson.org |
2 | | // Copyright © 2014-2026, Benoit BLANCHON |
3 | | // MIT License |
4 | | |
5 | | #pragma once |
6 | | |
7 | | #include <ArduinoJson/Configuration.hpp> |
8 | | #include <ArduinoJson/Json/JsonSerializer.hpp> |
9 | | #include <ArduinoJson/Serialization/measure.hpp> |
10 | | #include <ArduinoJson/Serialization/serialize.hpp> |
11 | | |
12 | | ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE |
13 | | |
14 | | template <typename TWriter> |
15 | | class PrettyJsonSerializer : public JsonSerializer<TWriter> { |
16 | | using base = JsonSerializer<TWriter>; |
17 | | |
18 | | public: |
19 | | PrettyJsonSerializer(TWriter writer, ResourceManager* resources) |
20 | | : base(writer, resources), nesting_(0) {} |
21 | | |
22 | 0 | size_t visitArray(VariantData* array) { |
23 | 0 | ARDUINOJSON_ASSERT(array != nullptr); |
24 | 0 | ARDUINOJSON_ASSERT(array->isArray()); |
25 | 0 |
|
26 | 0 | auto slotId = array->content.asCollection.head; |
27 | 0 | if (slotId != NULL_SLOT) { |
28 | 0 | base::write("[\r\n"); |
29 | 0 | nesting_++; |
30 | 0 | while (slotId != NULL_SLOT) { |
31 | 0 | indent(); |
32 | 0 | auto slot = base::resources_->getVariant(slotId); |
33 | 0 | VariantImpl::accept(*this, slot, base::resources_); |
34 | 0 |
|
35 | 0 | slotId = slot->next; |
36 | 0 | base::write(slotId == NULL_SLOT ? "\r\n" : ",\r\n"); |
37 | 0 | } |
38 | 0 | nesting_--; |
39 | 0 | indent(); |
40 | 0 | base::write("]"); |
41 | 0 | } else { |
42 | 0 | base::write("[]"); |
43 | 0 | } |
44 | 0 | return this->bytesWritten(); |
45 | 0 | } Unexecuted instantiation: ArduinoJson::V742HB42::detail::PrettyJsonSerializer<ArduinoJson::V742HB42::detail::StaticStringWriter>::visitArray(ArduinoJson::V742HB42::detail::VariantData*) Unexecuted instantiation: ArduinoJson::V742HB42::detail::PrettyJsonSerializer<ArduinoJson::V742HB42::detail::DummyWriter>::visitArray(ArduinoJson::V742HB42::detail::VariantData*) |
46 | | |
47 | 0 | size_t visitObject(VariantData* object) { |
48 | 0 | ARDUINOJSON_ASSERT(object != nullptr); |
49 | 0 | ARDUINOJSON_ASSERT(object->isObject()); |
50 | 0 |
|
51 | 0 | auto slotId = object->content.asCollection.head; |
52 | 0 | if (slotId != NULL_SLOT) { |
53 | 0 | base::write("{\r\n"); |
54 | 0 | nesting_++; |
55 | 0 | bool isKey = true; |
56 | 0 | while (slotId != NULL_SLOT) { |
57 | 0 | if (isKey) |
58 | 0 | indent(); |
59 | 0 | auto slot = base::resources_->getVariant(slotId); |
60 | 0 | VariantImpl::accept(*this, slot, base::resources_); |
61 | 0 | slotId = slot->next; |
62 | 0 | if (isKey) |
63 | 0 | base::write(": "); |
64 | 0 | else |
65 | 0 | base::write(slotId == NULL_SLOT ? "\r\n" : ",\r\n"); |
66 | 0 | isKey = !isKey; |
67 | 0 | } |
68 | 0 | nesting_--; |
69 | 0 | indent(); |
70 | 0 | base::write("}"); |
71 | 0 | } else { |
72 | 0 | base::write("{}"); |
73 | 0 | } |
74 | 0 | return this->bytesWritten(); |
75 | 0 | } Unexecuted instantiation: ArduinoJson::V742HB42::detail::PrettyJsonSerializer<ArduinoJson::V742HB42::detail::StaticStringWriter>::visitObject(ArduinoJson::V742HB42::detail::VariantData*) Unexecuted instantiation: ArduinoJson::V742HB42::detail::PrettyJsonSerializer<ArduinoJson::V742HB42::detail::DummyWriter>::visitObject(ArduinoJson::V742HB42::detail::VariantData*) |
76 | | |
77 | | using base::visit; |
78 | | |
79 | | private: |
80 | 0 | void indent() { |
81 | 0 | for (uint8_t i = 0; i < nesting_; i++) |
82 | 0 | base::write(ARDUINOJSON_TAB); |
83 | 0 | } Unexecuted instantiation: ArduinoJson::V742HB42::detail::PrettyJsonSerializer<ArduinoJson::V742HB42::detail::StaticStringWriter>::indent() Unexecuted instantiation: ArduinoJson::V742HB42::detail::PrettyJsonSerializer<ArduinoJson::V742HB42::detail::DummyWriter>::indent() |
84 | | |
85 | | uint8_t nesting_; |
86 | | }; |
87 | | |
88 | | ARDUINOJSON_END_PRIVATE_NAMESPACE |
89 | | |
90 | | ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE |
91 | | |
92 | | // Produces JsonDocument to create a prettified JSON document. |
93 | | // https://arduinojson.org/v7/api/json/serializejsonpretty/ |
94 | | template < |
95 | | typename TDestination, |
96 | | detail::enable_if_t<!detail::is_pointer<TDestination>::value, int> = 0> |
97 | | inline size_t serializeJsonPretty(JsonVariantConst source, |
98 | | TDestination& destination) { |
99 | | using namespace ArduinoJson::detail; |
100 | | return serialize<PrettyJsonSerializer>(source, destination); |
101 | | } |
102 | | |
103 | | // Produces JsonDocument to create a prettified JSON document. |
104 | | // https://arduinojson.org/v7/api/json/serializejsonpretty/ |
105 | | inline size_t serializeJsonPretty(JsonVariantConst source, void* buffer, |
106 | 0 | size_t bufferSize) { |
107 | 0 | using namespace ArduinoJson::detail; |
108 | 0 | return serialize<PrettyJsonSerializer>(source, buffer, bufferSize); |
109 | 0 | } |
110 | | |
111 | | // Computes the length of the document that serializeJsonPretty() produces. |
112 | | // https://arduinojson.org/v7/api/json/measurejsonpretty/ |
113 | 0 | inline size_t measureJsonPretty(JsonVariantConst source) { |
114 | 0 | using namespace ArduinoJson::detail; |
115 | 0 | return measure<PrettyJsonSerializer>(source); |
116 | 0 | } |
117 | | |
118 | | ARDUINOJSON_END_PUBLIC_NAMESPACE |