@@ -9,30 +9,42 @@ use sqlite::ResultCode;
99use sqlite_nostd as sqlite;
1010use sqlite_nostd:: { Connection , Context , Value } ;
1111
12- use serde_json as json;
13-
1412use crate :: create_sqlite_text_fn;
1513use crate :: error:: SQLiteError ;
1614
1715/// Given any number of JSON TEXT arguments, merge them into a single JSON object.
1816///
19- /// TODO: If we know these are all valid JSON objects, we could perhaps do string concatenation instead.
17+ /// This assumes each argument is a valid JSON object, with no duplicate keys.
18+ /// No JSON parsing or validation is performed - this performs simple string concatenation.
2019fn powersync_json_merge_impl (
2120 _ctx : * mut sqlite:: context ,
2221 args : & [ * mut sqlite:: value ] ,
2322) -> Result < String , SQLiteError > {
24- let mut v_result = json:: Value :: Object ( json:: Map :: new ( ) ) ;
23+ if args. is_empty ( ) {
24+ return Ok ( "{}" . to_string ( ) ) ;
25+ }
26+ let mut result = String :: from ( "{" ) ;
2527 for arg in args {
26- let v: json:: Value = json:: from_str ( arg. text ( ) ) ?;
27- if let json:: Value :: Object ( map) = v {
28- for ( key, value) in map {
29- v_result[ key] = value;
30- }
31- } else {
28+ let chunk = arg. text ( ) ;
29+ if chunk. is_empty ( ) || !chunk. starts_with ( '{' ) || !chunk. ends_with ( '}' ) {
3230 return Err ( SQLiteError :: from ( ResultCode :: MISMATCH ) ) ;
3331 }
32+
33+ // Strip outer braces
34+ let inner = & chunk[ 1 ..( chunk. len ( ) - 1 ) ] ;
35+
36+ // If this is not the first chunk, insert a comma
37+ if result. len ( ) > 1 {
38+ result. push ( ',' ) ;
39+ }
40+
41+ // Append the inner content
42+ result. push_str ( inner) ;
3443 }
35- return Ok ( v_result. to_string ( ) ) ;
44+
45+ // Close the outer brace
46+ result. push ( '}' ) ;
47+ Ok ( result)
3648}
3749
3850create_sqlite_text_fn ! (
0 commit comments