Skip to content

Commit 0037305

Browse files
committed
fix json2 regressions and v2 sanitizer issue
1 parent 492c01f commit 0037305

3 files changed

Lines changed: 101 additions & 104 deletions

File tree

vlib/v2/types/checker.v

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -394,11 +394,7 @@ fn (c &Checker) qualify_type_name(name string) string {
394394
return '${c.cur_file_module}__${name}'
395395
}
396396

397-
fn (c &Checker) type_ref_name(expr ast.Expr, resolved Type) string {
398-
resolved_name := if type_data_ptr_is_nil(resolved) { '' } else { resolved.name() }
399-
if resolved_name != '' && resolved_name != 'void' {
400-
return resolved_name
401-
}
397+
fn (c &Checker) type_ref_name(expr ast.Expr) string {
402398
match expr {
403399
ast.Ident {
404400
return c.qualify_type_name(expr.name)
@@ -2379,7 +2375,7 @@ fn (mut c Checker) process_pending_struct_decls() {
23792375
// Union members may be aliases whose base structs are resolved later in
23802376
// process_pending_type_decls. Keep a named placeholder here so
23812377
// find_field_or_method can re-resolve the live type from scope.
2382-
embedded_name := c.type_ref_name(embedded_expr, embedded_type)
2378+
embedded_name := c.type_ref_name(embedded_expr)
23832379
if embedded_name != '' {
23842380
embedded << Struct{
23852381
name: embedded_name

vlib/x/json2/decode.v

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -316,61 +316,59 @@ fn get_dynamic_from_element[T](_t T) []T {
316316
// decode_value decodes a value from the JSON nodes.
317317
@[manualfree]
318318
fn (mut decoder Decoder) decode_value[T](mut val T) ! {
319-
// Custom Decoders (only for struct types)
320-
$if T.unaliased_typ is $struct {
321-
$if val is StringDecoder {
322-
struct_info := decoder.current_node.value
323-
324-
if struct_info.value_kind == .string {
325-
val.from_json_string(decoder.json[struct_info.position + 1..struct_info.position +
326-
struct_info.length - 1]) or {
327-
decoder.decode_error('${typeof(*val).name}: ${err.msg()}')!
328-
}
329-
if decoder.current_node != unsafe { nil } {
330-
decoder.current_node = decoder.current_node.next
331-
}
319+
// Custom Decoders
320+
$if val is StringDecoder {
321+
struct_info := decoder.current_node.value
332322

333-
return
323+
if struct_info.value_kind == .string {
324+
val.from_json_string(decoder.json[struct_info.position + 1..struct_info.position +
325+
struct_info.length - 1]) or {
326+
decoder.decode_error('${typeof(*val).name}: ${err.msg()}')!
327+
}
328+
if decoder.current_node != unsafe { nil } {
329+
decoder.current_node = decoder.current_node.next
334330
}
335-
}
336-
$if val is NumberDecoder {
337-
struct_info := decoder.current_node.value
338331

339-
if struct_info.value_kind == .number {
340-
val.from_json_number(decoder.json[struct_info.position..struct_info.position +
341-
struct_info.length]) or {
342-
decoder.decode_error('${typeof(*val).name}: ${err.msg()}')!
343-
}
344-
if decoder.current_node != unsafe { nil } {
345-
decoder.current_node = decoder.current_node.next
346-
}
332+
return
333+
}
334+
}
335+
$if val is NumberDecoder {
336+
struct_info := decoder.current_node.value
347337

348-
return
338+
if struct_info.value_kind == .number {
339+
val.from_json_number(decoder.json[struct_info.position..struct_info.position +
340+
struct_info.length]) or {
341+
decoder.decode_error('${typeof(*val).name}: ${err.msg()}')!
342+
}
343+
if decoder.current_node != unsafe { nil } {
344+
decoder.current_node = decoder.current_node.next
349345
}
350-
}
351-
$if val is BooleanDecoder {
352-
struct_info := decoder.current_node.value
353346

354-
if struct_info.value_kind == .boolean {
355-
val.from_json_boolean(decoder.json[struct_info.position] == `t`)
356-
if decoder.current_node != unsafe { nil } {
357-
decoder.current_node = decoder.current_node.next
358-
}
347+
return
348+
}
349+
}
350+
$if val is BooleanDecoder {
351+
struct_info := decoder.current_node.value
359352

360-
return
353+
if struct_info.value_kind == .boolean {
354+
val.from_json_boolean(decoder.json[struct_info.position] == `t`)
355+
if decoder.current_node != unsafe { nil } {
356+
decoder.current_node = decoder.current_node.next
361357
}
362-
}
363-
$if val is NullDecoder {
364-
struct_info := decoder.current_node.value
365358

366-
if struct_info.value_kind == .null {
367-
val.from_json_null()
368-
if decoder.current_node != unsafe { nil } {
369-
decoder.current_node = decoder.current_node.next
370-
}
359+
return
360+
}
361+
}
362+
$if val is NullDecoder {
363+
struct_info := decoder.current_node.value
371364

372-
return
365+
if struct_info.value_kind == .null {
366+
val.from_json_null()
367+
if decoder.current_node != unsafe { nil } {
368+
decoder.current_node = decoder.current_node.next
373369
}
370+
371+
return
374372
}
375373
}
376374
$if T.unaliased_typ is voidptr {
@@ -616,11 +614,6 @@ fn (mut decoder Decoder) decode_value[T](mut val T) ! {
616614
decoder.decode_value(mut unwrapped_val)!
617615
val.$(field.name) = unwrapped_val
618616
}
619-
} $else $if field.indirections != 0 {
620-
// skip pointer fields - cannot decode pointers from JSON
621-
if decoder.current_node != unsafe { nil } {
622-
decoder.current_node = decoder.current_node.next
623-
}
624617
} $else {
625618
decoder.decode_value(mut val.$(field.name))!
626619
}

vlib/x/json2/encode.v

Lines changed: 57 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -436,65 +436,73 @@ fn (mut encoder Encoder) encode_struct_fields[T](val T, was_first bool, old_used
436436

437437
mut write_field := true
438438

439-
$if field.indirections != 0 {
440-
// Pointer fields are skipped during JSON encoding to prevent
441-
// compile-time type walking through complex pointed-to types
439+
if field_info.is_skip {
442440
write_field = false
443-
} $else {
444-
if !field_info.is_skip {
445-
value := val.$(field.name)
446-
447-
if field_info.is_omitempty {
448-
$if value is $option {
449-
write_field = check_not_empty(value) or { false }
450-
} $else {
451-
write_field = check_not_empty(value) or { false }
452-
}
441+
} else {
442+
value := val.$(field.name)
443+
444+
if field_info.is_omitempty {
445+
$if value is $option {
446+
write_field = check_not_empty(value) or { false }
447+
} $else {
448+
write_field = check_not_empty(value) or { false }
453449
}
450+
}
454451

455-
if !field_info.is_required {
456-
$if value is $option {
457-
if value == none {
458-
write_field = false
459-
}
452+
if !field_info.is_required {
453+
$if value is $option {
454+
if value == none {
455+
write_field = false
460456
}
461457
}
458+
}
459+
}
462460

463-
if write_field {
464-
if is_first {
465-
if encoder.prettify {
466-
encoder.increment_level()
467-
}
468-
is_first = false
469-
} else {
470-
encoder.output << `,`
471-
}
472-
if encoder.prettify {
473-
encoder.add_indent()
474-
}
461+
$if field.indirections != 0 {
462+
if val.$(field.name) == unsafe { nil } {
463+
write_field = false
464+
}
465+
}
475466

476-
if field_info.key_name in old_used_keys {
477-
encoder.encode_string(prefix + field_info.key_name)
478-
} else {
479-
encoder.encode_string(field_info.key_name)
480-
used_keys << field_info.key_name
481-
}
467+
if write_field {
468+
if is_first {
469+
if encoder.prettify {
470+
encoder.increment_level()
471+
}
472+
is_first = false
473+
} else {
474+
encoder.output << `,`
475+
}
476+
if encoder.prettify {
477+
encoder.add_indent()
478+
}
482479

483-
encoder.output << `:`
484-
if encoder.prettify {
485-
encoder.output << ` `
486-
}
480+
if field_info.key_name in old_used_keys {
481+
encoder.encode_string(prefix + field_info.key_name)
482+
} else {
483+
encoder.encode_string(field_info.key_name)
484+
used_keys << field_info.key_name
485+
}
487486

488-
$if field is $option {
489-
if val.$(field.name) == none {
490-
unsafe { encoder.output.push_many(null_string.str, null_string.len) }
491-
} else {
492-
encoder.encode_value(val.$(field.name))
493-
}
494-
} $else {
495-
encoder.encode_value(val.$(field.name))
496-
}
487+
encoder.output << `:`
488+
if encoder.prettify {
489+
encoder.output << ` `
490+
}
491+
492+
$if field is $option {
493+
if val.$(field.name) == none {
494+
unsafe { encoder.output.push_many(null_string.str, null_string.len) }
495+
} else {
496+
encoder.encode_value(val.$(field.name))
497497
}
498+
} $else $if field.indirections == 1 {
499+
encoder.encode_value(*val.$(field.name))
500+
} $else $if field.indirections == 2 {
501+
encoder.encode_value(**val.$(field.name))
502+
} $else $if field.indirections == 3 {
503+
encoder.encode_value(***val.$(field.name))
504+
} $else {
505+
encoder.encode_value(val.$(field.name))
498506
}
499507
}
500508
}

0 commit comments

Comments
 (0)