Skip to content

Commit 8b6e1f9

Browse files
authored
Fix for always_null_t in object handling and faster always null write (#1545)
1 parent eb7b085 commit 8b6e1f9

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

include/glaze/json/write.hpp

+25-17
Original file line numberDiff line numberDiff line change
@@ -1730,7 +1730,7 @@ namespace glz
17301730
}
17311731
else {
17321732
if constexpr (null_t<val_t> && Opts.skip_null_members) {
1733-
if constexpr (always_null_t<T>)
1733+
if constexpr (always_null_t<val_t>)
17341734
return;
17351735
else {
17361736
const auto is_null = [&]() {
@@ -1823,17 +1823,27 @@ namespace glz
18231823
std::memset(&b[ix], Opts.indentation_char, ctx.indentation_level);
18241824
ix += ctx.indentation_level;
18251825
}
1826+
1827+
using val_t = field_t<T, I>;
18261828

18271829
// MSVC requires get<I> rather than keys[I]
18281830
static constexpr auto key = glz::get<I>(reflect<T>::keys); // GCC 14 requires auto here
1829-
if constexpr (Opts.prettify) {
1830-
static constexpr auto quoted_key = quoted_key_v<key, Opts.prettify>;
1831-
static constexpr auto n = quoted_key.size();
1832-
std::memcpy(&b[ix], quoted_key.data(), n);
1833-
ix += n;
1831+
if constexpr (always_null_t<val_t>) {
1832+
if constexpr (I == 0 || Opts.prettify) {
1833+
static constexpr auto quoted_key = join_v<quoted_key_v<key, Opts.prettify>, chars<"null">>;
1834+
static constexpr auto n = quoted_key.size();
1835+
std::memcpy(&b[ix], quoted_key.data(), n);
1836+
ix += n;
1837+
}
1838+
else {
1839+
static constexpr auto quoted_key = join_v<chars<",">, quoted_key_v<key, Opts.prettify>, chars<"null">>;
1840+
static constexpr auto n = quoted_key.size();
1841+
std::memcpy(&b[ix], quoted_key.data(), n);
1842+
ix += n;
1843+
}
18341844
}
18351845
else {
1836-
if constexpr (I == 0) {
1846+
if constexpr (I == 0 || Opts.prettify) {
18371847
static constexpr auto quoted_key = quoted_key_v<key, Opts.prettify>;
18381848
static constexpr auto n = quoted_key.size();
18391849
std::memcpy(&b[ix], quoted_key.data(), n);
@@ -1845,17 +1855,15 @@ namespace glz
18451855
std::memcpy(&b[ix], quoted_key.data(), n);
18461856
ix += n;
18471857
}
1848-
}
18491858

1850-
using val_t = field_t<T, I>;
1851-
1852-
static constexpr auto check_opts = required_padding<val_t>() ? write_unchecked_on<Opts>() : Opts;
1853-
if constexpr (reflectable<T>) {
1854-
to<JSON, val_t>::template op<check_opts>(get_member(value, get<I>(t)), ctx, b, ix);
1855-
}
1856-
else {
1857-
to<JSON, val_t>::template op<check_opts>(get_member(value, get<I>(reflect<T>::values)), ctx, b,
1858-
ix);
1859+
static constexpr auto check_opts = required_padding<val_t>() ? write_unchecked_on<Opts>() : Opts;
1860+
if constexpr (reflectable<T>) {
1861+
to<JSON, val_t>::template op<check_opts>(get_member(value, get<I>(t)), ctx, b, ix);
1862+
}
1863+
else {
1864+
to<JSON, val_t>::template op<check_opts>(get_member(value, get<I>(reflect<T>::values)), ctx, b,
1865+
ix);
1866+
}
18591867
}
18601868
});
18611869
}

0 commit comments

Comments
 (0)