8000 Should `toJson` be called for null classes? · Issue #16 · vibe-d/vibe-serialization · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Should toJson be called for null classes? #16

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

Open
VPanteleev-S7 opened this issue May 15, 2025 · 2 comments
Open

Should toJson be called for null classes? #16

VPanteleev-S7 opened this issue May 15, 2025 · 2 comments

Comments

@VPanteleev-S7

Copy link
Contributor
/+dub.sdl:
 dependency "vibe-serialization" version="*"
+/
import std.stdio;
import vibe.data.json;

@safe:

class C {
	final Json toJson() const @trusted {
		debug stderr.writeln("C.toJson: ", cast(void*)this);
		return Json(42);
	}
	static typeof(this) fromJson(Json json) @safe {
		return new C;
	}
}


void main() {
	C a = null;
	debug writeln(serializeToJson(a));
}

Looks like, if toJson is defined, it is called even when the class instance is null.

I'm not sure if this is intentional - looks like it might be a potential footgun. For instance, we have to be careful that toJson is not virtual.

@VPanteleev-S7
Copy link
Contributor Author

ae.utils.json has the same issue 🙈

@s-ludwig
Copy link
Member

Since the normal path explicitly checks for null, definitely looks like an oversight.

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

No branches or pull requests

2 participants
0