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

json2: add compatibility of @[skip] in json2 #22077

Merged
merged 2 commits into from
Aug 19, 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
3 changes: 3 additions & 0 deletions vlib/x/json2/decoder.v
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ fn decode_struct[T](_ T, res map[string]Any) !T {
mut json_name := field.name

for attr in field.attrs {
if attr.contains('skip') {
skip_field = true
}
if attr.contains('json: ') {
json_name = attr.replace('json: ', '')
if json_name == '-' {
Expand Down
6 changes: 6 additions & 0 deletions vlib/x/json2/encoder.v
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ fn (e &Encoder) encode_struct[U](val U, level int, mut buf []u8) ! {
$for field in U.fields {
mut @continue := false
for attr in field.attrs {
if attr.contains('skip') {
@continue = true
}
if attr.contains('json: ') {
if attr.replace('json: ', '') == '-' {
@continue = true
Expand Down Expand Up @@ -227,6 +230,9 @@ fn (e &Encoder) encode_struct[U](val U, level int, mut buf []u8) ! {
mut json_name := ''

for attr in field.attrs {
if attr.contains('skip') {
ignore_field = true
}
if attr.contains('json: ') {
json_name = attr.replace('json: ', '')
if json_name == '-' {
Expand Down
34 changes: 34 additions & 0 deletions vlib/x/json2/tests/decode_struct_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ mut:
val map[string]string @[json: '-']
}

struct StructTypeSkippedFields5[T] {
mut:
val T @[skip]
val1 T @[skip]
val2 T @[skip]
val3 T @[skip]
}

struct StructTypeSkippedFields6[T] {
mut:
val T
val1 T @[skip]
val2 T
val3 T @[skip]
}

fn test_types() {
assert json.decode[StructType[string]]('{"val": ""}')!.val == ''
assert json.decode[StructType[string]]('{"val": "0"}')!.val == '0'
Expand Down Expand Up @@ -188,4 +204,22 @@ fn test_skipped_fields() {
} else {
assert false
}

if x := json.decode[StructTypeSkippedFields5[int]]('{"val":10,"val1":10,"val2":10,"val3":10}') {
assert x.val == 0
assert x.val1 == 0
assert x.val2 == 0
assert x.val3 == 0
} else {
assert false
}

if x := json.decode[StructTypeSkippedFields6[int]]('{"val":10,"val1":10,"val2":10,"val3":10}') {
assert x.val == 10
assert x.val1 == 0
assert x.val2 == 10
assert x.val3 == 0
} else {
assert false
}
}
14 changes: 14 additions & 0 deletions vlib/x/json2/tests/encode_struct_skippable_fields_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ mut:
val3 time.Time @[json: '-']
}

struct StructTypeSkippedFields9 {
mut:
val string
val1 i64 @[skip]
val2 f64
val3 time.Time @[skip]
}

fn test_encode_struct_skipped_fields() {
assert json.encode(StructTypeSkippedFields[string]{
val: 'string_val'
Expand Down Expand Up @@ -131,4 +139,10 @@ fn test_encode_struct_skipped_fields() {
val1: 1
val2: 1.0
}) == '{"val":"string_val","val2":1.0}'

assert json.encode(StructTypeSkippedFields9{
val: 'string_val'
val1: 1
val2: 1.0
}) == '{"val":"string_val","val2":1.0}'
}
Loading