Skip to content

TomlMapper writes NaN/Infinity as bare tokens, producing TOML it cannot read back #696

Description

@EverNife

Was making some hardcore tests on my EveryConfig and got a problem on the TOML parser.

The problem

When a value is a non-finite double (NaN, positive infinity, or negative infinity), TomlMapper writes it using the Java text form NaN, Infinity, or -Infinity. That is not valid TOML. The TOML spec uses nan, inf, and -inf instead. Because of this, the output produced by the writer cannot be parsed again by the same TomlMapper: reading it back fails with Unknown token.

This is a writer-only problem. The reader already understands the correct TOML tokens nan, inf, +inf, and -inf, and reads them back as proper floating-point values. So writing nan / inf / -inf instead of the Java form would make these values round-trip, and it would need no change to the reader.

How to reproduce

import com.fasterxml.jackson.dataformat.toml.TomlMapper;
import java.util.Collections;

TomlMapper mapper = new TomlMapper();

// The writer produces a line that is not valid TOML.
String toml = mapper.writeValueAsString(Collections.singletonMap("x", Double.NaN));
System.out.println(toml);   // x = NaN

// Reading the writer's own output fails.
mapper.readTree(toml);      // throws TomlStreamReadException: Unknown token

// The reader does accept the spec form, which shows the fix is safe.
mapper.readTree("x = nan"); // ok: "x" is a number whose value is NaN

Reproduced with jackson-dataformat-toml 2.22.0 (latest at the time of writing). The behavior is the same on older 2.x releases.

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    TOMLIssue related to TOML format backend

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions