Skip to content

Commit 0e9e9b6

Browse files
committed
x.json2: replace x.json2.decode() with x.json2.decoder2.decode()
1 parent 696b2fb commit 0e9e9b6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+321
-1409
lines changed

vlib/v/checker/tests/comptime_selector_assign.vv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import x.json2 { Any, raw_decode }
1+
import x.json2 { Any, decode }
22

33
struct Income {
44
mut:
@@ -22,7 +22,7 @@ pub fn structuring[T](res Any) T {
2222
}
2323

2424
fn main() {
25-
res := raw_decode('{
25+
res := decode[Any]('{
2626
"email": ["sdvsdv", "sds"],
2727
"code": 12
2828
}')!.as_map()

vlib/v/tests/aliases/alias_sumtype_method_call_test.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ struct Data {
88

99
fn test_alias_sumtype_method_call() {
1010
a := '{"a":"a","b":1}'
11-
json := json2.raw_decode(a) or { panic(err) }
11+
json := json2.decode[json2.Any](a) or { panic(err) }
1212
data := Data{json}
1313
json_str := data.prop.str()
1414
println(json_str)

vlib/v/tests/comptime/comptime_map_generic_test.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub mut:
77

88
fn test_main() {
99
user_json := '{"numbers":{"home":"123456","work":"987653"}}'
10-
res := json2.raw_decode(user_json)!.as_map()
10+
res := json2.decode[json2.Any](user_json)!.as_map()
1111

1212
mut numbers := map[string]string{}
1313
decode_map(mut numbers, res['numbers']!.as_map())!

vlib/v/tests/result_with_index_expr_test.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn test_result_with_index() {
1111
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
1212
"body": "quia et suscipitsuscipit recusandae consequuntur expedita et cumreprehenderit molestiae ut ut quas totamnostrum rerum est autem sunt rem eveniet architecto"
1313
}'
14-
raw_data := json2.raw_decode(resp)!
14+
raw_data := json2.decode[json2.Any](resp)!
1515

1616
data := raw_data as map[string]json2.Any
1717

vlib/v/tests/skip_unused/generic_call_from_json.vv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub struct App {
1111

1212
@['/'; POST]
1313
fn (app &App) index(mut ctx Context) veb.Result {
14-
j := json2.raw_decode(ctx.req.data) or { return ctx.request_error('error') }
14+
j := json2.decode[json2.Any](ctx.req.data) or { return ctx.request_error('error') }
1515

1616
m := j.as_map()
1717
myarr := m['myarr'] or { panic(error) }

vlib/x/json2/decoder2/check.v renamed to vlib/x/json2/check.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module decoder2
1+
module json2
22

33
// increment checks eof and increments checker by one
44
@[inline]

vlib/x/json2/custom.v

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,52 @@ pub interface JsonEncoder {
1313
pub interface Encodable {
1414
json_str() string
1515
}
16+
17+
// implements decoding json strings, e.g. "hello, \u2164!"
18+
pub interface StringDecoder {
19+
mut:
20+
// called with raw string (minus apostrophes) e.g. 'hello, \u2164!'
21+
from_json_string(raw_string string) !
22+
}
23+
24+
// implements decoding json numbers, e.g. -1.234e23
25+
pub interface NumberDecoder {
26+
mut:
27+
// called with raw string of number e.g. '-1.234e23'
28+
from_json_number(raw_number string) !
29+
}
30+
31+
// implements decoding json true/false
32+
pub interface BooleanDecoder {
33+
mut:
34+
// called with converted bool
35+
// already checked so no error needed
36+
from_json_boolean(boolean_value bool)
37+
}
38+
39+
// implements decoding json null
40+
pub interface NullDecoder {
41+
mut:
42+
// only has one value
43+
// already checked so no error needed
44+
from_json_null()
45+
}
46+
47+
// Implement once generic interfaces are more stable
48+
49+
// // implements decoding json arrays, e.g. ["hi", "bye", 0.9, true]
50+
// // elements are already decoded
51+
// pub interface ArrayDecoder[T] {
52+
// mut:
53+
// // called for every element in array e.g. 'hi', 'bye', '0.9', 'true'
54+
// from_json_value(raw_value T)!
55+
// }
56+
57+
// // implements decoding json object, e.g. {"name": "foo", "name": "bar", "age": 99}
58+
// // this allows for duplicate/ordered keys to be decoded
59+
// // elements are already decoded
60+
// pub interface ObjectDecoder[T] {
61+
// mut:
62+
// // called for every key-value pair in object (minus apostrophes for keys) e.g. ('name': 'foo'), ('name': 'bar'), ('age': '99')
63+
// from_json_pair(raw_key string, raw_value T)!
64+
// }

vlib/x/json2/decoder2/decode.v renamed to vlib/x/json2/decode.v

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module decoder2
1+
module json2
22

33
import strconv
44
import strings
@@ -122,16 +122,6 @@ fn (list &LinkedList[T]) free() {
122122
list.len = 0
123123
}
124124

125-
// ValueKind represents the kind of a JSON value.
126-
enum ValueKind {
127-
array
128-
object
129-
string
130-
number
131-
boolean
132-
null
133-
}
134-
135125
const max_context_length = 50
136126
const max_extra_characters = 5
137127
const tab_width = 8

vlib/x/json2/decoder2/decode_sumtype.v renamed to vlib/x/json2/decode_sumtype.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module decoder2
1+
module json2
22

33
import time
44

0 commit comments

Comments
 (0)