| 1 | import x.json2 |
| 2 | |
| 3 | fn decode_any_issue_24128(j string) !json2.Any { |
| 4 | return json2.decode[json2.Any](j) |
| 5 | } |
| 6 | |
| 7 | fn decode_map_issue_24128(j string) !map[string]json2.Any { |
| 8 | return json2.decode[map[string]json2.Any](j) |
| 9 | } |
| 10 | |
| 11 | fn test_decode_any_and_map_any_in_same_program() { |
| 12 | for payload in [ |
| 13 | '{}', |
| 14 | '{"a":"b"}', |
| 15 | '{"a":1}', |
| 16 | '{"a":3.14}', |
| 17 | '{"a":{"b":"c"}}', |
| 18 | '{"a":true}', |
| 19 | ] { |
| 20 | decoded_any := decode_any_issue_24128(payload)! |
| 21 | decoded_map := decode_map_issue_24128(payload)! |
| 22 | |
| 23 | match decoded_any { |
| 24 | map[string]json2.Any { |
| 25 | assert decoded_any.str() == decoded_map.str() |
| 26 | } |
| 27 | else { |
| 28 | assert false |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | fn test_decode_map_any_keeps_numeric_values() { |
| 35 | payload := { |
| 36 | 'sub': json2.Any('453636') |
| 37 | 'aud': json2.Any('Coachonko') |
| 38 | 'iss': json2.Any('Coachonko') |
| 39 | 'nbf': json2.Any(1688229809) |
| 40 | 'iat': json2.Any(1688229809) |
| 41 | 'jti': json2.Any('e0a26a81-9cc0-4f52-b672-4f6f3d3b44a5') |
| 42 | 'exp': json2.Any(1688273009) |
| 43 | } |
| 44 | encoded := json2.encode(payload) |
| 45 | decoded := json2.decode[map[string]json2.Any](encoded)! |
| 46 | |
| 47 | assert decoded.len == payload.len |
| 48 | assert 'sub' in decoded |
| 49 | assert 'aud' in decoded |
| 50 | assert 'iss' in decoded |
| 51 | assert 'nbf' in decoded |
| 52 | assert 'iat' in decoded |
| 53 | assert 'jti' in decoded |
| 54 | assert 'exp' in decoded |
| 55 | |
| 56 | assert decoded['sub']!.str() == '453636' |
| 57 | assert decoded['aud']!.str() == 'Coachonko' |
| 58 | assert decoded['iss']!.str() == 'Coachonko' |
| 59 | assert decoded['jti']!.str() == 'e0a26a81-9cc0-4f52-b672-4f6f3d3b44a5' |
| 60 | assert decoded['nbf']!.f64() == f64(1688229809) |
| 61 | assert decoded['iat']!.f64() == f64(1688229809) |
| 62 | assert decoded['exp']!.f64() == f64(1688273009) |
| 63 | } |
| 64 | |