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

Serialization field order by declaration order #358

Open
WongKit opened this issue Jun 5, 2023 · 3 comments
Open

Serialization field order by declaration order #358

WongKit opened this issue Jun 5, 2023 · 3 comments
Labels
enhancement Requires or request to feature enhancement

Comments

@WongKit
Copy link

WongKit commented Jun 5, 2023

Hi,

is it possible to optionally let MsgPack sort the serialized data by the original declaration order instead of the alphabetical one? I am working in a project, where the original field names are scrambled by an obfuscator.
Using the MessagePackMember attribute would only be the very last resort due to the huge amount of serialized classes to be changed manually.

According to StackOverflow, the MetadataToken property might be used for sorting.
https://stackoverflow.com/a/8067702

@yfakariya
Copy link
Member

Sorry, I missed this post for a long time.

Use [MessagePackMember(Id)] where Id is continuous, zero-based integers.

// This should be serialized to ["A", 1] instead of [1, "A"]
public class Foo
{
    [MessagePackMember(0)]
    public string StringProperty { get; set; } = "A";

    [MessagePackMember(1)]
    public int IntegerProperty { get; set; } = 1;
}

@WongKit
Copy link
Author

WongKit commented Apr 14, 2024

No problem! Is there any other way? As I wrote above, I am already aware of the MessagePackMember attribute 😉

@yfakariya
Copy link
Member

Oh, I panicked for delayed response.
I understand that your post sugested to change default behavior more handy. Usage of metadata token looks good starting point, but it introduces breaking changes. So, I will implement it when I restart work on wip/v2 branch or more "mild" v2 drop.

Thank you :)

@yfakariya yfakariya added enhancement Requires or request to feature enhancement and removed question labels Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Requires or request to feature enhancement
Projects
None yet
Development

No branches or pull requests

2 participants