File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -946,6 +946,7 @@ fn (mut decoder Decoder) decode_number[T](val &T) ! {
946946 }
947947
948948 // doing it like this means the minimum of signed numbers does not overflow before being inverted
949+ * val = 0 // initialize to zero before accumulating digits
949950 if ! is_negative {
950951 digit_amount := get_number_digits (* val)
951952
Original file line number Diff line number Diff line change 1+ import x.json2
2+
3+ struct TestStruct {
4+ age u16 = 25
5+ }
6+
7+ fn test_u16_in_struct () {
8+ original := TestStruct{}
9+ encoded := json2 .encode (original)
10+ println ('Encoded: ${encoded} ' )
11+
12+ decoded := json2 .decode[TestStruct](encoded) or { panic ('Failed to decode: ${err} ' ) }
13+ println ('Decoded: ${decoded} ' )
14+ println ('Age value: ${decoded.age} ' )
15+
16+ assert decoded.age == 25 , 'Expected age 25, got ${decoded.age} '
17+ }
18+
19+ fn test_u16_direct () {
20+ decoded := json2 .decode[u16 ]('25' ) or { panic ('Failed: ${err} ' ) }
21+ println ('Direct u16 decode of "25": ${decoded} ' )
22+ assert decoded == 25 , 'Expected 25, got ${decoded} '
23+ }
24+
25+ fn main () {
26+ test_u16_direct ()
27+ test_u16_in_struct ()
28+ println ('All tests passed!' )
29+ }
You can’t perform that action at this time.
0 commit comments