-
Notifications
You must be signed in to change notification settings - Fork 142
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
Double quoted keys for custom struct json write #1477
Comments
It seems like you are not wanting the |
Boy, this is tricky. I got a firm grasp on the problem now. You were write to put out the Normally Glaze tries to identify the type of JSON output based on the C++ type. In this case the C++ type is custom and therefore we can't draw conclusions about what type of JSON output it will generate. This seems to imply that we ought to have a way of tagging the type of the JSON that is being produced by our custom serializer. The tricky thing is that this creates two paths to JSON type handling, the path for the C++ concepts that make the value readable and writable in JSON (such as I think it is best to use the If your custom_type just didn't add the quotes, there wouldn't be a problem, but I can understand how the quotes might be desirable in different contexts. I think supporting I need to give this more thought, but I think a general solution would be broadly useful. |
Sorry about that :) Would it be possible to authorize custom json write for strings only and adapt glaze code in write pair content accordingly? This way, we know that we always need to quote the value in glaze code, and client code in to<JSON override does not need to care about adding more quotes (they will be already added, if it's a key or a value). The code to fix the issue would not be so complicated nor expensive in terms of processing. However, I agree that it would be a step back from the model you provided, that we could write for int / bool / float values. I think that the need to have custom json writing for non string values is quite rare anyway. |
I think an elegant approach might be to define a C++ type with known format association to indicate how this custom type will serialize data. Maybe it should be named something like template <>
struct glz::indicate_type<custom_type>
{
using type = std::string; // indicates that the format will appear like a std::string, but with custom read/write
}; |
I realize this can exist inside of the Hence I'm think of using the syntax: template <>
struct glz::meta<custom_type>
{
using mimic = std::string;
}; I feel like |
I like your solution of a mimic type. But I would rather see a value from an enum class instead, to avoid ambiguity for instance for strings ( |
I can see merits of an enum class. I'm thinking about how this applies to other specifications beyond JSON. But, I think these could be special enumerations for different use cases. I think I agree and I propose: template <>
struct glz::meta<custom_type>
{
static constexpr auto mimic = mimic_type::string;
}; |
I noticed that for structs with custom json write, objects when used as keys are double quoted. For instance, in json_test,
custom_struct
fails with this test:In json/write.hpp, when we write a pair of key value for custom write which calls string type of write, the condition for this if is
false
soquoted_t
is added in a string which will itself be quoted as well, resulting in double quoted string:In my application, I try to guess at the context by scanning the buffer for previous
"
,:
or[
for instance to guess if need to add quotes or not, but it's not very practical.I tried to figure out how in client code we could identify that we are writing for a key or a value. In Json, keys should always be quoted, but it's not true for values such as bool / int for instance. Maybe one solution could be to give additional hint in the context that we are writing a key or value in order for it to know if it needs to add the quotes or not.
WDYT ?
The text was updated successfully, but these errors were encountered: