Skip to content
19 changes: 1 addition & 18 deletions cmd/tools/vls.v
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,6 @@ const vls_src_folder = os.join_path(vls_folder, 'src')
const server_not_found_err = error_with_code('Language server is not installed nor found.',
101)

const json_enc = json2.Encoder{
newline: `\n`
newline_spaces_count: 2
escape_unicode: false
}

fn (upd VlsUpdater) check_or_create_vls_folder() ! {
if !os.exists(vls_folder) {
upd.log('Creating .vls folder...')
Expand Down Expand Up @@ -106,22 +100,11 @@ fn (upd VlsUpdater) update_manifest(new_path string, from_source bool, timestamp
}
}

mut manifest_file := os.open_file(vls_manifest_path, 'w+')!
defer {
manifest_file.close()
}

manifest['server_path'] = json2.Any(new_path)
manifest['last_updated'] = json2.Any(timestamp.format_ss())
manifest['from_source'] = json2.Any(from_source)

mut buffer := []u8{}

json_enc.encode_value(manifest, mut buffer)!

manifest_file.write(buffer)!

unsafe { buffer.free() }
os.write_file(vls_manifest_path, json2.encode(manifest))!
}

fn (upd VlsUpdater) init_download_prebuilt() ! {
Expand Down
5 changes: 5 additions & 0 deletions vlib/math/big/json_decode.v → vlib/math/big/json_custom.v
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ pub fn (mut result Integer) from_json_number(raw_number string) ! {
result = result * integer_from_int(-1)
}
}

// to_json implements a custom encoder for json2
pub fn (result Integer) to_json() string {
return result.str()
}
5 changes: 5 additions & 0 deletions vlib/time/json_decode.c.v → vlib/time/json_custom.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@ pub fn (mut t Time) from_json_string(raw_string string) ! {

return error('Expected iso8601/rfc3339/unix time but got: ${raw_string}')
}

// to_json implements a custom encoder for json2 (rfc3339)
pub fn (t Time) to_json() string {
return '"' + t.format_rfc3339() + '"'
}
2 changes: 1 addition & 1 deletion vlib/toml/tests/testdata/json_encoding_test.out
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "\u3072\u3089\u304c\u306a": "\u3072\u3089" }
{ "ひらがな": "ひら" }
4 changes: 2 additions & 2 deletions vlib/v/gen/js/sourcemap/basic_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fn test_simple() {

json_data := sm.to_json()

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"}'
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"}'
assert json_data.str() == expected
}

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

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"}'
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"}'
assert json_data.str() == expected
}
5 changes: 5 additions & 0 deletions vlib/x/json2/constants.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module json2

const true_string = 'true'
const false_string = 'false'
const null_string = 'null'
98 changes: 0 additions & 98 deletions vlib/x/json2/count.v

This file was deleted.

99 changes: 0 additions & 99 deletions vlib/x/json2/count_test.v

This file was deleted.

15 changes: 15 additions & 0 deletions vlib/x/json2/custom.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module json2

// implements encoding json, this is not validated so implementations must be correct
pub interface JsonEncoder {
// to_json returns a string containing an objects json representation
to_json() string
}

// Encodable is an interface, that allows custom implementations for encoding structs to their string based JSON representations.

@[deprecated: 'use `to_json` to implement `JsonEncoder` instead']
@[deprecated_after: '2025-10-30']
pub interface Encodable {
json_str() string
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct OptAnyStruct[T] {
fn test_values() {
assert json.decode[AnyStruct[json2.Any]]('{"val":5}')!.val.int() == 5
assert json.decode[OptAnyStruct[json2.Any]]('{}')!.val == none
assert json.decode[OptAnyStruct[json2.Any]]('{"val":null}')!.val == none
assert json.decode[AnyStruct[[]json2.Any]]('{"val":[5,10]}')!.val.map(it.int()) == [
5,
10,
Expand Down
Loading
Loading