58 bool versionIncluded =
true;
89 return Visitor::convert (t, options);
99 constexpr auto fallbackVersion = detail::ForwardingSerialisationTraits<T>::marshallingVersion;
100 const auto versionToUse = options.getExplicitVersion()
101 .value_or (fallbackVersion);
103 if (versionToUse > fallbackVersion)
109 Visitor visitor { versionToUse, options.getVersionIncluded() };
110 detail::doSave (visitor, t);
111 return visitor.value;
116 template <
typename... Ts>
117 void operator() (Ts&&... ts)
124 : version (explicitVersion),
127 if (! (version.has_value() && includeVersion))
131 obj->setProperty (
"__version__", *version);
132 return obj.release();
134 versionIncluded (includeVersion) {}
136 template <
typename T>
137 void visit (
const T& t)
147 else if (
auto converted = convert (t))
157 template <
typename T>
160 if (! value.has_value())
164 value =
new DynamicObject;
166 auto* obj = value->getDynamicObject();
181 if (! trySetProperty (*obj,
named))
185 template <
typename T>
186 void visit (
const SerialisationSize<T>&)
191 void visit (
const bool& t)
196 void visit (
const String& t)
201 void visit (
const var& t)
206 template <
typename T>
209 return convert (t, Options{}.withVersionIncluded (versionIncluded));
214 if (! value.has_value())
219 else if (
auto* array = value->getArray())
225 template <
typename T>
226 bool trySetProperty (DynamicObject& obj,
const Named<T>& n)
228 if (
const auto converted = convert (n.value))
230 obj.setProperty (Identifier (
std::string (n.name)), *converted);
239 bool versionIncluded =
true;
266 template <
typename T>
269 return Visitor::convert<T> (v);
276 template <
typename T>
281 if (
auto* obj = v.getDynamicObject())
282 if (obj->hasProperty (
"__version__"))
283 return (
int) obj->getProperty (
"__version__");
288 Visitor visitor { version, v };
290 detail::doLoad (visitor, t);
297 template <
typename... Ts>
298 void operator() (Ts&&... ts)
305 : version (vn), input (i) {}
307 template <
typename T>
320 auto node = getNodeToRead();
322 if (! node.has_value())
325 auto converted = convert<T> (*node);
327 if (converted.has_value())
334 template <
typename T>
337 auto node = getNodeToRead();
339 if (! node.has_value())
342 auto* obj = node->getDynamicObject();
344 failed = obj ==
nullptr || ! tryGetProperty (*obj,
named);
347 template <
typename T>
348 void visit (
const SerialisationSize<T>& t)
353 if (
auto* array = input.getArray())
355 t.size =
static_cast<T
> (array->size());
356 currentArrayIndex = 0;
369 void visit (String& t)
407 const auto* array = input.getArray();
409 if (array ==
nullptr)
412 if ((
int) currentArrayIndex < array->
size())
413 return array->getReference ((
int) currentArrayIndex++);
419 template <
typename TypeToRead,
typename T>
422 auto node = getNodeToRead();
424 if (! node.has_value())
427 auto typed = pullTyped (tag, *node);
429 if (typed.has_value())
430 t =
static_cast<T
> (*typed);
435 template <
typename T>
436 static bool tryGetProperty (
const DynamicObject& obj,
const Named<T>& n)
438 const Identifier identifier (String (n.name.data(), n.name.size()));
440 if (! obj.hasProperty (identifier))
443 const auto converted = convert<T> (obj.getProperty (identifier));
445 if (! converted.has_value())
448 n.value = *converted;
476template <
typename Type>
479 static Type fromVar (
const var& v)
481 return static_cast<Type
> (v);
484 static var toVar (
const Type& t)
495 static String fromVar (
const var& v) {
return v.toString(); }
496 static var toVar (
const String& s) {
return s; }
514template <
typename Type>
517 static_assert (detail::serialisationKind<Type> != detail::SerialisationKind::none);
519 static Type fromVar (
const var& v)
521 auto converted = FromVar::convert<Type> (v);
522 jassert (converted.has_value());
523 return std::move (converted).value_or (Type{});
526 static var toVar (
const Type& t)
528 auto converted = ToVar::convert<> (t);
529 jassert (converted.has_value());
530 return std::move (converted).value_or (
var{});
Allows converting a var to an object of arbitrary type.
static std::optional< T > convert(const var &v)
Attempts to convert a var to an instance of type T.
Options that control conversion from arbitrary types to juce::var.
ToVarOptions withVersionIncluded(bool x) const
By default, conversion will include version information for any type with a non-null marshallingVersi...
auto getVersionIncluded() const
ToVarOptions withExplicitVersion(std::optional< int > x) const
By default, conversion will serialise the type using the marshallingVersion defined for that type.
auto getExplicitVersion() const
Allows converting an object of arbitrary type to var.
static std::optional< var > convert(const T &t, const Options &options={})
Attempts to convert the argument to a var using the serialisation utilities specified for that type.
A variant class, that can be used to hold a range of primitive values.
Object withMember(Object copy, Member OtherObject::*member, Other &&value)
Copies an object, sets one of the copy's members to the specified value, and then returns the copy.
constexpr auto named(std::string_view c, T &t)
Produces a Named instance that holds a mutable reference.
long long int64
A platform-independent 64-bit integer type.
std::u16string convert(const std::string &utf8Str)
convert an UTF-8 string to an UTF-16 string
A helper type that can be used to implement specialisations of VariantConverter that use FromVar::con...
This template-overloaded class can be used to convert between var and custom types.