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

Support type aliases #19

Closed
mookid8000 opened this issue Mar 3, 2016 · 5 comments
Closed

Support type aliases #19

mookid8000 opened this issue Mar 3, 2016 · 5 comments

Comments

@mookid8000
Copy link

When I use Newtonsoft JSON.NET, I usually end up with a custom binder that allows me to configure short type names for the types I know will be serialized frequently.

This results in reduced overhead, as e.g. a simple Date value type otherwise serialized as

{
    "$type": "SomeProject.SomeComponent.SomeNamespace.Values.Date, SomeProject.SomeComponent",
    "Day": 3,
    "Month": 3,
    "Year": 2016
}

can become something like this:

{
    "$type": "*Date",
    "Day": 3,
    "Month": 3,
    "Year": 2016
}

If I understand correctly, Wire would serialize my Date type like this:

SomeProject.SomeComponent.SomeNamespace.Values.Date, SomeProject.SomeComponent
08 # int
0003
08 # int
0003
08 # int
07E0

which - if I could configure a type alias called *Date would be much more compact:

*Date
08 # int
0003
08 # int
0003
08 # int
07E0

How do you like this idea?

@rogeralsing
Copy link
Collaborator

There is actually a built in way that might solve this with little changes to Wire.
Each time a type name is emitted, it is assigned an index and every time the same type is emitted again in the same stream, it will simply emit the type index instead.

By pre-filling the typename/index lookup, we could acheive the same effect with better compression.
That being said, all primitive/standard types already support that kind of indexing out of the box.
e.g. DateTime is emitted as 05 (see https://github.com/rogeralsing/Wire/blob/master/Wire/ValueSerializers/DateTimeSerializer.cs)

But it would absolutely be possible to prefill the custom user type lookup.
We just need a way to initialize that lookup when creating the serializer.

@mookid8000
Copy link
Author

I was hoping that you would say something like that.... and yes, I also got the feeling from the code that the design had all the necessary building blocks already.

I'll take a look at it when I need a break 😁

@rogeralsing
Copy link
Collaborator

Here is the lookup that handles types to index lookup
https://github.com/rogeralsing/Wire/blob/master/Wire/SerializerSession.cs#L9
And here is the reverse for deserialization https://github.com/rogeralsing/Wire/blob/master/Wire/DeserializeSession.cs#L9

Those lists needs to be prefilled, the index is sequential so the user should really only supply an IEnumerable of types in the serializer options IMO.
And then those types needs to be added to the above lookups with and incrementation of https://github.com/rogeralsing/Wire/blob/master/Wire/SerializerSession.cs#L12

I think that would solve it, there could be more to it, but at the top of my head, that is it.

@mellinoe
Copy link

I'm also having some difficulties converting my serialization from Newtonsoft without this. In my case, I actually need to do some additional logic to get a System.Type from a type name, because my project loads certain assemblies into a different AssemblyLoadContext. If a type is being deserialized from such an assembly, I need to use Assembly.GetType(), and fall back to regular Type.GetType.

@generatives
Copy link

I am running into a similar issue as @mellinoe. My assemblies are being loaded manually from byte arrays by a framework (https://duality.adamslair.net/) and Type.GetType causes the assembly to be reloaded from a file. The different contexts cause an exception when Wire tries to deserialize.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants