-
Notifications
You must be signed in to change notification settings - Fork 78
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
Premature parsing of json #125
Comments
I think we could do this automatically by asking whether |
Is it possible to ask whether The root issue IMO is that |
Something else to consider regarding This is the problem I mean in
This situation is similar to the one that appears when dealing with uploaded files in an https server. How regular params and the files can be parsed. |
For those interested, an acceptable workaround for our application has been casting require "pg"
record Foo, str : String, ints : Array(Int32) do
include JSON::Serializable
end
db = PG.connect("postgresql:///")
res = db.query_all(<<-SQL, as: String).map { |json| Foo.from_json(json) }
SELECT meta::text
FROM (VALUES
('{"str": "a", "ints": [1, 2, 3]}'::jsonb),
('{"str": "b", "ints": [4, 5, 6]}'::jsonb)
) t(meta)
SQL
p(res) # => [Foo(@ints=[1, 2, 3], @str="a"), Foo(@ints=[4, 5, 6], @str="b")] You can make nice generic wrappers around this type of query for what is likely an acceptable cost. Better still performance wise, you can pull JSON values out into the result set, from which you can use record Bar, str : String, int : Int32 do
include DB::Serializable
end
res = db.query_all(<<-SQL, as: Bar)
SELECT meta->>'str' as str, (meta->>'int')::integer as int
FROM (VALUES
('{"str": "a", "int": 1337}'::jsonb),
('{"str": "b", "int": 1338}'::jsonb)
) t(meta)
SQL
p(res) # => [Bar(@int=1337, @str="a"), Bar(@int=1338, @str="b")] Whether this is more or less practical, YMMV depending on use case |
To parse json into my own type using
JSON.mapping
, I have to do:This marshal/unmarshal thrashing could be avoided if
pg
just returnedJSON::PullParser
or some other type that doesn't scrap the ability to marshal viaJSON.mapping
The text was updated successfully, but these errors were encountered: