mirror of
https://github.com/vlang/v.git
synced 2025-09-11 08:25:42 -04:00
json2.decoder2: prepare decoder in json2 to be replaced by json2.decode2 (#23078)
This commit is contained in:
parent
210239fb01
commit
de3b184b01
@ -79,7 +79,7 @@ fn (upd VlsUpdater) check_or_create_vls_folder() ! {
|
|||||||
|
|
||||||
fn (upd VlsUpdater) manifest_config() !map[string]json2.Any {
|
fn (upd VlsUpdater) manifest_config() !map[string]json2.Any {
|
||||||
manifest_buf := os.read_file(vls_manifest_path) or { '{}' }
|
manifest_buf := os.read_file(vls_manifest_path) or { '{}' }
|
||||||
manifest_contents := json2.raw_decode(manifest_buf)!.as_map()
|
manifest_contents := json2.decode[json2.Any](manifest_buf)!.as_map()
|
||||||
return manifest_contents
|
return manifest_contents
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,7 +168,7 @@ fn (upd VlsUpdater) download_prebuilt() ! {
|
|||||||
|
|
||||||
upd.log('Finding prebuilt executables from GitHub release..')
|
upd.log('Finding prebuilt executables from GitHub release..')
|
||||||
resp := http.get('https://api.github.com/repos/vlang/vls/releases')!
|
resp := http.get('https://api.github.com/repos/vlang/vls/releases')!
|
||||||
releases_json := json2.raw_decode(resp.body)!.arr()
|
releases_json := json2.decode[json2.Any](resp.body)!.arr()
|
||||||
if releases_json.len == 0 {
|
if releases_json.len == 0 {
|
||||||
return error('Unable to fetch latest VLS release data: No releases found.')
|
return error('Unable to fetch latest VLS release data: No releases found.')
|
||||||
}
|
}
|
||||||
|
@ -2,5 +2,5 @@ import x.json2
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
x := '[[],[],[]]'
|
x := '[[],[],[]]'
|
||||||
println(json2.raw_decode(x)!)
|
println(json2.decode[json2.Any](x)!)
|
||||||
}
|
}
|
||||||
|
@ -77,7 +77,7 @@ fn main() {
|
|||||||
resp := http.get('https://reqres.in/api/products/1')!
|
resp := http.get('https://reqres.in/api/products/1')!
|
||||||
|
|
||||||
// This returns an Any type
|
// This returns an Any type
|
||||||
raw_product := json2.raw_decode(resp.body)!
|
raw_product := json2.decode[json2.Any](resp.body)!
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -90,7 +90,7 @@ import net.http
|
|||||||
fn main() {
|
fn main() {
|
||||||
resp := http.get('https://reqres.in/api/products/1')!
|
resp := http.get('https://reqres.in/api/products/1')!
|
||||||
|
|
||||||
raw_product := json2.raw_decode(resp.body)!
|
raw_product := json2.decode[json2.Any](resp.body)!
|
||||||
|
|
||||||
product := raw_product.as_map()
|
product := raw_product.as_map()
|
||||||
data := product['data'] as map[string]json2.Any
|
data := product['data'] as map[string]json2.Any
|
||||||
|
@ -60,16 +60,13 @@ fn new_parser(srce string, convert_type bool) Parser {
|
|||||||
|
|
||||||
// decode is a generic function that decodes a JSON string into the target type.
|
// decode is a generic function that decodes a JSON string into the target type.
|
||||||
pub fn decode[T](src string) !T {
|
pub fn decode[T](src string) !T {
|
||||||
|
$if T is Any {
|
||||||
|
return raw_decode(src)!
|
||||||
|
}
|
||||||
res := raw_decode(src)!.as_map()
|
res := raw_decode(src)!.as_map()
|
||||||
return decode_struct[T](T{}, res)
|
return decode_struct[T](T{}, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
// decode_array is a generic function that decodes a JSON string into the array target type.
|
|
||||||
pub fn decode_array[T](src string) ![]T {
|
|
||||||
res := raw_decode(src)!.as_map()
|
|
||||||
return decode_struct_array(T{}, res)
|
|
||||||
}
|
|
||||||
|
|
||||||
// decode_struct_array is a generic function that decodes a JSON map into array struct T.
|
// decode_struct_array is a generic function that decodes a JSON map into array struct T.
|
||||||
fn decode_struct_array[T](_ T, res map[string]Any) ![]T {
|
fn decode_struct_array[T](_ T, res map[string]Any) ![]T {
|
||||||
$if T is $struct {
|
$if T is $struct {
|
||||||
@ -428,6 +425,7 @@ fn (mut p Parser) decode_array() !Any {
|
|||||||
return Any(items)
|
return Any(items)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@[deprecated_after: '2025-03-18']
|
||||||
fn (mut p Parser) decode_object() !Any {
|
fn (mut p Parser) decode_object() !Any {
|
||||||
mut fields := map[string]Any{}
|
mut fields := map[string]Any{}
|
||||||
p.next_with_err()!
|
p.next_with_err()!
|
||||||
|
@ -777,10 +777,25 @@ fn (mut decoder Decoder) decode_value[T](mut val T) ! {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if current_field_info.value.is_raw {
|
if current_field_info.value.is_raw {
|
||||||
$if field.typ is $enum {
|
$if field.unaliased_typ is $enum {
|
||||||
// workaround to avoid the error: enums can only be assigned `int` values
|
// workaround to avoid the error: enums can only be assigned `int` values
|
||||||
return error('`raw` attribute cannot be used with enum fields')
|
return error('`raw` attribute cannot be used with enum fields')
|
||||||
} $else $if field.typ is string || field.typ is ?string {
|
} $else $if field.typ is ?string {
|
||||||
|
position := decoder.current_node.value.position
|
||||||
|
end := position + decoder.current_node.value.length
|
||||||
|
|
||||||
|
val.$(field.name) = decoder.json[position..end]
|
||||||
|
decoder.current_node = decoder.current_node.next
|
||||||
|
|
||||||
|
for {
|
||||||
|
if decoder.current_node == unsafe { nil }
|
||||||
|
|| decoder.current_node.value.position + decoder.current_node.value.length >= end {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
decoder.current_node = decoder.current_node.next
|
||||||
|
}
|
||||||
|
} $else $if field.typ is string {
|
||||||
position := decoder.current_node.value.position
|
position := decoder.current_node.value.position
|
||||||
end := position + decoder.current_node.value.length
|
end := position + decoder.current_node.value.length
|
||||||
|
|
||||||
|
@ -6,9 +6,15 @@ fn (mut decoder Decoder) get_decoded_sumtype_workaround[T](initialized_sumtype T
|
|||||||
$if initialized_sumtype is $sumtype {
|
$if initialized_sumtype is $sumtype {
|
||||||
$for v in initialized_sumtype.variants {
|
$for v in initialized_sumtype.variants {
|
||||||
if initialized_sumtype is v {
|
if initialized_sumtype is v {
|
||||||
mut val := initialized_sumtype
|
$if v is $array {
|
||||||
decoder.decode_value(mut val)!
|
mut val := initialized_sumtype.clone()
|
||||||
return T(val)
|
decoder.decode_value(mut val)!
|
||||||
|
return T(val)
|
||||||
|
} $else {
|
||||||
|
mut val := initialized_sumtype
|
||||||
|
decoder.decode_value(mut val)!
|
||||||
|
return T(val)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
8
vlib/x/json2/decoder2/decoder_deprecated.v
Normal file
8
vlib/x/json2/decoder2/decoder_deprecated.v
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
module decoder2
|
||||||
|
|
||||||
|
// decode_array is a generic function that decodes a JSON string into the array target type.
|
||||||
|
@[deprecated: 'use `decode` instead']
|
||||||
|
@[deprecated_after: '2025-03-18']
|
||||||
|
pub fn decode_array[T](src string) !T {
|
||||||
|
return decode[T](src)
|
||||||
|
}
|
@ -1,6 +1,5 @@
|
|||||||
import x.json2.decoder2 as json
|
import x.json2.decoder2 as json
|
||||||
import x.json2
|
import x.json2
|
||||||
import time
|
|
||||||
|
|
||||||
type Prices = Price | []Price
|
type Prices = Price | []Price
|
||||||
|
|
||||||
|
@ -7,11 +7,13 @@ pub struct DecodeError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// code returns the error code of DecodeError
|
// code returns the error code of DecodeError
|
||||||
|
@[deprecated_after: '2025-03-18']
|
||||||
pub fn (err DecodeError) code() int {
|
pub fn (err DecodeError) code() int {
|
||||||
return 3
|
return 3
|
||||||
}
|
}
|
||||||
|
|
||||||
// msg returns the message of the DecodeError
|
// msg returns the message of the DecodeError
|
||||||
|
@[deprecated_after: '2025-03-18']
|
||||||
pub fn (err DecodeError) msg() string {
|
pub fn (err DecodeError) msg() string {
|
||||||
return format_message(err.message, err.line, err.column)
|
return format_message(err.message, err.line, err.column)
|
||||||
}
|
}
|
||||||
@ -23,11 +25,13 @@ pub struct InvalidTokenError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// code returns the error code of the InvalidTokenError
|
// code returns the error code of the InvalidTokenError
|
||||||
|
@[deprecated_after: '2025-03-18']
|
||||||
pub fn (err InvalidTokenError) code() int {
|
pub fn (err InvalidTokenError) code() int {
|
||||||
return 2
|
return 2
|
||||||
}
|
}
|
||||||
|
|
||||||
// msg returns the message of the InvalidTokenError
|
// msg returns the message of the InvalidTokenError
|
||||||
|
@[deprecated_after: '2025-03-18']
|
||||||
pub fn (err InvalidTokenError) msg() string {
|
pub fn (err InvalidTokenError) msg() string {
|
||||||
footer_text := if err.expected != .none { ', expecting `${err.expected}`' } else { '' }
|
footer_text := if err.expected != .none { ', expecting `${err.expected}`' } else { '' }
|
||||||
return format_message('invalid token `${err.token.kind}`${footer_text}', err.token.line,
|
return format_message('invalid token `${err.token.kind}`${footer_text}', err.token.line,
|
||||||
@ -41,11 +45,13 @@ pub struct UnknownTokenError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// code returns the error code of the UnknownTokenError
|
// code returns the error code of the UnknownTokenError
|
||||||
|
@[deprecated_after: '2025-03-18']
|
||||||
pub fn (err UnknownTokenError) code() int {
|
pub fn (err UnknownTokenError) code() int {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
// msg returns the error message of the UnknownTokenError
|
// msg returns the error message of the UnknownTokenError
|
||||||
|
@[deprecated_after: '2025-03-18']
|
||||||
pub fn (err UnknownTokenError) msg() string {
|
pub fn (err UnknownTokenError) msg() string {
|
||||||
return format_message("unknown token '${err.token.lit}' when decoding ${err.kind}.",
|
return format_message("unknown token '${err.token.lit}' when decoding ${err.kind}.",
|
||||||
err.token.line, err.token.full_col())
|
err.token.line, err.token.full_col())
|
||||||
@ -68,12 +74,15 @@ pub fn raw_decode(src string) !Any {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Same with `raw_decode`, but skips the type conversion for certain types when decoding a certain value.
|
// Same with `raw_decode`, but skips the type conversion for certain types when decoding a certain value.
|
||||||
|
@[deprecated: 'use `decode[json.Any]` instead']
|
||||||
|
@[deprecated_after: '2025-03-18']
|
||||||
pub fn fast_raw_decode(src string) !Any {
|
pub fn fast_raw_decode(src string) !Any {
|
||||||
mut p := new_parser(src, false)
|
mut p := new_parser(src, false)
|
||||||
return p.decode()
|
return p.decode()
|
||||||
}
|
}
|
||||||
|
|
||||||
// decode - decodes provided JSON
|
// decode - decodes provided JSON
|
||||||
|
@[deprecated_after: '2025-03-18']
|
||||||
pub fn (mut p Parser) decode() !Any {
|
pub fn (mut p Parser) decode() !Any {
|
||||||
p.next()
|
p.next()
|
||||||
p.next_with_err()!
|
p.next_with_err()!
|
||||||
@ -85,3 +94,11 @@ pub fn (mut p Parser) decode() !Any {
|
|||||||
}
|
}
|
||||||
return fi
|
return fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// decode_array is a generic function that decodes a JSON string into the array target type.
|
||||||
|
@[deprecated: 'use `decode` instead']
|
||||||
|
@[deprecated_after: '2025-03-18']
|
||||||
|
pub fn decode_array[T](src string) ![]T {
|
||||||
|
res := raw_decode(src)!.as_map()
|
||||||
|
return decode_struct_array(T{}, res)
|
||||||
|
}
|
||||||
|
@ -100,7 +100,7 @@ fn encode_array[T](val []T) string {
|
|||||||
// encode_pretty ...
|
// encode_pretty ...
|
||||||
pub fn encode_pretty[T](typed_data T) string {
|
pub fn encode_pretty[T](typed_data T) string {
|
||||||
encoded := encode(typed_data)
|
encoded := encode(typed_data)
|
||||||
raw_decoded := raw_decode(encoded) or { 0 }
|
raw_decoded := decode[Any](encoded) or { 0 }
|
||||||
return raw_decoded.prettify_json_str()
|
return raw_decoded.prettify_json_str()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
module main
|
|
||||||
|
|
||||||
import x.json2
|
import x.json2
|
||||||
|
|
||||||
const data = '
|
const data = '
|
||||||
@ -31,7 +29,7 @@ const data = '
|
|||||||
}
|
}
|
||||||
'
|
'
|
||||||
|
|
||||||
struct Comment {
|
pub struct Comment {
|
||||||
id string
|
id string
|
||||||
message string
|
message string
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user