Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json: allow i32 decoding and encoding #21162

Merged
merged 3 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
}
spytheman marked this conversation as resolved.
Show resolved Hide resolved
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')
}
spytheman marked this conversation as resolved.
Show resolved Hide resolved
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