Skip to content

Commit

Permalink
json: allow i32 decoding and encoding (#21162)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Apr 2, 2024
1 parent c086bee commit 656009d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
16 changes: 16 additions & 0 deletions vlib/json/json_i32_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import json

pub struct StructB {
kind string
value i32
}

fn test_json_i32() {
struct_b := json.decode(StructB, '{"kind": "Int32", "value": 100}')!
assert struct_b == StructB{
kind: 'Int32'
value: 100
}

assert json.encode(struct_b) == '{"kind":"Int32","value":100}'
}
14 changes: 10 additions & 4 deletions vlib/v/gen/c/json.v
Original file line number Diff line number Diff line change
Expand Up @@ -902,20 +902,26 @@ fn gen_js_get_opt(dec_name string, field_type string, styp string, tmp string, n
}

fn js_enc_name(typ string) string {
suffix := typ.replace('*', '_ptr')
mut suffix := typ.replace('*', '_ptr')
if typ == 'i32' {
suffix = typ.replace('i32', 'int')
}
name := 'json__encode_${suffix}'
return util.no_dots(name)
}

fn js_dec_name(typ string) string {
suffix := typ.replace('*', '_ptr')
mut suffix := typ.replace('*', '_ptr')
if typ == 'i32' {
suffix = typ.replace('i32', 'int')
}
name := 'json__decode_${suffix}'
return util.no_dots(name)
}

fn is_js_prim(typ string) bool {
return typ in ['int', 'rune', 'string', 'bool', 'f32', 'f64', 'i8', 'i16', 'i64', 'u8', 'u16',
'u32', 'u64', 'byte']
return typ in ['int', 'rune', 'string', 'bool', 'f32', 'f64', 'i8', 'i16', 'i32', 'i64', 'u8',
'u16', 'u32', 'u64', 'byte']
}

fn (mut g Gen) decode_array(utyp ast.Type, value_type ast.Type, fixed_array_size int, ret_styp string) string {
Expand Down

0 comments on commit 656009d

Please sign in to comment.