diff --git a/core/json.rs b/core/json.rs index 6372cbd65..e9dc1f42a 100644 --- a/core/json.rs +++ b/core/json.rs @@ -6,7 +6,10 @@ use serde_json::Value; pub fn get_json(json_value: &OwnedValue) -> crate::Result { match json_value { OwnedValue::Text(ref t) => { - if let Ok(json) = json5::from_str::(t.as_str()) { + // Replace instances of two single quotes ('') with a double quote (") + // This is necessary to correctly format the string for parsing + let corrected_t = t.replace("''", "\""); + if let Ok(json) = json5::from_str::(&corrected_t) { Ok(OwnedValue::Text(Rc::new(json.to_string()))) } else { crate::bail_parse_error!("malformed JSON"); diff --git a/testing/all.test b/testing/all.test index ad08d418b..26ee60589 100755 --- a/testing/all.test +++ b/testing/all.test @@ -12,3 +12,4 @@ source $testdir/where.test source $testdir/like.test source $testdir/scalar-functions.test source $testdir/orderby.test +source $testdir/json.test \ No newline at end of file diff --git a/testing/json.test b/testing/json.test new file mode 100644 index 000000000..1e717e09e --- /dev/null +++ b/testing/json.test @@ -0,0 +1,57 @@ +#!/usr/bin/env tclsh + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +do_execsql_test json5-ecma-script-1 { + select json('{a:5,b:6}') ; +} {{{"a":5,"b":6}}} + +do_execsql_test json5-ecma-script-2 { + SELECT json('{ MNO_123$xyz : 789 }'); +} {{{"MNO_123$xyz":789}}} + +do_execsql_test json5-with-single-trailing-comma-valid { + select json('{"a":5, "b":6, }'); +} {{{"a":5,"b":6}}} + +do_execsql_test json5-single-quoted { + SELECT json('{"a": ''abcd''}'); +} {{{"a":"abcd"}}} + +do_execsql_test json5-character-escaped-1 { + select json('{a: "abc\x35\x4f\x6Exyz"}'); +} {{{"a":"abc5Onxyz"}}} + +do_execsql_test json5-character-escaped-2 { + select json('{a: "\x6a\x6A\x6b\x6B\x6c\x6C\x6d\x6D\x6e\x6E\x6f\x6F"}'); +} {{{"a":"jjkkllmmnnoo"}}} + +do_execsql_test json5-hexadecimal-1 { + SELECT json('{a: 0x0}') +} {{{"a":0}}} + +do_execsql_test json5-hexadecimal-2 { + SELECT json('{a: 0xabcdef}') +} {{{"a":11259375}}} + +do_execsql_test json5-number-1 { + SELECT json('{x: 4.}') +} {{{"x":4.0}}} + +do_execsql_test json5-number-2 { + SELECT json('{x: +4.}') +} {{{"x":4.0}}} + +do_execsql_test json5-number-3 { + SELECT json('{x: -4.}') +} {{{"x":-4.0}}} + +do_execsql_test json5-number-4 { + SELECT json('{x: +4.e1}') +} {{{"x":40.0}}} + +do_execsql_test json5-multi-comment { + SELECT json(' /* abc */ { /*def*/ aaa /* xyz */ : // to the end of line + 123 /* xyz */ , /* 123 */ }') +} {{{"aaa":123}}}