2022-01-21 15:36:25 +01:00
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using Bit.Core.Utilities;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Test.Helpers;
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2022-01-21 15:36:25 +01:00
|
|
|
|
public class JsonHelpersTests
|
|
|
|
|
{
|
|
|
|
|
private static void CompareJson<T>(T value, JsonSerializerOptions options, Newtonsoft.Json.JsonSerializerSettings settings)
|
|
|
|
|
{
|
|
|
|
|
var stgJson = JsonSerializer.Serialize(value, options);
|
|
|
|
|
var nsJson = Newtonsoft.Json.JsonConvert.SerializeObject(value, settings);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(stgJson, nsJson);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void DefaultJsonOptions()
|
2022-08-29 20:53:16 +02:00
|
|
|
|
{
|
2022-01-21 15:36:25 +01:00
|
|
|
|
var testObject = new SimpleTestObject
|
|
|
|
|
{
|
|
|
|
|
Id = 0,
|
|
|
|
|
Name = "Test",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
CompareJson(testObject, JsonHelpers.Default, new Newtonsoft.Json.JsonSerializerSettings());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void IndentedJsonOptions()
|
2022-08-29 20:53:16 +02:00
|
|
|
|
{
|
2022-01-21 15:36:25 +01:00
|
|
|
|
var testObject = new SimpleTestObject
|
|
|
|
|
{
|
|
|
|
|
Id = 10,
|
|
|
|
|
Name = "Test Name"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
CompareJson(testObject, JsonHelpers.Indented, new Newtonsoft.Json.JsonSerializerSettings
|
|
|
|
|
{
|
|
|
|
|
Formatting = Newtonsoft.Json.Formatting.Indented,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void NullValueHandlingJsonOptions()
|
|
|
|
|
{
|
|
|
|
|
var testObject = new SimpleTestObject
|
|
|
|
|
{
|
|
|
|
|
Id = 14,
|
|
|
|
|
Name = null,
|
|
|
|
|
};
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2022-01-21 15:36:25 +01:00
|
|
|
|
CompareJson(testObject, JsonHelpers.IgnoreWritingNull, new Newtonsoft.Json.JsonSerializerSettings
|
|
|
|
|
{
|
|
|
|
|
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2022-01-21 15:36:25 +01:00
|
|
|
|
public class SimpleTestObject
|
2022-08-29 20:53:16 +02:00
|
|
|
|
{
|
2022-01-21 15:36:25 +01:00
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
public string Name { get; set; }
|
2022-08-29 20:53:16 +02:00
|
|
|
|
}
|