pub fn json_pretty_with_indentation<J: JsonOrNullableJsonOrJsonbOrNullableJsonb + MaybeNullableValue<Text>, j, indentation>(
j: j,
indentation: indentation,
) -> json_pretty_with_indentation<J, j, indentation>
Available on crate feature
sqlite
only.Expand description
Converts the given json value to pretty-printed, indented text
ยงExample
let version = diesel::select(sql::<Text>("sqlite_version();"))
.get_result::<String>(connection)?;
// Querying SQLite version should not fail.
let version_components: Vec<&str> = version.split('.').collect();
let major: u32 = version_components[0].parse().unwrap();
let minor: u32 = version_components[1].parse().unwrap();
let patch: u32 = version_components[2].parse().unwrap();
if major > 3 || (major == 3 && minor >= 46) {
/* Valid sqlite version, do nothing */
} else {
println!("SQLite version is too old, skipping the test.");
return Ok(());
}
let result = diesel::select(json_pretty_with_indentation::<Json, _, _>(json!([{"f1":1,"f2":null},2,null,3]), " "))
.get_result::<String>(connection)?;
assert_eq!(r#"[
{
"f1": 1,
"f2": null
},
2,
null,
3
]"#, result);
let result = diesel::select(json_pretty_with_indentation::<Json, _, _>(json!([{"f1":1,"f2":null},2,null,3]), None::<&str>))
.get_result::<String>(connection)?;
assert_eq!(r#"[
{
"f1": 1,
"f2": null
},
2,
null,
3
]"#, result);
let result = diesel::select(json_pretty_with_indentation::<Json, _, _>(json!({"a": 1, "b": "cd"}), " "))
.get_result::<String>(connection)?;
assert_eq!(r#"{
"a": 1,
"b": "cd"
}"#, result);
let result = diesel::select(json_pretty_with_indentation::<Json, _, _>(json!("abc"), " "))
.get_result::<String>(connection)?;
assert_eq!(r#""abc""#, result);
let result = diesel::select(json_pretty_with_indentation::<Json, _, _>(json!(22), " "))
.get_result::<String>(connection)?;
assert_eq!(r#"22"#, result);
let result = diesel::select(json_pretty_with_indentation::<Json, _, _>(json!(false), None::<&str>))
.get_result::<String>(connection)?;
assert_eq!(r#"false"#, result);
let result = diesel::select(json_pretty_with_indentation::<Json, _, _>(json!(null), None::<&str>))
.get_result::<String>(connection)?;
assert_eq!(r#"null"#, result);
let result = diesel::select(json_pretty_with_indentation::<Json, _, _>(json!({}), " "))
.get_result::<String>(connection)?;
assert_eq!(r#"{}"#, result);
let result = diesel::select(json_pretty_with_indentation::<Nullable<Json>, _, _>(None::<Value>, None::<&str>))
.get_result::<Option<String>>(connection)?;
assert!(result.is_none());
let result = diesel::select(json_pretty_with_indentation::<Jsonb, _, _>(json!([{"f1":1,"f2":null},2,null,3]), " "))
.get_result::<String>(connection)?;
assert_eq!(r#"[
{
"f1": 1,
"f2": null
},
2,
null,
3
]"#, result);
let result = diesel::select(json_pretty_with_indentation::<Jsonb, _, _>(json!([{"f1":1,"f2":null},2,null,3]), None::<&str>))
.get_result::<String>(connection)?;
assert_eq!(r#"[
{
"f1": 1,
"f2": null
},
2,
null,
3
]"#, result);
let result = diesel::select(json_pretty_with_indentation::<Jsonb, _, _>(json!({"a": 1, "b": "cd"}), " "))
.get_result::<String>(connection)?;
assert_eq!(r#"{
"a": 1,
"b": "cd"
}"#, result);
let result = diesel::select(json_pretty_with_indentation::<Jsonb, _, _>(json!("abc"), " "))
.get_result::<String>(connection)?;
assert_eq!(r#""abc""#, result);
let result = diesel::select(json_pretty_with_indentation::<Jsonb, _, _>(json!(22), " "))
.get_result::<String>(connection)?;
assert_eq!(r#"22"#, result);
let result = diesel::select(json_pretty_with_indentation::<Jsonb, _, _>(json!(false), None::<&str>))
.get_result::<String>(connection)?;
assert_eq!(r#"false"#, result);
let result = diesel::select(json_pretty_with_indentation::<Jsonb, _, _>(json!(null), None::<&str>))
.get_result::<String>(connection)?;
assert_eq!(r#"null"#, result);
let result = diesel::select(json_pretty_with_indentation::<Jsonb, _, _>(json!({}), " "))
.get_result::<String>(connection)?;
assert_eq!(r#"{}"#, result);
let result = diesel::select(json_pretty_with_indentation::<Nullable<Jsonb>, _, _>(None::<Value>, None::<&str>))
.get_result::<Option<String>>(connection)?;
assert!(result.is_none());