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

Allow `omit_defaults` to exclude fields when encoded value is `{}` (empty dict) #645

Open wikiped opened 4 months ago

wikiped commented 4 months ago

Description

The following code:

import msgspec as ms

class Sub(ms.Struct, omit_defaults=True):
    f1: str = ""
    f2: int = 0

class Struct(ms.Struct, omit_defaults=True, tag=True):
    f1: int = 1
    sub: Sub = ms.field(default_factory=Sub)

print(ms.yaml.encode(Struct()).decode())
print(ms.json.encode(Struct()).decode())

y = """
type: Struct

"""
j = '{"type":"Struct"}'

print()
print(ms.yaml.decode(y, type=Struct))
print(ms.json.decode(j, type=Struct))

Will output:

type: Struct
sub: {}

{"type":"Struct","sub":{}}

Struct(f1=1, sub=Sub(f1='', f2=0))
Struct(f1=1, sub=Sub(f1='', f2=0))

Although it is clear why the empty dict is being encoded, it is still desirable to avoid that, given that decoding absent fields does not cause any errors when the empty constructor (i.e. empty dict) is producing the expected default.

It would seem that recursive check with matches_default on subclasses of msgspec.Struct might do the job, but this would probably mean some hit on perfomance (?).

Perhaps there is a way to check the resulting '{}' value and omit it instead?

Thank you for considering this.