Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
drunkwcodes opened this issue Aug 5, 2023 · 5 comments · Fixed by #601
Closed
Assignees
Labels
enhancement New feature or request

Comments

@drunkwcodes
Copy link

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.

toml-lang/toml#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 = ""
@slonik-az
Copy link

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.

@yukinarit
Copy link
Owner

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

@drunkwcodes
Copy link
Author

@yukinarit It will raise an error and stop serializing.

@yukinarit yukinarit added the enhancement New feature or request label Sep 7, 2023
@yukinarit yukinarit self-assigned this Sep 9, 2023
yukinarit added a commit that referenced this issue Oct 20, 2024
The `skip_none` attribute is an optional feature originally
implemented to prevent null values from appearing in TOML
serialized outputs. When set to True, any field in the class with
a None value is excluded from the serialized output, ensuring that
TOML files (or other formats) remain clean and free from null
entries.

Closes #421
yukinarit added a commit that referenced this issue Oct 20, 2024
The `skip_none` attribute is an optional feature originally
implemented to prevent null values from appearing in TOML
serialized outputs. When set to True, any field in the class with
a None value is excluded from the serialized output, ensuring that
TOML files (or other formats) remain clean and free from null
entries.

Closes #421
yukinarit added a commit that referenced this issue Oct 21, 2024
The `skip_none` attribute is an optional feature originally
implemented to prevent null values from appearing in TOML
serialized outputs. When set to True, any field in the class with
a None value is excluded from the serialized output, ensuring that
TOML files (or other formats) remain clean and free from null
entries.

Closes #421
@yukinarit
Copy link
Owner

Hi @drunkwcodes

I finally took some time to solve this issue! pyserde v0.21.0 can automatically omit None fields for TOML.

NOTE: This works for dataclass fields level only, not array not dict. Refer to test_toml.py to understand the detailed behavior.

@drunkwcodes
Copy link
Author

@yukinarit thank you for your persistent hard work. I'm looking at it right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Shipped 🚀
Development

Successfully merging a pull request may close this issue.

3 participants