@@ -277,6 +277,57 @@ TEST(JsonUtilsTest, SafeGetToWithNullableNonNull) {
277277 EXPECT_FALSE (is_null);
278278}
279279
280+ TEST (JsonUtilsTest, SafeGetToWithNullableString) {
281+ auto const * const key = " v" ;
282+ auto constexpr kJsonText = R"( {"v":"Apple"})" ;
283+ auto json = nlohmann::json::parse (kJsonText , nullptr , false );
284+ EXPECT_TRUE (json.is_object ());
285+
286+ std::string val;
287+ bool is_null;
288+ EXPECT_TRUE (SafeGetToWithNullable (val, is_null, json, key));
289+ EXPECT_EQ (val, " Apple" );
290+ }
291+
292+ TEST (JsonUtilsTest, SafeGetToWithNullableStringAsNumber) {
293+ auto const * const key = " v" ;
294+ auto constexpr kJsonText = R"( {"v":"123"})" ;
295+ auto json = nlohmann::json::parse (kJsonText , nullptr , false );
296+ EXPECT_TRUE (json.is_object ());
297+
298+ std::string val;
299+ bool is_null;
300+ EXPECT_TRUE (SafeGetToWithNullable (val, is_null, json, key));
301+ EXPECT_EQ (val, " 123" );
302+ }
303+
304+ TEST (JsonUtilsTest, SafeGetToWithNullableStringAsFloat) {
305+ auto const * const key = " v" ;
306+ auto constexpr kJsonText = R"( {"v":"123.123"})" ;
307+ auto json = nlohmann::json::parse (kJsonText , nullptr , false );
308+ EXPECT_TRUE (json.is_object ());
309+
310+ std::string val;
311+ bool is_null;
312+ EXPECT_TRUE (SafeGetToWithNullable (val, is_null, json, key));
313+ EXPECT_EQ (val, " 123.123" );
314+ }
315+
316+ TEST (JsonUtilsTest, SafeGetToWithNullableStringAsArray) {
317+ auto const * const key = " v" ;
318+ auto constexpr kExpectedJsonText =
319+ R"( [{"v":"1"},{"v":"2"},{"v":"3"},{"v":"4"},{"v":"5"}])" ;
320+ auto constexpr kJsonText =
321+ R"( {"v":[{"v":"1"},{"v":"2"},{"v":"3"},{"v":"4"},{"v":"5"}]})" ;
322+ auto json = nlohmann::json::parse (kJsonText , nullptr , false );
323+ EXPECT_TRUE (json.is_object ());
324+
325+ std::string val;
326+ bool is_null;
327+ EXPECT_TRUE (SafeGetToWithNullable (val, is_null, json, key));
328+ EXPECT_EQ (val, kExpectedJsonText );
329+ }
330+
280331GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
281332} // namespace bigquery_v2_minimal_internal
282333} // namespace cloud
0 commit comments