From: Alexey Spiridonov Date: Fri, 15 Jan 2016 20:53:27 +0000 (-0800) Subject: Print null correctly X-Git-Tag: deprecate-dynamic-initializer~152 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=c1ab48e4f588c0b6429cf741436cb7a794c92539;p=folly.git Print null correctly Summary: Null by itself was printing as 0, now it prints as null, which is consistent with the 'pseudo json' output. Reviewed By: yfeldblum Differential Revision: D2789284 fb-gh-sync-id: f318b8d0f8349f4b36f868c419842fb50bee9517 --- diff --git a/folly/dynamic-inl.h b/folly/dynamic-inl.h index 8bdcf2f4..73ab5d37 100644 --- a/folly/dynamic-inl.h +++ b/folly/dynamic-inl.h @@ -728,6 +728,14 @@ struct dynamic::PrintImpl { out << t; } }; +// Otherwise, null, being (void*)0, would print as 0. +template <> +struct dynamic::PrintImpl { + static void print(dynamic const& d, std::ostream& out, void* const& nul) { + DCHECK_EQ((void*)0, nul); + out << "null"; + } +}; template<> struct dynamic::PrintImpl { static void print(dynamic const& d, diff --git a/folly/test/DynamicTest.cpp b/folly/test/DynamicTest.cpp index a065e14b..e7427190 100644 --- a/folly/test/DynamicTest.cpp +++ b/folly/test/DynamicTest.cpp @@ -475,6 +475,12 @@ TEST(Dynamic, Brackets) { EXPECT_NE(ds, md["key1"]); } +TEST(Dynamic, PrintNull) { + std::stringstream ss; + ss << folly::dynamic(nullptr); + EXPECT_EQ("null", ss.str()); +} + int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); gflags::ParseCommandLineFlags(&argc, &argv, true);