diff --git a/modules/juce_core/containers/juce_DynamicObject.cpp b/modules/juce_core/containers/juce_DynamicObject.cpp index 3a019c11ee..4e263b98b3 100644 --- a/modules/juce_core/containers/juce_DynamicObject.cpp +++ b/modules/juce_core/containers/juce_DynamicObject.cpp @@ -77,3 +77,43 @@ void DynamicObject::clear() { properties.clear(); } + +void DynamicObject::writeAsJSON (OutputStream& out, const int indentLevel, const bool allOnOneLine) +{ + out << '{'; + if (! allOnOneLine) + out << newLine; + + for (LinkedListPointer* i = &(properties.values);;) + { + if (NamedValueSet::NamedValue* const v = i->get()) + { + if (! allOnOneLine) + JSONFormatter::writeSpaces (out, indentLevel + JSONFormatter::indentSize); + + out << '"'; + JSONFormatter::writeString (out, v->name); + out << "\": "; + JSONFormatter::write (out, v->value, indentLevel + JSONFormatter::indentSize, allOnOneLine); + + if (v->nextListItem.get() != nullptr) + { + if (allOnOneLine) + out << ", "; + else + out << ',' << newLine; + } + else if (! allOnOneLine) + out << newLine; + + i = &(v->nextListItem); + } + else + break; + } + + if (! allOnOneLine) + JSONFormatter::writeSpaces (out, indentLevel); + + out << '}'; +} diff --git a/modules/juce_core/containers/juce_DynamicObject.h b/modules/juce_core/containers/juce_DynamicObject.h index e59c26ff3f..39dcb4773f 100644 --- a/modules/juce_core/containers/juce_DynamicObject.h +++ b/modules/juce_core/containers/juce_DynamicObject.h @@ -104,6 +104,13 @@ public: /** Returns the NamedValueSet that holds the object's properties. */ NamedValueSet& getProperties() noexcept { return properties; } + /** Writes this object to a text stream in JSON format. + This method is used by JSON::toString and JSON::writeToStream, and you should + never need to call it directly, but it's virtual so that custom object types + can stringify themselves appropriately. + */ + virtual void writeAsJSON (OutputStream&, int indentLevel, bool allOnOneLine); + private: //============================================================================== NamedValueSet properties; diff --git a/modules/juce_core/containers/juce_NamedValueSet.h b/modules/juce_core/containers/juce_NamedValueSet.h index 699d2fef7d..d649847343 100644 --- a/modules/juce_core/containers/juce_NamedValueSet.h +++ b/modules/juce_core/containers/juce_NamedValueSet.h @@ -155,7 +155,7 @@ private: friend class LinkedListPointer; LinkedListPointer values; - friend class JSONFormatter; + friend class DynamicObject; }; diff --git a/modules/juce_core/json/juce_JSON.cpp b/modules/juce_core/json/juce_JSON.cpp index 74b49cc136..8945fcc435 100644 --- a/modules/juce_core/json/juce_JSON.cpp +++ b/modules/juce_core/json/juce_JSON.cpp @@ -355,8 +355,8 @@ public: } else if (v.isObject()) { - if (DynamicObject* const object = v.getDynamicObject()) - writeObject (out, *object, indentLevel, allOnOneLine); + if (DynamicObject* object = v.getDynamicObject()) + object->writeAsJSON (out, indentLevel, allOnOneLine); else jassertfalse; // Only DynamicObjects can be converted to JSON! } @@ -460,7 +460,7 @@ public: out << ']'; } - static void writeObject (OutputStream& out, DynamicObject& object, +/* static void writeObject (OutputStream& out, DynamicObject& object, const int indentLevel, const bool allOnOneLine) { NamedValueSet& props = object.getProperties(); @@ -503,7 +503,7 @@ public: writeSpaces (out, indentLevel); out << '}'; - } + }*/ enum { indentSize = 2 }; }; diff --git a/modules/juce_core/juce_core.cpp b/modules/juce_core/juce_core.cpp index 2a51815ce5..04fea71165 100644 --- a/modules/juce_core/juce_core.cpp +++ b/modules/juce_core/juce_core.cpp @@ -110,7 +110,6 @@ namespace juce { #include "containers/juce_AbstractFifo.cpp" -#include "containers/juce_DynamicObject.cpp" #include "containers/juce_NamedValueSet.cpp" #include "containers/juce_PropertySet.cpp" #include "containers/juce_Variant.cpp" @@ -121,6 +120,7 @@ namespace juce #include "files/juce_FileSearchPath.cpp" #include "files/juce_TemporaryFile.cpp" #include "json/juce_JSON.cpp" +#include "containers/juce_DynamicObject.cpp" #include "logging/juce_FileLogger.cpp" #include "logging/juce_Logger.cpp" #include "maths/juce_BigInteger.cpp"