diff --git a/Aaru.Tests/Filesystems/ReadOnlyFilesystemTest.cs b/Aaru.Tests/Filesystems/ReadOnlyFilesystemTest.cs index 62811354f..36e8438d3 100644 --- a/Aaru.Tests/Filesystems/ReadOnlyFilesystemTest.cs +++ b/Aaru.Tests/Filesystems/ReadOnlyFilesystemTest.cs @@ -2,6 +2,8 @@ using System; using System.Collections.Generic; using System.IO; using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; using Aaru.Checksums; using Aaru.CommonTypes; using Aaru.CommonTypes.Enums; @@ -9,8 +11,6 @@ using Aaru.CommonTypes.Interfaces; using Aaru.CommonTypes.Structs; using Aaru.Core; using FluentAssertions; -using Newtonsoft.Json; -using Newtonsoft.Json.Converters; using NUnit.Framework; 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)); - var serializer = new JsonSerializer + var serializerOptions = new JsonSerializerOptions { - Formatting = Formatting.Indented, - MaxDepth = 16384, - NullValueHandling = NullValueHandling.Ignore + Converters = + { + 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) test.Contents = - serializer. - Deserialize< - Dictionary>(new JsonTextReader(new StringReader(test.ContentsJson))); + JsonSerializer.Deserialize>(test.ContentsJson, serializerOptions); else if(File.Exists($"{testFile}.contents.json")) { - var sr = new StreamReader($"{testFile}.contents.json"); - test.Contents = serializer.Deserialize>(new JsonTextReader(sr)); + var sr = new FileStream($"{testFile}.contents.json", FileMode.Open); + test.Contents = JsonSerializer.Deserialize>(sr, serializerOptions); } if(test.Contents is null) @@ -222,17 +223,20 @@ public abstract class ReadOnlyFilesystemTest : FilesystemTest Dictionary contents = BuildDirectory(fs, "/"); - var serializer = new JsonSerializer + var serializerOptions = new JsonSerializerOptions { - Formatting = Formatting.Indented, - MaxDepth = 16384, - NullValueHandling = NullValueHandling.Ignore + Converters = + { + new JsonStringEnumConverter() + }, + MaxDepth = 1536, + WriteIndented = true, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + PropertyNameCaseInsensitive = true }; - serializer.Converters.Add(new StringEnumConverter()); - - var sw = new StreamWriter($"{testFile}.contents.json"); - serializer.Serialize(sw, contents); + var sw = new FileStream($"{testFile}.contents.json", FileMode.Create); + JsonSerializer.Serialize(sw, contents, serializerOptions); sw.Close(); } } diff --git a/Aaru.Tests/Images/BlockMediaImageTest.cs b/Aaru.Tests/Images/BlockMediaImageTest.cs index b2f634650..34bca4276 100644 --- a/Aaru.Tests/Images/BlockMediaImageTest.cs +++ b/Aaru.Tests/Images/BlockMediaImageTest.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; using System.IO; +using System.Text.Json; +using System.Text.Json.Serialization; using Aaru.Checksums; using Aaru.CommonTypes; using Aaru.CommonTypes.Enums; @@ -8,8 +10,6 @@ using Aaru.CommonTypes.Interfaces; using Aaru.Core; using Aaru.Tests.Filesystems; using FluentAssertions.Execution; -using Newtonsoft.Json; -using Newtonsoft.Json.Converters; using NUnit.Framework; namespace Aaru.Tests.Images; @@ -205,18 +205,20 @@ public abstract class BlockMediaImageTest : BaseMediaImageTest if(!File.Exists(expectedDataFilename)) continue; - var serializer = new JsonSerializer + var serializerOptions = new JsonSerializerOptions { - Formatting = Formatting.Indented, - MaxDepth = 16384, - NullValueHandling = NullValueHandling.Ignore + Converters = + { + 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 StreamReader(expectedDataFilename); - - VolumeData[] expectedData = serializer.Deserialize(new JsonTextReader(sr)); + var sr = new FileStream(expectedDataFilename, FileMode.Open); + VolumeData[] expectedData = JsonSerializer.Deserialize(sr, serializerOptions); Assert.NotNull(expectedData); @@ -259,8 +261,8 @@ public abstract class BlockMediaImageTest : BaseMediaImageTest }; } - var sw = new StreamWriter(expectedDataFilename); - serializer.Serialize(sw, expectedData); + var sw = new FileStream(expectedDataFilename, FileMode.Create); + JsonSerializer.Serialize(sw, expectedData, serializerOptions); sw.Close(); */ diff --git a/Aaru.Tests/Images/OpticalMediaImageTest.cs b/Aaru.Tests/Images/OpticalMediaImageTest.cs index ca7a954ca..fbd121b79 100644 --- a/Aaru.Tests/Images/OpticalMediaImageTest.cs +++ b/Aaru.Tests/Images/OpticalMediaImageTest.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; using System.Threading.Tasks; using Aaru.Checksums; using Aaru.CommonTypes; @@ -13,8 +15,6 @@ using Aaru.Core; using Aaru.Tests.Filesystems; using FluentAssertions; using FluentAssertions.Execution; -using Newtonsoft.Json; -using Newtonsoft.Json.Converters; using NUnit.Framework; namespace Aaru.Tests.Images; @@ -251,29 +251,31 @@ public abstract class OpticalMediaImageTest : BaseMediaImageTest Assert.AreEqual(ErrorNumber.NoError, ret, string.Format(Localization.Unmountable_0, testFile)); - var serializer = new JsonSerializer + var serializerOptions = new JsonSerializerOptions { - Formatting = Formatting.Indented, - MaxDepth = 16384, - NullValueHandling = NullValueHandling.Ignore + Converters = + { + 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) track.FileSystems[i].Contents = - serializer. - Deserialize< - Dictionary>(new JsonTextReader(new StringReader(track. - FileSystems[i]. - ContentsJson))); + JsonSerializer. + Deserialize>(track.FileSystems[i].ContentsJson, + serializerOptions); else if(File.Exists($"{testFile}.track{track.Number}.filesystem{i}.contents.json")) { - var sr = new StreamReader($"{testFile}.track{track.Number}.filesystem{i - }.contents.json"); + var sr = + new FileStream($"{testFile}.track{track.Number}.filesystem{i}.contents.json", + FileMode.Open); track.FileSystems[i].Contents = - serializer.Deserialize>(new JsonTextReader(sr)); + JsonSerializer.Deserialize>(sr, serializerOptions); } if(track.FileSystems[i].Contents is null) @@ -285,8 +287,8 @@ public abstract class OpticalMediaImageTest : BaseMediaImageTest // Uncomment to generate JSON file /* var contents = ReadOnlyFilesystemTest.BuildDirectory(rofs, "/"); - var sw = new StreamWriter($"{testFile}.track{track.Number}.filesystem{i}.contents.json"); - serializer.Serialize(sw, contents); + var sw = new FileStream($"{testFile}.track{track.Number}.filesystem{i}.contents.json", FileMode.Create); + JsonSerializer.Serialize(sw, contents, serializerOptions); sw.Close();*/ } } diff --git a/Aaru.Tests/Issues/FsExtractHashIssueTest.cs b/Aaru.Tests/Issues/FsExtractHashIssueTest.cs index c9521ef31..f2b143e29 100644 --- a/Aaru.Tests/Issues/FsExtractHashIssueTest.cs +++ b/Aaru.Tests/Issues/FsExtractHashIssueTest.cs @@ -2,6 +2,8 @@ using System; using System.Collections.Generic; using System.IO; using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; using Aaru.Checksums; using Aaru.CommonTypes; using Aaru.CommonTypes.Enums; @@ -9,8 +11,6 @@ using Aaru.CommonTypes.Interfaces; using Aaru.CommonTypes.Structs; using Aaru.Core; using FluentAssertions; -using Newtonsoft.Json; -using Newtonsoft.Json.Converters; using NUnit.Framework; using FileAttributes = Aaru.CommonTypes.Structs.FileAttributes; @@ -75,17 +75,20 @@ public abstract class FsExtractHashIssueTest Assert.True(File.Exists($"{TestFile}.unittest.json")); - var serializer = new JsonSerializer + var serializerOptions = new JsonSerializerOptions { - Formatting = Formatting.Indented, - MaxDepth = 16384, - NullValueHandling = NullValueHandling.Ignore + Converters = + { + 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 StreamReader($"{TestFile}.unittest.json"); - FsExtractHashData expectedData = serializer.Deserialize(new JsonTextReader(sr)); + var sr = new FileStream($"{TestFile}.unittest.json", FileMode.Open); + FsExtractHashData expectedData = JsonSerializer.Deserialize(sr, serializerOptions); Assert.NotNull(expectedData);