-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathJsonTest.cs
67 lines (55 loc) · 2.06 KB
/
JsonTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System.IO;
using System.Linq;
using System.Text.Json;
using Manx_Search_Data.TestData;
using Manx_Search_Data.TestUtil;
using NUnit.Framework;
using JsonSerializer = System.Text.Json.JsonSerializer;
namespace Manx_Search_Data;
[TestFixture]
public class JsonTest
{
[DatapointSource]
// ReSharper disable once UnusedMember.Global
public OpenSourceDocument[] AllDocuments = Documents.AllDocuments.OfType<OpenSourceDocument>().ToArray();
public dynamic ReadNewtonsoft(string input) => Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(input);
public dynamic DeserializeJson(string input)
{
return JsonSerializer.Deserialize<dynamic>(input, options: new JsonSerializerOptions
{
AllowTrailingCommas = true,
});
// catch
// {
// Newtonsoft can handle newlines in strings
// return ReadNewtonsoft(input);
// throw;
// }
}
private JsonSerializerOptions _options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true,
};
/// <summary>
/// Ensure each file is valid for JSON.NET, which parses more strictly than Newtonsoft
/// </summary>
/// <param name="document"></param>
[Theory]
public void CsvFileIsJalidJsonNet(OpenSourceDocument document)
{
var path = document.LocationOnDisk + "/manifest.json.txt";
var input = File.ReadAllText(path);
var inputJson = DeserializeJson(input);
// var output = JsonConvert.SerializeObject(inputJson, new JsonSerializerSettings()
// {
// Formatting = Formatting.Indented,
// Converters = { new ExpandoObjectConverter() },
// });
var output = JsonSerializer.Serialize(inputJson, _options);
// This modifies files in the test dir, not under source control.
// Copy them over if you want to replace
// File.WriteAllText(path, output);
Assert.AreEqual(input, output);
}
}