Skip to content

Commit bae7684

Browse files
authored
json2: replace encoder with new implementation (#25224)
1 parent 69e80ba commit bae7684

20 files changed

Lines changed: 902 additions & 1102 deletions

File tree

‎cmd/tools/vls.v‎

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,6 @@ const vls_src_folder = os.join_path(vls_folder, 'src')
6464
const server_not_found_err = error_with_code('Language server is not installed nor found.',
6565
101)
6666

67-
const json_enc = json2.Encoder{
68-
newline: `\n`
69-
newline_spaces_count: 2
70-
escape_unicode: false
71-
}
72-
7367
fn (upd VlsUpdater) check_or_create_vls_folder() ! {
7468
if !os.exists(vls_folder) {
7569
upd.log('Creating .vls folder...')
@@ -106,22 +100,11 @@ fn (upd VlsUpdater) update_manifest(new_path string, from_source bool, timestamp
106100
}
107101
}
108102

109-
mut manifest_file := os.open_file(vls_manifest_path, 'w+')!
110-
defer {
111-
manifest_file.close()
112-
}
113-
114103
manifest['server_path'] = json2.Any(new_path)
115104
manifest['last_updated'] = json2.Any(timestamp.format_ss())
116105
manifest['from_source'] = json2.Any(from_source)
117106

118-
mut buffer := []u8{}
119-
120-
json_enc.encode_value(manifest, mut buffer)!
121-
122-
manifest_file.write(buffer)!
123-
124-
unsafe { buffer.free() }
107+
os.write_file(vls_manifest_path, json2.encode(manifest))!
125108
}
126109

127110
fn (upd VlsUpdater) init_download_prebuilt() ! {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,8 @@ pub fn (mut result Integer) from_json_number(raw_number string) ! {
2828
result = result * integer_from_int(-1)
2929
}
3030
}
31+
32+
// to_json implements a custom encoder for json2
33+
pub fn (result Integer) to_json() string {
34+
return result.str()
35+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,8 @@ pub fn (mut t Time) from_json_string(raw_string string) ! {
3434

3535
return error('Expected iso8601/rfc3339/unix time but got: ${raw_string}')
3636
}
37+
38+
// to_json implements a custom encoder for json2 (rfc3339)
39+
pub fn (t Time) to_json() string {
40+
return '"' + t.format_rfc3339() + '"'
41+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{ "\u3072\u3089\u304c\u306a": "\u3072\u3089" }
1+
{ "ひらがな": "ひら" }

‎vlib/v/gen/js/sourcemap/basic_test.v‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ fn test_simple() {
132132

133133
json_data := sm.to_json()
134134

135-
expected := '{"version":3,"file":"hello.js","sourceRoot":"\\/","sources":["hello.v"],"sourcesContent":["fn main(){nprintln(\'Hello World! Helo \$a\')\\n}"],"names":["hello_name"],"mappings":"AAAA;AAAA,EAAA,OAAO,CAACA,GAAR,CAAY,aAAZ,CAAA,CAAA;AAAA"}'
135+
expected := '{"version":3,"file":"hello.js","sourceRoot":"/","sources":["hello.v"],"sourcesContent":["fn main(){nprintln(\'Hello World! Helo \$a\')\\n}"],"names":["hello_name"],"mappings":"AAAA;AAAA,EAAA,OAAO,CAACA,GAAR,CAAY,aAAZ,CAAA,CAAA;AAAA"}'
136136
assert json_data.str() == expected
137137
}
138138

@@ -153,6 +153,6 @@ fn test_source_null() {
153153
}, 3, 1, '')
154154
json_data := sm.to_json()
155155

156-
expected := '{"version":3,"file":"hello.js","sourceRoot":"\\/","sources":["hello.v","hello_lib1.v","hello_lib2.v"],"sourcesContent":[null,null,null],"names":[],"mappings":"CA+\\/\\/\\/\\/\\/HA;CCAA;CCAA"}'
156+
expected := '{"version":3,"file":"hello.js","sourceRoot":"/","sources":["hello.v","hello_lib1.v","hello_lib2.v"],"sourcesContent":[null,null,null],"names":[],"mappings":"CA+/////HA;CCAA;CCAA"}'
157157
assert json_data.str() == expected
158158
}

‎vlib/x/json2/constants.v‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module json2
2+
3+
const true_string = 'true'
4+
const false_string = 'false'
5+
const null_string = 'null'

‎vlib/x/json2/count.v‎

Lines changed: 0 additions & 98 deletions
This file was deleted.

‎vlib/x/json2/count_test.v‎

Lines changed: 0 additions & 99 deletions
This file was deleted.

‎vlib/x/json2/custom.v‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module json2
2+
3+
// implements encoding json, this is not validated so implementations must be correct
4+
pub interface JsonEncoder {
5+
// to_json returns a string containing an objects json representation
6+
to_json() string
7+
}
8+
9+
// Encodable is an interface, that allows custom implementations for encoding structs to their string based JSON representations.
10+
11+
@[deprecated: 'use `to_json` to implement `JsonEncoder` instead']
12+
@[deprecated_after: '2025-10-30']
13+
pub interface Encodable {
14+
json_str() string
15+
}

‎vlib/x/json2/decoder2/tests/json2_tests/decode_and_encode_struct_any_test.v‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ struct OptAnyStruct[T] {
1616
fn test_values() {
1717
assert json.decode[AnyStruct[json2.Any]]('{"val":5}')!.val.int() == 5
1818
assert json.decode[OptAnyStruct[json2.Any]]('{}')!.val == none
19+
assert json.decode[OptAnyStruct[json2.Any]]('{"val":null}')!.val == none
1920
assert json.decode[AnyStruct[[]json2.Any]]('{"val":[5,10]}')!.val.map(it.int()) == [
2021
5,
2122
10,

0 commit comments

Comments
 (0)