Skip to content

Conversation

kengwang
Copy link
Contributor

@kengwang kengwang commented Sep 16, 2025

Motivation

Since the PbElem field of CommonElem(53) contains actual data, the ServiceType field is used to distinguish the type of data contained therein.

The TLVs also have this design model style.

It is hoped that the data structure can reflect this model design, making it easier to convert during serialization and deserialization.

Current

(Not Implemented CommonElem in V2)
In Lagrange.Core, directly read to a byte array and re-serialize.

Reference

Reference STJ's JsonDerivedTypeAttribute and JsonPolymorphicAttribute structures to achieve polymorphism.

Example

See Lagrange.Proto.Test/ProtoPolymorphismTest.cs

[ProtoPolymorphic(FieldNumber = 1)]
[ProtoDerivedType(typeof(DerivedClassA), 2)]
[ProtoDerivedType(typeof(DerivedClassB), 3)]
public class BaseClass
{
    public BaseClass() : this(-1) { }

    public BaseClass(int identifier)
    {
        IdentifierProperty = identifier;
    }

    [ProtoMember(1)] public int IdentifierProperty { get; set; }
}

public class DerivedClassA() : BaseClass(2)
{
    [ProtoMember(2)] public string NameProperty { get; set; }
}

public class DerivedClassB() : BaseClass(3)
{
    [ProtoMember(2)] public float ValueProperty { get; set; }
}

BaseClass original = new DerivedClassA { NameProperty = "TestName" };
byte[] bytes = ProtoSerializer.Serialize(original);
BaseClass deserialized = ProtoSerializer.Deserialize<BaseClass>(bytes);

To-do

  • Implement in Lagrange.Proto.Generator
  • Support multi-level inheritance
  • Optimize code performance

Just a crazy idea

@kengwang
Copy link
Contributor Author

Currently, it doesn't support multi-level inheritance. still improving.

Try to go to the root type first and then (un)wrap the protobuf type discriminator step by step, going down to the current type.

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

Successfully merging this pull request may close these issues.

1 participant