decoder2: support custom decoders (#25021)
Some checks failed
json decoder benchmark CI / json-encode-benchmark (push) Waiting to run
Graphics CI / gg-regressions (push) Waiting to run
vlib modules CI / build-module-docs (push) Waiting to run
native backend CI / native-backend-ubuntu (push) Waiting to run
native backend CI / native-backend-windows (push) Waiting to run
Shy and PV CI / v-compiles-puzzle-vibes (push) Waiting to run
Sanitized CI / sanitize-undefined-clang (push) Waiting to run
Sanitized CI / sanitize-undefined-gcc (push) Waiting to run
Sanitized CI / tests-sanitize-address-clang (push) Waiting to run
Sanitized CI / sanitize-address-msvc (push) Waiting to run
Sanitized CI / sanitize-address-gcc (push) Waiting to run
Sanitized CI / sanitize-memory-clang (push) Waiting to run
sdl CI / v-compiles-sdl-examples (push) Waiting to run
Time CI / time-linux (push) Waiting to run
Time CI / time-macos (push) Waiting to run
Time CI / time-windows (push) Waiting to run
toml CI / toml-module-pass-external-test-suites (push) Waiting to run
Tools CI / tools-linux (clang) (push) Waiting to run
Tools CI / tools-linux (gcc) (push) Waiting to run
Tools CI / tools-linux (tcc) (push) Waiting to run
Tools CI / tools-macos (clang) (push) Waiting to run
Tools CI / tools-windows (gcc) (push) Waiting to run
Tools CI / tools-windows (msvc) (push) Waiting to run
Tools CI / tools-windows (tcc) (push) Waiting to run
Tools CI / tools-docker-ubuntu-musl (push) Waiting to run
vab CI / v-compiles-os-android (push) Waiting to run
vab CI / vab-compiles-v-examples (push) Waiting to run
wasm backend CI / wasm-backend (ubuntu-22.04) (push) Waiting to run
wasm backend CI / wasm-backend (windows-2022) (push) Waiting to run
json encoder benchmark CI / json-encode-benchmark (push) Has been cancelled

This commit is contained in:
Larsimusrex 2025-08-02 13:07:29 +02:00 committed by GitHub
parent b628626923
commit 809e617501
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 243 additions and 12 deletions

View file

@ -1,7 +1,6 @@
module decoder2
import strconv
import time
import strings
const null_in_string = 'null'
@ -533,8 +532,7 @@ fn (mut checker Decoder) check_json_format(val string) ! {
}
.number {
// check if the JSON string is a valid float or integer
if val[0] == `-` {
if val[checker.checker_idx] == `-` {
checker.checker_idx++
}
@ -786,15 +784,6 @@ fn (mut decoder Decoder) decode_value[T](mut val T) ! {
} $else $if T.unaliased_typ is $sumtype {
decoder.decode_sumtype(mut val)!
return
} $else $if T.unaliased_typ is time.Time {
time_info := decoder.current_node.value
if time_info.value_kind == .string_ {
string_time := decoder.json.substr_unsafe(time_info.position + 1, time_info.position +
time_info.length - 1)
val = time.parse_rfc3339(string_time) or { time.Time{} }
}
} $else $if T.unaliased_typ is $map {
decoder.decode_map(mut val)!
return
@ -811,6 +800,50 @@ fn (mut decoder Decoder) decode_value[T](mut val T) ! {
} $else $if T.unaliased_typ is $struct {
struct_info := decoder.current_node.value
// Custom Decoders
$if val is StringDecoder {
if struct_info.value_kind == .string_ {
val.from_json_string(decoder.json[struct_info.position + 1..struct_info.position +
struct_info.length - 1])!
if decoder.current_node != unsafe { nil } {
decoder.current_node = decoder.current_node.next
}
return
}
}
$if val is NumberDecoder {
if struct_info.value_kind == .number {
val.from_json_number(decoder.json[struct_info.position..struct_info.position +
struct_info.length])!
if decoder.current_node != unsafe { nil } {
decoder.current_node = decoder.current_node.next
}
return
}
}
$if val is BooleanDecoder {
if struct_info.value_kind == .boolean {
val.from_json_boolean(decoder.json[struct_info.position] == `t`)
if decoder.current_node != unsafe { nil } {
decoder.current_node = decoder.current_node.next
}
return
}
}
$if val is NullDecoder {
if struct_info.value_kind == .null {
val.from_json_null()
if decoder.current_node != unsafe { nil } {
decoder.current_node = decoder.current_node.next
}
return
}
}
// struct field info linked list
mut struct_fields_info := LinkedList[StructFieldInfo]{}