Skip to content

Conversation

get200
Copy link
Contributor

@get200 get200 commented Sep 3, 2025

Fix issue #25237

Copy link

Connected to Huly®: V_0.6-24681

@get200
Copy link
Contributor Author

get200 commented Sep 5, 2025

// D:\\v\\v\\vlib\\json\\tests\\json_option_struct_test.v
import json

pub struct SomeStruct {
pub mut:
	test ?string
}

pub struct MyStruct {
pub mut:
	result ?SomeStruct
	id     string
}

fn test_main() {
	a := MyStruct{
		id:     'some id'
		result: SomeStruct{}
	}
	encoded_string := json.encode(a)
	assert encoded_string == '{"result":{},"id":"some id"}'
	test := json.decode(MyStruct, encoded_string)!
	dump(test)
	dump(a)
	dump(test == a)
	assert test == a
}

$ v run D:\v\v\vlib\json\tests\json_option_struct_test.v               
[D:\\v\\v\\vlib\\json\\tests\\json_option_struct_test.v:22] test: MyStruct{
    result: Option(none)
    id: 'some id'
}
[D:\\v\\v\\vlib\\json\\tests\\json_option_struct_test.v:23] a: MyStruct{
    result: Option(SomeStruct{
        test: Option(none)
    })
    id: 'some id'
}
[D:\\v\\v\\vlib\\json\\tests\\json_option_struct_test.v:24] test == a: true

@spytheman may be this is a bug?

…change default json.decode behavior for reduce ambiguity '{}' will be default type value and 'null' will be none value for option value
…change default json.decode behavior for reduce ambiguity '{}' will be default type value and 'null' will be none value for option value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant