@@ -77,3 +77,43 @@ void DynamicObject::clear() | |||||
{ | { | ||||
properties.clear(); | properties.clear(); | ||||
} | } | ||||
void DynamicObject::writeAsJSON (OutputStream& out, const int indentLevel, const bool allOnOneLine) | |||||
{ | |||||
out << '{'; | |||||
if (! allOnOneLine) | |||||
out << newLine; | |||||
for (LinkedListPointer<NamedValueSet::NamedValue>* 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 << '}'; | |||||
} |
@@ -104,6 +104,13 @@ public: | |||||
/** Returns the NamedValueSet that holds the object's properties. */ | /** Returns the NamedValueSet that holds the object's properties. */ | ||||
NamedValueSet& getProperties() noexcept { return 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: | private: | ||||
//============================================================================== | //============================================================================== | ||||
NamedValueSet properties; | NamedValueSet properties; | ||||
@@ -155,7 +155,7 @@ private: | |||||
friend class LinkedListPointer<NamedValue>; | friend class LinkedListPointer<NamedValue>; | ||||
LinkedListPointer<NamedValue> values; | LinkedListPointer<NamedValue> values; | ||||
friend class JSONFormatter; | |||||
friend class DynamicObject; | |||||
}; | }; | ||||
@@ -355,8 +355,8 @@ public: | |||||
} | } | ||||
else if (v.isObject()) | 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 | else | ||||
jassertfalse; // Only DynamicObjects can be converted to JSON! | jassertfalse; // Only DynamicObjects can be converted to JSON! | ||||
} | } | ||||
@@ -460,7 +460,7 @@ public: | |||||
out << ']'; | out << ']'; | ||||
} | } | ||||
static void writeObject (OutputStream& out, DynamicObject& object, | |||||
/* static void writeObject (OutputStream& out, DynamicObject& object, | |||||
const int indentLevel, const bool allOnOneLine) | const int indentLevel, const bool allOnOneLine) | ||||
{ | { | ||||
NamedValueSet& props = object.getProperties(); | NamedValueSet& props = object.getProperties(); | ||||
@@ -503,7 +503,7 @@ public: | |||||
writeSpaces (out, indentLevel); | writeSpaces (out, indentLevel); | ||||
out << '}'; | out << '}'; | ||||
} | |||||
}*/ | |||||
enum { indentSize = 2 }; | enum { indentSize = 2 }; | ||||
}; | }; | ||||
@@ -110,7 +110,6 @@ namespace juce | |||||
{ | { | ||||
#include "containers/juce_AbstractFifo.cpp" | #include "containers/juce_AbstractFifo.cpp" | ||||
#include "containers/juce_DynamicObject.cpp" | |||||
#include "containers/juce_NamedValueSet.cpp" | #include "containers/juce_NamedValueSet.cpp" | ||||
#include "containers/juce_PropertySet.cpp" | #include "containers/juce_PropertySet.cpp" | ||||
#include "containers/juce_Variant.cpp" | #include "containers/juce_Variant.cpp" | ||||
@@ -121,6 +120,7 @@ namespace juce | |||||
#include "files/juce_FileSearchPath.cpp" | #include "files/juce_FileSearchPath.cpp" | ||||
#include "files/juce_TemporaryFile.cpp" | #include "files/juce_TemporaryFile.cpp" | ||||
#include "json/juce_JSON.cpp" | #include "json/juce_JSON.cpp" | ||||
#include "containers/juce_DynamicObject.cpp" | |||||
#include "logging/juce_FileLogger.cpp" | #include "logging/juce_FileLogger.cpp" | ||||
#include "logging/juce_Logger.cpp" | #include "logging/juce_Logger.cpp" | ||||
#include "maths/juce_BigInteger.cpp" | #include "maths/juce_BigInteger.cpp" | ||||