Skip to content

Commit 0c33710

Browse files
authored
Merge pull request #64 from prozolic/v1.7.3
Upgrade v1.7.3
2 parents 4419271 + 8bb2532 commit 0c33710

3 files changed

Lines changed: 45 additions & 57 deletions

File tree

README.md

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,16 @@ Define the `class`, `struct`, `record` and `record struct` to be serialized and
110110
public partial class CsTomlClass
111111
{
112112
[TomlValueOnSerialized]
113-
public string Key { get; set; }
113+
public string? Key { get; set; }
114114

115115
[TomlValueOnSerialized]
116116
public int? Number { get; set; }
117117

118118
[TomlValueOnSerialized]
119-
public int[] Array { get; set; }
119+
public int[]? Array { get; set; }
120120

121121
[TomlValueOnSerialized(aliasName: "alias")]
122-
public string Value { get; set; }
122+
public string? Value { get; set; }
123123

124124
[TomlValueOnSerialized]
125125
public TableClass Table { get; set; } = new TableClass();
@@ -129,7 +129,7 @@ public partial class CsTomlClass
129129
public partial class TableClass
130130
{
131131
[TomlValueOnSerialized]
132-
public string Key { get; set; }
132+
public string? Key { get; set; }
133133

134134
[TomlValueOnSerialized]
135135
public int Number { get; set; }
@@ -150,76 +150,57 @@ See [Built-in support type](#built-in-support-type) for more information on avai
150150
#pragma warning disable CS8602 // Dereference of a possibly null reference.
151151
#pragma warning disable CS8603 // Possible null reference return.
152152
#pragma warning disable CS8604 // Possible null reference argument for parameter.
153-
#pragma warning disable CS8619 // Possible null reference assignment fix
153+
#pragma warning disable CS8619 // Possible null reference assignment fix.
154+
#pragma warning disable CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types.
154155

155156
using CsToml;
157+
using CsToml.Error;
156158
using CsToml.Formatter;
157159
using CsToml.Formatter.Resolver;
158160

159161
namespace ConsoleApp;
160162

161-
partial class CsTomlClass : ITomlSerializedObject<CsTomlClass>
163+
partial class CsTomlClass : ITomlSerializedObject<CsTomlClass?>
162164
{
163165

164-
static CsTomlClass ITomlSerializedObject<CsTomlClass>.Deserialize(ref TomlDocumentNode rootNode, CsTomlSerializerOptions options)
166+
static CsTomlClass? ITomlSerializedObject<CsTomlClass?>.Deserialize(ref TomlDocumentNode rootNode, CsTomlSerializerOptions options)
165167
{
166-
var __Key__RootNode = rootNode["Key"u8];
167-
var __Key__ = options.Resolver.GetFormatter<string>()!.Deserialize(ref __Key__RootNode, options);
168-
var __Value__RootNode = rootNode["alias"u8];
169-
var __Value__ = options.Resolver.GetFormatter<string>()!.Deserialize(ref __Value__RootNode, options);
170-
var __Array__RootNode = rootNode["Array"u8];
171-
var __Array__ = options.Resolver.GetFormatter<int[]>()!.Deserialize(ref __Array__RootNode, options);
172-
var __Number__RootNode = rootNode["Number"u8];
173-
var __Number__ = options.Resolver.GetFormatter<int?>()!.Deserialize(ref __Number__RootNode, options);
174-
var __Table__RootNode = rootNode["Table"u8];
168+
if (!(rootNode.HasValue || rootNode.IsTableHeader)) return default;
169+
170+
var __Table__RootNode = rootNode[@"Table"u8];
175171
var __Table__ = options.Resolver.GetFormatter<global::ConsoleApp.TableClass>()!.Deserialize(ref __Table__RootNode, options);
176172

177173
var target = new CsTomlClass(){
178-
Key = __Key__,
179-
Value = __Value__,
180-
Array = __Array__,
181-
Number = __Number__,
182174
Table = __Table__,
183175
};
184176

185177
return target;
186178
}
187179

188-
static void ITomlSerializedObject<CsTomlClass>.Serialize<TBufferWriter>(ref Utf8TomlDocumentWriter<TBufferWriter> writer, CsTomlClass target, CsTomlSerializerOptions options)
180+
static void ITomlSerializedObject<CsTomlClass?>.Serialize<TBufferWriter>(ref Utf8TomlDocumentWriter<TBufferWriter> writer, CsTomlClass? target, CsTomlSerializerOptions options)
189181
{
190-
writer.BeginScope();
191-
writer.WriteKey("Key"u8);
192-
writer.WriteEqual();
193-
options.Resolver.GetFormatter<string>()!.Serialize(ref writer, target.Key, options);
194-
writer.EndKeyValue();
195-
writer.WriteKey("alias"u8);
196-
writer.WriteEqual();
197-
options.Resolver.GetFormatter<string>()!.Serialize(ref writer, target.Value, options);
198-
writer.EndKeyValue();
199-
writer.WriteKey("Array"u8);
200-
writer.WriteEqual();
201-
options.Resolver.GetFormatter<int[]>()!.Serialize(ref writer, target.Array, options);
202-
writer.EndKeyValue();
203-
writer.WriteKey("Number"u8);
204-
writer.WriteEqual();
205-
options.Resolver.GetFormatter<int?>()!.Serialize(ref writer, target.Number, options);
206-
writer.EndKeyValue();
182+
if (target == null) ThrowIfNull(nameof(target));
183+
207184
if (options.SerializeOptions.TableStyle == TomlTableStyle.Header && (writer.State == TomlValueState.Default || writer.State == TomlValueState.Table)){
208-
writer.WriteTableHeader("Table"u8);
185+
writer.WriteTableHeader(@"Table"u8);
209186
writer.WriteNewLine();
210187
writer.BeginCurrentState(TomlValueState.Table);
211-
writer.PushKey("Table"u8);
188+
writer.PushKey(@"Table"u8);
212189
options.Resolver.GetFormatter<global::ConsoleApp.TableClass>()!.Serialize(ref writer, target.Table, options);
213190
writer.PopKey();
214191
writer.EndCurrentState();
215192
}
216193
else
217194
{
218-
writer.PushKey("Table"u8);
195+
writer.PushKey(@"Table"u8);
219196
options.Resolver.GetFormatter<global::ConsoleApp.TableClass>()!.Serialize(ref writer, target.Table, options);
220197
writer.PopKey();
221198
}
222-
writer.EndScope();
199+
200+
static void ThrowIfNull(string args)
201+
{
202+
throw new CsTomlException($@"Serialization failed because the argument '{args}' is null.");
203+
}
223204
}
224205

225206
static void ITomlSerializedObjectRegister.Register()
@@ -230,10 +211,6 @@ partial class CsTomlClass : ITomlSerializedObject<CsTomlClass>
230211
}
231212

232213
// Register Formatter in advance.
233-
if (!TomlValueFormatterResolver.IsRegistered<int?>())
234-
{
235-
TomlValueFormatterResolver.Register(new NullableFormatter<int>());
236-
}
237214
if (!TomlValueFormatterResolver.IsRegistered<global::ConsoleApp.TableClass>())
238215
{
239216
TomlValueFormatterResolver.Register<global::ConsoleApp.TableClass>();
@@ -256,22 +233,26 @@ partial class CsTomlClass : ITomlSerializedObject<CsTomlClass>
256233
#pragma warning disable CS8602 // Dereference of a possibly null reference.
257234
#pragma warning disable CS8603 // Possible null reference return.
258235
#pragma warning disable CS8604 // Possible null reference argument for parameter.
259-
#pragma warning disable CS8619 // Possible null reference assignment fix
236+
#pragma warning disable CS8619 // Possible null reference assignment fix.
237+
#pragma warning disable CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types.
260238

261239
using CsToml;
240+
using CsToml.Error;
262241
using CsToml.Formatter;
263242
using CsToml.Formatter.Resolver;
264243

265244
namespace ConsoleApp;
266245

267-
partial class TableClass : ITomlSerializedObject<TableClass>
246+
partial class TableClass : ITomlSerializedObject<TableClass?>
268247
{
269248

270-
static TableClass ITomlSerializedObject<TableClass>.Deserialize(ref TomlDocumentNode rootNode, CsTomlSerializerOptions options)
249+
static TableClass? ITomlSerializedObject<TableClass?>.Deserialize(ref TomlDocumentNode rootNode, CsTomlSerializerOptions options)
271250
{
272-
var __Key__RootNode = rootNode["Key"u8];
251+
if (!(rootNode.HasValue || rootNode.IsTableHeader)) return default;
252+
253+
var __Key__RootNode = rootNode[@"Key"u8];
273254
var __Key__ = options.Resolver.GetFormatter<string>()!.Deserialize(ref __Key__RootNode, options);
274-
var __Number__RootNode = rootNode["Number"u8];
255+
var __Number__RootNode = rootNode[@"Number"u8];
275256
var __Number__ = options.Resolver.GetFormatter<int>()!.Deserialize(ref __Number__RootNode, options);
276257

277258
var target = new TableClass(){
@@ -282,18 +263,25 @@ partial class TableClass : ITomlSerializedObject<TableClass>
282263
return target;
283264
}
284265

285-
static void ITomlSerializedObject<TableClass>.Serialize<TBufferWriter>(ref Utf8TomlDocumentWriter<TBufferWriter> writer, TableClass target, CsTomlSerializerOptions options)
266+
static void ITomlSerializedObject<TableClass?>.Serialize<TBufferWriter>(ref Utf8TomlDocumentWriter<TBufferWriter> writer, TableClass? target, CsTomlSerializerOptions options)
286267
{
268+
if (target == null) ThrowIfNull(nameof(target));
269+
287270
writer.BeginScope();
288-
writer.WriteKey("Key"u8);
271+
writer.WriteKey(@"Key"u8);
289272
writer.WriteEqual();
290273
options.Resolver.GetFormatter<string>()!.Serialize(ref writer, target.Key, options);
291274
writer.EndKeyValue();
292-
writer.WriteKey("Number"u8);
275+
writer.WriteKey(@"Number"u8);
293276
writer.WriteEqual();
294277
options.Resolver.GetFormatter<int>()!.Serialize(ref writer, target.Number, options);
295278
writer.EndKeyValue(true);
296279
writer.EndScope();
280+
281+
static void ThrowIfNull(string args)
282+
{
283+
throw new CsTomlException($@"Serialization failed because the argument '{args}' is null.");
284+
}
297285
}
298286

299287
static void ITomlSerializedObjectRegister.Register()

src/CsToml.Extensions/CsTomlFileSerializer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public static async ValueTask<T> DeserializeAsync<T>(string tomlFilePath, CsToml
9898
}
9999
}
100100

101-
public static void Serialize<T>(string tomlFilePath, T? value, CsTomlSerializerOptions? options = null)
101+
public static void Serialize<T>(string tomlFilePath, T value, CsTomlSerializerOptions? options = null)
102102
{
103103
if (Path.GetExtension(tomlFilePath) != TomlExtension)
104104
throw new FormatException($"TOML file should use the extension .toml");
@@ -116,7 +116,7 @@ public static void Serialize<T>(string tomlFilePath, T? value, CsTomlSerializerO
116116
bufferWriter.WriteTo(fileWriter.ByteWriter);
117117
}
118118

119-
public static async ValueTask SerializeAsync<T>(string tomlFilePath, T? value, CsTomlSerializerOptions? options = null, bool configureAwait = false, CancellationToken cancellationToken = default)
119+
public static async ValueTask SerializeAsync<T>(string tomlFilePath, T value, CsTomlSerializerOptions? options = null, bool configureAwait = false, CancellationToken cancellationToken = default)
120120
{
121121
if (Path.GetExtension(tomlFilePath) != TomlExtension)
122122
throw new FormatException($"TOML file should use the extension .toml");

src/NuGet.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<RepositoryUrl>$(PackageProjectUrl)</RepositoryUrl>
99
<RepositoryType>git</RepositoryType>
1010
<PackageLicenseExpression>MIT</PackageLicenseExpression>
11-
<VersionPrefix>1.7.2</VersionPrefix>
11+
<VersionPrefix>1.7.3</VersionPrefix>
1212
</PropertyGroup>
1313

1414
<ItemGroup>

0 commit comments

Comments
 (0)