1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-25 02:04:23 +00:00

Add some json helpers

This commit is contained in:
Roland Rabien 2024-11-18 08:44:10 -08:00
parent 29e6bee01f
commit dc52aafe26
4 changed files with 81 additions and 0 deletions

View file

@ -708,6 +708,21 @@ bool var::hasProperty (const Identifier& propertyName) const noexcept
return false;
}
juce::StringArray var::getProperties() const
{
if (auto* o = getDynamicObject())
{
juce::StringArray names;
for (auto itr : o->getProperties())
names.add (itr.name.toString());
return names;
}
return {};
}
var::NativeFunction var::getNativeFunction() const
{
return isMethod() && (value.methodValue != nullptr) ? *value.methodValue : nullptr;
@ -893,6 +908,8 @@ var::NativeFunctionArgs::NativeFunctionArgs (const var& t, const var* args, int
{
}
//==============================================================================
//==============================================================================
#if JUCE_ALLOW_STATIC_NULL_VARIABLES

View file

@ -262,6 +262,8 @@ public:
var getProperty (const Identifier& propertyName, const var& defaultReturnValue) const;
/** Returns true if this variant is an object and if it has the given property. */
bool hasProperty (const Identifier& propertyName) const noexcept;
/** Returns property names if this variant is an object. */
juce::StringArray getProperties() const;
/** Invokes a named method call with no arguments. */
var call (const Identifier& method) const;

View file

@ -124,6 +124,57 @@ std::optional<var> JSONUtils::setPointer (const var& v,
return {};
}
var JSONUtils::getPointer (const var& v, String pointer, const var& defaultValue)
{
if (pointer.isEmpty())
return defaultValue;
if (! pointer.startsWith ("/"))
{
// This is not a well-formed JSON pointer
jassertfalse;
return {};
}
const auto findResult = pointer.indexOfChar (1, '/');
const auto pos = findResult < 0 ? pointer.length() : findResult;
const String head (pointer.begin() + 1, pointer.begin() + pos);
const String tail (pointer.begin() + pos, pointer.end());
const auto unescaped = head.replace ("~1", "/").replace ("~0", "~");
if (auto* object = v.getDynamicObject())
{
if (tail.isEmpty())
return object->hasProperty (unescaped) ? object->getProperty (unescaped) : defaultValue;
else
return getPointer (object->getProperty (unescaped), tail, defaultValue);
}
else if (auto* array = v.getArray())
{
const auto index = [&]() -> size_t
{
if (unescaped == "-")
return (size_t) array->size();
if (unescaped == "0")
return 0;
if (! unescaped.startsWith ("0"))
return (size_t) unescaped.getLargeIntValue();
return std::numeric_limits<size_t>::max();
}();
if (tail.isEmpty())
return isPositiveAndBelow (index, array->size()) ? (*array)[int (index)] : defaultValue;
else
return getPointer ((*array)[(int) index], tail, defaultValue);
}
return defaultValue;
}
bool JSONUtils::deepEqual (const var& a, const var& b)
{
const auto compareObjects = [] (const DynamicObject& x, const DynamicObject& y)

View file

@ -57,6 +57,17 @@ struct JSONUtils
*/
static std::optional<var> setPointer (const var& v, String pointer, const var& newValue);
/** Given a JSON array/object 'v', a string representing a JSON pointer,
returns the value of the property or array index referenced by the pointer
If the pointer cannot be followed, due to referencing missing array indices
or fields, then this returns defaultValue.
For more details, check the JSON Pointer RFC 6901:
https://datatracker.ietf.org/doc/html/rfc6901
*/
static var getPointer (const var& v, String pointer, const var& defaultValue);
/** Converts the provided key/value pairs into a JSON object. */
static var makeObject (const std::map<Identifier, var>& source);