Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object of type 'AtomicInterval' is not JSON serializable #6

Closed
gfitas opened this issue Dec 4, 2018 · 8 comments
Closed

Object of type 'AtomicInterval' is not JSON serializable #6

gfitas opened this issue Dec 4, 2018 · 8 comments
Assignees
Labels
enhancement New feature or request

Comments

@gfitas
Copy link

gfitas commented Dec 4, 2018

Hi

I was wondering if it will be possible to make Interval and AtomicInterval JSON serializable ? 馃檹 馃槃

Thank you in advance

@AlexandreDecan AlexandreDecan added the enhancement New feature or request label Dec 4, 2018
@AlexandreDecan
Copy link
Owner

Hi!

I will have a look at what is required to support json serialization. That shouldn't be difficult to implement, but maybe there's a more generic way to support different (other) kinds of serialization.

I'll keep you in touch.

@AlexandreDecan AlexandreDecan self-assigned this Dec 4, 2018
@AlexandreDecan
Copy link
Owner

It seems there is no "hook" that can be written on Interval or AtomicInterval to seamlessly support (de)serialization. However, I already see two workarounds:

  • The first one is to implement new methods/functions that convert Interval instances from/to a Python built-in data type (e.g. tuples). The question then is: how to convert the bounds of an atomic interval, as they can be any custom objects;
  • The second one, which is already available, is to rely on the from_string and to_string functions so that you can convert Interval instances from/to strings, and store them as strings in JSON. In that case, custom objects are explicitly supported through the conv attribute. However, this approach could be less straightforward for custom types (but very convenient, e.g., for integers).

Could you give a try to the latter solution?

@gfitas
Copy link
Author

gfitas commented Dec 6, 2018

In my case, I use your library in an python Api that communicate in json. So if I use your last solution, I would have to parse the string intervals from JavaScript. Maybe you could generate array of custom object like this :
[{lowerV: 2, lowerT:'close',upperV: 4, upperT:'open'},
{lowerV: 5, lowerT:'open',upperV: 6, upperT:'close'},...]

Easy to read, easy to parse, easy to apply from any languages

@AlexandreDecan
Copy link
Owner

The problem is to encode/decode arbitrary objects that could be used as bounds. Integers are supported out of the box by json, but not, e.g., Timestamp.

Notice that your example is already "supported" with a bit of Python:

>>> i = I.closed(3, 4) | I.closed(7, 9)
>>> [(x.left, x.lower, x.upper, x.right) for x in i]
[(True, 3, 4, True), (True, 7, 9, True)]

>>> v = [(True, 3, 4, True), (True, 7, 9, True)]
>>> I.Interval(*[I.AtomicInterval(*x) for x in v])
[3,4] | [7,9]

I can add functions that do this job. They should also accept a parameter to specify how the bounds (arbitrary objects) can be converted from/to built-in data types. For example:

>>> i = I.closed(Timestamp(3), Timestamp(4)) | I.closed(Timestamp(7), Timestamp(9))
>>> I.to_data(i, conv=lambda t: t.as_int())
[(True, 3, 4, True), (True, 7, 9, True)]

>>> v = [(True, 3, 4, True), (True, 7, 9, True)]
>>> I.from_data(v, conv=Timestamp)
>>> [Timestamp(3),Timestamp(4)] | [Timestamp(7),Timestamp(9)]

@gfitas
Copy link
Author

gfitas commented Dec 6, 2018

Yes that will be super great

@AlexandreDecan
Copy link
Owner

I opened a PR (#7). I still need to find why doctest is failing in Python 2.7.
I will probably bundle another new feature with release 1.7.0, I hope to find some time to work on it today. If so, you can expect 1.7.0 to be released by the end of the day.

@AlexandreDecan
Copy link
Owner

Closed by #7

@AlexandreDecan
Copy link
Owner

1.7.0 will be released in a few minutes, with from_data and to_data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants