-
-
Notifications
You must be signed in to change notification settings - Fork 111
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
Optimize JSON (De)serialization (Final Types et al) #482
Comments
Note that if |
I would suggest that the "final" feature is not needed for this, and you could get the desired savings in more situations via a more general mechanism. Since objects are naturally processed and validated recursively, you should already know the parent's type when you encounter a field value. From the parent's type definition, you already know the declared type of the field. $class could simply be omitted wherever it matches the declared field type. Also, when only the type name within the $class differs from the declared field type, the namespace and version could be omitted, leaving an abbreviated $class field containing only the type name. For example:
The $class in the owner could simply be "HumanOwner" or "CorporateOwner". The namespace and version would be assumed to match that of the Owner type in that case. I opened #542 to represent this broader mechanism, as this issue was still specific to the "final" feature last I looked. |
Note that type determination based on the type of the property only works if you assume that the model doesn't change between when the instance was serialised and when it was deserialised. For example, if version 1 of the model is:
It is assumed that no $class is required, because the Version 2 of the model is:
A $class is now required as a type discriminator to deserialise the The advantage of the Reading #542 I note your suggestion that if |
I don't think that's true. It should work regardless. The $class of the top-level object - which is always required - declares the exact version of the top-level object that you're dealing with. If it's v1 then you know you're in the first case. If it's v2 then you know you're in the second case. There's no ambiguity or assumptions about the model not changing. (Furthermore, I don't see why a $class would be required even in your v2 model, at least according to my proposed rule. Given that Owner is not abstract, a $class-less owner would still be legal and unambiguous. It would only become required if Owner became abstract.) As for your final comment, isn't the whole system based on the assumption that specific model versions are immutable? A specific model version [email protected] should be identical during serialization and deserialization. If people are mutating models without changing the version then all kinds of things will break. I assume there is some fundamental misunderstanding here. Can you post an example model and data that is ambiguous or interesting according to the rules in #542 ? |
It seems that your understanding of my proposal is that we allow $class to be omitted when and only when there's exactly one concrete type that a field could possibly be, so that adding more possible types suddenly makes $class required where it wasn't before. Something like this:
I agree that would be a bad idea, but it's not my proposal. My proposal is much simpler and more generally applicable. It allows $class to be omitted whenever a field type is not abstract, and when omitted, it is understood to be the exact concrete type the field is declared as. To revisit your v2 example above:
I don't believe there is any form of model evolution for which this rule causes problems, assuming you're not mutating existing models without changing their versions, which can break all kinds of things. But if you have an example, I'd be glad to see it. As for making such changes (to whether $class is required) obvious to the modeler, in my example the modeler would have to add or remove the |
This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 10 days. |
Return from whence thou came, bot. |
Feature Request 🛍️
Use Case
Allow modellers to declare that types are "final" and cannot be extended. This allows for optimisation of the serialised JSON, as an array of final types can no longer be polymorphic.
The $class is required in serialised instances because concepts can be extended. We could support the addition of a final keyword which would indicate that a concept could not be extended, meaning that the type of an object can be inferred by introspecting the model.
Possible Solution
Context
Detailed Description
The text was updated successfully, but these errors were encountered: