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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Int32 and Float32 to JSON::Type #3908

Conversation

aurimasniekis
Copy link

Added Int32 and Float32 types to JSON::Type.

I am working on implementing JSON type field for crystal-mysql and faced a problem that type checking fails then it gets Int32 or Float32 values even if normal to_json works perfectly fine.

@bcardiff
Copy link
Member

JSON::Any is more used for parsing JSON in a generic way. There won't be much use of Int32/Float32 in those cases: parsing {"a":1} is a Int32 or Int64? The use will need to deal with the union all the time an Int is expected.

crystal-mysql should use Int64/Float64 when building a JSON in a straight way.

On a given schema, a JSON.mapping could be supported though and there a lot of flexibility can be achieved.

@bcardiff
Copy link
Member

Note: I am eager to see that JSON addition in mysql :-), so big 👍 there.

@aurimasniekis
Copy link
Author

I don't know if I implement it correctly as I am just newbie with Crystal but it works for me. crystal-lang/crystal-mysql#24

@asterite
Copy link
Member

I actually have my doubts about automatically parsing a JSON field from a database. crystal-pg does that too, but I think we should just return a String. Since you are probably in control of the database you know what you put in that column typed as JSON, and the best thing to do is to use JSON.mapping with that. If you get the result already parsed there's no way to use JSON.mapping. So I would drop that from all drivers and return String instead.

@aurimasniekis
Copy link
Author

Thats also a good point. I started by extending only string with native json type it was working but I went bit further and added json parsing and generation as it is json type...

@bcardiff
Copy link
Member

Since the JSON can be a type interpreted by the database (as parameters also, not just query results). I wouldn't mind for the drivers to support them. But I would also expect that if the schema is fixed a JSON.mapping or other strict parsing technique will be used.

Probably there is no performance difference between a native json representation of the driver and just the json-to-io done by crystal, so is more an api discussion than a discussion about what happens under the hood.

But sometimes json is used to store not fixed schema data. JSON.mapping might be a bit annoying here. So I am not totally sure JSON should be discarded as a first class citizen in the db family.

@bcardiff
Copy link
Member

I'll close this. We can continue the discussion of json & db at crystal-lang/crystal-mysql#24 or maybe even at crystal-db since most of the drivers supports json nowadays.

@bcardiff bcardiff closed this Jan 17, 2017
@aurimasniekis aurimasniekis deleted the feature/lower-size-types-for-json-type branch January 17, 2017 18:52
@aurimasniekis
Copy link
Author

Yeah, ok 👍

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

Successfully merging this pull request may close these issues.

None yet

3 participants