-
Notifications
You must be signed in to change notification settings - Fork 99
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
Representation for durations and time_points #1044
Comments
Direct parsing is implemented using a special event handler that is a tree of dedicated subhandlers for components of the target value, and works by descending to the first subhandlers that accepts a parsing event. The implementation relies on cooperation of parent and child subhandlers. In order to allow users to add support for their types we would have to make this system a public API and document how all that works. I'm not sure it's a very good idea, because writing those subhandlers is very tricky. I have an alternative idea, which I'm still not sure I could pull off, but we'll see. The idea is to have representation "proxies". You define a trait that your type is supposed to be represented as some other type, and converter/parser will create an object of that type and will do conversion/parsing into that object instead. This would provide a significant degree of customisation opportunities, like representing a chrono::duration as a number of ticks, or a chrono::time_point as an ISO 8601 string. |
Support for |
What should be the representation of |
I remembered you mentioned the idea of having "proxies" in your latest blog post but to be honest I did not get it. What would be the proxy for I support the idea of idea of having |
Representing them as a number of ticks has the drawback of all durations being represented in the same way (no way to tell 10 seconds and 10 minutes apart). Representing them as floating point value of seconds opens up the possibility of rounding errors. So, both approaches are not ideal. |
We have the following options (for e.g. a duration of 42 minutes):
(1) has the disadvantage as stated above. Even though it works (for a struct member, for instance) because the period is encoded in the source code as the duration type, it would cause issues if two or more durations are put into a variant. (2) is unnecessarily hard to parse and kind of defeats the point of using a structured format such as JSON. (5) and (6) are the most human-readable, but overly verbose. For a file with many durations in it, they'll cause a lot of unnecessary overhead. (4) is more human-readable than (3), but harder to parse. This leaves me with (3). Interestingly though, we can in principle make several of these work when parsing, because they can be disambiguated (but we do need to choose one when serializing.) So we can in theory add support for one of these, e.g. (3), today, and then later add support for e.g. (4) if we decide we like that better. Edit: there's also the option of |
On second thought, I like |
I would think that, in most applications, both side of the serialization agrees on the period, so the ticks representation would work. Is it not the responsability of the schema to provide the unit/period? |
As Peter mentioned, every ambiguous representation becomes a problem for variants. I.e. you have
I would think, for JSON you usually don't control the format, which is why I prefer representations that don't just technically work, but are actually likely to be used by people. The spec you linked recommends ISO 8601 strings. It's actually not that hard to implement, so maybe we should use that. |
Although, maybe not. The linked ABNF doesn't allow fractions of seconds. |
Indeed ISO 8601 is limited in precision to ms. Maybe it is not the responsibility of the JSON value itself to carry the unit/period. Instead this info could come from a schema. Something like:
as discussed in json-schema-org/json-schema-vocabularies#46. Taking Boost.Units quantities for example, we would probably represent it as numeric (or whatever the underlying value type matches best) and leave the unit to the schema? Side note: it is possible (tested to some extend) to generate a schema from a described struct. |
CBOR requires time points to be serialized according to The timesatmps in RFC3339 aren't allowed to be in local time, interestingly. Also, I reopened the linked spec (JSON Schema) and the contents seem to have been dramatically changed, and now it also recommends RFC3339. Back to CBOR: it also has a dedicated type for epoch offset objects (what So, it seems like there's no real default approach to timestamps. I am inclined to add several context objects for And as I write this, I get yet another idea: represent |
About direct parsing, the documentation states that:
I wonder where this limitation stands?
I have a described struct with an
std::chrono::duration
member. While I have the proper tag_invoke overloads, since there is no built-in conversion support for std::chrono, I can't useparse_into
. Is that correct?Would it be possible to add new customization points to allow direct parsing of custom types?
Any chance to add a conversion category for date /time / duration (if that make sense)?
The text was updated successfully, but these errors were encountered: