-
DescriptionI encountered a problem, where I'm not sure whether I'm doing something wrong. I want to create a type for a hash value. For this I use the std::array:
(Note: size is actually longer, but for simplicity I chose 6) Serialization and deserialization works fine. Hash gets converted to an array of 6 values in the json representation. However, I would prefer to have a compact representation as a string, showing hex values. So I started custom implementations for de-/serialization (inside the namespace
When I want to de-/serialize my custom implementations are only used if I add an explicit call to the functions including the namespace.
ExampleI've created an example to visualize what I'm doing:
When I run this example I get this output:
So either assignment as well as direct call to to_/from_json use the internal "std::array" version. Only by calling explicitly QuestionIs there anything I'm missing out, or do I need to call the conversion explicitly in my case? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Your problem is that |
Beta Was this translation helpful? Give feedback.
Your problem is that
tools::Hash
isstd::array<uint8_t, 6>
. It is an alias. So the functions you wrote intools
namespace do not matter. This type actually is in namespacestd
.So to achieve what you want, you could either fully specialize
adl_serializer
for that type (see documentation), or use a distinct type that actually is intools
namespace. A simple and lazy trick would be inheritance:struct Hash : std::array<uint8_t, 6> {};
.