dezhidki / Tommy

A single-file TOML reader and writer for C#
MIT License
212 stars 16 forks source link

Parse from a string rather than a file #43

Open yetipicard opened 7 months ago

yetipicard commented 7 months ago

Hi,

I need to parse a string representing a toml file. Tommy seems to only be able to read from a file from what I've seen. Is this right, or did I miss something?

dezhidki commented 7 months ago

Hi!

Sure, you can pass a StringReader to Tommy containing the TOML string as such:

var tomlString = """
title = "TOML Example"

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

[database]
server = "192.168.1.1"
ports = [ 8001, 8001, 8002 ]
connection_max = 5000
enabled = true
""";

var table= TOML.Parse(new StringReader(tomlString));

Such an approach is used, for example, in the unit tests.