Browse Source

Moved some JSON formatting logic into DynamicObject::writeAsJSON

tags/2021-05-28
jules 11 years ago
parent
commit
1a2aff80a7
5 changed files with 53 additions and 6 deletions
  1. +40
    -0
      modules/juce_core/containers/juce_DynamicObject.cpp
  2. +7
    -0
      modules/juce_core/containers/juce_DynamicObject.h
  3. +1
    -1
      modules/juce_core/containers/juce_NamedValueSet.h
  4. +4
    -4
      modules/juce_core/json/juce_JSON.cpp
  5. +1
    -1
      modules/juce_core/juce_core.cpp

+ 40
- 0
modules/juce_core/containers/juce_DynamicObject.cpp View File

@@ -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 << '}';
}

+ 7
- 0
modules/juce_core/containers/juce_DynamicObject.h View File

@@ -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;


+ 1
- 1
modules/juce_core/containers/juce_NamedValueSet.h View File

@@ -155,7 +155,7 @@ private:
friend class LinkedListPointer<NamedValue>;
LinkedListPointer<NamedValue> values;
friend class JSONFormatter;
friend class DynamicObject;
};


+ 4
- 4
modules/juce_core/json/juce_JSON.cpp View File

@@ -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 };
};


+ 1
- 1
modules/juce_core/juce_core.cpp View File

@@ -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…
Cancel
Save