mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-10 23:44:24 +00:00
Moved some JSON formatting logic into DynamicObject::writeAsJSON
This commit is contained in:
parent
48f76460b8
commit
1a2aff80a7
5 changed files with 53 additions and 6 deletions
|
|
@ -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<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. */
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -155,7 +155,7 @@ private:
|
|||
friend class LinkedListPointer<NamedValue>;
|
||||
LinkedListPointer<NamedValue> values;
|
||||
|
||||
friend class JSONFormatter;
|
||||
friend class DynamicObject;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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 };
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue