-
-
Notifications
You must be signed in to change notification settings - Fork 7k
BJData optimized binary array type #4513
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
Conversation
🔴 Amalgamation check failed! 🔴The source code has not been amalgamated. @nebkat |
Please run |
d9b1035
to
7f12cd6
Compare
Would it make sense to introduce a |
Thanks a lot! Feels much better without a breaking change! Regarding the draft version - would it make sense to add an int parameter Regarding deprecation - adding a new function to the API just for the sake of deprecating another feels odd. I think we should rather deprecate the default parameter for the mentioned What do you think? |
I like it, didn't think of that, much better solution! So perhaps something like this? /// how to encode BJData
enum class bjdata_version_t
{
draft2 JSON_HEDLEY_DEPRECATED_FOR(3.12.0, draft3),
draft3,
};
...
static std::vector<std::uint8_t> to_bjdata(const basic_json& j,
const bool use_size = false,
const bool use_type = false,
const bjdata_version_t version = bjdata_version_t::draft2)
{
std::vector<std::uint8_t> result;
to_bjdata(j, result, use_size, use_type, version);
return result;
}
// warning: 'nlohmann::json_abi_v3_11_3::detail::bjdata_version_t::draft2' is deprecated: Since 3.12.0; use draft3 [-Wdeprecated-declarations]
// 4315 | const bjdata_version_t version = bjdata_version_t::draft2) Could technically even put |
You mean using BJData as parameter for UBJSON output/input? |
Yes, internally.
// json/include/nlohmann/detail/output/binary_writer.hpp
void write_ubjson(const BasicJsonType& j, const bool use_count,
const bool use_type, const bool add_prefix = true,
- const bool use_bjdata = false)
+ const bjdata_version_t version = bjdata_version_t::ubjson) // or "draft0", which could be argued is equivalent
{ Then we can do:
/// @brief create a UBJSON serialization of a given JSON value
/// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
static void to_ubjson(const basic_json& j, detail::output_adapter<std::uint8_t> o,
const bool use_size = false, const bool use_type = false)
{
- binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type);
+ binary_writer<std::uint8_t>(o).write_bjdata(j, use_size, use_type, bjdata_version_t::ubjson); // Or defaulted as above
}
/// @brief create a BJData serialization of a given JSON value
/// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
static void to_bjdata(const basic_json& j, detail::output_adapter<std::uint8_t> o,
- const bool use_size = false, const bool use_type = false)
+ const bool use_size = false, const bool use_type = false, const bjdata_version_t version = bjdata_version_t::draft2)
{
- binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type, true, true);
+ binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type, true, version);
} Would cut down on the amount of |
Yes, what I meant: would this be something that could - in the long run - be exposed to the customer? As in: deprecating |
Ah right - yes, but if anything I would deprecate |
Well, I thought of BJData to be a dialect of UBJSON, so I don't think replacing the UBJSON with those of BJData is a good idea. |
The change in endianness between UBJSON/BJData makes it feel like more than just a dialect to me so I'd fear labelling it as such (and having both use I suppose that applies to my proposal of a A third option would be a public facing enum class ubjson_dialect_t
{
ubjson_spec12,
bjdata_draft2,
bjdata_draft3,
}; |
You are right - let's keep the two formats separated in the public API (while they may of course share code in the detail namespace). I think the idea of a version enum from #4513 (comment) makes sense here in order not to break existing code. |
🔴 Amalgamation check failed! 🔴The source code has not been amalgamated. @nebkat |
c9dc364
to
61d1385
Compare
🔴 Amalgamation check failed! 🔴The source code has not been amalgamated. @nebkat |
Please see https://github.com/nlohmann/json/pull/4513/checks?check_run_id=35168472051 on how to sign off your commits. |
Introduces a dedicated `B` marker for bytes. This is used as the strong type marker in optimized array format to encode binary data such that it can also be decoded back to binary data (instead of decoding as an integer array). See NeuroJSON/bjdata#6 for further information. Signed-off-by: Nebojsa Cvetkovic <[email protected]>
That unfortunately leaves Possible alternatives are:
|
ADL should allow you to write
This could be a way for the future - also the
I see. But adding yet another function to the public API is also not too nice.
I see, though I wonder how many people this actually affects.
No. The public API is already so large.
I see your point, but I think we should extend the current What do you think? |
Didn't realize ADL let you do this, that's not so bad.
Agreed, I think that is the best way forward. I've reverted back to the commit where nothing is deprecated, so no current users will be affected, and power users (probably just me for the next while) can specify |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
Thanks!! |
Thanks for the assistance! |
@nebkat Can you create another PR for the #4513 (review)? |
See NeuroJSON/bjdata#6 for further information.
Introduces a dedicated
B
marker for bytes.This is used as the strong type marker in optimized array format to encode binary data such that it can also be decoded back to binary data (instead of wrongly decoding as an integer array).
Draft while awaiting the release of BJData draft 3.
Would a
legacy_binary
flag be desirable to continue supporting draft 2 expectations of uint8 typed arrays for binary?Pull request checklist
Read the Contribution Guidelines for detailed information.
include/nlohmann
directory, runmake amalgamate
to create the single-header filessingle_include/nlohmann/json.hpp
andsingle_include/nlohmann/json_fwd.hpp
. The whole process is described here.