2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <boost/iterator/iterator_adaptor.hpp>
21 #include <boost/iterator/iterator_facade.hpp>
22 #include <folly/Likely.h>
23 #include <folly/Conv.h>
24 #include <folly/Format.h>
26 //////////////////////////////////////////////////////////////////////
31 struct hash< ::folly::dynamic> {
32 size_t operator()(::folly::dynamic const& d) const {
39 //////////////////////////////////////////////////////////////////////
41 // This is a higher-order preprocessor macro to aid going from runtime
42 // types to the compile time type system.
43 #define FB_DYNAMIC_APPLY(type, apply) \
73 //////////////////////////////////////////////////////////////////////
77 struct TypeError : std::runtime_error {
78 explicit TypeError(const std::string& expected, dynamic::Type actual);
79 explicit TypeError(const std::string& expected,
80 dynamic::Type actual1, dynamic::Type actual2);
85 //////////////////////////////////////////////////////////////////////
89 // This helper is used in destroy() to be able to run destructors on
90 // types like "int64_t" without a compiler error.
92 template<class T> static void destroy(T* t) { t->~T(); }
96 * Helper for implementing numeric conversions in operators on
97 * numbers. Just promotes to double when one of the arguments is
98 * double, or throws if either is not a numeric type.
100 template<template<class> class Op>
101 dynamic numericOp(dynamic const& a, dynamic const& b) {
102 if (!a.isNumber() || !b.isNumber()) {
103 throw TypeError("numeric", a.type(), b.type());
105 if (a.type() != b.type()) {
106 auto& integ = a.isInt() ? a : b;
107 auto& nonint = a.isInt() ? b : a;
108 return Op<double>()(to<double>(integ.asInt()), nonint.asDouble());
111 return Op<double>()(a.asDouble(), b.asDouble());
113 return Op<int64_t>()(a.asInt(), b.asInt());
118 //////////////////////////////////////////////////////////////////////
121 * We're doing this instead of a simple member typedef to avoid the
122 * undefined behavior of parameterizing std::unordered_map<> with an
125 * Note: Later we may add separate order tracking here (a multi-index
128 struct dynamic::ObjectImpl : std::unordered_map<dynamic, dynamic> {};
130 //////////////////////////////////////////////////////////////////////
132 // Helper object for creating objects conveniently. See object and
133 // the dynamic::dynamic(ObjectMaker&&) ctor.
134 struct dynamic::ObjectMaker {
135 friend struct dynamic;
137 explicit ObjectMaker() : val_(dynamic::object) {}
138 explicit ObjectMaker(dynamic key, dynamic val)
139 : val_(dynamic::object)
141 val_.insert(std::move(key), std::move(val));
144 // Make sure no one tries to save one of these into an lvalue with
145 // auto or anything like that.
146 ObjectMaker(ObjectMaker&&) = default;
147 ObjectMaker(ObjectMaker const&) = delete;
148 ObjectMaker& operator=(ObjectMaker const&) = delete;
149 ObjectMaker& operator=(ObjectMaker&&) = delete;
151 // This returns an rvalue-reference instead of an lvalue-reference
152 // to allow constructs like this to moved instead of copied:
153 // dynamic a = dynamic::object("a", "b")("c", "d")
154 ObjectMaker&& operator()(dynamic key, dynamic val) {
155 val_.insert(std::move(key), std::move(val));
156 return std::move(*this);
163 inline void dynamic::array(EmptyArrayTag) {}
165 template <class... Args>
166 inline dynamic dynamic::array(Args&& ...args) {
167 return dynamic(Array{std::forward<Args>(args)...});
170 inline dynamic::ObjectMaker dynamic::object() { return ObjectMaker(); }
171 inline dynamic::ObjectMaker dynamic::object(dynamic a, dynamic b) {
172 return ObjectMaker(std::move(a), std::move(b));
175 //////////////////////////////////////////////////////////////////////
177 struct dynamic::item_iterator : boost::iterator_adaptor<
178 dynamic::item_iterator,
179 dynamic::ObjectImpl::iterator> {
180 /* implicit */ item_iterator(base_type b) : iterator_adaptor_(b) {}
182 using object_type = dynamic::ObjectImpl;
185 friend class boost::iterator_core_access;
188 struct dynamic::value_iterator : boost::iterator_adaptor<
189 dynamic::value_iterator,
190 dynamic::ObjectImpl::iterator,
192 /* implicit */ value_iterator(base_type b) : iterator_adaptor_(b) {}
194 using object_type = dynamic::ObjectImpl;
197 dynamic& dereference() const {
198 return base_reference()->second;
200 friend class boost::iterator_core_access;
203 struct dynamic::const_item_iterator
204 : boost::iterator_adaptor<dynamic::const_item_iterator,
205 dynamic::ObjectImpl::const_iterator> {
206 /* implicit */ const_item_iterator(base_type b) : iterator_adaptor_(b) { }
207 /* implicit */ const_item_iterator(item_iterator i)
208 : iterator_adaptor_(i.base()) {}
209 /* implicit */ const_item_iterator(dynamic::ObjectImpl::iterator i)
210 : iterator_adaptor_(i) {}
212 using object_type = dynamic::ObjectImpl const;
215 friend class boost::iterator_core_access;
218 struct dynamic::const_key_iterator
219 : boost::iterator_adaptor<dynamic::const_key_iterator,
220 dynamic::ObjectImpl::const_iterator,
222 /* implicit */ const_key_iterator(base_type b) : iterator_adaptor_(b) { }
224 using object_type = dynamic::ObjectImpl const;
227 dynamic const& dereference() const {
228 return base_reference()->first;
230 friend class boost::iterator_core_access;
233 struct dynamic::const_value_iterator
234 : boost::iterator_adaptor<dynamic::const_value_iterator,
235 dynamic::ObjectImpl::const_iterator,
237 /* implicit */ const_value_iterator(base_type b) : iterator_adaptor_(b) { }
238 /* implicit */ const_value_iterator(value_iterator i)
239 : iterator_adaptor_(i.base()) {}
240 /* implicit */ const_value_iterator(dynamic::ObjectImpl::iterator i)
241 : iterator_adaptor_(i) {}
243 using object_type = dynamic::ObjectImpl const;
246 dynamic const& dereference() const {
247 return base_reference()->second;
249 friend class boost::iterator_core_access;
252 //////////////////////////////////////////////////////////////////////
254 inline dynamic::dynamic() : dynamic(nullptr) {}
256 inline dynamic::dynamic(std::nullptr_t) : type_(NULLT) {}
258 inline dynamic::dynamic(void (*)(EmptyArrayTag))
261 new (&u_.array) Array();
264 inline dynamic::dynamic(ObjectMaker (*)())
267 new (getAddress<ObjectImpl>()) ObjectImpl();
270 inline dynamic::dynamic(StringPiece s)
273 new (&u_.string) std::string(s.data(), s.size());
276 inline dynamic::dynamic(char const* s)
279 new (&u_.string) std::string(s);
282 inline dynamic::dynamic(std::string s)
285 new (&u_.string) std::string(std::move(s));
288 inline dynamic::dynamic(ObjectMaker&& maker)
291 new (getAddress<ObjectImpl>())
292 ObjectImpl(std::move(*maker.val_.getAddress<ObjectImpl>()));
295 inline dynamic::dynamic(dynamic const& o)
301 inline dynamic::dynamic(dynamic&& o) noexcept
304 *this = std::move(o);
307 inline dynamic::~dynamic() noexcept { destroy(); }
309 // Integral types except bool convert to int64_t, float types to double.
311 struct dynamic::NumericTypeHelper<
312 T, typename std::enable_if<std::is_integral<T>::value>::type> {
314 !kIsObjC || sizeof(T) > sizeof(char),
315 "char-sized types are ambiguous in objc; cast to bool or wider type");
316 using type = int64_t;
319 struct dynamic::NumericTypeHelper<bool> {
323 struct dynamic::NumericTypeHelper<float> {
327 struct dynamic::NumericTypeHelper<double> {
331 template<class T, class NumericType /* = typename NumericTypeHelper<T>::type */>
332 dynamic::dynamic(T t) {
333 type_ = TypeInfo<NumericType>::type;
334 new (getAddress<NumericType>()) NumericType(NumericType(t));
337 template <class Iterator>
338 dynamic::dynamic(Iterator first, Iterator last)
341 new (&u_.array) Array(first, last);
344 //////////////////////////////////////////////////////////////////////
346 inline dynamic::const_iterator dynamic::begin() const {
347 return get<Array>().begin();
349 inline dynamic::const_iterator dynamic::end() const {
350 return get<Array>().end();
353 inline dynamic::iterator dynamic::begin() {
354 return get<Array>().begin();
356 inline dynamic::iterator dynamic::end() {
357 return get<Array>().end();
361 struct dynamic::IterableProxy {
363 typedef typename It::value_type value_type;
364 typedef typename It::object_type object_type;
366 /* implicit */ IterableProxy(object_type* o) : o_(o) {}
380 inline dynamic::IterableProxy<dynamic::const_key_iterator> dynamic::keys()
382 return &(get<ObjectImpl>());
385 inline dynamic::IterableProxy<dynamic::const_value_iterator> dynamic::values()
387 return &(get<ObjectImpl>());
390 inline dynamic::IterableProxy<dynamic::const_item_iterator> dynamic::items()
392 return &(get<ObjectImpl>());
395 inline dynamic::IterableProxy<dynamic::value_iterator> dynamic::values() {
396 return &(get<ObjectImpl>());
399 inline dynamic::IterableProxy<dynamic::item_iterator> dynamic::items() {
400 return &(get<ObjectImpl>());
403 inline bool dynamic::isString() const {
404 return get_nothrow<std::string>() != nullptr;
406 inline bool dynamic::isObject() const {
407 return get_nothrow<ObjectImpl>() != nullptr;
409 inline bool dynamic::isBool() const {
410 return get_nothrow<bool>() != nullptr;
412 inline bool dynamic::isArray() const {
413 return get_nothrow<Array>() != nullptr;
415 inline bool dynamic::isDouble() const {
416 return get_nothrow<double>() != nullptr;
418 inline bool dynamic::isInt() const {
419 return get_nothrow<int64_t>() != nullptr;
421 inline bool dynamic::isNull() const {
422 return get_nothrow<void*>() != nullptr;
424 inline bool dynamic::isNumber() const {
425 return isInt() || isDouble();
428 inline dynamic::Type dynamic::type() const {
432 inline std::string dynamic::asString() const {
433 return asImpl<std::string>();
435 inline double dynamic::asDouble() const {
436 return asImpl<double>();
438 inline int64_t dynamic::asInt() const {
439 return asImpl<int64_t>();
441 inline bool dynamic::asBool() const {
442 return asImpl<bool>();
445 inline const std::string& dynamic::getString() const& {
446 return get<std::string>();
448 inline double dynamic::getDouble() const& { return get<double>(); }
449 inline int64_t dynamic::getInt() const& { return get<int64_t>(); }
450 inline bool dynamic::getBool() const& { return get<bool>(); }
452 inline std::string& dynamic::getString()& {
453 return get<std::string>();
455 inline double& dynamic::getDouble() & { return get<double>(); }
456 inline int64_t& dynamic::getInt() & { return get<int64_t>(); }
457 inline bool& dynamic::getBool() & { return get<bool>(); }
459 inline std::string&& dynamic::getString()&& {
460 return std::move(get<std::string>());
462 inline double dynamic::getDouble() && { return get<double>(); }
463 inline int64_t dynamic::getInt() && { return get<int64_t>(); }
464 inline bool dynamic::getBool() && { return get<bool>(); }
466 inline const char* dynamic::data() const& {
467 return get<std::string>().data();
469 inline const char* dynamic::c_str() const& {
470 return get<std::string>().c_str();
472 inline StringPiece dynamic::stringPiece() const {
473 return get<std::string>();
477 struct dynamic::CompareOp {
478 static bool comp(T const& a, T const& b) { return a < b; }
481 struct dynamic::CompareOp<dynamic::ObjectImpl> {
482 static bool comp(ObjectImpl const&, ObjectImpl const&) {
483 // This code never executes; it is just here for the compiler.
488 inline dynamic& dynamic::operator+=(dynamic const& o) {
489 if (type() == STRING && o.type() == STRING) {
490 *getAddress<std::string>() += *o.getAddress<std::string>();
493 *this = detail::numericOp<std::plus>(*this, o);
497 inline dynamic& dynamic::operator-=(dynamic const& o) {
498 *this = detail::numericOp<std::minus>(*this, o);
502 inline dynamic& dynamic::operator*=(dynamic const& o) {
503 *this = detail::numericOp<std::multiplies>(*this, o);
507 inline dynamic& dynamic::operator/=(dynamic const& o) {
508 *this = detail::numericOp<std::divides>(*this, o);
512 #define FB_DYNAMIC_INTEGER_OP(op) \
513 inline dynamic& dynamic::operator op(dynamic const& o) { \
514 if (!isInt() || !o.isInt()) { \
515 throw TypeError("int64", type(), o.type()); \
517 *getAddress<int64_t>() op o.asInt(); \
521 FB_DYNAMIC_INTEGER_OP(%=)
522 FB_DYNAMIC_INTEGER_OP(|=)
523 FB_DYNAMIC_INTEGER_OP(&=)
524 FB_DYNAMIC_INTEGER_OP(^=)
526 #undef FB_DYNAMIC_INTEGER_OP
528 inline dynamic& dynamic::operator++() {
533 inline dynamic& dynamic::operator--() {
538 inline dynamic const& dynamic::operator[](dynamic const& idx) const& {
542 inline dynamic&& dynamic::operator[](dynamic const& idx) && {
543 return std::move((*this)[idx]);
546 template<class K, class V> inline dynamic& dynamic::setDefault(K&& k, V&& v) {
547 auto& obj = get<ObjectImpl>();
548 return obj.insert(std::make_pair(std::forward<K>(k),
549 std::forward<V>(v))).first->second;
552 template<class K> inline dynamic& dynamic::setDefault(K&& k, dynamic&& v) {
553 auto& obj = get<ObjectImpl>();
554 return obj.insert(std::make_pair(std::forward<K>(k),
555 std::move(v))).first->second;
558 template<class K> inline dynamic& dynamic::setDefault(K&& k, const dynamic& v) {
559 auto& obj = get<ObjectImpl>();
560 return obj.insert(std::make_pair(std::forward<K>(k), v)).first->second;
563 inline dynamic* dynamic::get_ptr(dynamic const& idx) & {
564 return const_cast<dynamic*>(const_cast<dynamic const*>(this)->get_ptr(idx));
567 inline dynamic& dynamic::at(dynamic const& idx) & {
568 return const_cast<dynamic&>(const_cast<dynamic const*>(this)->at(idx));
571 inline dynamic&& dynamic::at(dynamic const& idx) && {
572 return std::move(at(idx));
575 inline bool dynamic::empty() const {
582 inline std::size_t dynamic::count(dynamic const& key) const {
583 return find(key) != items().end() ? 1u : 0u;
586 inline dynamic::const_item_iterator dynamic::find(dynamic const& key) const {
587 return get<ObjectImpl>().find(key);
589 inline dynamic::item_iterator dynamic::find(dynamic const& key) {
590 return get<ObjectImpl>().find(key);
593 template<class K, class V> inline void dynamic::insert(K&& key, V&& val) {
594 auto& obj = get<ObjectImpl>();
595 auto rv = obj.insert({ std::forward<K>(key), nullptr });
596 rv.first->second = std::forward<V>(val);
599 inline void dynamic::update(const dynamic& mergeObj) {
600 if (!isObject() || !mergeObj.isObject()) {
601 throw TypeError("object", type(), mergeObj.type());
604 for (const auto& pair : mergeObj.items()) {
605 (*this)[pair.first] = pair.second;
609 inline void dynamic::update_missing(const dynamic& mergeObj1) {
610 if (!isObject() || !mergeObj1.isObject()) {
611 throw TypeError("object", type(), mergeObj1.type());
614 // Only add if not already there
615 for (const auto& pair : mergeObj1.items()) {
616 if ((*this).find(pair.first) == (*this).items().end()) {
617 (*this)[pair.first] = pair.second;
622 inline dynamic dynamic::merge(
623 const dynamic& mergeObj1,
624 const dynamic& mergeObj2) {
626 // No checks on type needed here because they are done in update_missing
627 // Note that we do update_missing here instead of update() because
628 // it will prevent the extra writes that would occur with update()
629 auto ret = mergeObj2;
630 ret.update_missing(mergeObj1);
634 inline std::size_t dynamic::erase(dynamic const& key) {
635 auto& obj = get<ObjectImpl>();
636 return obj.erase(key);
639 inline dynamic::iterator dynamic::erase(const_iterator it) {
640 auto& arr = get<Array>();
641 // std::vector doesn't have an erase method that works on const iterators,
642 // even though the standard says it should, so this hack converts to a
643 // non-const iterator before calling erase.
644 return get<Array>().erase(arr.begin() + (it - arr.begin()));
647 inline dynamic::const_key_iterator dynamic::erase(const_key_iterator it) {
648 return const_key_iterator(get<ObjectImpl>().erase(it.base()));
651 inline dynamic::const_key_iterator dynamic::erase(
652 const_key_iterator first,
653 const_key_iterator last) {
654 return const_key_iterator(get<ObjectImpl>().erase(first.base(),
658 inline dynamic::value_iterator dynamic::erase(const_value_iterator it) {
659 return value_iterator(get<ObjectImpl>().erase(it.base()));
662 inline dynamic::value_iterator dynamic::erase(
663 const_value_iterator first,
664 const_value_iterator last) {
665 return value_iterator(get<ObjectImpl>().erase(first.base(), last.base()));
668 inline dynamic::item_iterator dynamic::erase(const_item_iterator it) {
669 return item_iterator(get<ObjectImpl>().erase(it.base()));
672 inline dynamic::item_iterator dynamic::erase(
673 const_item_iterator first,
674 const_item_iterator last) {
675 return item_iterator(get<ObjectImpl>().erase(first.base(), last.base()));
678 inline void dynamic::resize(std::size_t sz, dynamic const& c) {
679 auto& arr = get<Array>();
683 inline void dynamic::push_back(dynamic const& v) {
684 auto& arr = get<Array>();
688 inline void dynamic::push_back(dynamic&& v) {
689 auto& arr = get<Array>();
690 arr.push_back(std::move(v));
693 inline void dynamic::pop_back() {
694 auto& arr = get<Array>();
698 //////////////////////////////////////////////////////////////////////
700 inline dynamic::dynamic(Array&& r) : type_(ARRAY) {
701 new (&u_.array) Array(std::move(r));
704 #define FOLLY_DYNAMIC_DEC_TYPEINFO(T, str, val) \
705 template <> struct dynamic::TypeInfo<T> { \
706 static constexpr const char* name = str; \
707 static constexpr dynamic::Type type = val; \
711 FOLLY_DYNAMIC_DEC_TYPEINFO(void*, "null", dynamic::NULLT)
712 FOLLY_DYNAMIC_DEC_TYPEINFO(bool, "boolean", dynamic::BOOL)
713 FOLLY_DYNAMIC_DEC_TYPEINFO(std::string, "string", dynamic::STRING)
714 FOLLY_DYNAMIC_DEC_TYPEINFO(dynamic::Array, "array", dynamic::ARRAY)
715 FOLLY_DYNAMIC_DEC_TYPEINFO(double, "double", dynamic::DOUBLE)
716 FOLLY_DYNAMIC_DEC_TYPEINFO(int64_t, "int64", dynamic::INT64)
717 FOLLY_DYNAMIC_DEC_TYPEINFO(dynamic::ObjectImpl, "object", dynamic::OBJECT)
719 #undef FOLLY_DYNAMIC_DEC_TYPEINFO
722 T dynamic::asImpl() const {
724 case INT64: return to<T>(*get_nothrow<int64_t>());
725 case DOUBLE: return to<T>(*get_nothrow<double>());
726 case BOOL: return to<T>(*get_nothrow<bool>());
728 return to<T>(*get_nothrow<std::string>());
730 throw TypeError("int/double/bool/string", type());
734 // Return a T* to our type, or null if we're not that type.
736 T* dynamic::get_nothrow() & noexcept {
737 if (type_ != TypeInfo<T>::type) {
740 return getAddress<T>();
744 T const* dynamic::get_nothrow() const& noexcept {
745 return const_cast<dynamic*>(this)->get_nothrow<T>();
748 // Return T* for where we can put a T, without type checking. (Memory
749 // might be uninitialized, even.)
751 T* dynamic::getAddress() noexcept {
752 return GetAddrImpl<T>::get(u_);
756 T const* dynamic::getAddress() const noexcept {
757 return const_cast<dynamic*>(this)->getAddress<T>();
760 template<class T> struct dynamic::GetAddrImpl {};
761 template<> struct dynamic::GetAddrImpl<void*> {
762 static void** get(Data& d) noexcept { return &d.nul; }
764 template<> struct dynamic::GetAddrImpl<dynamic::Array> {
765 static Array* get(Data& d) noexcept { return &d.array; }
767 template<> struct dynamic::GetAddrImpl<bool> {
768 static bool* get(Data& d) noexcept { return &d.boolean; }
770 template<> struct dynamic::GetAddrImpl<int64_t> {
771 static int64_t* get(Data& d) noexcept { return &d.integer; }
773 template<> struct dynamic::GetAddrImpl<double> {
774 static double* get(Data& d) noexcept { return &d.doubl; }
777 struct dynamic::GetAddrImpl<std::string> {
778 static std::string* get(Data& d) noexcept {
782 template<> struct dynamic::GetAddrImpl<dynamic::ObjectImpl> {
783 static_assert(sizeof(ObjectImpl) <= sizeof(Data::objectBuffer),
784 "In your implementation, std::unordered_map<> apparently takes different"
785 " amount of space depending on its template parameters. This is "
786 "weird. Make objectBuffer bigger if you want to compile dynamic.");
788 static ObjectImpl* get(Data& d) noexcept {
789 void* data = &d.objectBuffer;
790 return static_cast<ObjectImpl*>(data);
796 if (auto* p = get_nothrow<T>()) {
799 throw TypeError(TypeInfo<T>::name, type());
803 T const& dynamic::get() const {
804 return const_cast<dynamic*>(this)->get<T>();
807 //////////////////////////////////////////////////////////////////////
810 * Helper for implementing operator<<. Throws if the type shouldn't
814 struct dynamic::PrintImpl {
815 static void print(dynamic const&, std::ostream& out, T const& t) {
819 // Otherwise, null, being (void*)0, would print as 0.
821 struct dynamic::PrintImpl<void*> {
822 static void print(dynamic const& /* d */,
825 DCHECK_EQ((void*)0, nul);
830 struct dynamic::PrintImpl<dynamic::ObjectImpl> {
831 static void print(dynamic const& d,
833 dynamic::ObjectImpl const&) {
834 d.print_as_pseudo_json(out);
838 struct dynamic::PrintImpl<dynamic::Array> {
839 static void print(dynamic const& d,
841 dynamic::Array const&) {
842 d.print_as_pseudo_json(out);
846 inline void dynamic::print(std::ostream& out) const {
847 #define FB_X(T) PrintImpl<T>::print(*this, out, *getAddress<T>())
848 FB_DYNAMIC_APPLY(type_, FB_X);
852 inline std::ostream& operator<<(std::ostream& out, dynamic const& d) {
857 //////////////////////////////////////////////////////////////////////
859 // Secialization of FormatValue so dynamic objects can be formatted
861 class FormatValue<dynamic> {
863 explicit FormatValue(const dynamic& val) : val_(val) { }
865 template <class FormatCallback>
866 void format(FormatArg& arg, FormatCallback& cb) const {
867 switch (val_.type()) {
869 FormatValue<std::nullptr_t>(nullptr).format(arg, cb);
872 FormatValue<bool>(val_.asBool()).format(arg, cb);
875 FormatValue<int64_t>(val_.asInt()).format(arg, cb);
877 case dynamic::STRING:
878 FormatValue<std::string>(val_.asString()).format(arg, cb);
880 case dynamic::DOUBLE:
881 FormatValue<double>(val_.asDouble()).format(arg, cb);
884 FormatValue(val_.at(arg.splitIntKey())).format(arg, cb);
886 case dynamic::OBJECT:
887 FormatValue(val_.at(arg.splitKey().toString())).format(arg, cb);
897 class FormatValue<detail::DefaultValueWrapper<dynamic, V>> {
899 explicit FormatValue(
900 const detail::DefaultValueWrapper<dynamic, V>& val)
903 template <class FormatCallback>
904 void format(FormatArg& arg, FormatCallback& cb) const {
905 auto& c = val_.container;
910 case dynamic::STRING:
911 case dynamic::DOUBLE:
912 FormatValue<dynamic>(c).format(arg, cb);
916 int key = arg.splitIntKey();
917 if (key >= 0 && size_t(key) < c.size()) {
918 FormatValue<dynamic>(c.at(key)).format(arg, cb);
920 FormatValue<V>(val_.defaultValue).format(arg, cb);
924 case dynamic::OBJECT:
926 auto pos = c.find(arg.splitKey());
927 if (pos != c.items().end()) {
928 FormatValue<dynamic>(pos->second).format(arg, cb);
930 FormatValue<V>(val_.defaultValue).format(arg, cb);
938 const detail::DefaultValueWrapper<dynamic, V>& val_;
943 #undef FB_DYNAMIC_APPLY