-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Request: binary serialization/deserialization #358
Comments
I also had a look at MessagePack, and it seems to be a reasonable approach. As it can express more than JSON, I hope that users would understand that the library would not be able to parse arbitrary MessagePack input. |
I started to implement MessagePack. Commit 543745a contains a basic serializer/deserializer for JSON values as static functions. It works with a Feedback would be greatly appreciated:
|
Wow, I couldn’t believe how fast you did implement a first version. And it seems to be working! I did test the space savings in some of my use cases and got the following results.
Thank you for your great JSON implementation! |
I beg to differ. MsgPack is not standardized, whereas CBOR, for example, is. There are a myriad of binary data formats out there, but, for better or worse, CBOR is standardized in RFC 7049, whereas MsgPack is not. |
+1 for CBOR! |
I shall have a look at CBOR. |
Ok, standards are great, I like them. CBOR is a PROPOSED standard and could still get changed. The proposal has some nice properties and it seems to be an improved MessagePack protocol. Unfortunately it’s not widely adopted and there are a lot of poor implementations. There’s no modern C++ 11 implementation and the old style C++ implementation is looking more like C code. |
Oh, you wanted to call a library, I thought you wanted to write everything yourselves. But anyway, even it is "only" a proposed standard, this is still better than MSGPACK. Should it change, we will know about it, but we might not find out if MSGPACK changes. |
I started to also implement CBOR. As the implementation tends to be rather verbose, I am thinking of moving the binary serialization/deserialization into a new header. In any case, I need to add more test cases. |
Hi @nlohmann, I've had a look at your code. The implementation could be less verbose by
|
@marton78 Good points. There may be even more possibilities to generalize between the MessagePack and CBOR sections given the two formats are so similar. |
I really want to move the code for MessagePack and CBOR out of the main header (it's over 1000 lines of code...) and I need some ideas how to best do this. My thoughts:
The syntax would then be: // assuming vec is a std::vector<uint8_t>
json j = nlohmann::from_msgpack(vec);
auto vec2 = nlohmann::to_msgpack(j); I would be happy about opinions, pointers to best practices, etc. Furthermore, any feedback on the interfaces, in particular using |
You could keep it in the class but in another file through a If you did need access to private functions/data, you could forward declare a Probably the easiest is to just put the declarations of the long statics in the class definition, and then put the full definitions in another header. |
Just write a separate library. You are outgrowing the bounds of this one. Slap in some RPC while writing the new one :) |
If I would include the other header (say, |
Yes, the second header should only be needed for callers of those functions. |
I think the best thing to do would be to make a new repository entirely. |
Or keep the functionality in the same file, ifdefed-out by default. Whoever needs it can enable it with #define NLOHMANN_JSON_ENABLE_CBOR In the source, or via a compiler switch. |
Any idea how to move the code into a second file without also needing a preprocessor macro? |
Put declarations in the main file, and definitions in another. Such files are typically named *.inl |
I.e what @gregmarr said |
Or another idea (just thinking): What about splitting the code into several files for development and have them merge by a script into a single (large) header? |
(Maybe my judgement of a large header is wrong, but I fear that letting it grow (a) increases compilation times, (b) makes understanding the code harder, and (c) pushes a lot of functionality into code that may only need a fraction.) |
A big benefit of having one file with declarations and one-or-many files with definitions is that it would allow for explicit template instantiation, so that the compilation overhead related to json.hpp can be factored out into a separate translation unit. |
I am currently cleaning up and bringing the test coverage to 100%. I hope to release CBOR/MessagePack by next week. |
Merged e906933. |
Hi @nlohmann, great job! I have looked at your code, some remarks:
All in all very nice job, thanks! |
@marton78 Thanks for the feedback.
|
@marton78 Sorry, but I can't find the spot where I can use |
Maybe it's this part? case value_t::number_float:
{
// Double-Precision Float
v.push_back(0xfb);
const uint8_t* helper = reinterpret_cast<const uint8_t*>(&(j.m_value.number_float));
for (size_t i = 0; i < 8; ++i)
{
v.push_back(helper[7 - i]);
}
break;
} Also, if this part of code expects the |
Yes, it's this part, |
This looks like the endianness handling business? |
Lol definetly not, look at my swap file to get some ideas: https://github.com/user1095108/generic/blob/master/swap.hpp |
FYI: I asked to advertize the latest release on the CBOR (cbor/cbor.github.io#25) and MessagePack (msgpack/msgpack#221) website. |
Wouldn’t it be useful to have the possibility to serialize the json object to a more compact form?
This would make sense for storing the container or sending it over an internet connection.
The MessagePack (http://msgpack.org/) format or some proprietary format directly derived from your internal representation would be fine.
Or could you probably just provide some generic interface for the binary serialization/deserialization?
I think this approach could be more useable than just gzipping the whole JSON data.
As an example this library did implement some alternative representations of a json container: http://greatpanic.com/progdocs/UniversalContainer.html
(But this library doesn’t work properly and doesn’t use C++ 11)
The text was updated successfully, but these errors were encountered: