From 5df31c16f7d372715e97c6a2acd1b3cceef1291f Mon Sep 17 00:00:00 2001 From: Robin Martijn Date: Mon, 26 Aug 2019 17:01:23 +0200 Subject: [PATCH] json: fix #1751; support unsigned integers --- compiler/jsgen.v | 3 ++- vlib/json/json_primitives.v | 45 +++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/compiler/jsgen.v b/compiler/jsgen.v index 7e547a9b5d..d752e3b52e 100644 --- a/compiler/jsgen.v +++ b/compiler/jsgen.v @@ -124,7 +124,8 @@ string res = tos2(""); fn is_js_prim(typ string) bool { return typ == 'int' || typ == 'string' || typ == 'bool' || typ == 'f32' || typ == 'f64' || - typ == 'i8' || typ == 'i16' || typ == 'i32' || typ == 'i64' + typ == 'i8' || typ == 'i16' || typ == 'i32' || typ == 'i64' || + typ == 'u8' || typ == 'u16' || typ == 'u32' || typ == 'u64' } fn (p mut Parser) decode_array(array_type string) string { diff --git a/vlib/json/json_primitives.v b/vlib/json/json_primitives.v index ca92cd88b2..9b62093160 100644 --- a/vlib/json/json_primitives.v +++ b/vlib/json/json_primitives.v @@ -49,6 +49,34 @@ fn jsdecode_i64(root *C.cJSON) i64 { return i64(root.valuedouble) //i64 is double in C } +fn jsdecode_u8(root *C.cJSON) u8 { + if isnil(root) { + return u8(0) + } + return u8(root.valueint) +} + +fn jsdecode_u16(root *C.cJSON) u16 { + if isnil(root) { + return u16(0) + } + return u16(root.valueint) +} + +fn jsdecode_u32(root *C.cJSON) u32 { + if isnil(root) { + return u32(0) + } + return u32(root.valueint) +} + +fn jsdecode_u64(root *C.cJSON) u64 { + if isnil(root) { + return u64(0) + } + return u64(root.valueint) +} + fn jsdecode_f32(root *C.cJSON) f32 { if isnil(root) { return f32(0) @@ -95,6 +123,7 @@ fn jsencode_i8(val i8) *C.cJSON { fn jsencode_i16(val i16) *C.cJSON { return C.cJSON_CreateNumber(val) } + fn jsencode_i32(val i32) *C.cJSON { return C.cJSON_CreateNumber(val) } @@ -103,6 +132,22 @@ fn jsencode_i64(val i64) *C.cJSON { return C.cJSON_CreateNumber(val) } +fn jsencode_u8(val u8) *C.cJSON { + return C.cJSON_CreateNumber(val) +} + +fn jsencode_u16(val u16) *C.cJSON { + return C.cJSON_CreateNumber(val) +} + +fn jsencode_u32(val u32) *C.cJSON { + return C.cJSON_CreateNumber(val) +} + +fn jsencode_u64(val u64) *C.cJSON { + return C.cJSON_CreateNumber(val) +} + fn jsencode_f32(val f32) *C.cJSON { return C.cJSON_CreateNumber(val) }