mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
json: custom json field names with "[json:field_name]"
This commit is contained in:
parent
7086547471
commit
e39cb41d6b
@ -85,15 +85,17 @@ string res = tos2("");
|
|||||||
if field.attr == 'skip' {
|
if field.attr == 'skip' {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
name := if field.attr.starts_with('json:') {
|
||||||
|
field.attr.right(5)
|
||||||
|
} else {
|
||||||
|
field.name
|
||||||
|
}
|
||||||
field_type := p.table.find_type(field.typ)
|
field_type := p.table.find_type(field.typ)
|
||||||
name := field.name
|
|
||||||
_typ := field.typ.replace('*', '')
|
_typ := field.typ.replace('*', '')
|
||||||
|
|
||||||
enc_name := js_enc_name(_typ)
|
enc_name := js_enc_name(_typ)
|
||||||
|
|
||||||
if field.attr == 'raw' {
|
if field.attr == 'raw' {
|
||||||
dec += ' /*prim*/ res->$name = tos2(cJSON_PrintUnformatted(js_get(root, "$field.name")));\n'
|
dec += ' res->$field.name = tos2(cJSON_PrintUnformatted(' +
|
||||||
|
'js_get(root, "$name")));\n'
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Now generate decoders for all field types in this struct
|
// Now generate decoders for all field types in this struct
|
||||||
@ -103,16 +105,15 @@ string res = tos2("");
|
|||||||
dec_name := js_dec_name(_typ)
|
dec_name := js_dec_name(_typ)
|
||||||
|
|
||||||
if is_js_prim(_typ) {
|
if is_js_prim(_typ) {
|
||||||
dec += ' /*prim*/ res->$name = $dec_name(js_get(root, "$field.name"))'
|
dec += ' res->$field.name = $dec_name(js_get(' +
|
||||||
// dec += '.data'
|
'root, "$name"))'
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
dec += ' /*!!*/ $dec_name(js_get(root, "$field.name"), & (res->$name))'
|
dec += ' $dec_name(js_get(root, "$name"), & (res->$name))'
|
||||||
}
|
}
|
||||||
dec += ';\n'
|
dec += ';\n'
|
||||||
}
|
}
|
||||||
|
enc += ' cJSON_AddItemToObject(o, "$name",$enc_name(val.$field.name)); \n'
|
||||||
enc += ' cJSON_AddItemToObject(o, "$name", $enc_name(val.$name)); \n'
|
|
||||||
}
|
}
|
||||||
// cJSON_delete
|
// cJSON_delete
|
||||||
//p.cgen.fns << '$dec return opt_ok(res); \n}'
|
//p.cgen.fns << '$dec return opt_ok(res); \n}'
|
||||||
|
@ -645,6 +645,10 @@ fn (p mut Parser) struct_decl() {
|
|||||||
if p.tok == .lsbr {
|
if p.tok == .lsbr {
|
||||||
p.next()
|
p.next()
|
||||||
attr = p.check_name()
|
attr = p.check_name()
|
||||||
|
if p.tok == .colon {
|
||||||
|
p.check(.colon)
|
||||||
|
attr += ':' + p.check_name()
|
||||||
|
}
|
||||||
p.check(.rsbr)
|
p.check(.rsbr)
|
||||||
}
|
}
|
||||||
if attr == 'raw' && field_type != 'string' {
|
if attr == 'raw' && field_type != 'string' {
|
||||||
|
@ -1,16 +1,18 @@
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
struct User {
|
struct User {
|
||||||
age int
|
age int
|
||||||
nums []int
|
nums []int
|
||||||
|
last_name string [json:lastName]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_parse_user() {
|
fn test_parse_user() {
|
||||||
s := '{"age": 10, "nums": [1,2,3]}'
|
s := '{"age": 10, "nums": [1,2,3], "lastName": "Johnson"}'
|
||||||
u := json.decode(User, s) or {
|
u := json.decode(User, s) or {
|
||||||
exit(1)
|
exit(1)
|
||||||
}
|
}
|
||||||
assert u.age == 10
|
assert u.age == 10
|
||||||
|
assert u.last_name == 'Johnson'
|
||||||
assert u.nums.len == 3
|
assert u.nums.len == 3
|
||||||
assert u.nums[0] == 1
|
assert u.nums[0] == 1
|
||||||
assert u.nums[1] == 2
|
assert u.nums[1] == 2
|
||||||
|
Loading…
Reference in New Issue
Block a user