Use System.Text.Json instead of NewtonSoft.Json

This commit is contained in:
2022-12-07 20:39:49 +00:00
parent 5744119b9c
commit 46077ed55f
4 changed files with 73 additions and 62 deletions

View File

@@ -2,6 +2,8 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Aaru.Checksums; using Aaru.Checksums;
using Aaru.CommonTypes; using Aaru.CommonTypes;
using Aaru.CommonTypes.Enums; using Aaru.CommonTypes.Enums;
@@ -9,8 +11,6 @@ using Aaru.CommonTypes.Interfaces;
using Aaru.CommonTypes.Structs; using Aaru.CommonTypes.Structs;
using Aaru.Core; using Aaru.Core;
using FluentAssertions; using FluentAssertions;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using NUnit.Framework; using NUnit.Framework;
using FileAttributes = Aaru.CommonTypes.Structs.FileAttributes; using FileAttributes = Aaru.CommonTypes.Structs.FileAttributes;
@@ -115,24 +115,25 @@ public abstract class ReadOnlyFilesystemTest : FilesystemTest
Assert.AreEqual(ErrorNumber.NoError, ret, string.Format(Localization.Unmountable_0, testFile)); Assert.AreEqual(ErrorNumber.NoError, ret, string.Format(Localization.Unmountable_0, testFile));
var serializer = new JsonSerializer var serializerOptions = new JsonSerializerOptions
{ {
Formatting = Formatting.Indented, Converters =
MaxDepth = 16384, {
NullValueHandling = NullValueHandling.Ignore new JsonStringEnumConverter()
},
MaxDepth = 1536, // More than this an we get a StackOverflowException
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
PropertyNameCaseInsensitive = true
}; };
serializer.Converters.Add(new StringEnumConverter());
if(test.ContentsJson != null) if(test.ContentsJson != null)
test.Contents = test.Contents =
serializer. JsonSerializer.Deserialize<Dictionary<string, FileData>>(test.ContentsJson, serializerOptions);
Deserialize<
Dictionary<string, FileData>>(new JsonTextReader(new StringReader(test.ContentsJson)));
else if(File.Exists($"{testFile}.contents.json")) else if(File.Exists($"{testFile}.contents.json"))
{ {
var sr = new StreamReader($"{testFile}.contents.json"); var sr = new FileStream($"{testFile}.contents.json", FileMode.Open);
test.Contents = serializer.Deserialize<Dictionary<string, FileData>>(new JsonTextReader(sr)); test.Contents = JsonSerializer.Deserialize<Dictionary<string, FileData>>(sr, serializerOptions);
} }
if(test.Contents is null) if(test.Contents is null)
@@ -222,17 +223,20 @@ public abstract class ReadOnlyFilesystemTest : FilesystemTest
Dictionary<string, FileData> contents = BuildDirectory(fs, "/"); Dictionary<string, FileData> contents = BuildDirectory(fs, "/");
var serializer = new JsonSerializer var serializerOptions = new JsonSerializerOptions
{ {
Formatting = Formatting.Indented, Converters =
MaxDepth = 16384, {
NullValueHandling = NullValueHandling.Ignore new JsonStringEnumConverter()
},
MaxDepth = 1536,
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
PropertyNameCaseInsensitive = true
}; };
serializer.Converters.Add(new StringEnumConverter()); var sw = new FileStream($"{testFile}.contents.json", FileMode.Create);
JsonSerializer.Serialize(sw, contents, serializerOptions);
var sw = new StreamWriter($"{testFile}.contents.json");
serializer.Serialize(sw, contents);
sw.Close(); sw.Close();
} }
} }

View File

@@ -1,6 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
using Aaru.Checksums; using Aaru.Checksums;
using Aaru.CommonTypes; using Aaru.CommonTypes;
using Aaru.CommonTypes.Enums; using Aaru.CommonTypes.Enums;
@@ -8,8 +10,6 @@ using Aaru.CommonTypes.Interfaces;
using Aaru.Core; using Aaru.Core;
using Aaru.Tests.Filesystems; using Aaru.Tests.Filesystems;
using FluentAssertions.Execution; using FluentAssertions.Execution;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using NUnit.Framework; using NUnit.Framework;
namespace Aaru.Tests.Images; namespace Aaru.Tests.Images;
@@ -205,18 +205,20 @@ public abstract class BlockMediaImageTest : BaseMediaImageTest
if(!File.Exists(expectedDataFilename)) if(!File.Exists(expectedDataFilename))
continue; continue;
var serializer = new JsonSerializer var serializerOptions = new JsonSerializerOptions
{ {
Formatting = Formatting.Indented, Converters =
MaxDepth = 16384, {
NullValueHandling = NullValueHandling.Ignore new JsonStringEnumConverter()
},
MaxDepth = 1536, // More than this an we get a StackOverflowException
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
PropertyNameCaseInsensitive = true
}; };
serializer.Converters.Add(new StringEnumConverter()); var sr = new FileStream(expectedDataFilename, FileMode.Open);
VolumeData[] expectedData = JsonSerializer.Deserialize<VolumeData[]>(sr, serializerOptions);
var sr = new StreamReader(expectedDataFilename);
VolumeData[] expectedData = serializer.Deserialize<VolumeData[]>(new JsonTextReader(sr));
Assert.NotNull(expectedData); Assert.NotNull(expectedData);
@@ -259,8 +261,8 @@ public abstract class BlockMediaImageTest : BaseMediaImageTest
}; };
} }
var sw = new StreamWriter(expectedDataFilename); var sw = new FileStream(expectedDataFilename, FileMode.Create);
serializer.Serialize(sw, expectedData); JsonSerializer.Serialize(sw, expectedData, serializerOptions);
sw.Close(); sw.Close();
*/ */

View File

@@ -3,6 +3,8 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks; using System.Threading.Tasks;
using Aaru.Checksums; using Aaru.Checksums;
using Aaru.CommonTypes; using Aaru.CommonTypes;
@@ -13,8 +15,6 @@ using Aaru.Core;
using Aaru.Tests.Filesystems; using Aaru.Tests.Filesystems;
using FluentAssertions; using FluentAssertions;
using FluentAssertions.Execution; using FluentAssertions.Execution;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using NUnit.Framework; using NUnit.Framework;
namespace Aaru.Tests.Images; namespace Aaru.Tests.Images;
@@ -251,29 +251,31 @@ public abstract class OpticalMediaImageTest : BaseMediaImageTest
Assert.AreEqual(ErrorNumber.NoError, ret, Assert.AreEqual(ErrorNumber.NoError, ret,
string.Format(Localization.Unmountable_0, testFile)); string.Format(Localization.Unmountable_0, testFile));
var serializer = new JsonSerializer var serializerOptions = new JsonSerializerOptions
{ {
Formatting = Formatting.Indented, Converters =
MaxDepth = 16384, {
NullValueHandling = NullValueHandling.Ignore new JsonStringEnumConverter()
},
MaxDepth = 1536, // More than this an we get a StackOverflowException
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
PropertyNameCaseInsensitive = true
}; };
serializer.Converters.Add(new StringEnumConverter());
if(track.FileSystems[i].ContentsJson != null) if(track.FileSystems[i].ContentsJson != null)
track.FileSystems[i].Contents = track.FileSystems[i].Contents =
serializer. JsonSerializer.
Deserialize< Deserialize<Dictionary<string, FileData>>(track.FileSystems[i].ContentsJson,
Dictionary<string, FileData>>(new JsonTextReader(new StringReader(track. serializerOptions);
FileSystems[i].
ContentsJson)));
else if(File.Exists($"{testFile}.track{track.Number}.filesystem{i}.contents.json")) else if(File.Exists($"{testFile}.track{track.Number}.filesystem{i}.contents.json"))
{ {
var sr = new StreamReader($"{testFile}.track{track.Number}.filesystem{i var sr =
}.contents.json"); new FileStream($"{testFile}.track{track.Number}.filesystem{i}.contents.json",
FileMode.Open);
track.FileSystems[i].Contents = track.FileSystems[i].Contents =
serializer.Deserialize<Dictionary<string, FileData>>(new JsonTextReader(sr)); JsonSerializer.Deserialize<Dictionary<string, FileData>>(sr, serializerOptions);
} }
if(track.FileSystems[i].Contents is null) if(track.FileSystems[i].Contents is null)
@@ -285,8 +287,8 @@ public abstract class OpticalMediaImageTest : BaseMediaImageTest
// Uncomment to generate JSON file // Uncomment to generate JSON file
/* var contents = ReadOnlyFilesystemTest.BuildDirectory(rofs, "/"); /* var contents = ReadOnlyFilesystemTest.BuildDirectory(rofs, "/");
var sw = new StreamWriter($"{testFile}.track{track.Number}.filesystem{i}.contents.json"); var sw = new FileStream($"{testFile}.track{track.Number}.filesystem{i}.contents.json", FileMode.Create);
serializer.Serialize(sw, contents); JsonSerializer.Serialize(sw, contents, serializerOptions);
sw.Close();*/ sw.Close();*/
} }
} }

View File

@@ -2,6 +2,8 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Aaru.Checksums; using Aaru.Checksums;
using Aaru.CommonTypes; using Aaru.CommonTypes;
using Aaru.CommonTypes.Enums; using Aaru.CommonTypes.Enums;
@@ -9,8 +11,6 @@ using Aaru.CommonTypes.Interfaces;
using Aaru.CommonTypes.Structs; using Aaru.CommonTypes.Structs;
using Aaru.Core; using Aaru.Core;
using FluentAssertions; using FluentAssertions;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using NUnit.Framework; using NUnit.Framework;
using FileAttributes = Aaru.CommonTypes.Structs.FileAttributes; using FileAttributes = Aaru.CommonTypes.Structs.FileAttributes;
@@ -75,17 +75,20 @@ public abstract class FsExtractHashIssueTest
Assert.True(File.Exists($"{TestFile}.unittest.json")); Assert.True(File.Exists($"{TestFile}.unittest.json"));
var serializer = new JsonSerializer var serializerOptions = new JsonSerializerOptions
{ {
Formatting = Formatting.Indented, Converters =
MaxDepth = 16384, {
NullValueHandling = NullValueHandling.Ignore new JsonStringEnumConverter()
},
MaxDepth = 1536, // More than this an we get a StackOverflowException
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
PropertyNameCaseInsensitive = true
}; };
serializer.Converters.Add(new StringEnumConverter()); var sr = new FileStream($"{TestFile}.unittest.json", FileMode.Open);
FsExtractHashData expectedData = JsonSerializer.Deserialize<FsExtractHashData>(sr, serializerOptions);
var sr = new StreamReader($"{TestFile}.unittest.json");
FsExtractHashData expectedData = serializer.Deserialize<FsExtractHashData>(new JsonTextReader(sr));
Assert.NotNull(expectedData); Assert.NotNull(expectedData);