GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing msgspec.dev — Schema Evolution Schema Evolution¶
This can be useful if, for example, you have clients and servers with mismatched versions. For schema evolution to work smoothly, you need to follow a few guidelines:
For example, suppose we had a >>> import msgspec >>> class User(msgspec.Struct): ... """A struct representing a user""" ... name: str ... groups: set[str] = set() ... email: str | None = None Then suppose we wanted to add a new >>> class User2(msgspec.Struct): ... """An updated version of the User struct, now with a phone number""" ... name: str ... groups: set[str] = set() ... email: str | None = None ... phone : str | None = None Messages serialized using both the old and new schemas can still be exchanged without error. If an old message is deserialized using the new schema, the missing fields all have default values that will be used. Likewise, if a new message is deserialized with the old schema the unknown new fields will be efficiently skipped without decoding. >>> old_dec = msgspec.json.Decoder(User) >>> new_dec = msgspec.json.Decoder(User2) >>> new_msg = msgspec.json.encode( ... User2("bob", groups={"finance"}, phone="512-867-5309") ... ) >>> old_dec.decode(new_msg) # deserializing a new msg with an older decoder User(name='bob', groups={'finance'}, email=None) >>> old_msg = msgspec.json.encode( ... User("alice", groups={"admin", "engineering"}) ... ) >>> new_dec.decode(old_msg) # deserializing an old msg with a new decoder User2(name="alice", groups={"admin", "engineering"}, email=None, phone=None) |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |