diesel::sqlite::expression::dsl

Function json_pretty

Source
pub fn json_pretty<J: JsonOrNullableJsonOrJsonbOrNullableJsonb + MaybeNullableValue<Text>, j>(
    j: j,
) -> json_pretty<J, j>
where j: AsExpression<J>,
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::<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::<Json, _>(json!({"a": 1, "b": "cd"})))
    .get_result::<String>(connection)?;

assert_eq!(r#"{
    "a": 1,
    "b": "cd"
}"#, result);

let result = diesel::select(json_pretty::<Json, _>(json!("abc")))
    .get_result::<String>(connection)?;

assert_eq!(r#""abc""#, result);

let result = diesel::select(json_pretty::<Json, _>(json!(22)))
    .get_result::<String>(connection)?;

assert_eq!(r#"22"#, result);

let result = diesel::select(json_pretty::<Json, _>(json!(false)))
    .get_result::<String>(connection)?;

assert_eq!(r#"false"#, result);

let result = diesel::select(json_pretty::<Json, _>(json!(null)))
    .get_result::<String>(connection)?;

assert_eq!(r#"null"#, result);

let result = diesel::select(json_pretty::<Json, _>(json!({})))
    .get_result::<String>(connection)?;

assert_eq!(r#"{}"#, result);

let result = diesel::select(json_pretty::<Nullable<Json>, _>(None::<Value>))
    .get_result::<Option<String>>(connection)?;

assert!(result.is_none());

let result = diesel::select(json_pretty::<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::<Jsonb, _>(json!({"a": 1, "b": "cd"})))
    .get_result::<String>(connection)?;

assert_eq!(r#"{
    "a": 1,
    "b": "cd"
}"#, result);

let result = diesel::select(json_pretty::<Jsonb, _>(json!("abc")))
    .get_result::<String>(connection)?;

assert_eq!(r#""abc""#, result);

let result = diesel::select(json_pretty::<Jsonb, _>(json!(22)))
    .get_result::<String>(connection)?;

assert_eq!(r#"22"#, result);

let result = diesel::select(json_pretty::<Jsonb, _>(json!(false)))
    .get_result::<String>(connection)?;

assert_eq!(r#"false"#, result);

let result = diesel::select(json_pretty::<Jsonb, _>(json!(null)))
    .get_result::<String>(connection)?;

assert_eq!(r#"null"#, result);

let result = diesel::select(json_pretty::<Jsonb, _>(json!({})))
    .get_result::<String>(connection)?;

assert_eq!(r#"{}"#, result);

let result = diesel::select(json_pretty::<Nullable<Jsonb>, _>(None::<Value>))
    .get_result::<Option<String>>(connection)?;

assert!(result.is_none());