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

Json columns are read as JSON::PullParser instead of JSON::Any #232

Merged
merged 2 commits into from Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions spec/pg/decoder_spec.cr
Expand Up @@ -38,11 +38,11 @@ describe PG::Decoders do
UUID.new("7d61d548-124c-4b38-bc05-cfbb88cfd1d1")

if Helper.db_version_gte(9, 2)
test_decode "json", %('[1,"a",true]'::json), JSON.parse(%([1,"a",true]))
test_decode "json", %('{"a":1}'::json), JSON.parse(%({"a":1}))
test_decode "json", %('[1,"a",true]'::json), JSON::PullParser.new(%([1,"a",true]))
test_decode "json", %('{"a":1}'::json), JSON::PullParser.new(%({"a":1}))
end
if Helper.db_version_gte(9, 4)
test_decode "jsonb", "'[1,2,3]'::jsonb", JSON.parse("[1,2,3]")
test_decode "jsonb", "'[1,2,3]'::jsonb", JSON::PullParser.new("[1,2,3]")
end

test_decode "timestamptz", "'2015-02-03 16:15:13-01'::timestamptz",
Expand Down
12 changes: 12 additions & 0 deletions spec/pg/driver_spec.cr
Expand Up @@ -112,6 +112,18 @@ describe PG::Driver do
end
end

it "converts json result into JSON::Any if requested" do
with_db do |db|
db.exec "drop table if exists users"
db.exec "create table users (properties jsonb)"
db.exec "insert into users values ($1)", "{\"hello\": \"world\"}"

db.query "select properties from users limit 1" do |rs|
assert_single_read rs, JSON::Any, JSON.parse("{\"hello\": \"world\"}")
end
end
end

describe "transactions" do
it "can read inside transaction and rollback after" do
with_db do |db|
Expand Down
8 changes: 8 additions & 0 deletions spec/spec_helper.cr
Expand Up @@ -37,3 +37,11 @@ def test_decode(name, query, expected, file = __FILE__, line = __LINE__)
value.should eq(expected), file: file, line: line
end
end

def test_decode(name, query, expected : JSON::PullParser, file = __FILE__, line = __LINE__)
it name, file, line do
value = PG_DB.query_one "select #{query}", &.read
json_value = value.is_a?(JSON::PullParser) ? JSON::Any.new(value) : value
json_value.should eq(JSON::Any.new(expected)), file: file, line: line
end
end
4 changes: 2 additions & 2 deletions src/pg/decoder.cr
Expand Up @@ -382,11 +382,11 @@ module PG
io.read_fully(Slice.new(buffer, bytesize))
{bytesize, 0}
end
JSON.parse(string)
JSON::PullParser.new(string)
end

def type
JSON::Any
JSON::PullParser
end
end

Expand Down
10 changes: 10 additions & 0 deletions src/pg/result_set.cr
Expand Up @@ -124,6 +124,16 @@ class PG::ResultSet < ::DB::ResultSet
end
end

def read(t : JSON::Any.class) : JSON::Any
value = read(JSON::PullParser)
JSON::Any.new(value)
end

def read(t : JSON::Any?.class) : JSON::Any?
value = read(JSON::PullParser?)
JSON::Any.new(value) if value
end

private def read_array(t : T.class) : T forall T
col_bytesize = conn.read_i32
if col_bytesize == -1
Expand Down