Skip to content

Commit f94fa74

Browse files
authored
x.json2: rename Any.arr() to Any.as_array() for consistency with as_map(), as_map_of_string(); deprecate Any.arr() (#26455)
1 parent 69d972e commit f94fa74

8 files changed

Lines changed: 34 additions & 27 deletions

File tree

‎cmd/tools/vls.v‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,13 @@ fn (upd VlsUpdater) download_prebuilt() ! {
151151

152152
upd.log('Finding prebuilt executables from GitHub release..')
153153
resp := http.get('https://api.github.com/repos/vlang/vls/releases')!
154-
releases_json := json2.decode[json2.Any](resp.body)!.arr()
154+
releases_json := json2.decode[json2.Any](resp.body)!.as_array()
155155
if releases_json.len == 0 {
156156
return error('Unable to fetch latest VLS release data: No releases found.')
157157
}
158158

159159
latest_release := releases_json[0].as_map()
160-
assets := latest_release['assets']!.arr()
160+
assets := latest_release['assets']!.as_array()
161161

162162
mut checksum_asset_idx := -1
163163
mut exec_asset_idx := -1

‎examples/get_weather/get_weather.v‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ fn translate(q string, sl string, tl string) !string {
5454

5555
json_resp := json.decode[json.Any](resp.body)!
5656

57-
a := json_resp.arr()
57+
a := json_resp.as_array()
5858
if a.len > 0 {
59-
a0 := a[0].arr()
59+
a0 := a[0].as_array()
6060
if a0.len > 0 {
61-
a00 := a0[0].arr()
61+
a00 := a0[0].as_array()
6262
if a00.len > 0 {
6363
return a00[0].str()
6464
}

‎vlib/v/tests/skip_unused/generic_call_from_json.vv‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ fn f() ! {
44
j := json2.decode[json2.Any]('')!
55
m := j.as_map()
66
myarr := m['myarr'] or { panic(error) }
7-
m2 := myarr.arr()
7+
m2 := myarr.as_array()
88
for mc in m2 {
9-
parts := mc.arr()
9+
parts := mc.as_array()
1010
// Compiler error occurs here -->
1111
first := parts[0].str()
1212
second := parts[1].str()

‎vlib/x/json2/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ fn (mut p Person) from_json(f json2.Any) {
153153
`x.json2` provides methods for turning `Any` types into usable types.
154154
The following list shows the possible outputs when casting a value to an incompatible type.
155155

156-
1. Casting non-array values as array (`arr()`) will return an array with the value as the content.
156+
1. Casting non-array values with `as_array()` will return an array with the value as the content.
157157
2. Casting non-map values as map (`as_map()`) will return a map with the value as the content.
158158
3. Casting non-string values to string (`str()`) will return the
159159
JSON string representation of the value.

‎vlib/x/json2/json2.v‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,14 @@ pub fn (f Any) bool() bool {
238238
}
239239

240240
// arr uses `Any` as an array.
241+
@[deprecated: 'use as_array() instead']
242+
@[deprecated_after: '2026-03-27']
241243
pub fn (f Any) arr() []Any {
244+
return f.as_array()
245+
}
246+
247+
// as_array uses `Any` as an array.
248+
pub fn (f Any) as_array() []Any {
242249
if f is []Any {
243250
return f
244251
} else if f is map[string]Any {

‎vlib/x/json2/tests/any_test.v‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -287,23 +287,23 @@ fn test_as_map_of_strings() {
287287
}
288288
}
289289

290-
fn test_arr() {
291-
assert sample_data['u8'] or { 0 }.arr()[0].u8() == 1
292-
assert sample_data['u16'] or { 0 }.arr()[0].u16() == 2
293-
assert sample_data['u32'] or { 0 }.arr()[0].u32() == 3
294-
assert sample_data['u64'] or { 0 }.arr()[0].u64() == 4
295-
assert sample_data['i8'] or { 0 }.arr()[0].i8() == 5
296-
assert sample_data['i16'] or { 0 }.arr()[0].i16() == 6
297-
assert sample_data['i32'] or { 0 }.arr()[0].i32() == 7
298-
assert sample_data['int'] or { 0 }.arr()[0].int() == 8
299-
assert sample_data['i64'] or { 0 }.arr()[0].i64() == 9
300-
assert sample_data['f32'] or { 0 }.arr()[0].f32() == 2.3
301-
assert sample_data['f64'] or { 0 }.arr()[0].f64() == 1.283
302-
assert sample_data['bool'] or { 0 }.arr()[0].bool() == false
303-
assert sample_data['str'] or { 0 }.arr()[0].str() == 'test'
304-
assert is_null(sample_data['null'] or { 0 }.arr()[0]) == true
305-
assert sample_data['arr'] or { 0 }.arr()[0].str() == 'lol'
306-
assert sample_data['obj'] or { 0 }.arr()[0].int() == 10
290+
fn test_as_array() {
291+
assert sample_data['u8'] or { 0 }.as_array()[0].u8() == 1
292+
assert sample_data['u16'] or { 0 }.as_array()[0].u16() == 2
293+
assert sample_data['u32'] or { 0 }.as_array()[0].u32() == 3
294+
assert sample_data['u64'] or { 0 }.as_array()[0].u64() == 4
295+
assert sample_data['i8'] or { 0 }.as_array()[0].i8() == 5
296+
assert sample_data['i16'] or { 0 }.as_array()[0].i16() == 6
297+
assert sample_data['i32'] or { 0 }.as_array()[0].i32() == 7
298+
assert sample_data['int'] or { 0 }.as_array()[0].int() == 8
299+
assert sample_data['i64'] or { 0 }.as_array()[0].i64() == 9
300+
assert sample_data['f32'] or { 0 }.as_array()[0].f32() == 2.3
301+
assert sample_data['f64'] or { 0 }.as_array()[0].f64() == 1.283
302+
assert sample_data['bool'] or { 0 }.as_array()[0].bool() == false
303+
assert sample_data['str'] or { 0 }.as_array()[0].str() == 'test'
304+
assert is_null(sample_data['null'] or { 0 }.as_array()[0]) == true
305+
assert sample_data['arr'] or { 0 }.as_array()[0].str() == 'lol'
306+
assert sample_data['obj'] or { 0 }.as_array()[0].int() == 10
307307
}
308308

309309
fn test_bool() {

‎vlib/x/json2/tests/decoder_test.v‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn test_raw_decode_number() {
1919

2020
fn test_raw_decode_array() {
2121
raw_arr := json.decode[json.Any]('["Foo", 1]')!
22-
arr := raw_arr.arr()
22+
arr := raw_arr.as_array()
2323
assert arr[0] or { 0 }.str() == 'Foo'
2424
assert arr[1] or { 0 }.int() == 1
2525
}

‎vlib/x/json2/tests/json2_tests/decoder_test.v‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn test_raw_decode_number() {
1919

2020
fn test_raw_decode_array() {
2121
raw_arr := json.decode[json.Any]('["Foo", 1]')!
22-
arr := raw_arr.arr()
22+
arr := raw_arr.as_array()
2323
assert arr[0] or { 0 }.str() == 'Foo'
2424
assert arr[1] or { 0 }.int() == 1
2525
}

0 commit comments

Comments
 (0)