Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read_to & rfl::SkipIfMissing processor #338

Open
golxzn opened this issue Jan 22, 2025 · 1 comment
Open

read_to & rfl::SkipIfMissing processor #338

golxzn opened this issue Jan 22, 2025 · 1 comment

Comments

@golxzn
Copy link

golxzn commented Jan 22, 2025

Overview

Sometimes it's necessary to update the existing structure using JSON, which doesn't contain all the fields, but modified:

struct player_info {
	std::string id{};
	std::string name{};
	uint32_t steam_id{};
// and more fields...
};

// somewhere
request(info_update_request).then([] (auto patch) {
	// patch: {"name":"i've changed nickname!"}
	auto &info{ services<player_info>() };
	rfl::json::read_to<player_info, rlf::SkipIfMissing>(info, patch);
});

I hope the idea is clear.

Example

We can try to make our own read and write methods like that:

auto original{ write_doc(patching_struct) }; // aka rfl::json::write, but stealing yyjson_doc
auto patch{ yyjson_read(std::data(json), std::size(json), YYJSON_READ_NOFLAG) };
auto patched{ yyjson_merge_patch(original,
	yyjson_doc_get_root(original),
	yyjson_doc_get_root(patch)
) };
// yyjson_mut_doc_get_root(patched)
const auto root{ yyjson_doc_get_root(patched) };
const auto value{ rfl::json::read<T, rfl::DefaultIfMissing>(
	rfl::json::Reader::InputVarType{ root }
) };

But there are 2 problems:

  1. The parser could use yyjson_val, but yyjson_merge_patch returns yyjson_mut_val;
  2. It's inefficient to parse the structure to the yyjson_doc object just for filling missing fields.

The problem could be solved by ourselves by implementing the whole writer and reader, but the author could do it much more elegant. The first issue could be solved by yyjson_mut_doc_imut_copy as well, but again, such simple stuff like changing field inside an existing object shouldn't be such complicated and slow.

@liuzicheng1987
Copy link
Contributor

@golxzn , let me think about this...I think this is a very interesting problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants