pyserde icon indicating copy to clipboard operation
pyserde copied to clipboard

Omit the corresponding key/value pairs when the value is `None` to use TOML serialization

Open drunkwcodes opened this issue 1 year ago • 3 comments

I asked Toml maintainers to add null to TOML spec, and I got the reply which suggest me to omit the null key/value pairs.

https://github.com/toml-lang/toml/issues/975

It is usual in python to use None, and TOML can not be used in these cases. I really like TOML, can pyserde adjust serialization procedure to overcome this restriction?

@serde
@dataclass
class RingInfo:
    """Confrom mojo core-metadata"""

    name: str
    version: str
    metadata_version: str = "0.1"
    # Below are optional
    dynamic: list[str] | None = None
    platforms: list[str] | None = None
    supported_platforms: list[str] | None = None
    summary: str = ""
    description: str = ""
    description_content_type: str = "text/markdown"
    keywords: list[str] | None = None
    home_page: str = ""
    download_url: str = ""
    author: str = get_user_email_from_git()[0]
    author_email: str = get_user_email_from_git()[1]
    maintainer: str = get_user_email_from_git()[0]
    maintainer_email: str = get_user_email_from_git()[1]
    license: str = ""
    classifiers: list[str] | None = None
    requires_dist: list[str] | None = None
    requires_mojo: str = ""
    requires_external: list[str] | None = None
    project_urls: dict[str, str] | None = None
    provides_extra: list[str] | None = None
    provides_dist: list[str] | None = None
    obsoletes_dist: list[str] | None = None
    file_name: str = ""

drunkwcodes avatar Aug 05 '23 15:08 drunkwcodes

What happens if None is part of array? Are you making array shorter and shifting all the values that followed the None?

I still cannot wrap my head around why TOML is fighting against having null tooth and nail.

slonik-az avatar Aug 11 '23 00:08 slonik-az

Hi sorry for the delay. I was just back from vacation. What will happen if you pass an object with null property to toml?

yukinarit avatar Sep 02 '23 12:09 yukinarit

@yukinarit It will raise an error and stop serializing.

drunkwcodes avatar Sep 02 '23 12:09 drunkwcodes