# JSONB with no version number.
send
Parse {"Query": "SELECT $1::JSONB"}
Bind {"ParameterFormatCodes": [1], "Parameters": [{"text":""}]}
Sync
----

until
ErrorResponse
ReadyForQuery
----
{"Type":"ParseComplete"}
{"Type":"ErrorResponse","Code":"08P01"}
{"Type":"ReadyForQuery","TxStatus":"I"}

# JSONB version 2 followed by two double quotes (ASCII 0x22). This is a
# valid JSON string, but the version is invalid and thus should produce
# an error.
send
Bind {"ParameterFormatCodes": [1], "Parameters": [{"binary":"022222"}]}
Sync
----

until mapError=(XX000, 08P01)
ErrorResponse
ReadyForQuery
----
{"Type":"ErrorResponse","Code":"08P01"}
{"Type":"ReadyForQuery","TxStatus":"I"}

# Test binary output encoding for JSONB.
send
Bind {"ParameterFormatCodes": [0], "ResultFormatCodes": [1], "Parameters": [{"text":"{\"key\": \"val\"}"}]}
Execute
Sync
----

until
ReadyForQuery
----
{"Type":"BindComplete"}
{"Type":"DataRow","Values":[{"binary":"017b226b6579223a202276616c227d"}]}
{"Type":"CommandComplete","CommandTag":"SELECT 1"}
{"Type":"ReadyForQuery","TxStatus":"I"}


# Check that we can handle JSON parameters.
send
Parse {"Query": "SELECT $1::JSON", "ParameterOIDs": [114]}
Describe {"ObjectType": "S"}
Bind {"ParameterFormatCodes": [0], "Parameters": [{"text":"\"\""}]}
Execute
Sync
----

until noncrdb_only
ReadyForQuery
----
{"Type":"ParseComplete"}
{"Type":"ParameterDescription","ParameterOIDs":[114]}
{"Type":"RowDescription","Fields":[{"Name":"json","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":114,"DataTypeSize":-1,"TypeModifier":-1,"Format":0}]}
{"Type":"BindComplete"}
{"Type":"DataRow","Values":[{"text":"\"\""}]}
{"Type":"CommandComplete","CommandTag":"SELECT 1"}
{"Type":"ReadyForQuery","TxStatus":"I"}

# CRDB currently only supports decoding JSON values. Encoding will always be in
# JSONB format.
until crdb_only
ReadyForQuery
----
{"Type":"ParseComplete"}
{"Type":"ParameterDescription","ParameterOIDs":[114]}
{"Type":"RowDescription","Fields":[{"Name":"jsonb","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":3802,"DataTypeSize":-1,"TypeModifier":-1,"Format":0}]}
{"Type":"BindComplete"}
{"Type":"DataRow","Values":[{"text":"\"\""}]}
{"Type":"CommandComplete","CommandTag":"SELECT 1"}
{"Type":"ReadyForQuery","TxStatus":"I"}

# JSON with two double quotes (ASCII 0x22) is a valid JSON string.
send
Bind {"ParameterFormatCodes": [1], "Parameters": [{"binary":"2222"}]}
Execute
Sync
----

until
ReadyForQuery
----
{"Type":"BindComplete"}
{"Type":"DataRow","Values":[{"text":"\"\""}]}
{"Type":"CommandComplete","CommandTag":"SELECT 1"}
{"Type":"ReadyForQuery","TxStatus":"I"}

# Test binary output encoding for JSON.
send
Bind {"ParameterFormatCodes": [0], "ResultFormatCodes": [1], "Parameters": [{"text":"{\"key\": \"val\"}"}]}
Execute
Sync
----

until noncrdb_only
ReadyForQuery
----
{"Type":"BindComplete"}
{"Type":"DataRow","Values":[{"text":"{\"key\": \"val\"}"}]}
{"Type":"CommandComplete","CommandTag":"SELECT 1"}
{"Type":"ReadyForQuery","TxStatus":"I"}

# CRDB currently only supports decoding JSON values. Encoding will always be in
# JSONB format.
until crdb_only
ReadyForQuery
----
{"Type":"BindComplete"}
{"Type":"DataRow","Values":[{"binary":"017b226b6579223a202276616c227d"}]}
{"Type":"CommandComplete","CommandTag":"SELECT 1"}
{"Type":"ReadyForQuery","TxStatus":"I"}

# Test formatting for spaces between key and value in a json.
# https://github.com/cockroachdb/cockroach/issues/95434.
send
Parse {"Query": "SELECT $1::JSON"}
Bind {"ParameterFormatCodes": [0], "Parameters": [{"text":"{\"key\":    \"val\"}"}]}
Describe {"ObjectType": "S"}
Execute
Sync
----

# PG's format output follows the input.
until noncrdb_only
ReadyForQuery
----
{"Type":"ParseComplete"}
{"Type":"BindComplete"}
{"Type":"ParameterDescription","ParameterOIDs":[114]}
{"Type":"RowDescription","Fields":[{"Name":"json","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":114,"DataTypeSize":-1,"TypeModifier":-1,"Format":0}]}
{"Type":"DataRow","Values":[{"text":"{\"key\":    \"val\"}"}]}
{"Type":"CommandComplete","CommandTag":"SELECT 1"}
{"Type":"ReadyForQuery","TxStatus":"I"}

# CRDB has a fixed len of space between the key and value when formatting the json.
until crdb_only
ReadyForQuery
----
{"Type":"ParseComplete"}
{"Type":"BindComplete"}
{"Type":"ParameterDescription","ParameterOIDs":[3802]}
{"Type":"RowDescription","Fields":[{"Name":"jsonb","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":3802,"DataTypeSize":-1,"TypeModifier":-1,"Format":0}]}
{"Type":"DataRow","Values":[{"text":"{\"key\": \"val\"}"}]}
{"Type":"CommandComplete","CommandTag":"SELECT 1"}
{"Type":"ReadyForQuery","TxStatus":"I"}

send
Parse {"Query": "SELECT $1::JSON"}
Bind {"ParameterFormatCodes": [0], "Parameters": [{"text":"{\"key\":\"val\"}"}]}
Describe {"ObjectType": "S"}
Execute
Sync
----

until noncrdb_only
ReadyForQuery
----
{"Type":"ParseComplete"}
{"Type":"BindComplete"}
{"Type":"ParameterDescription","ParameterOIDs":[114]}
{"Type":"RowDescription","Fields":[{"Name":"json","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":114,"DataTypeSize":-1,"TypeModifier":-1,"Format":0}]}
{"Type":"DataRow","Values":[{"text":"{\"key\":\"val\"}"}]}
{"Type":"CommandComplete","CommandTag":"SELECT 1"}
{"Type":"ReadyForQuery","TxStatus":"I"}

# CRDB has a fixed len of space between the key and value when formatting the json.
until crdb_only
ReadyForQuery
----
{"Type":"ParseComplete"}
{"Type":"BindComplete"}
{"Type":"ParameterDescription","ParameterOIDs":[3802]}
{"Type":"RowDescription","Fields":[{"Name":"jsonb","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":3802,"DataTypeSize":-1,"TypeModifier":-1,"Format":0}]}
{"Type":"DataRow","Values":[{"text":"{\"key\": \"val\"}"}]}
{"Type":"CommandComplete","CommandTag":"SELECT 1"}
{"Type":"ReadyForQuery","TxStatus":"I"}
