This commit is contained in:
Nicolas Luck
2024-01-29 18:44:20 +01:00
parent 9aacfff35d
commit 7bb9c00356
2 changed files with 28 additions and 49 deletions

View File

@@ -19,19 +19,22 @@ pub fn prolog_value_to_json_tring(value: Value) -> String {
Value::Float(f) => format!("{}", f),
Value::Rational(r) => format!("{}", r),
Value::Atom(a) => format!("{}", a.as_str()),
Value::String(s) =>
Value::String(s) => {
if let Err(_e) = serde_json::from_str::<serde_json::Value>(s.as_str()) {
//treat as string literal
//escape double quotes
format!("\"{}\"", s
.replace("\"", "\\\"")
.replace("\n", "\\n")
.replace("\t", "\\t")
.replace("\r", "\\r"))
format!(
"\"{}\"",
s.replace("\"", "\\\"")
.replace("\n", "\\n")
.replace("\t", "\\t")
.replace("\r", "\\r")
)
} else {
//return valid json string
s
},
}
}
Value::List(l) => {
let mut string_result = "[".to_string();
for (i, v) in l.iter().enumerate() {
@@ -64,7 +67,11 @@ fn prolog_match_to_json_string(query_match: &QueryMatch) -> String {
if i > 0 {
string_result.push_str(",");
}
string_result.push_str(&format!("\"{}\":{}", k, prolog_value_to_json_tring(v.clone())));
string_result.push_str(&format!(
"\"{}\":{}",
k,
prolog_value_to_json_tring(v.clone())
));
}
string_result.push_str("}");
string_result