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.
103 template <class T> static void destroy(T* t) { t->~T(); }
107 * Helper for implementing numeric conversions in operators on
108 * numbers. Just promotes to double when one of the arguments is
109 * double, or throws if either is not a numeric type.
111 template <template <class> class Op>
112 dynamic numericOp(dynamic const& a, dynamic const& b) {
113 if (!a.isNumber() || !b.isNumber()) {
114 throwTypeError_("numeric", a.type(), b.type());
116 if (a.type() != b.type()) {
117 auto& integ = a.isInt() ? a : b;
118 auto& nonint = a.isInt() ? b : a;
119 return Op<double>()(to<double>(integ.asInt()), nonint.asDouble());
122 return Op<double>()(a.asDouble(), b.asDouble());
124 return Op<int64_t>()(a.asInt(), b.asInt());
129 //////////////////////////////////////////////////////////////////////
132 * We're doing this instead of a simple member typedef to avoid the
133 * undefined behavior of parameterizing std::unordered_map<> with an
136 * Note: Later we may add separate order tracking here (a multi-index
139 struct dynamic::ObjectImpl : std::unordered_map<dynamic, dynamic> {};
141 //////////////////////////////////////////////////////////////////////
143 // Helper object for creating objects conveniently. See object and
144 // the dynamic::dynamic(ObjectMaker&&) ctor.
145 struct dynamic::ObjectMaker {
146 friend struct dynamic;
148 explicit ObjectMaker() : val_(dynamic::object) {}
149 explicit ObjectMaker(dynamic key, dynamic val)
150 : val_(dynamic::object)
152 val_.insert(std::move(key), std::move(val));
155 // Make sure no one tries to save one of these into an lvalue with
156 // auto or anything like that.
157 ObjectMaker(ObjectMaker&&) = default;
158 ObjectMaker(ObjectMaker const&) = delete;
159 ObjectMaker& operator=(ObjectMaker const&) = delete;
160 ObjectMaker& operator=(ObjectMaker&&) = delete;
162 // This returns an rvalue-reference instead of an lvalue-reference
163 // to allow constructs like this to moved instead of copied:
164 // dynamic a = dynamic::object("a", "b")("c", "d")
165 ObjectMaker&& operator()(dynamic key, dynamic val) {
166 val_.insert(std::move(key), std::move(val));
167 return std::move(*this);
174 inline void dynamic::array(EmptyArrayTag) {}
176 template <class... Args>
177 inline dynamic dynamic::array(Args&& ...args) {
178 return dynamic(Array{std::forward<Args>(args)...});
181 inline dynamic::ObjectMaker dynamic::object() { return ObjectMaker(); }
182 inline dynamic::ObjectMaker dynamic::object(dynamic a, dynamic b) {
183 return ObjectMaker(std::move(a), std::move(b));
186 //////////////////////////////////////////////////////////////////////
188 struct dynamic::item_iterator : boost::iterator_adaptor<
189 dynamic::item_iterator,
190 dynamic::ObjectImpl::iterator> {
191 /* implicit */ item_iterator(base_type b) : iterator_adaptor_(b) {}
193 using object_type = dynamic::ObjectImpl;
196 friend class boost::iterator_core_access;
199 struct dynamic::value_iterator : boost::iterator_adaptor<
200 dynamic::value_iterator,
201 dynamic::ObjectImpl::iterator,
203 /* implicit */ value_iterator(base_type b) : iterator_adaptor_(b) {}
205 using object_type = dynamic::ObjectImpl;
208 dynamic& dereference() const {
209 return base_reference()->second;
211 friend class boost::iterator_core_access;
214 struct dynamic::const_item_iterator
215 : boost::iterator_adaptor<dynamic::const_item_iterator,
216 dynamic::ObjectImpl::const_iterator> {
217 /* implicit */ const_item_iterator(base_type b) : iterator_adaptor_(b) { }
218 /* implicit */ const_item_iterator(item_iterator i)
219 : iterator_adaptor_(i.base()) {}
220 /* implicit */ const_item_iterator(dynamic::ObjectImpl::iterator i)
221 : iterator_adaptor_(i) {}
223 using object_type = dynamic::ObjectImpl const;
226 friend class boost::iterator_core_access;
229 struct dynamic::const_key_iterator
230 : boost::iterator_adaptor<dynamic::const_key_iterator,
231 dynamic::ObjectImpl::const_iterator,
233 /* implicit */ const_key_iterator(base_type b) : iterator_adaptor_(b) { }
235 using object_type = dynamic::ObjectImpl const;
238 dynamic const& dereference() const {
239 return base_reference()->first;
241 friend class boost::iterator_core_access;
244 struct dynamic::const_value_iterator
245 : boost::iterator_adaptor<dynamic::const_value_iterator,
246 dynamic::ObjectImpl::const_iterator,
248 /* implicit */ const_value_iterator(base_type b) : iterator_adaptor_(b) { }
249 /* implicit */ const_value_iterator(value_iterator i)
250 : iterator_adaptor_(i.base()) {}
251 /* implicit */ const_value_iterator(dynamic::ObjectImpl::iterator i)
252 : iterator_adaptor_(i) {}
254 using object_type = dynamic::ObjectImpl const;
257 dynamic const& dereference() const {
258 return base_reference()->second;
260 friend class boost::iterator_core_access;
263 //////////////////////////////////////////////////////////////////////
265 inline dynamic::dynamic() : dynamic(nullptr) {}
267 inline dynamic::dynamic(std::nullptr_t) : type_(NULLT) {}
269 inline dynamic::dynamic(void (*)(EmptyArrayTag))
272 new (&u_.array) Array();
275 inline dynamic::dynamic(ObjectMaker (*)())
278 new (getAddress<ObjectImpl>()) ObjectImpl();
281 inline dynamic::dynamic(StringPiece s)
284 new (&u_.string) std::string(s.data(), s.size());
287 inline dynamic::dynamic(char const* s)
290 new (&u_.string) std::string(s);
293 inline dynamic::dynamic(std::string s)
296 new (&u_.string) std::string(std::move(s));
299 inline dynamic::dynamic(ObjectMaker&& maker)
302 new (getAddress<ObjectImpl>())
303 ObjectImpl(std::move(*maker.val_.getAddress<ObjectImpl>()));
306 inline dynamic::dynamic(dynamic const& o)
312 inline dynamic::dynamic(dynamic&& o) noexcept
315 *this = std::move(o);
318 inline dynamic::~dynamic() noexcept { destroy(); }
320 // Integral types except bool convert to int64_t, float types to double.
322 struct dynamic::NumericTypeHelper<
323 T, typename std::enable_if<std::is_integral<T>::value>::type> {
325 !kIsObjC || sizeof(T) > sizeof(char),
326 "char-sized types are ambiguous in objc; cast to bool or wider type");
327 using type = int64_t;
330 struct dynamic::NumericTypeHelper<bool> {
334 struct dynamic::NumericTypeHelper<float> {
338 struct dynamic::NumericTypeHelper<double> {
344 class NumericType /* = typename NumericTypeHelper<T>::type */>
345 dynamic::dynamic(T t) {
346 type_ = TypeInfo<NumericType>::type;
347 new (getAddress<NumericType>()) NumericType(NumericType(t));
350 template <class Iterator>
351 dynamic::dynamic(Iterator first, Iterator last)
354 new (&u_.array) Array(first, last);
357 //////////////////////////////////////////////////////////////////////
359 inline dynamic::const_iterator dynamic::begin() const {
360 return get<Array>().begin();
362 inline dynamic::const_iterator dynamic::end() const {
363 return get<Array>().end();
366 inline dynamic::iterator dynamic::begin() {
367 return get<Array>().begin();
369 inline dynamic::iterator dynamic::end() {
370 return get<Array>().end();
374 struct dynamic::IterableProxy {
376 typedef typename It::value_type value_type;
377 typedef typename It::object_type object_type;
379 /* implicit */ IterableProxy(object_type* o) : o_(o) {}
393 inline dynamic::IterableProxy<dynamic::const_key_iterator> dynamic::keys()
395 return &(get<ObjectImpl>());
398 inline dynamic::IterableProxy<dynamic::const_value_iterator> dynamic::values()
400 return &(get<ObjectImpl>());
403 inline dynamic::IterableProxy<dynamic::const_item_iterator> dynamic::items()
405 return &(get<ObjectImpl>());
408 inline dynamic::IterableProxy<dynamic::value_iterator> dynamic::values() {
409 return &(get<ObjectImpl>());
412 inline dynamic::IterableProxy<dynamic::item_iterator> dynamic::items() {
413 return &(get<ObjectImpl>());
416 inline bool dynamic::isString() const {
417 return get_nothrow<std::string>() != nullptr;
419 inline bool dynamic::isObject() const {
420 return get_nothrow<ObjectImpl>() != nullptr;
422 inline bool dynamic::isBool() const {
423 return get_nothrow<bool>() != nullptr;
425 inline bool dynamic::isArray() const {
426 return get_nothrow<Array>() != nullptr;
428 inline bool dynamic::isDouble() const {
429 return get_nothrow<double>() != nullptr;
431 inline bool dynamic::isInt() const {
432 return get_nothrow<int64_t>() != nullptr;
434 inline bool dynamic::isNull() const {
435 return get_nothrow<std::nullptr_t>() != nullptr;
437 inline bool dynamic::isNumber() const {
438 return isInt() || isDouble();
441 inline dynamic::Type dynamic::type() const {
445 inline std::string dynamic::asString() const {
446 return asImpl<std::string>();
448 inline double dynamic::asDouble() const {
449 return asImpl<double>();
451 inline int64_t dynamic::asInt() const {
452 return asImpl<int64_t>();
454 inline bool dynamic::asBool() const {
455 return asImpl<bool>();
458 inline const std::string& dynamic::getString() const& {
459 return get<std::string>();
461 inline double dynamic::getDouble() const& { return get<double>(); }
462 inline int64_t dynamic::getInt() const& { return get<int64_t>(); }
463 inline bool dynamic::getBool() const& { return get<bool>(); }
465 inline std::string& dynamic::getString()& {
466 return get<std::string>();
468 inline double& dynamic::getDouble() & { return get<double>(); }
469 inline int64_t& dynamic::getInt() & { return get<int64_t>(); }
470 inline bool& dynamic::getBool() & { return get<bool>(); }
472 inline std::string&& dynamic::getString()&& {
473 return std::move(get<std::string>());
475 inline double dynamic::getDouble() && { return get<double>(); }
476 inline int64_t dynamic::getInt() && { return get<int64_t>(); }
477 inline bool dynamic::getBool() && { return get<bool>(); }
479 inline const char* dynamic::data() const& {
480 return get<std::string>().data();
482 inline const char* dynamic::c_str() const& {
483 return get<std::string>().c_str();
485 inline StringPiece dynamic::stringPiece() const {
486 return get<std::string>();
490 struct dynamic::CompareOp {
491 static bool comp(T const& a, T const& b) { return a < b; }
494 struct dynamic::CompareOp<dynamic::ObjectImpl> {
495 static bool comp(ObjectImpl const&, ObjectImpl const&) {
496 // This code never executes; it is just here for the compiler.
501 struct dynamic::CompareOp<std::nullptr_t> {
502 static bool comp(std::nullptr_t const&, std::nullptr_t const&) {
507 inline dynamic& dynamic::operator+=(dynamic const& o) {
508 if (type() == STRING && o.type() == STRING) {
509 *getAddress<std::string>() += *o.getAddress<std::string>();
512 *this = detail::numericOp<std::plus>(*this, o);
516 inline dynamic& dynamic::operator-=(dynamic const& o) {
517 *this = detail::numericOp<std::minus>(*this, o);
521 inline dynamic& dynamic::operator*=(dynamic const& o) {
522 *this = detail::numericOp<std::multiplies>(*this, o);
526 inline dynamic& dynamic::operator/=(dynamic const& o) {
527 *this = detail::numericOp<std::divides>(*this, o);
531 #define FB_DYNAMIC_INTEGER_OP(op) \
532 inline dynamic& dynamic::operator op(dynamic const& o) { \
533 if (!isInt() || !o.isInt()) { \
534 throwTypeError_("int64", type(), o.type()); \
536 *getAddress<int64_t>() op o.asInt(); \
540 FB_DYNAMIC_INTEGER_OP(%=)
541 FB_DYNAMIC_INTEGER_OP(|=)
542 FB_DYNAMIC_INTEGER_OP(&=)
543 FB_DYNAMIC_INTEGER_OP(^=)
545 #undef FB_DYNAMIC_INTEGER_OP
547 inline dynamic& dynamic::operator++() {
552 inline dynamic& dynamic::operator--() {
557 inline dynamic const& dynamic::operator[](dynamic const& idx) const& {
561 inline dynamic&& dynamic::operator[](dynamic const& idx) && {
562 return std::move((*this)[idx]);
565 template <class K, class V> inline dynamic& dynamic::setDefault(K&& k, V&& v) {
566 auto& obj = get<ObjectImpl>();
567 return obj.insert(std::make_pair(std::forward<K>(k),
568 std::forward<V>(v))).first->second;
571 template <class K> inline dynamic& dynamic::setDefault(K&& k, dynamic&& v) {
572 auto& obj = get<ObjectImpl>();
573 return obj.insert(std::make_pair(std::forward<K>(k),
574 std::move(v))).first->second;
577 template <class K> inline dynamic& dynamic::setDefault(K&& k, const dynamic& v) {
578 auto& obj = get<ObjectImpl>();
579 return obj.insert(std::make_pair(std::forward<K>(k), v)).first->second;
582 inline dynamic* dynamic::get_ptr(dynamic const& idx) & {
583 return const_cast<dynamic*>(const_cast<dynamic const*>(this)->get_ptr(idx));
586 inline dynamic& dynamic::at(dynamic const& idx) & {
587 return const_cast<dynamic&>(const_cast<dynamic const*>(this)->at(idx));
590 inline dynamic&& dynamic::at(dynamic const& idx) && {
591 return std::move(at(idx));
594 inline bool dynamic::empty() const {
601 inline std::size_t dynamic::count(dynamic const& key) const {
602 return find(key) != items().end() ? 1u : 0u;
605 inline dynamic::const_item_iterator dynamic::find(dynamic const& key) const {
606 return get<ObjectImpl>().find(key);
608 inline dynamic::item_iterator dynamic::find(dynamic const& key) {
609 return get<ObjectImpl>().find(key);
612 template <class K, class V> inline void dynamic::insert(K&& key, V&& val) {
613 auto& obj = get<ObjectImpl>();
614 auto rv = obj.insert({ std::forward<K>(key), nullptr });
615 rv.first->second = std::forward<V>(val);
618 inline void dynamic::update(const dynamic& mergeObj) {
619 if (!isObject() || !mergeObj.isObject()) {
620 throwTypeError_("object", type(), mergeObj.type());
623 for (const auto& pair : mergeObj.items()) {
624 (*this)[pair.first] = pair.second;
628 inline void dynamic::update_missing(const dynamic& mergeObj1) {
629 if (!isObject() || !mergeObj1.isObject()) {
630 throwTypeError_("object", type(), mergeObj1.type());
633 // Only add if not already there
634 for (const auto& pair : mergeObj1.items()) {
635 if ((*this).find(pair.first) == (*this).items().end()) {
636 (*this)[pair.first] = pair.second;
641 inline dynamic dynamic::merge(
642 const dynamic& mergeObj1,
643 const dynamic& mergeObj2) {
645 // No checks on type needed here because they are done in update_missing
646 // Note that we do update_missing here instead of update() because
647 // it will prevent the extra writes that would occur with update()
648 auto ret = mergeObj2;
649 ret.update_missing(mergeObj1);
653 inline std::size_t dynamic::erase(dynamic const& key) {
654 auto& obj = get<ObjectImpl>();
655 return obj.erase(key);
658 inline dynamic::iterator dynamic::erase(const_iterator it) {
659 auto& arr = get<Array>();
660 // std::vector doesn't have an erase method that works on const iterators,
661 // even though the standard says it should, so this hack converts to a
662 // non-const iterator before calling erase.
663 return get<Array>().erase(arr.begin() + (it - arr.begin()));
666 inline dynamic::const_key_iterator dynamic::erase(const_key_iterator it) {
667 return const_key_iterator(get<ObjectImpl>().erase(it.base()));
670 inline dynamic::const_key_iterator dynamic::erase(
671 const_key_iterator first,
672 const_key_iterator last) {
673 return const_key_iterator(get<ObjectImpl>().erase(first.base(),
677 inline dynamic::value_iterator dynamic::erase(const_value_iterator it) {
678 return value_iterator(get<ObjectImpl>().erase(it.base()));
681 inline dynamic::value_iterator dynamic::erase(
682 const_value_iterator first,
683 const_value_iterator last) {
684 return value_iterator(get<ObjectImpl>().erase(first.base(), last.base()));
687 inline dynamic::item_iterator dynamic::erase(const_item_iterator it) {
688 return item_iterator(get<ObjectImpl>().erase(it.base()));
691 inline dynamic::item_iterator dynamic::erase(
692 const_item_iterator first,
693 const_item_iterator last) {
694 return item_iterator(get<ObjectImpl>().erase(first.base(), last.base()));
697 inline void dynamic::resize(std::size_t sz, dynamic const& c) {
698 auto& arr = get<Array>();
702 inline void dynamic::push_back(dynamic const& v) {
703 auto& arr = get<Array>();
707 inline void dynamic::push_back(dynamic&& v) {
708 auto& arr = get<Array>();
709 arr.push_back(std::move(v));
712 inline void dynamic::pop_back() {
713 auto& arr = get<Array>();
717 //////////////////////////////////////////////////////////////////////
719 inline dynamic::dynamic(Array&& r) : type_(ARRAY) {
720 new (&u_.array) Array(std::move(r));
723 #define FOLLY_DYNAMIC_DEC_TYPEINFO(T, str, val) \
724 template <> struct dynamic::TypeInfo<T> { \
725 static constexpr const char* name = str; \
726 static constexpr dynamic::Type type = val; \
730 FOLLY_DYNAMIC_DEC_TYPEINFO(std::nullptr_t, "null", dynamic::NULLT)
731 FOLLY_DYNAMIC_DEC_TYPEINFO(bool, "boolean", dynamic::BOOL)
732 FOLLY_DYNAMIC_DEC_TYPEINFO(std::string, "string", dynamic::STRING)
733 FOLLY_DYNAMIC_DEC_TYPEINFO(dynamic::Array, "array", dynamic::ARRAY)
734 FOLLY_DYNAMIC_DEC_TYPEINFO(double, "double", dynamic::DOUBLE)
735 FOLLY_DYNAMIC_DEC_TYPEINFO(int64_t, "int64", dynamic::INT64)
736 FOLLY_DYNAMIC_DEC_TYPEINFO(dynamic::ObjectImpl, "object", dynamic::OBJECT)
738 #undef FOLLY_DYNAMIC_DEC_TYPEINFO
741 T dynamic::asImpl() const {
743 case INT64: return to<T>(*get_nothrow<int64_t>());
744 case DOUBLE: return to<T>(*get_nothrow<double>());
745 case BOOL: return to<T>(*get_nothrow<bool>());
747 return to<T>(*get_nothrow<std::string>());
749 throwTypeError_("int/double/bool/string", type());
753 // Return a T* to our type, or null if we're not that type.
755 T* dynamic::get_nothrow() & noexcept {
756 if (type_ != TypeInfo<T>::type) {
759 return getAddress<T>();
763 T const* dynamic::get_nothrow() const& noexcept {
764 return const_cast<dynamic*>(this)->get_nothrow<T>();
767 // Return T* for where we can put a T, without type checking. (Memory
768 // might be uninitialized, even.)
770 T* dynamic::getAddress() noexcept {
771 return GetAddrImpl<T>::get(u_);
775 T const* dynamic::getAddress() const noexcept {
776 return const_cast<dynamic*>(this)->getAddress<T>();
779 template <class T> struct dynamic::GetAddrImpl {};
780 template <> struct dynamic::GetAddrImpl<std::nullptr_t> {
781 static std::nullptr_t* get(Data& d) noexcept { return &d.nul; }
783 template <> struct dynamic::GetAddrImpl<dynamic::Array> {
784 static Array* get(Data& d) noexcept { return &d.array; }
786 template <> struct dynamic::GetAddrImpl<bool> {
787 static bool* get(Data& d) noexcept { return &d.boolean; }
789 template <> struct dynamic::GetAddrImpl<int64_t> {
790 static int64_t* get(Data& d) noexcept { return &d.integer; }
792 template <> struct dynamic::GetAddrImpl<double> {
793 static double* get(Data& d) noexcept { return &d.doubl; }
796 struct dynamic::GetAddrImpl<std::string> {
797 static std::string* get(Data& d) noexcept {
801 template <> struct dynamic::GetAddrImpl<dynamic::ObjectImpl> {
802 static_assert(sizeof(ObjectImpl) <= sizeof(Data::objectBuffer),
803 "In your implementation, std::unordered_map<> apparently takes different"
804 " amount of space depending on its template parameters. This is "
805 "weird. Make objectBuffer bigger if you want to compile dynamic.");
807 static ObjectImpl* get(Data& d) noexcept {
808 void* data = &d.objectBuffer;
809 return static_cast<ObjectImpl*>(data);
815 if (auto* p = get_nothrow<T>()) {
818 throwTypeError_(TypeInfo<T>::name, type());
822 T const& dynamic::get() const {
823 return const_cast<dynamic*>(this)->get<T>();
826 //////////////////////////////////////////////////////////////////////
829 * Helper for implementing operator<<. Throws if the type shouldn't
833 struct dynamic::PrintImpl {
834 static void print(dynamic const&, std::ostream& out, T const& t) {
838 // Otherwise, null, being (void*)0, would print as 0.
840 struct dynamic::PrintImpl<std::nullptr_t> {
841 static void print(dynamic const& /* d */,
843 std::nullptr_t const&) {
848 struct dynamic::PrintImpl<dynamic::ObjectImpl> {
849 static void print(dynamic const& d,
851 dynamic::ObjectImpl const&) {
852 d.print_as_pseudo_json(out);
856 struct dynamic::PrintImpl<dynamic::Array> {
857 static void print(dynamic const& d,
859 dynamic::Array const&) {
860 d.print_as_pseudo_json(out);
864 inline void dynamic::print(std::ostream& out) const {
865 #define FB_X(T) PrintImpl<T>::print(*this, out, *getAddress<T>())
866 FB_DYNAMIC_APPLY(type_, FB_X);
870 inline std::ostream& operator<<(std::ostream& out, dynamic const& d) {
875 //////////////////////////////////////////////////////////////////////
877 // Secialization of FormatValue so dynamic objects can be formatted
879 class FormatValue<dynamic> {
881 explicit FormatValue(const dynamic& val) : val_(val) { }
883 template <class FormatCallback>
884 void format(FormatArg& arg, FormatCallback& cb) const {
885 switch (val_.type()) {
887 FormatValue<std::nullptr_t>(nullptr).format(arg, cb);
890 FormatValue<bool>(val_.asBool()).format(arg, cb);
893 FormatValue<int64_t>(val_.asInt()).format(arg, cb);
895 case dynamic::STRING:
896 FormatValue<std::string>(val_.asString()).format(arg, cb);
898 case dynamic::DOUBLE:
899 FormatValue<double>(val_.asDouble()).format(arg, cb);
902 FormatValue(val_.at(arg.splitIntKey())).format(arg, cb);
904 case dynamic::OBJECT:
905 FormatValue(val_.at(arg.splitKey().toString())).format(arg, cb);
915 class FormatValue<detail::DefaultValueWrapper<dynamic, V>> {
917 explicit FormatValue(
918 const detail::DefaultValueWrapper<dynamic, V>& val)
921 template <class FormatCallback>
922 void format(FormatArg& arg, FormatCallback& cb) const {
923 auto& c = val_.container;
928 case dynamic::STRING:
929 case dynamic::DOUBLE:
930 FormatValue<dynamic>(c).format(arg, cb);
934 int key = arg.splitIntKey();
935 if (key >= 0 && size_t(key) < c.size()) {
936 FormatValue<dynamic>(c.at(key)).format(arg, cb);
938 FormatValue<V>(val_.defaultValue).format(arg, cb);
942 case dynamic::OBJECT:
944 auto pos = c.find(arg.splitKey());
945 if (pos != c.items().end()) {
946 FormatValue<dynamic>(pos->second).format(arg, cb);
948 FormatValue<V>(val_.defaultValue).format(arg, cb);
956 const detail::DefaultValueWrapper<dynamic, V>& val_;
961 #undef FB_DYNAMIC_APPLY