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.
21 #include <boost/iterator/iterator_adaptor.hpp>
22 #include <boost/iterator/iterator_facade.hpp>
24 #include <folly/Conv.h>
25 #include <folly/Format.h>
26 #include <folly/Likely.h>
28 //////////////////////////////////////////////////////////////////////
33 struct hash<::folly::dynamic> {
34 size_t operator()(::folly::dynamic const& d) const {
41 //////////////////////////////////////////////////////////////////////
43 // This is a higher-order preprocessor macro to aid going from runtime
44 // types to the compile time type system.
45 #define FB_DYNAMIC_APPLY(type, apply) \
49 apply(std::nullptr_t); \
75 //////////////////////////////////////////////////////////////////////
79 struct TypeError : std::runtime_error {
80 explicit TypeError(const std::string& expected, dynamic::Type actual);
82 const std::string& expected,
83 dynamic::Type actual1,
84 dynamic::Type actual2);
85 ~TypeError() override;
88 [[noreturn]] void throwTypeError_(
89 std::string const& expected,
90 dynamic::Type actual);
91 [[noreturn]] void throwTypeError_(
92 std::string const& expected,
93 dynamic::Type actual1,
94 dynamic::Type actual2);
96 //////////////////////////////////////////////////////////////////////
100 // This helper is used in destroy() to be able to run destructors on
101 // types like "int64_t" without a compiler error.
104 static void destroy(T* t) {
110 * Helper for implementing numeric conversions in operators on
111 * numbers. Just promotes to double when one of the arguments is
112 * double, or throws if either is not a numeric type.
114 template <template <class> class Op>
115 dynamic numericOp(dynamic const& a, dynamic const& b) {
116 if (!a.isNumber() || !b.isNumber()) {
117 throwTypeError_("numeric", a.type(), b.type());
119 if (a.type() != b.type()) {
120 auto& integ = a.isInt() ? a : b;
121 auto& nonint = a.isInt() ? b : a;
122 return Op<double>()(to<double>(integ.asInt()), nonint.asDouble());
125 return Op<double>()(a.asDouble(), b.asDouble());
127 return Op<int64_t>()(a.asInt(), b.asInt());
130 } // namespace detail
132 //////////////////////////////////////////////////////////////////////
135 * We're doing this instead of a simple member typedef to avoid the
136 * undefined behavior of parameterizing std::unordered_map<> with an
139 * Note: Later we may add separate order tracking here (a multi-index
142 struct dynamic::ObjectImpl : std::unordered_map<dynamic, dynamic> {};
144 //////////////////////////////////////////////////////////////////////
146 // Helper object for creating objects conveniently. See object and
147 // the dynamic::dynamic(ObjectMaker&&) ctor.
148 struct dynamic::ObjectMaker {
149 friend struct dynamic;
151 explicit ObjectMaker() : val_(dynamic::object) {}
152 explicit ObjectMaker(dynamic key, dynamic val)
153 : val_(dynamic::object)
155 val_.insert(std::move(key), std::move(val));
158 // Make sure no one tries to save one of these into an lvalue with
159 // auto or anything like that.
160 ObjectMaker(ObjectMaker&&) = default;
161 ObjectMaker(ObjectMaker const&) = delete;
162 ObjectMaker& operator=(ObjectMaker const&) = delete;
163 ObjectMaker& operator=(ObjectMaker&&) = delete;
165 // This returns an rvalue-reference instead of an lvalue-reference
166 // to allow constructs like this to moved instead of copied:
167 // dynamic a = dynamic::object("a", "b")("c", "d")
168 ObjectMaker&& operator()(dynamic key, dynamic val) {
169 val_.insert(std::move(key), std::move(val));
170 return std::move(*this);
177 inline void dynamic::array(EmptyArrayTag) {}
179 template <class... Args>
180 inline dynamic dynamic::array(Args&& ...args) {
181 return dynamic(Array{std::forward<Args>(args)...});
184 inline dynamic::ObjectMaker dynamic::object() { return ObjectMaker(); }
185 inline dynamic::ObjectMaker dynamic::object(dynamic a, dynamic b) {
186 return ObjectMaker(std::move(a), std::move(b));
189 //////////////////////////////////////////////////////////////////////
191 struct dynamic::item_iterator : boost::iterator_adaptor<
192 dynamic::item_iterator,
193 dynamic::ObjectImpl::iterator> {
194 /* implicit */ item_iterator(base_type b) : iterator_adaptor_(b) {}
196 using object_type = dynamic::ObjectImpl;
199 friend class boost::iterator_core_access;
202 struct dynamic::value_iterator : boost::iterator_adaptor<
203 dynamic::value_iterator,
204 dynamic::ObjectImpl::iterator,
206 /* implicit */ value_iterator(base_type b) : iterator_adaptor_(b) {}
208 using object_type = dynamic::ObjectImpl;
211 dynamic& dereference() const {
212 return base_reference()->second;
214 friend class boost::iterator_core_access;
217 struct dynamic::const_item_iterator
218 : boost::iterator_adaptor<dynamic::const_item_iterator,
219 dynamic::ObjectImpl::const_iterator> {
220 /* implicit */ const_item_iterator(base_type b) : iterator_adaptor_(b) { }
221 /* implicit */ const_item_iterator(item_iterator i)
222 : iterator_adaptor_(i.base()) {}
223 /* implicit */ const_item_iterator(dynamic::ObjectImpl::iterator i)
224 : iterator_adaptor_(i) {}
226 using object_type = dynamic::ObjectImpl const;
229 friend class boost::iterator_core_access;
232 struct dynamic::const_key_iterator
233 : boost::iterator_adaptor<dynamic::const_key_iterator,
234 dynamic::ObjectImpl::const_iterator,
236 /* implicit */ const_key_iterator(base_type b) : iterator_adaptor_(b) { }
238 using object_type = dynamic::ObjectImpl const;
241 dynamic const& dereference() const {
242 return base_reference()->first;
244 friend class boost::iterator_core_access;
247 struct dynamic::const_value_iterator
248 : boost::iterator_adaptor<dynamic::const_value_iterator,
249 dynamic::ObjectImpl::const_iterator,
251 /* implicit */ const_value_iterator(base_type b) : iterator_adaptor_(b) { }
252 /* implicit */ const_value_iterator(value_iterator i)
253 : iterator_adaptor_(i.base()) {}
254 /* implicit */ const_value_iterator(dynamic::ObjectImpl::iterator i)
255 : iterator_adaptor_(i) {}
257 using object_type = dynamic::ObjectImpl const;
260 dynamic const& dereference() const {
261 return base_reference()->second;
263 friend class boost::iterator_core_access;
266 //////////////////////////////////////////////////////////////////////
268 inline dynamic::dynamic() : dynamic(nullptr) {}
270 inline dynamic::dynamic(std::nullptr_t) : type_(NULLT) {}
272 inline dynamic::dynamic(void (*)(EmptyArrayTag))
275 new (&u_.array) Array();
278 inline dynamic::dynamic(ObjectMaker (*)())
281 new (getAddress<ObjectImpl>()) ObjectImpl();
284 inline dynamic::dynamic(StringPiece s)
287 new (&u_.string) std::string(s.data(), s.size());
290 inline dynamic::dynamic(char const* s)
293 new (&u_.string) std::string(s);
296 inline dynamic::dynamic(std::string s)
299 new (&u_.string) std::string(std::move(s));
302 inline dynamic::dynamic(ObjectMaker&& maker)
305 new (getAddress<ObjectImpl>())
306 ObjectImpl(std::move(*maker.val_.getAddress<ObjectImpl>()));
309 inline dynamic::dynamic(dynamic const& o)
315 inline dynamic::dynamic(dynamic&& o) noexcept
318 *this = std::move(o);
321 inline dynamic::~dynamic() noexcept { destroy(); }
323 // Integral types except bool convert to int64_t, float types to double.
325 struct dynamic::NumericTypeHelper<
326 T, typename std::enable_if<std::is_integral<T>::value>::type> {
328 !kIsObjC || sizeof(T) > sizeof(char),
329 "char-sized types are ambiguous in objc; cast to bool or wider type");
330 using type = int64_t;
333 struct dynamic::NumericTypeHelper<bool> {
337 struct dynamic::NumericTypeHelper<float> {
341 struct dynamic::NumericTypeHelper<double> {
347 class NumericType /* = typename NumericTypeHelper<T>::type */>
348 dynamic::dynamic(T t) {
349 type_ = TypeInfo<NumericType>::type;
350 new (getAddress<NumericType>()) NumericType(NumericType(t));
353 template <class Iterator>
354 dynamic::dynamic(Iterator first, Iterator last)
357 new (&u_.array) Array(first, last);
360 //////////////////////////////////////////////////////////////////////
362 inline dynamic::const_iterator dynamic::begin() const {
363 return get<Array>().begin();
365 inline dynamic::const_iterator dynamic::end() const {
366 return get<Array>().end();
369 inline dynamic::iterator dynamic::begin() {
370 return get<Array>().begin();
372 inline dynamic::iterator dynamic::end() {
373 return get<Array>().end();
377 struct dynamic::IterableProxy {
379 typedef typename It::value_type value_type;
380 typedef typename It::object_type object_type;
382 /* implicit */ IterableProxy(object_type* o) : o_(o) {}
396 inline dynamic::IterableProxy<dynamic::const_key_iterator> dynamic::keys()
398 return &(get<ObjectImpl>());
401 inline dynamic::IterableProxy<dynamic::const_value_iterator> dynamic::values()
403 return &(get<ObjectImpl>());
406 inline dynamic::IterableProxy<dynamic::const_item_iterator> dynamic::items()
408 return &(get<ObjectImpl>());
411 inline dynamic::IterableProxy<dynamic::value_iterator> dynamic::values() {
412 return &(get<ObjectImpl>());
415 inline dynamic::IterableProxy<dynamic::item_iterator> dynamic::items() {
416 return &(get<ObjectImpl>());
419 inline bool dynamic::isString() const {
420 return get_nothrow<std::string>() != nullptr;
422 inline bool dynamic::isObject() const {
423 return get_nothrow<ObjectImpl>() != nullptr;
425 inline bool dynamic::isBool() const {
426 return get_nothrow<bool>() != nullptr;
428 inline bool dynamic::isArray() const {
429 return get_nothrow<Array>() != nullptr;
431 inline bool dynamic::isDouble() const {
432 return get_nothrow<double>() != nullptr;
434 inline bool dynamic::isInt() const {
435 return get_nothrow<int64_t>() != nullptr;
437 inline bool dynamic::isNull() const {
438 return get_nothrow<std::nullptr_t>() != nullptr;
440 inline bool dynamic::isNumber() const {
441 return isInt() || isDouble();
444 inline dynamic::Type dynamic::type() const {
448 inline std::string dynamic::asString() const {
449 return asImpl<std::string>();
451 inline double dynamic::asDouble() const {
452 return asImpl<double>();
454 inline int64_t dynamic::asInt() const {
455 return asImpl<int64_t>();
457 inline bool dynamic::asBool() const {
458 return asImpl<bool>();
461 inline const std::string& dynamic::getString() const& {
462 return get<std::string>();
464 inline double dynamic::getDouble() const& { return get<double>(); }
465 inline int64_t dynamic::getInt() const& { return get<int64_t>(); }
466 inline bool dynamic::getBool() const& { return get<bool>(); }
468 inline std::string& dynamic::getString()& {
469 return get<std::string>();
471 inline double& dynamic::getDouble() & { return get<double>(); }
472 inline int64_t& dynamic::getInt() & { return get<int64_t>(); }
473 inline bool& dynamic::getBool() & { return get<bool>(); }
475 inline std::string&& dynamic::getString()&& {
476 return std::move(get<std::string>());
478 inline double dynamic::getDouble() && { return get<double>(); }
479 inline int64_t dynamic::getInt() && { return get<int64_t>(); }
480 inline bool dynamic::getBool() && { return get<bool>(); }
482 inline const char* dynamic::data() const& {
483 return get<std::string>().data();
485 inline const char* dynamic::c_str() const& {
486 return get<std::string>().c_str();
488 inline StringPiece dynamic::stringPiece() const {
489 return get<std::string>();
493 struct dynamic::CompareOp {
494 static bool comp(T const& a, T const& b) { return a < b; }
497 struct dynamic::CompareOp<dynamic::ObjectImpl> {
498 static bool comp(ObjectImpl const&, ObjectImpl const&) {
499 // This code never executes; it is just here for the compiler.
504 struct dynamic::CompareOp<std::nullptr_t> {
505 static bool comp(std::nullptr_t const&, std::nullptr_t const&) {
510 inline dynamic& dynamic::operator+=(dynamic const& o) {
511 if (type() == STRING && o.type() == STRING) {
512 *getAddress<std::string>() += *o.getAddress<std::string>();
515 *this = detail::numericOp<std::plus>(*this, o);
519 inline dynamic& dynamic::operator-=(dynamic const& o) {
520 *this = detail::numericOp<std::minus>(*this, o);
524 inline dynamic& dynamic::operator*=(dynamic const& o) {
525 *this = detail::numericOp<std::multiplies>(*this, o);
529 inline dynamic& dynamic::operator/=(dynamic const& o) {
530 *this = detail::numericOp<std::divides>(*this, o);
534 #define FB_DYNAMIC_INTEGER_OP(op) \
535 inline dynamic& dynamic::operator op(dynamic const& o) { \
536 if (!isInt() || !o.isInt()) { \
537 throwTypeError_("int64", type(), o.type()); \
539 *getAddress<int64_t>() op o.asInt(); \
543 FB_DYNAMIC_INTEGER_OP(%=)
544 FB_DYNAMIC_INTEGER_OP(|=)
545 FB_DYNAMIC_INTEGER_OP(&=)
546 FB_DYNAMIC_INTEGER_OP(^=)
548 #undef FB_DYNAMIC_INTEGER_OP
550 inline dynamic& dynamic::operator++() {
555 inline dynamic& dynamic::operator--() {
560 inline dynamic const& dynamic::operator[](dynamic const& idx) const& {
564 inline dynamic&& dynamic::operator[](dynamic const& idx) && {
565 return std::move((*this)[idx]);
568 template <class K, class V> inline dynamic& dynamic::setDefault(K&& k, V&& v) {
569 auto& obj = get<ObjectImpl>();
570 return obj.insert(std::make_pair(std::forward<K>(k),
571 std::forward<V>(v))).first->second;
574 template <class K> inline dynamic& dynamic::setDefault(K&& k, dynamic&& v) {
575 auto& obj = get<ObjectImpl>();
576 return obj.insert(std::make_pair(std::forward<K>(k),
577 std::move(v))).first->second;
580 template <class K> inline dynamic& dynamic::setDefault(K&& k, const dynamic& v) {
581 auto& obj = get<ObjectImpl>();
582 return obj.insert(std::make_pair(std::forward<K>(k), v)).first->second;
585 inline dynamic* dynamic::get_ptr(dynamic const& idx) & {
586 return const_cast<dynamic*>(const_cast<dynamic const*>(this)->get_ptr(idx));
589 inline dynamic& dynamic::at(dynamic const& idx) & {
590 return const_cast<dynamic&>(const_cast<dynamic const*>(this)->at(idx));
593 inline dynamic&& dynamic::at(dynamic const& idx) && {
594 return std::move(at(idx));
597 inline bool dynamic::empty() const {
604 inline std::size_t dynamic::count(dynamic const& key) const {
605 return find(key) != items().end() ? 1u : 0u;
608 inline dynamic::const_item_iterator dynamic::find(dynamic const& key) const {
609 return get<ObjectImpl>().find(key);
611 inline dynamic::item_iterator dynamic::find(dynamic const& key) {
612 return get<ObjectImpl>().find(key);
615 template <class K, class V> inline void dynamic::insert(K&& key, V&& val) {
616 auto& obj = get<ObjectImpl>();
617 auto rv = obj.insert({ std::forward<K>(key), nullptr });
618 rv.first->second = std::forward<V>(val);
621 inline void dynamic::update(const dynamic& mergeObj) {
622 if (!isObject() || !mergeObj.isObject()) {
623 throwTypeError_("object", type(), mergeObj.type());
626 for (const auto& pair : mergeObj.items()) {
627 (*this)[pair.first] = pair.second;
631 inline void dynamic::update_missing(const dynamic& mergeObj1) {
632 if (!isObject() || !mergeObj1.isObject()) {
633 throwTypeError_("object", type(), mergeObj1.type());
636 // Only add if not already there
637 for (const auto& pair : mergeObj1.items()) {
638 if ((*this).find(pair.first) == (*this).items().end()) {
639 (*this)[pair.first] = pair.second;
644 inline dynamic dynamic::merge(
645 const dynamic& mergeObj1,
646 const dynamic& mergeObj2) {
648 // No checks on type needed here because they are done in update_missing
649 // Note that we do update_missing here instead of update() because
650 // it will prevent the extra writes that would occur with update()
651 auto ret = mergeObj2;
652 ret.update_missing(mergeObj1);
656 inline std::size_t dynamic::erase(dynamic const& key) {
657 auto& obj = get<ObjectImpl>();
658 return obj.erase(key);
661 inline dynamic::iterator dynamic::erase(const_iterator it) {
662 auto& arr = get<Array>();
663 // std::vector doesn't have an erase method that works on const iterators,
664 // even though the standard says it should, so this hack converts to a
665 // non-const iterator before calling erase.
666 return get<Array>().erase(arr.begin() + (it - arr.begin()));
669 inline dynamic::const_key_iterator dynamic::erase(const_key_iterator it) {
670 return const_key_iterator(get<ObjectImpl>().erase(it.base()));
673 inline dynamic::const_key_iterator dynamic::erase(
674 const_key_iterator first,
675 const_key_iterator last) {
676 return const_key_iterator(get<ObjectImpl>().erase(first.base(),
680 inline dynamic::value_iterator dynamic::erase(const_value_iterator it) {
681 return value_iterator(get<ObjectImpl>().erase(it.base()));
684 inline dynamic::value_iterator dynamic::erase(
685 const_value_iterator first,
686 const_value_iterator last) {
687 return value_iterator(get<ObjectImpl>().erase(first.base(), last.base()));
690 inline dynamic::item_iterator dynamic::erase(const_item_iterator it) {
691 return item_iterator(get<ObjectImpl>().erase(it.base()));
694 inline dynamic::item_iterator dynamic::erase(
695 const_item_iterator first,
696 const_item_iterator last) {
697 return item_iterator(get<ObjectImpl>().erase(first.base(), last.base()));
700 inline void dynamic::resize(std::size_t sz, dynamic const& c) {
701 auto& arr = get<Array>();
705 inline void dynamic::push_back(dynamic const& v) {
706 auto& arr = get<Array>();
710 inline void dynamic::push_back(dynamic&& v) {
711 auto& arr = get<Array>();
712 arr.push_back(std::move(v));
715 inline void dynamic::pop_back() {
716 auto& arr = get<Array>();
720 //////////////////////////////////////////////////////////////////////
722 inline dynamic::dynamic(Array&& r) : type_(ARRAY) {
723 new (&u_.array) Array(std::move(r));
726 #define FOLLY_DYNAMIC_DEC_TYPEINFO(T, str, val) \
727 template <> struct dynamic::TypeInfo<T> { \
728 static constexpr const char* name = str; \
729 static constexpr dynamic::Type type = val; \
733 FOLLY_DYNAMIC_DEC_TYPEINFO(std::nullptr_t, "null", dynamic::NULLT)
734 FOLLY_DYNAMIC_DEC_TYPEINFO(bool, "boolean", dynamic::BOOL)
735 FOLLY_DYNAMIC_DEC_TYPEINFO(std::string, "string", dynamic::STRING)
736 FOLLY_DYNAMIC_DEC_TYPEINFO(dynamic::Array, "array", dynamic::ARRAY)
737 FOLLY_DYNAMIC_DEC_TYPEINFO(double, "double", dynamic::DOUBLE)
738 FOLLY_DYNAMIC_DEC_TYPEINFO(int64_t, "int64", dynamic::INT64)
739 FOLLY_DYNAMIC_DEC_TYPEINFO(dynamic::ObjectImpl, "object", dynamic::OBJECT)
741 #undef FOLLY_DYNAMIC_DEC_TYPEINFO
744 T dynamic::asImpl() const {
746 case INT64: return to<T>(*get_nothrow<int64_t>());
747 case DOUBLE: return to<T>(*get_nothrow<double>());
748 case BOOL: return to<T>(*get_nothrow<bool>());
750 return to<T>(*get_nothrow<std::string>());
752 throwTypeError_("int/double/bool/string", type());
756 // Return a T* to our type, or null if we're not that type.
758 T* dynamic::get_nothrow() & noexcept {
759 if (type_ != TypeInfo<T>::type) {
762 return getAddress<T>();
766 T const* dynamic::get_nothrow() const& noexcept {
767 return const_cast<dynamic*>(this)->get_nothrow<T>();
770 // Return T* for where we can put a T, without type checking. (Memory
771 // might be uninitialized, even.)
773 T* dynamic::getAddress() noexcept {
774 return GetAddrImpl<T>::get(u_);
778 T const* dynamic::getAddress() const noexcept {
779 return const_cast<dynamic*>(this)->getAddress<T>();
782 template <class T> struct dynamic::GetAddrImpl {};
783 template <> struct dynamic::GetAddrImpl<std::nullptr_t> {
784 static std::nullptr_t* get(Data& d) noexcept { return &d.nul; }
786 template <> struct dynamic::GetAddrImpl<dynamic::Array> {
787 static Array* get(Data& d) noexcept { return &d.array; }
789 template <> struct dynamic::GetAddrImpl<bool> {
790 static bool* get(Data& d) noexcept { return &d.boolean; }
792 template <> struct dynamic::GetAddrImpl<int64_t> {
793 static int64_t* get(Data& d) noexcept { return &d.integer; }
795 template <> struct dynamic::GetAddrImpl<double> {
796 static double* get(Data& d) noexcept { return &d.doubl; }
799 struct dynamic::GetAddrImpl<std::string> {
800 static std::string* get(Data& d) noexcept {
804 template <> struct dynamic::GetAddrImpl<dynamic::ObjectImpl> {
805 static_assert(sizeof(ObjectImpl) <= sizeof(Data::objectBuffer),
806 "In your implementation, std::unordered_map<> apparently takes different"
807 " amount of space depending on its template parameters. This is "
808 "weird. Make objectBuffer bigger if you want to compile dynamic.");
810 static ObjectImpl* get(Data& d) noexcept {
811 void* data = &d.objectBuffer;
812 return static_cast<ObjectImpl*>(data);
818 if (auto* p = get_nothrow<T>()) {
821 throwTypeError_(TypeInfo<T>::name, type());
825 T const& dynamic::get() const {
826 return const_cast<dynamic*>(this)->get<T>();
829 //////////////////////////////////////////////////////////////////////
832 * Helper for implementing operator<<. Throws if the type shouldn't
836 struct dynamic::PrintImpl {
837 static void print(dynamic const&, std::ostream& out, T const& t) {
841 // Otherwise, null, being (void*)0, would print as 0.
843 struct dynamic::PrintImpl<std::nullptr_t> {
844 static void print(dynamic const& /* d */,
846 std::nullptr_t const&) {
851 struct dynamic::PrintImpl<dynamic::ObjectImpl> {
852 static void print(dynamic const& d,
854 dynamic::ObjectImpl const&) {
855 d.print_as_pseudo_json(out);
859 struct dynamic::PrintImpl<dynamic::Array> {
860 static void print(dynamic const& d,
862 dynamic::Array const&) {
863 d.print_as_pseudo_json(out);
867 inline void dynamic::print(std::ostream& out) const {
868 #define FB_X(T) PrintImpl<T>::print(*this, out, *getAddress<T>())
869 FB_DYNAMIC_APPLY(type_, FB_X);
873 inline std::ostream& operator<<(std::ostream& out, dynamic const& d) {
878 //////////////////////////////////////////////////////////////////////
880 // Secialization of FormatValue so dynamic objects can be formatted
882 class FormatValue<dynamic> {
884 explicit FormatValue(const dynamic& val) : val_(val) { }
886 template <class FormatCallback>
887 void format(FormatArg& arg, FormatCallback& cb) const {
888 switch (val_.type()) {
890 FormatValue<std::nullptr_t>(nullptr).format(arg, cb);
893 FormatValue<bool>(val_.asBool()).format(arg, cb);
896 FormatValue<int64_t>(val_.asInt()).format(arg, cb);
898 case dynamic::STRING:
899 FormatValue<std::string>(val_.asString()).format(arg, cb);
901 case dynamic::DOUBLE:
902 FormatValue<double>(val_.asDouble()).format(arg, cb);
905 FormatValue(val_.at(arg.splitIntKey())).format(arg, cb);
907 case dynamic::OBJECT:
908 FormatValue(val_.at(arg.splitKey().toString())).format(arg, cb);
918 class FormatValue<detail::DefaultValueWrapper<dynamic, V>> {
920 explicit FormatValue(
921 const detail::DefaultValueWrapper<dynamic, V>& val)
924 template <class FormatCallback>
925 void format(FormatArg& arg, FormatCallback& cb) const {
926 auto& c = val_.container;
931 case dynamic::STRING:
932 case dynamic::DOUBLE:
933 FormatValue<dynamic>(c).format(arg, cb);
937 int key = arg.splitIntKey();
938 if (key >= 0 && size_t(key) < c.size()) {
939 FormatValue<dynamic>(c.at(key)).format(arg, cb);
941 FormatValue<V>(val_.defaultValue).format(arg, cb);
945 case dynamic::OBJECT:
947 auto pos = c.find(arg.splitKey());
948 if (pos != c.items().end()) {
949 FormatValue<dynamic>(pos->second).format(arg, cb);
951 FormatValue<V>(val_.defaultValue).format(arg, cb);
959 const detail::DefaultValueWrapper<dynamic, V>& val_;
964 #undef FB_DYNAMIC_APPLY