jcrist / msgspec

A fast serialization and validation library, with builtin support for JSON, MessagePack, YAML, and TOML
https://jcristharif.com/msgspec/
BSD 3-Clause "New" or "Revised" License
2.01k stars 59 forks source link

Encode field name #694

Open edpyt opened 1 month ago

edpyt commented 1 month ago

Question

Hello! How to use struct field name instead of field object name? case:

import msgspec

class MyStruct(msgspec.Struct):
    my_f: str = msgspec.field(default="", name="struct_f")

encoded = msgspec.yaml.encode(msgspec.convert({"struct_f": "Hello"}, type=MyStruct))
print(encoded)  # uses `struct_f` instead of `my_f`