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

More (binary) serializers support: protobuf, messagepack #305

Open
justmara opened this issue Dec 12, 2022 · 6 comments
Open

More (binary) serializers support: protobuf, messagepack #305

justmara opened this issue Dec 12, 2022 · 6 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@justmara
Copy link

justmara commented Dec 12, 2022

Describe the feature

These serializers (protobuf-net, messagepack-csharp) are fast binary ones, already stated as industry standards (like, protobuf is used in gRPC) and they require a bit special handling: members decoration with attributes, accessible constructor.
These two are pretty hard to be added to models, generated with current version of Vogen.
Messagepack can make use of [SerializationConstructor] attribute, for example.

PS: Or is it possible to deal with these using current Vogen version and I (maybe) missed some guide for it?

@justmara justmara added the enhancement New feature or request label Dec 12, 2022
@SteveDunn
Copy link
Owner

Hi - thanks for the feature suggestion. I think it'd make an excellent suggestion!

I'm not very familiar with this stuff though, so I'll do a bit of research and add them - hopefully within a week or so.

Thanks again!

@justmara
Copy link
Author

justmara commented Dec 12, 2022

I've dug a bit into it and looks like MessagePack formatter is very simple:

public class SkuIdMessagePackFormatter : IMessagePackFormatter<SkuId>
{
    public void Serialize(ref MessagePackWriter writer, SkuId value, MessagePackSerializerOptions options) => writer.WriteInt64(value.Value);
    
    public SkuId Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
    {
        if (reader.TryReadNil()) return SkuId.Unspecified; // this must be some more general for sure :)

        options.Security.DepthStep(ref reader);

        var value = reader.ReadInt64();

        reader.Depth--;
        return SkuId.From(value);
    }
}

But the protobuf one seems to be pointless overall - it will require adding this type to proto, protobuf-gen wont recognize that special type anyway... Looks like this conversion must be done manually/on mapping stage and does not require any special handling, so can be avoided :)

@justmara
Copy link
Author

Also there is very interesting MemoryPack that I wanted to try out, but it uses its own roslyn generator and it fails even building project where it and vogen are added at the same time :(

@SteveDunn
Copy link
Owner

Interesting! Many thanks, I'll take a look at those.

@SteveDunn SteveDunn added the help wanted Extra attention is needed label Jan 25, 2023
@DomasM
Copy link

DomasM commented Oct 24, 2023

protobuf-net can be used with Vogen types very easily if adding dependency to protobuf-net is OK, but this information is not easy to find imho. I guess it's possible to set surrogate for every required type at the runtime too, but this seems more convenient for my use case.

[ValueObject(typeof(string))]
[ProtoContract(Surrogate = typeof(string))]
public partial class BoxId {
//...
}

BoxId type now will be serialized as a string in all messages/grpc calls. If one is generating .proto files for other applications from C# code, proto files will include Surrogate type as the type.
@SteveDunn maybe this information should be included in readme?

@SteveDunn
Copy link
Owner

protobuf-net can be used with Vogen types very easily if adding dependency to protobuf-net is OK, but this information is not easy to find imho. I guess it's possible to set surrogate for every required type at the runtime too, but this seems more convenient for my use case.

[ValueObject(typeof(string))]
[ProtoContract(Surrogate = typeof(string))]
public partial class BoxId {
//...
}

BoxId type now will be serialized as a string in all messages/grpc calls. If one is generating .proto files for other applications from C# code, proto files will include Surrogate type as the type. @SteveDunn maybe this information should be included in readme?

Many thanks for the feedback @DomasM ! I'll certainly add that to the readme!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants