pub fn json_error_position<X: TextOrNullableTextOrBinaryOrNullableBinary + MaybeNullableValue<Integer>, x>(
x: x,
) -> json_error_position<X, x>where
x: AsExpression<X>,
Available on crate feature
sqlite
only.Expand description
The json_error_position(X) function returns 0 if the input X is a well-formed JSON or JSON5 string. If the input X contains one or more syntax errors, then this function returns the character position of the first syntax error. The left-most character is position 1.
If the input X is a BLOB, then this routine returns 0 if X is a well-formed JSONB blob. If the return value is positive, then it represents the approximate 1-based position in the BLOB of the first detected error.
This function requires at least SQLite 3.46 or newer
ยงExample
let result = diesel::select(json_error_position::<Text, _>(r#"{"a": "b", "c": 1}"#))
.get_result::<i32>(connection)?;
assert_eq!(0, result);
let result = diesel::select(json_error_position::<Text, _>(r#"{"a": b", "c": 1}"#))
.get_result::<i32>(connection)?;
assert_eq!(7, result);
let json5 = r#"
{
// A traditional message.
message: 'hello world',
// A number for some reason.
n: 42,
}
"#;
let result =
diesel::select(json_error_position::<Text, _>(json5)).get_result::<i32>(connection)?;
assert_eq!(0, result);
let json5_with_error = r#"
{
// A traditional message.
message: hello world',
// A number for some reason.
n: 42,
}
"#;
let result = diesel::select(json_error_position::<Text, _>(json5_with_error))
.get_result::<i32>(connection)?;
assert_eq!(59, result);
let result = diesel::select(json_error_position::<Nullable<Text>, _>(None::<&str>))
.get_result::<Option<i32>>(connection)?;
assert_eq!(None, result);
let result = diesel::select(json_error_position::<Binary, _>(br#"{"a": "b", "c": 1}"#))
.get_result::<i32>(connection)?;
assert_eq!(0, result);
let result = diesel::select(json_error_position::<Binary, _>(br#"{"a": b", "c": 1}"#))
.get_result::<i32>(connection)?;
assert_eq!(7, result);
let result = diesel::select(json_error_position::<Nullable<Binary>, _>(None::<Vec<u8>>))
.get_result::<Option<i32>>(connection)?;
assert_eq!(None, result);