From 1bf7d968f935e8dd6c296eb460558672ae8d7da2 Mon Sep 17 00:00:00 2001 From: pancake Date: Thu, 25 Mar 2021 16:53:39 +0100 Subject: [PATCH] x.json2: add u64 (#9457) --- vlib/x/json2/decoder.v | 2 +- vlib/x/json2/encoder.v | 2 +- vlib/x/json2/json2.v | 9 +++++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/vlib/x/json2/decoder.v b/vlib/x/json2/decoder.v index dbf04c3397..5009b4f84c 100644 --- a/vlib/x/json2/decoder.v +++ b/vlib/x/json2/decoder.v @@ -4,7 +4,7 @@ module json2 // `Any` is a sum type that lists the possible types to be decoded and used. -pub type Any = Null | []Any | bool | f32 | f64 | i64 | int | map[string]Any | string +pub type Any = Null | []Any | bool | f32 | f64 | i64 | u64 | int | map[string]Any | string // `Null` struct is a simple representation of the `null` value in JSON. pub struct Null { diff --git a/vlib/x/json2/encoder.v b/vlib/x/json2/encoder.v index 63d5244836..9faeb70327 100644 --- a/vlib/x/json2/encoder.v +++ b/vlib/x/json2/encoder.v @@ -70,7 +70,7 @@ pub fn (f Any) json_str() string { int { return f.str() } - i64 { + u64, i64 { return f.str() } f32 { diff --git a/vlib/x/json2/json2.v b/vlib/x/json2/json2.v index 03c8ebdfcf..0e5012c016 100644 --- a/vlib/x/json2/json2.v +++ b/vlib/x/json2/json2.v @@ -71,6 +71,15 @@ pub fn (f Any) i64() i64 { } } +// u64 uses `Any` as a 64-bit unsigned integer. +pub fn (f Any) u64() u64 { + match f { + u64 { return f } + int, i64, f32, f64, bool { return u64(f) } + else { return 0 } + } +} + // f32 uses `Any` as a 32-bit float. pub fn (f Any) f32() f32 { match f {