Skip to content

Problem with deserialize string as enum in MessagePack #1929

Description

@iguskov1810

Previously, we exchanged between servers using JSON. We decided to switch to MessagePack to save traffic and problems began with his capriciousness. The fact is that one server (Java) sends another enum as a string, but on this other server the Enum is not a string, but real Enum.

Json calmly handled such cases, but MessagePack did not.

Are there standard ways to make the code below work?

    public enum MyEnum
    {
        [EnumMember(Value = "A")]
        A,
        [EnumMember(Value = "B")]
        B
    }
[MessagePackObject]
class TestClass1
{
    [Key("Values")]
    public List<string> Values { get; set; }
}

[MessagePackObject]
class TestClass2
{
    [Key("Values")]
    public List<MyEnum> Values { get; set; }
}
public static readonly MessagePackSerializerOptions SerializerOptions =
      MessagePackSerializerOptions.Standard
          .WithResolver(CompositeResolver.Create(
              BuiltinResolver.Instance,
              StandardResolver.Instance
          ))
      ;

var testClass1 = new TestClass1() { Values = ["A"] };
var data = MessagePackSerializer.Serialize(TestClass1, SerializerOptions);
var TestClass2= MessagePackSerializer.Deserialize<TestClass2>(data, SerializerOptions);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions