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

Encode support #1

Closed
lenovouser opened this issue May 21, 2023 · 4 comments · Fixed by #2
Closed

Encode support #1

lenovouser opened this issue May 21, 2023 · 4 comments · Fixed by #2
Labels
enhancement New feature or request

Comments

@lenovouser
Copy link

I am trying to find a replacement for @iarna/toml since that doesn't have full spec 1.0 support as far as I know. I would love to see encoding support implemented for this library 😊

@cyyynthia cyyynthia added the enhancement New feature or request label May 21, 2023
@cyyynthia
Copy link
Member

A basic implementation of a TOML serialization should be easy and straightforward to do. However, when I initially thought about doing a stringify method, I was concerned about the case of when the implementation should produce inline tables, and cases where it should produce a classic table. Same concerns can arise for strings vs. multiline strings, etc.

These concern excluded, writing a serializer that only cares about producing a valid TOML representation of an object shouldn't be too difficult.

@iarna/toml is indeed not TOML 1.0 compliant (and fails to parse some documents; I know it has some big problems with dates in inline tables, it's the reason I can't benchmark it against my test 5MB file).

@cyyynthia
Copy link
Member

I've implemented a TOML serializer in #2, which is much, much faster than the other parsers 🎉 (I honestly have no clue where the 4x performance improvement comes from, but I'm very happy with it).

The serializer in its current shape has the following gotchas, as documented in the updated README:

  • undefined and null values on objects are ignored (does not produce a key/value).
  • undefined and null values in arrays are rejected.
  • Functions, classes and symbols are rejected.
  • floats will be serialized as integers if they don't have a decimal part.
    • stringify(parse('a = 1.0')) === 'a = 1'
  • JS Date will be serialized as Offset Date Time
    • TomlDate from this library can be used to represent all date types

@lenovouser
Copy link
Author

Wow, that was quick! 🥳

@cyyynthia
Copy link
Member

Yeah, a simple serializer isn't hard to build, though it's fairly minimal and its only concern as of now is to generate a valid toml representation of an object, with the only "fancy" feature being support for generating arrays of tables using the [[table]] syntax.

I want to add additional formatting options to the stringify method in the future, but for now the simple way works.

For a little comparison, this is what smol-toml produces when stringifying the TOML example document:

title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00.000-08:00

[database]
enabled = true
ports = [ 8000, 8001, 8002 ]
data = [ [ "delta", "phi" ], [ 3.14 ] ]

[database.temp_targets]
cpu = 79.5
case = 72

[servers]
[servers.alpha]
ip = "10.0.0.1"
role = "frontend"

[servers.beta]
ip = "10.0.0.2"
role = "backend"

vs. what @iarna/toml produces:

title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T15:32:00.000Z

[database]
enabled = true
ports = [ 8_000, 8_001, 8_002 ]
data = [ [ "delta", "phi" ], [ 3.14 ] ]

  [database.temp_targets]
  cpu = 79.5
  case = 72

[servers.alpha]
ip = "10.0.0.1"
role = "frontend"

[servers.beta]
ip = "10.0.0.2"
role = "backend"

Most notable differences are:

  • all tables are explicitly defined ([servers] could be skipped)
  • numbers are not formatted with underscores
  • subtables are not indented
  • smol-toml doesn't add a newline at the end
  • dates are true to their original representation (when using TomlDate)

Version 1.1.0 with the stringify method will be released shortly 🎉

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
None yet
Development

Successfully merging a pull request may close this issue.

2 participants