Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c2a8616

Browse files
committedJul 13, 2018
serialization: Cover odd integer types in formats::coerce.
This also removes the annoying bit of `#ifdef` logic to detect OSX and Windows. Now, we always attempt to register and rely on `duplicate_type_action::ignore` to silently ignore. Issue #116
1 parent 0789c9e commit c2a8616

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed
 

‎src/jsonv/serialization.cpp

+21-13
Original file line numberDiff line numberDiff line change
@@ -423,12 +423,14 @@ bool formats::operator!=(const formats& other) const
423423
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
424424

425425
template <typename T>
426-
static void register_integer_adapter(formats& fmt)
426+
static void register_integer_adapter(formats& fmt,
427+
duplicate_type_action on_duplicate = duplicate_type_action::exception
428+
)
427429
{
428430
static auto instance = make_adapter([] (const value& from) { return T(from.as_integer()); },
429431
[] (const T& from) { return value(static_cast<std::int64_t>(from)); }
430432
);
431-
fmt.register_adapter(&instance);
433+
fmt.register_adapter(&instance, on_duplicate);
432434
}
433435

434436
static formats create_default_formats()
@@ -468,15 +470,13 @@ static formats create_default_formats()
468470
register_integer_adapter<std::uint32_t>(fmt);
469471
register_integer_adapter<std::int64_t>(fmt);
470472
register_integer_adapter<std::uint64_t>(fmt);
471-
#if defined(__APPLE__)
472-
register_integer_adapter<std::size_t>(fmt);
473-
register_integer_adapter<long>(fmt);
474-
#elif defined(_MSC_VER)
475-
// In MSVC's 64-bit compiler, `std::int64_t` is a distinct type from `long`. Since these are really common types,
476-
// we will add them explicitly.
477-
register_integer_adapter<long>(fmt);
478-
register_integer_adapter<unsigned long>(fmt);
479-
#endif
473+
474+
// These common types are usually covered by the explicitly-sized integers, but try to add them for platforms like
475+
// OSX and Windows.
476+
register_integer_adapter<std::size_t>(fmt, duplicate_type_action::ignore);
477+
register_integer_adapter<std::ptrdiff_t>(fmt, duplicate_type_action::ignore);
478+
register_integer_adapter<long>(fmt, duplicate_type_action::ignore);
479+
register_integer_adapter<unsigned long>(fmt, duplicate_type_action::ignore);
480480

481481
static auto double_extractor = make_adapter([] (const value& from) { return from.as_decimal(); },
482482
[] (const double& from) { return value(from); }
@@ -534,10 +534,11 @@ formats formats::reset_global()
534534
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
535535

536536
template <typename T>
537-
static void register_integer_coerce_extractor(formats& fmt)
537+
static void register_integer_coerce_extractor(formats& fmt,
538+
duplicate_type_action on_duplicate = duplicate_type_action::exception)
538539
{
539540
static auto instance = make_extractor([] (const value& from) { return T(coerce_integer(from)); });
540-
fmt.register_extractor(&instance);
541+
fmt.register_extractor(&instance, on_duplicate);
541542
}
542543

543544
static formats create_coerce_formats()
@@ -559,6 +560,13 @@ static formats create_coerce_formats()
559560
register_integer_coerce_extractor<std::int64_t>(fmt);
560561
register_integer_coerce_extractor<std::uint64_t>(fmt);
561562

563+
// These common types are usually covered by the explicitly-sized integers, but try to add them for platforms like
564+
// OSX and Windows.
565+
register_integer_coerce_extractor<std::size_t>(fmt, duplicate_type_action::ignore);
566+
register_integer_coerce_extractor<std::ptrdiff_t>(fmt, duplicate_type_action::ignore);
567+
register_integer_coerce_extractor<long>(fmt, duplicate_type_action::ignore);
568+
register_integer_coerce_extractor<unsigned long>(fmt, duplicate_type_action::ignore);
569+
562570
static auto double_extractor = make_extractor([] (const value& from) { return coerce_decimal(from); });
563571
fmt.register_extractor(&double_extractor);
564572
static auto float_extractor = make_extractor([] (const value& from) { return float(coerce_decimal(from)); });

0 commit comments

Comments
 (0)
Please sign in to comment.