Jsonb *in = PG_GETARG_JSONB_P(0);
JsonbValue v;
- if (!JsonbExtractScalar(&in->root, &v) || v.type != jbvBool)
+ if (!JsonbExtractScalar(&in->root, &v))
+ cannotCastJsonbValue(v.type, "boolean");
+
+ if (v.type == jbvNull)
+ {
+ PG_FREE_IF_COPY(in, 0);
+ PG_RETURN_NULL();
+ }
+
+ if (v.type != jbvBool)
cannotCastJsonbValue(v.type, "boolean");
PG_FREE_IF_COPY(in, 0);
JsonbValue v;
Numeric retValue;
- if (!JsonbExtractScalar(&in->root, &v) || v.type != jbvNumeric)
+ if (!JsonbExtractScalar(&in->root, &v))
+ cannotCastJsonbValue(v.type, "numeric");
+
+ if (v.type == jbvNull)
+ {
+ PG_FREE_IF_COPY(in, 0);
+ PG_RETURN_NULL();
+ }
+
+ if (v.type != jbvNumeric)
cannotCastJsonbValue(v.type, "numeric");
/*
JsonbValue v;
Datum retValue;
- if (!JsonbExtractScalar(&in->root, &v) || v.type != jbvNumeric)
+ if (!JsonbExtractScalar(&in->root, &v))
+ cannotCastJsonbValue(v.type, "smallint");
+
+ if (v.type == jbvNull)
+ {
+ PG_FREE_IF_COPY(in, 0);
+ PG_RETURN_NULL();
+ }
+
+ if (v.type != jbvNumeric)
cannotCastJsonbValue(v.type, "smallint");
retValue = DirectFunctionCall1(numeric_int2,
JsonbValue v;
Datum retValue;
- if (!JsonbExtractScalar(&in->root, &v) || v.type != jbvNumeric)
+ if (!JsonbExtractScalar(&in->root, &v))
+ cannotCastJsonbValue(v.type, "integer");
+
+ if (v.type == jbvNull)
+ {
+ PG_FREE_IF_COPY(in, 0);
+ PG_RETURN_NULL();
+ }
+
+ if (v.type != jbvNumeric)
cannotCastJsonbValue(v.type, "integer");
retValue = DirectFunctionCall1(numeric_int4,
JsonbValue v;
Datum retValue;
- if (!JsonbExtractScalar(&in->root, &v) || v.type != jbvNumeric)
+ if (!JsonbExtractScalar(&in->root, &v))
+ cannotCastJsonbValue(v.type, "bigint");
+
+ if (v.type == jbvNull)
+ {
+ PG_FREE_IF_COPY(in, 0);
+ PG_RETURN_NULL();
+ }
+
+ if (v.type != jbvNumeric)
cannotCastJsonbValue(v.type, "bigint");
retValue = DirectFunctionCall1(numeric_int8,
JsonbValue v;
Datum retValue;
- if (!JsonbExtractScalar(&in->root, &v) || v.type != jbvNumeric)
+ if (!JsonbExtractScalar(&in->root, &v))
+ cannotCastJsonbValue(v.type, "real");
+
+ if (v.type == jbvNull)
+ {
+ PG_FREE_IF_COPY(in, 0);
+ PG_RETURN_NULL();
+ }
+
+ if (v.type != jbvNumeric)
cannotCastJsonbValue(v.type, "real");
retValue = DirectFunctionCall1(numeric_float4,
JsonbValue v;
Datum retValue;
- if (!JsonbExtractScalar(&in->root, &v) || v.type != jbvNumeric)
+ if (!JsonbExtractScalar(&in->root, &v))
+ cannotCastJsonbValue(v.type, "double precision");
+
+ if (v.type == jbvNull)
+ {
+ PG_FREE_IF_COPY(in, 0);
+ PG_RETURN_NULL();
+ }
+
+ if (v.type != jbvNumeric)
cannotCastJsonbValue(v.type, "double precision");
retValue = DirectFunctionCall1(numeric_float8,
t
(1 row)
+select 'null'::jsonb::bool;
+ bool
+------
+
+(1 row)
+
select '[]'::jsonb::bool;
ERROR: cannot cast jsonb array to type boolean
select '1.0'::jsonb::float;
1
(1 row)
+select 'null'::jsonb::float;
+ float8
+--------
+
+(1 row)
+
select '[1.0]'::jsonb::float;
ERROR: cannot cast jsonb array to type double precision
+select '1.0'::jsonb::float4;
+ float4
+--------
+ 1
+(1 row)
+
+select 'null'::jsonb::float4;
+ float4
+--------
+
+(1 row)
+
+select '[1.0]'::jsonb::float4;
+ERROR: cannot cast jsonb array to type real
+select '12345'::jsonb::int2;
+ int2
+-------
+ 12345
+(1 row)
+
+select 'null'::jsonb::int2;
+ int2
+------
+
+(1 row)
+
+select '"hello"'::jsonb::int2;
+ERROR: cannot cast jsonb string to type smallint
select '12345'::jsonb::int4;
int4
-------
12345
(1 row)
+select 'null'::jsonb::int4;
+ int4
+------
+
+(1 row)
+
select '"hello"'::jsonb::int4;
ERROR: cannot cast jsonb string to type integer
+select '12345'::jsonb::int8;
+ int8
+-------
+ 12345
+(1 row)
+
+select 'null'::jsonb::int8;
+ int8
+------
+
+(1 row)
+
+select '"hello"'::jsonb::int8;
+ERROR: cannot cast jsonb string to type bigint
select '12345'::jsonb::numeric;
numeric
---------
12345
(1 row)
+select 'null'::jsonb::numeric;
+ numeric
+---------
+
+(1 row)
+
select '{}'::jsonb::numeric;
ERROR: cannot cast jsonb object to type numeric
select '12345.05'::jsonb::numeric;
-- casts
select 'true'::jsonb::bool;
+select 'null'::jsonb::bool;
select '[]'::jsonb::bool;
select '1.0'::jsonb::float;
+select 'null'::jsonb::float;
select '[1.0]'::jsonb::float;
+select '1.0'::jsonb::float4;
+select 'null'::jsonb::float4;
+select '[1.0]'::jsonb::float4;
+select '12345'::jsonb::int2;
+select 'null'::jsonb::int2;
+select '"hello"'::jsonb::int2;
select '12345'::jsonb::int4;
+select 'null'::jsonb::int4;
select '"hello"'::jsonb::int4;
+select '12345'::jsonb::int8;
+select 'null'::jsonb::int8;
+select '"hello"'::jsonb::int8;
select '12345'::jsonb::numeric;
+select 'null'::jsonb::numeric;
select '{}'::jsonb::numeric;
select '12345.05'::jsonb::numeric;
select '12345.05'::jsonb::float4;