Skip to content

Commit

Permalink
Support nullptr when serialize text.
Browse files Browse the repository at this point in the history
  • Loading branch information
PengZheng committed Jan 20, 2024
1 parent 7f1391d commit 6fffb96
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
40 changes: 40 additions & 0 deletions libs/dfi/gtest/src/json_serializer_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -946,3 +946,43 @@ TEST_F(JsonSerializerTests, SerializationDeserilizationTest) {
dynType_free(type, inst);
dynType_destroy(type);
}

TEST_F(JsonSerializerTests, SerializationDeserilizationNullStringTest) {
dyn_type *type;
void *inst;
int rc;
rc = dynType_parseWithStr("t", nullptr, nullptr, &type);
ASSERT_EQ(0, rc);
json_auto_t* root = json_null();
rc = jsonSerializer_deserializeJson(type, root, &inst);
ASSERT_EQ(0, rc);
EXPECT_EQ(nullptr, *(char**)inst);

json_auto_t* result = nullptr;
rc = jsonSerializer_serializeJson(type, inst, &result);
ASSERT_EQ(0, rc);
EXPECT_TRUE(json_equal(root, result));
dynType_free(type, inst);
dynType_destroy(type);

}

TEST_F(JsonSerializerTests, SerializationDeserilizationStringTest) {
dyn_type *type;
void *inst;
int rc;
rc = dynType_parseWithStr("t", nullptr, nullptr, &type);
ASSERT_EQ(0, rc);
json_auto_t* root = json_loads(R"("hello")", JSON_DECODE_ANY, NULL);
ASSERT_NE(nullptr, root);
rc = jsonSerializer_deserializeJson(type, root, &inst);
ASSERT_EQ(0, rc);
EXPECT_STREQ("hello", *(char**)inst);

json_auto_t* result = nullptr;
rc = jsonSerializer_serializeJson(type, inst, &result);
ASSERT_EQ(0, rc);
EXPECT_TRUE(json_equal(root, result));
dynType_free(type, inst);
dynType_destroy(type);
}
6 changes: 4 additions & 2 deletions libs/dfi/src/json_serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,11 @@ static int jsonSerializer_writeAny(const dyn_type* type, const void* input, json
case 'D' :
val = json_real(*(const double*)input);
break;
case 't' :
val = json_string(*(const char **) input);
case 't' : {
const char *strValue = *(const char **) input;
val = (strValue != NULL) ? json_string(strValue) : json_null();
break;
}
case 'E':
status = jsonSerializer_writeEnum(type, *(const int32_t*)input, &val);
break;
Expand Down

0 comments on commit 6fffb96

Please sign in to comment.