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

mixed fails to serialize objects #23

Open
withinboredom opened this issue Jun 6, 2023 · 7 comments
Open

mixed fails to serialize objects #23

withinboredom opened this issue Jun 6, 2023 · 7 comments
Labels
bug Something isn't working

Comments

@withinboredom
Copy link
Contributor

withinboredom commented Jun 6, 2023

Detailed description

When an object contains a mixed property containing an object, the property fails to serialize in MixedExporter due to not being an array, int, float, bool, or string.

Context

It would be nice to serialize generic objects.

Workaround

The workaround is to call $serde->serialize(...) on the mixed property and then serialize the parent object.

@Crell Crell added the bug Something isn't working label Aug 28, 2023
@Crell
Copy link
Owner

Crell commented Oct 17, 2023

The problem here is that we will never be able to deserialize an object to mixed, as there's no indication of what the type is. Serialization is possible, but you're really better off specifying a real type in PHP.

@withinboredom
Copy link
Contributor Author

withinboredom commented Oct 17, 2023

Deserialization isn't an issue (the type is identified correctly by the deserializer through ClassNameTypeMap and friends). However, serialization is failing here.

Use-case: basically a wrapper containing metadata around a (de)serializable domain object.

@Crell
Copy link
Owner

Crell commented Oct 17, 2023

On a lark, try the master branch. I pushed some cleanup yesterday that may have made objects work by accident. I just didn't write tests for it yet to be sure. 😄

@Crell
Copy link
Owner

Crell commented Oct 20, 2023

OK, tested it, and it won't work. The problem is the refactored approach sends the value to serialize back through the serializer, which means the same object goes through Serializer::serialize() twice. Which... trips the circular dependency detection. :-) I don't know how to resolve that at this point, so unless someone has an idea it likely won't make it into the 1.0 release this month.

@withinboredom
Copy link
Contributor Author

If it is mixed, and an object, can't it get the type via get_debug_type() for serialization? https://3v4l.org/iahAR Then make a decision from there?

@Crell
Copy link
Owner

Crell commented Oct 22, 2023

Yes, but there's a lot of prep-work involved in preparing an object for exporting. ObjectExporter is one of the more involved implementations. It's not reasonable to duplicate that logic in MixedExporter, and technically no exporter knows about another exporter explicitly, by design. If they did, that would introduce complicated dependencies within the exporters, which so far I've managed to avoid (AFAIK).

So what it does right now is use get_debug_type() to set the type and route back through the serializer, so whatever exporter is configured will catch it and work normally.

Which is what triggers the circular dependency detection.

@withinboredom
Copy link
Contributor Author

Ah, I see. FWIW, my workaround of simply serializing the mixed object to an array and then serializing the wrapping object into whatever seems to work fine. It takes a bit of extra work, but if you're wrapping an object with an object, you're already in a position to do that.

So, maybe this just needs to be documented so that it isn't a surprise?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants