v/vlib/x/json2/decoder2/tests/decode_custom_test.v
Larsimusrex d249bfb04e
Some checks failed
wasm backend CI / wasm-backend (windows-2022) (push) Has been cancelled
json decoder benchmark CI / json-encode-benchmark (push) Has been cancelled
Graphics CI / gg-regressions (push) Has been cancelled
vlib modules CI / build-module-docs (push) Has been cancelled
native backend CI / native-backend-ubuntu (push) Has been cancelled
native backend CI / native-backend-windows (push) Has been cancelled
Shy and PV CI / v-compiles-puzzle-vibes (push) Has been cancelled
Sanitized CI / sanitize-undefined-clang (push) Has been cancelled
Sanitized CI / sanitize-undefined-gcc (push) Has been cancelled
Sanitized CI / tests-sanitize-address-clang (push) Has been cancelled
Sanitized CI / sanitize-address-msvc (push) Has been cancelled
Sanitized CI / sanitize-address-gcc (push) Has been cancelled
Sanitized CI / sanitize-memory-clang (push) Has been cancelled
sdl CI / v-compiles-sdl-examples (push) Has been cancelled
Time CI / time-linux (push) Has been cancelled
Time CI / time-macos (push) Has been cancelled
Time CI / time-windows (push) Has been cancelled
toml CI / toml-module-pass-external-test-suites (push) Has been cancelled
Tools CI / tools-linux (clang) (push) Has been cancelled
Tools CI / tools-linux (gcc) (push) Has been cancelled
Tools CI / tools-linux (tcc) (push) Has been cancelled
Tools CI / tools-macos (clang) (push) Has been cancelled
Tools CI / tools-windows (gcc) (push) Has been cancelled
Tools CI / tools-windows (msvc) (push) Has been cancelled
Tools CI / tools-windows (tcc) (push) Has been cancelled
Tools CI / tools-docker-ubuntu-musl (push) Has been cancelled
vab CI / vab-compiles-v-examples (push) Has been cancelled
vab CI / v-compiles-os-android (push) Has been cancelled
wasm backend CI / wasm-backend (ubuntu-22.04) (push) Has been cancelled
decoder2: fix remaining json2 discrepancies (#25029)
2025-08-03 10:27:43 +03:00

99 lines
2.5 KiB
V

import decoder2 as json
import x.json2
import math.big
struct MyString implements json.StringDecoder, json.NumberDecoder, json.BooleanDecoder, json.NullDecoder {
mut:
data string
}
pub fn (mut ms MyString) from_json_string(raw_string string) ! {
ms.data = raw_string
}
pub fn (mut ms MyString) from_json_number(raw_number string) ! {
mut first := true
for digit in raw_number {
if first {
first = false
} else {
ms.data += '-'
}
ms.data += match digit {
`-` { 'minus' }
`.` { 'dot' }
`e`, `E` { 'e' }
`0` { 'zero' }
`1` { 'one' }
`2` { 'two' }
`3` { 'three' }
`4` { 'four' }
`5` { 'five' }
`6` { 'six' }
`7` { 'seven' }
`8` { 'eight' }
`9` { 'nine' }
else { 'none' }
}
}
}
pub fn (mut ms MyString) from_json_boolean(boolean_value bool) {
ms.data = if boolean_value { 'yes' } else { 'no' }
}
pub fn (mut ms MyString) from_json_null() {
ms.data = 'default value'
}
struct NoCustom {
a int
b string
}
type MyString2 = string
pub fn (mut ms MyString2) from_json_string(raw_string string) ! {
ms = raw_string.replace('-', '')
}
fn test_custom() {
assert json.decode[NoCustom]('{"a": 99, "b": "hi"}')! == NoCustom{
a: 99
b: 'hi'
}
assert json.decode[[]MyString]('["hi", -9.8e7, true, null]')! == [
MyString{
data: 'hi'
},
MyString{
data: 'minus-nine-dot-eight-e-seven'
},
MyString{
data: 'yes'
},
MyString{
data: 'default value'
},
]
}
fn test_null() {
assert json.decode[json2.Any]('null]')! == json2.Any(json2.null)
assert json.decode[json2.Any]('{"hi": 90, "bye": ["lol", -1, null]}')!.str() == '{"hi":90,"bye":["lol",-1,null]}'
}
fn test_big() {
assert json.decode[big.Integer]('0')!.str() == '0'
assert json.decode[big.Integer]('12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890')!.str() == '12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890'
assert json.decode[big.Integer]('-12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890')!.str() == '-12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890'
}
fn test_alias() {
assert json.decode[MyString2]('"h-e---l-lo"')! == 'hello'
}