jcrist / msgspec

A fast serialization and validation library, with builtin support for JSON, MessagePack, YAML, and TOML

Home Page:https://jcristharif.com/msgspec/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allow unknown tags, defaulting to tagged base

TLouf opened this issue · comments

Description

I'm dealing with line-delimited JSON files in which one field can be of multiple types, which all have very different structures. So I naturally defined a base Struct to parse all of these, doing something like:

class BaseType(msgspec.Struct, tag_field="tag"):
    pass

class InterestingSubType(BaseType):
    id: int
	...

However, I'm only interested in parsing a few of these types, so I end up having many

class NotInterestingSubType(BaseType):
    pass

and in the very first parent class:

class ObjectToParse(msgpec.Struct):
    ...
	field: InterestingSubType | NotInterestingSubType | ....

To avoid all this, I would have enjoyed being able to do just

class ObjectToParse(msgpec.Struct):
    ...
	field: InterestingSubType | BaseType

to mean that if the interesting tagged type is found, parse it as I described it, otherwise just return an empty BaseType to signal there was one of the NotInterestingSubTypes there. Also, in the same way that you can forbid unknown fields, you could also forbid unknown tags to get the current behavior.

I hope I managed to convey my idea clearly. Also I must say I've used msgspec only a little until now so I'm not 100% sure that's not possible already, but I've looked and tested quite extensively.