Comments (3)
msgspec.inspect.type_info
is one way, but it's a bit overkill for this use case. And __struct_encode_fields__
is technically non-public (and may be removed in the future). The best way to get this information is to use msgspec.struct.fields
(docs). You're looking for the encode_name
attribute on FieldInfo
in the result:
In [3]: class Example(msgspec.Struct):
...: user_id: int = msgspec.field(name="userId")
...:
In [4]: msgspec.structs.fields(Example)
Out[4]: (FieldInfo(name='user_id', encode_name='userId', type=<class 'int'>, default=NODEFAULT, default_factory=NODEFAULT),)
from msgspec.
@Olegt0rr, I think inspect.type_info
will do the right job for you.
from msgspec.inspect import type_info
# to access "user_id" field's metadata
field_meta = type_info(HelpIssue).fields[0]
assert field_meta.name == "user_id"
assert field_meta.encode_name == "userId"
References: https://jcristharif.com/msgspec/inspect.html
from msgspec.
@uwinx, thanks!
I've also found this way:
for name, encode_name in zip(
struct.__struct_fields__,
struct.__struct_encode_fields__,
):
...
from msgspec.
Related Issues (20)
- Let json.schema take an enc_hook for handling default values of custom types
- Support encoding any Enum value type
- Json Encoder: dataclass classes are encoded to empty dict
- Tighten typing of msgspec.json.schema
- Support numeric constraints for Decimal values
- Support decoding empty strings as unset for all field types
- Does msgspec have a concept of schema mode?
- Add an option to disable constraint checks
- Ability to add an example to a Struct as output to the JSON schema
- Unable to encode `lxml.etree._ElementUnicodeResult`
- fallback to dict for unknown type in tagged union? HOT 3
- Multiple types to same Struct HOT 1
- Encode field name
- msgspec.json.decode parsing a float value HOT 10
- Encode_into might not write data at the intended offset location if buffer is too small. HOT 4
- 3.13 support HOT 1
- Converting an object / dict to a struct where the struct contains a Raw field fails with "Expected `any`, got ..."
- Incorrect decoding of 65-bit integer
- Is there a way to stop parsing long lists of JSON objects when an object with a certain property value is found
- Allow passing custom globals to use when converting dataclasses
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from msgspec.