@@ -31,7 +31,11 @@ pub fn (mut d Decoder) decode_to_json[T](src []u8) !string {
3131 d.buffer = src
3232 d.next ()!
3333
34- mut result := ''
34+ mut result := []u8 {}
35+
36+ defer {
37+ unsafe { result.free () }
38+ }
3539
3640 data := d.buffer
3741
@@ -41,72 +45,75 @@ pub fn (mut d Decoder) decode_to_json[T](src []u8) !string {
4145
4246 mut d_for_array := new_decoder ()
4347
44- result + = '['
48+ result << `[`
4549
4650 for i in 0 .. array_len {
4751 if i > 0 {
48- result + = ','
52+ result << `,`
4953 }
5054
5155 element_json := d_for_array.decode_to_json[T](data[1 ..]) or {
5256 return error ('error converting array element to JSON ${err} ' )
5357 }
5458
55- result + = element_json
59+ unsafe { result. push_many ( element_json.str, element_json.len) }
5660 }
5761
58- result + = ']'
62+ result << `]`
5963 }
6064 mp_map_16 , mp_map_32 , mp_fix_map_min...mp_fix_map_max {
6165 map_len := d.read_map_len (src) or { return error ('error reading map length' ) }
6266
63- result + = '{'
67+ result << `{`
6468
6569 for i in 0 .. map_len {
6670 if i > 0 {
67- result + = ','
71+ result << `,`
6872 }
6973
7074 key := d.decode_to_json[string ](src) or {
7175 return error ('error converting map key to JSON: ${err} ' )
7276 }
77+ unsafe { result.push_many (key.str, key.len) }
78+ result << `:`
7379
7480 value_json := d.decode_to_json[T](src) or {
7581 return error ('error converting map value to JSON' )
7682 }
77-
78- result + = '${key} :${value_json} '
83+ unsafe { result.push_many (value_json.str ().str, value_json.str ().len) }
7984 }
8085
81- result + = '}'
86+ result << `}`
8287 }
8388 mp_nil {
84- result + = 'null'
89+ unsafe { result. push_many ( 'null' .str, 'null' .len) }
8590 }
8691 mp_true, mp_false {
8792 mut bool_val := false
8893 d.decode_bool (mut bool_val) or { return error ('error decoding boolean: ${err} ' ) }
89- result + = bool_val.str ()
94+ unsafe { result. push_many ( bool_val.str ().str, bool_val. str ().len) }
9095 }
9196 mp_f32 , mp_f64 {
9297 mut float_val := 0.0
9398 d.decode_float (mut float_val) or { return error ('error decoding float: ${err} ' ) }
94- result + = float_val.str ()
99+ unsafe { result. push_many ( float_val.str ().str, float_val. str ().len) }
95100 }
96101 mp_u8 , mp_u16 , mp_u32 , mp_u64 , mp_i8 , mp_i16 , mp_i32 , mp_i64 {
97102 mut int_val := 0
98103 d.decode_integer (mut int_val) or { return error ('error decoding integer: ${err} ' ) }
99- result + = int_val.str ()
104+ unsafe { result. push_many ( int_val.str ().str, int_val. str ().len) }
100105 }
101106 mp_str_8 , mp_str_16 , mp_str_32 , mp_fix_str_min...mp_fix_str_max {
102107 mut str_val := ''
103108 d.decode_string (mut str_val) or { return error ('error decoding string: ${err} ' ) }
104- result + = '"${str_val} "'
109+ result << `\" `
110+ unsafe { result.push_many (str_val.str, str_val.len) }
111+ result << `\" `
105112 }
106113 mp_bin_8 , mp_bin_16 , mp_bin_32 {
107114 bin_len := d.read_bin_len (src) or { return error ('error reading binary length' ) }
108115 for i in 0 .. bin_len {
109- result + = src[d.pos + i].str ()
116+ unsafe { result. push_many (src[d.pos + i]. str ().str, src[d.pos + i].str ().len) }
110117 }
111118
112119 d.pos + = bin_len
@@ -117,7 +124,7 @@ pub fn (mut d Decoder) decode_to_json[T](src []u8) !string {
117124 return error ('unsupported descriptor byte for conversion to JSON' )
118125 }
119126 }
120- return result
127+ return result. bytestr ()
121128}
122129
123130pub fn decode [T](src []u8 ) ! T {
0 commit comments