|
|
|
@ -67,7 +67,7 @@ class JsonUtilTest : public testing::Test { |
|
|
|
|
kTypeUrlPrefix, DescriptorPool::generated_pool())); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
string ToJson(const Message& message, const JsonOptions& options) { |
|
|
|
|
string ToJson(const Message& message, const JsonPrintOptions& options) { |
|
|
|
|
string result; |
|
|
|
|
GOOGLE_CHECK_OK(BinaryToJsonString(resolver_.get(), |
|
|
|
|
GetTypeUrl(message.GetDescriptor()), |
|
|
|
@ -75,10 +75,12 @@ class JsonUtilTest : public testing::Test { |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool FromJson(const string& json, Message* message) { |
|
|
|
|
bool FromJson(const string& json, Message* message, |
|
|
|
|
const JsonParseOptions& options) { |
|
|
|
|
string binary; |
|
|
|
|
if (!JsonToBinaryString(resolver_.get(), |
|
|
|
|
GetTypeUrl(message->GetDescriptor()), json, &binary) |
|
|
|
|
GetTypeUrl(message->GetDescriptor()), json, &binary, |
|
|
|
|
options) |
|
|
|
|
.ok()) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
@ -92,7 +94,7 @@ TEST_F(JsonUtilTest, TestWhitespaces) { |
|
|
|
|
TestMessage m; |
|
|
|
|
m.mutable_message_value(); |
|
|
|
|
|
|
|
|
|
JsonOptions options; |
|
|
|
|
JsonPrintOptions options; |
|
|
|
|
EXPECT_EQ("{\"messageValue\":{}}", ToJson(m, options)); |
|
|
|
|
options.add_whitespace = true; |
|
|
|
|
EXPECT_EQ( |
|
|
|
@ -104,7 +106,7 @@ TEST_F(JsonUtilTest, TestWhitespaces) { |
|
|
|
|
|
|
|
|
|
TEST_F(JsonUtilTest, TestDefaultValues) { |
|
|
|
|
TestMessage m; |
|
|
|
|
JsonOptions options; |
|
|
|
|
JsonPrintOptions options; |
|
|
|
|
EXPECT_EQ("{}", ToJson(m, options)); |
|
|
|
|
options.always_print_primitive_fields = true; |
|
|
|
|
EXPECT_EQ( |
|
|
|
@ -147,8 +149,9 @@ TEST_F(JsonUtilTest, ParseMessage) { |
|
|
|
|
" {\"value\": 40}, {\"value\": 96}\n" |
|
|
|
|
" ]\n" |
|
|
|
|
"}\n"; |
|
|
|
|
JsonParseOptions options; |
|
|
|
|
TestMessage m; |
|
|
|
|
ASSERT_TRUE(FromJson(input, &m)); |
|
|
|
|
ASSERT_TRUE(FromJson(input, &m, options)); |
|
|
|
|
EXPECT_EQ(1024, m.int32_value()); |
|
|
|
|
ASSERT_EQ(2, m.repeated_int32_value_size()); |
|
|
|
|
EXPECT_EQ(1, m.repeated_int32_value(0)); |
|
|
|
@ -162,20 +165,28 @@ TEST_F(JsonUtilTest, ParseMessage) { |
|
|
|
|
TEST_F(JsonUtilTest, ParseMap) { |
|
|
|
|
TestMap message; |
|
|
|
|
(*message.mutable_string_map())["hello"] = 1234; |
|
|
|
|
JsonOptions options; |
|
|
|
|
EXPECT_EQ("{\"stringMap\":{\"hello\":1234}}", ToJson(message, options)); |
|
|
|
|
JsonPrintOptions print_options; |
|
|
|
|
JsonParseOptions parse_options; |
|
|
|
|
EXPECT_EQ("{\"stringMap\":{\"hello\":1234}}", ToJson(message, print_options)); |
|
|
|
|
TestMap other; |
|
|
|
|
ASSERT_TRUE(FromJson(ToJson(message, options), &other)); |
|
|
|
|
ASSERT_TRUE(FromJson(ToJson(message, print_options), &other, parse_options)); |
|
|
|
|
EXPECT_EQ(message.DebugString(), other.DebugString()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TEST_F(JsonUtilTest, TestParseIgnoreUnknownFields) { |
|
|
|
|
TestMessage m; |
|
|
|
|
JsonParseOptions options; |
|
|
|
|
options.ignore_unknown_fields = true; |
|
|
|
|
EXPECT_TRUE(FromJson("{\"unknownName\":0}", &m, options)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TEST_F(JsonUtilTest, TestParseErrors) { |
|
|
|
|
TestMessage m; |
|
|
|
|
JsonOptions options; |
|
|
|
|
JsonParseOptions options; |
|
|
|
|
// Parsing should fail if the field name can not be recognized.
|
|
|
|
|
EXPECT_FALSE(FromJson("{\"unknownName\":0}", &m)); |
|
|
|
|
EXPECT_FALSE(FromJson("{\"unknownName\":0}", &m, options)); |
|
|
|
|
// Parsing should fail if the value is invalid.
|
|
|
|
|
EXPECT_FALSE(FromJson("{\"int32Value\":2147483648}", &m)); |
|
|
|
|
EXPECT_FALSE(FromJson("{\"int32Value\":2147483648}", &m, options)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
typedef pair<char*, int> Segment; |
|
|
|
|