diff --git a/README.md b/README.md index 33d4b2e..1b621d8 100644 --- a/README.md +++ b/README.md @@ -793,9 +793,9 @@ var serializedTomlText = Encoding.UTF8.GetString(serializedText.ByteSpan); // Name = "Mixed" ``` -#### Spec - -`Spec` can enable features in TOML v1.1.0. +#### Spec + +`Spec` can enable features in TOML v1.1.0. For more information, please click [TOML v1.1.0 features overview](#toml-v110-features-overview). Built-in support type @@ -914,7 +914,7 @@ It may be used to add optional features in the future. T Deserialize(ReadOnlySpan tomlText, CsTomlSerializerOptions? options = null) T Deserialize(ReadOnlySequence tomlSequence, CsTomlSerializerOptions? options = null) T Deserialize(Stream stream, CsTomlSerializerOptions? options = null) -DeserializeAsync(Stream stream, CsTomlSerializerOptions? options = null, bool configureAwait = false, CancellationToken cancellationToken = default) +ValueTask DeserializeAsync(Stream stream, CsTomlSerializerOptions? options = null, bool configureAwait = false, CancellationToken cancellationToken = default) ``` Asynchronous API is available as `CsTomlSerializer.DeserializeAsync`. @@ -940,7 +940,7 @@ var dict2 = CsTomlSerializer.Deserialize>(tomlText); ``` If a syntax error is found during deserialization, an `CsTomlSerializeException` is thrown after deserialization. -The contents of the thrown exception can be viewed at `CsTomlException.ParseExceptions`. +The contents of the thrown exception can be viewed at `CsTomlSerializeException.ParseExceptions`. ```csharp var tomlText = @" @@ -957,7 +957,7 @@ catch (CsTomlSerializeException ctse) { foreach (var cte in ctse.ParseExceptions!) { - // A syntax error (CsTomlException) was thrown during the parsing line 3. + // A syntax error (CsTomlException) was thrown while parsing line 3. var e = cte.InnerException; // CsToml.Error.CsTomlException: Escape characters 13 were included. var lineNumber = cte.LineNumber; } @@ -967,13 +967,13 @@ catch (CsTomlSerializeException ctse) Serialize API --- -`Serialize` has three overloads, including synchronous and asynchronous APIs. +`Serialize` has four overloads, including synchronous and asynchronous APIs. ```csharp ByteMemoryResult Serialize(T target, CsTomlSerializerOptions? options = null) void Serialize(ref TBufferWriter bufferWriter, T target, CsTomlSerializerOptions? options = null) void Serialize(Stream stream, T value, CsTomlSerializerOptions? options = null) -async ValueTask SerializeAsync(Stream stream, T value, CsTomlSerializerOptions? options = null, bool configureAwait = false, CancellationToken cancellationToken = default) +ValueTask SerializeAsync(Stream stream, T value, CsTomlSerializerOptions? options = null, bool configureAwait = false, CancellationToken cancellationToken = default) ``` `IBufferWriter` serializes directly into the buffer. @@ -1382,9 +1382,9 @@ var arrayIndex2 = document.RootNode["array"u8]["of"u8]["tables"u8][2]["array"u8] ### Complex samples. -```TOML -dotted.keys = "value" -configurations = [1, {key = [ { key2 = ["VALUE"]}]}] +```TOML +dotted.keys = "value" +configurations = [1, {key = [ { key2 = ["VALUE"]}]}] ``` ```csharp @@ -1464,8 +1464,8 @@ public enum TomlValueType `TomlDocumentNode.GetValue` and `TomlDocumentNode.TryGetValue` can be used to obtain a value converted from a Toml value type to a specified type. The type that can be specified for `T` is [Built-in support type](#built-in-support-type). -```TOML -key = "https://github.com/prozolic/CsToml" +```TOML +key = "https://github.com/prozolic/CsToml" ``` ```csharp @@ -1552,10 +1552,10 @@ var document = CsTomlSerializer.Deserialize(tomlText, v110Options) This feature permits newline characters after key/value pairs within inline tables. For example: -```toml -name = { - first = "CsToml", - last = "prozolic" } +```toml +name = { + first = "CsToml", + last = "prozolic" } ``` ### AllowTrailingCommaInInlineTables @@ -1563,8 +1563,8 @@ name = { This feature supports an optional trailing comma after the last key/value pair in inline tables. For example: -```toml -name = { first = "CsToml", last = "prozolic", } +```toml +name = { first = "CsToml", last = "prozolic", } ``` ### AllowSecondsOmissionInTime @@ -1634,6 +1634,30 @@ await CsTomlFileSerializer.SerializeAsync("test.toml", document); `CsTomlFileSerializer.Deserialize` and `CsTomlFileSerializer.DeserializeAsync` deserialize UTF8 strings in TOML files into `TomlDocument`. `CsTomlFileSerializer.Serialize` and `CsTomlFileSerializer.SerializeAsync` serialize the UTF8 string of `TomlDocument` to the TOML file. +By default, a `FormatException` will be thrown if the file extension is not `.toml`. +If you want to relax the file extension restriction, please use the overload that accepts `TomlFileExtensionPolicy.Relaxed`. +This overload will be available starting from v1.8.3. + +```csharp +public partial class CsTomlFileSerializer +{ + public static T Deserialize(string tomlFilePath, CsTomlSerializerOptions? options = null); + public static T Deserialize(string tomlFilePath, CsTomlSerializerOptions? options, TomlFileExtensionPolicy extensionPolicy); + public static ValueTask DeserializeAsync(string tomlFilePath, CsTomlSerializerOptions? options = null, bool configureAwait = false, CancellationToken cancellationToken = default); + public static ValueTask DeserializeAsync(string tomlFilePath, CsTomlSerializerOptions? options, TomlFileExtensionPolicy extensionPolicy, bool configureAwait = false, CancellationToken cancellationToken = default); + public static void Serialize(string tomlFilePath, T value, CsTomlSerializerOptions? options = null); + public static void Serialize(string tomlFilePath, T value, CsTomlSerializerOptions? options, TomlFileExtensionPolicy extensionPolicy); + public static ValueTask SerializeAsync(string tomlFilePath, T value, CsTomlSerializerOptions? options = null, bool configureAwait = false, CancellationToken cancellationToken = default); + public static ValueTask SerializeAsync(string tomlFilePath, T value, CsTomlSerializerOptions? options, TomlFileExtensionPolicy extensionPolicy, bool configureAwait = false, CancellationToken cancellationToken = default); +} + +public enum TomlFileExtensionPolicy +{ + Strict = 0, + Relaxed = 1 +} +``` + `CsToml.Extensions` uses [Cysharp/NativeMemoryArray](https://github.com/Cysharp/NativeMemoryArray) as a third party library. Microsoft.Extensions.Configuration extensions (`CsToml.Extensions.Configuration`) @@ -1668,8 +1692,10 @@ public static IConfigurationBuilder AddTomlFile(this IConfigurationBuilder build public static IConfigurationBuilder AddTomlFile(this IConfigurationBuilder builder, string path, bool optional); public static IConfigurationBuilder AddTomlFile(this IConfigurationBuilder builder, string path, bool optional, bool reloadOnChange); public static IConfigurationBuilder AddTomlFile(this IConfigurationBuilder builder, Microsoft.Extensions.FileProviders.IFileProvider? provider, string path, bool optional, bool reloadOnChange); +public static IConfigurationBuilder AddTomlFile(this IConfigurationBuilder builder, Microsoft.Extensions.FileProviders.IFileProvider? provider, string path, bool optional, bool reloadOnChange, CsTomlSerializerOptions? serializerOptions); public static IConfigurationBuilder AddTomlFile(this IConfigurationBuilder builder, Action configureSource); public static IConfigurationBuilder AddTomlStream(this IConfigurationBuilder builder, System.IO.Stream stream); +public static IConfigurationBuilder AddTomlStream(this IConfigurationBuilder builder, System.IO.Stream stream, CsTomlSerializerOptions? serializerOptions); ``` UnitTest diff --git a/src/CsToml/CsToml.csproj b/src/CsToml/CsToml.csproj index 5ed028c..c16bba7 100644 --- a/src/CsToml/CsToml.csproj +++ b/src/CsToml/CsToml.csproj @@ -18,7 +18,7 @@ - + diff --git a/src/NuGet.props b/src/NuGet.props index c7f61c7..496a263 100644 --- a/src/NuGet.props +++ b/src/NuGet.props @@ -8,7 +8,7 @@ $(PackageProjectUrl) git MIT - 1.8.2 + 1.8.3