Skip to content

Commit

Permalink
improve ubjson
Browse files Browse the repository at this point in the history
  • Loading branch information
xianjimli committed Feb 18, 2025
1 parent fc5e8a5 commit 349b513
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 完善list_view并给edit_ex增加水平滚动条(感谢兆坤提供补丁)
* 完善注释(感谢兆坤提供补丁)
* 增加函数 tk_cond_var_clear
* 完善 ubjson (感谢林福提供补丁)

2025/02/17
* 完善样式(感谢兆坤提供补丁)
Expand Down
2 changes: 1 addition & 1 deletion src/ubjson/ubjson_const.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ typedef enum _ubjson_marker_t {
UBJSON_MARKER_OBJECT_END = '}',

/* container optimized format */
UBJSON_MARKER_CONTAINER_TYPE = '&',
UBJSON_MARKER_CONTAINER_TYPE = '$',
UBJSON_MARKER_CONTAINER_COUNT = '#',
} ubjson_marker_t;

Expand Down
22 changes: 22 additions & 0 deletions tests/ubjson_parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,27 @@ TEST(UBJsonParser, ubjson_writer_write_kv_value) {
tk_object_unref(obj);
}

TEST(UBJsonParser, optimized_array) {
uint8_t buff[256];
uint8_t data[] = {0x7b, 0x55, 0x3, 0x6f, 0x70, 0x72, 0x55, 0x1, 0x55, 0x4, 0x6e, 0x61, 0x6d,
0x65, 0x53, 0x55, 0x3, 0x74, 0x6f, 0x6d, 0x55, 0x5, 0x64, 0x61, 0x74, 0x61,
0x73, 0x5b, 0x24, 0x55, 0x23, 0x55, 0x3, 0x1, 0x2, 0x3, 0x7d};
value_t v;
tk_object_t* obj = NULL;

ubjson_dump(data, sizeof(data));

obj = object_from_ubjson(data, sizeof(data));

ASSERT_EQ(tk_object_get_prop_int(obj, "opr", 0), 1);
ASSERT_STREQ(tk_object_get_prop_str(obj, "name"), "tom");
ASSERT_EQ(tk_object_get_prop_int_by_path(obj, "datas.[0]", -1), 1);
ASSERT_EQ(tk_object_get_prop_int_by_path(obj, "datas.[1]", -1), 2);
ASSERT_EQ(tk_object_get_prop_int_by_path(obj, "datas.[2]", -1), 3);

tk_object_unref(obj);
}

TEST(UBJsonParser, optimized_array_uint8) {
uint8_t buff[256];
uint8_t data[] = {100, 200, 255};
Expand Down Expand Up @@ -302,6 +323,7 @@ TEST(UBJsonParser, optimized_array_uint8) {

ubjson_dump(wb.data, wb.cursor);
}

TEST(UBJsonParser, optimized_array_int32) {
uint8_t buff[256];
int32_t data[] = {100, 200, 300};
Expand Down
22 changes: 22 additions & 0 deletions tests/ubjson_writer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,28 @@ TEST(UBJsonWriter, kv_wstring_len) {
ASSERT_EQ(wb.cursor, 7u);
}

TEST(UBJsonWriter, optimized_array) {
uint8_t buff[256];
uint8_t data[] = {1, 2, 3};
wbuffer_t wb;
ubjson_writer_t ub;
wbuffer_init(&wb, buff, sizeof(buff));
ubjson_writer_init(&ub, (ubjson_write_callback_t)wbuffer_write_binary, &wb);

ASSERT_EQ(ubjson_writer_write_object_begin(&ub), RET_OK);
ASSERT_EQ(ubjson_writer_write_kv_int(&ub, "opr", 1), RET_OK);
ASSERT_EQ(ubjson_writer_write_kv_str(&ub, "name", "tom"), RET_OK);
ASSERT_EQ(ubjson_writer_write_key(&ub, "datas"), RET_OK);
ASSERT_EQ(ubjson_writer_write_array_uint8(&ub, data, ARRAY_SIZE(data)), RET_OK);
ASSERT_EQ(ubjson_writer_write_object_end(&ub), RET_OK);

uint8_t ubjdata[] = {0x7b, 0x69, 0x3, 0x6f, 0x70, 0x72, 0x69, 0x1, 0x69, 0x4, 0x6e, 0x61, 0x6d,
0x65, 0x53, 0x69, 0x3, 0x74, 0x6f, 0x6d, 0x69, 0x5, 0x64, 0x61, 0x74, 0x61,
0x73, 0x5b, 0x24, 0x55, 0x23, 0x69, 0x3, 0x1, 0x2, 0x3, 0x7d};
ASSERT_EQ(wb.cursor, sizeof(ubjdata));
ASSERT_EQ(memcmp(buff, ubjdata, sizeof(ubjdata)), 0);
}

TEST(UBJsonWriter, optimized_array_uint8) {
uint8_t buff[256];
uint8_t data[120];
Expand Down

0 comments on commit 349b513

Please sign in to comment.