forked from KevinDockx/HttpCacheHeaders
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
218 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"sdk": { | ||
"version": "7.0.306", | ||
"rollForward": "latestMinor", | ||
"version": "7.0.400", | ||
"rollForward": "latestPatch", | ||
"allowPrerelease": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/Marvin.Cache.Headers/Interfaces/IStoreKeySerializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Any comments, input: @KevinDockx | ||
// Any issues, requests: https://github.com/KevinDockx/HttpCacheHeaders | ||
|
||
|
||
using System; | ||
using System.Text.Json; | ||
|
||
namespace Marvin.Cache.Headers.Interfaces | ||
{ | ||
/// <summary> | ||
/// Contract for a key serializer, used to serialize a <see cref="StoreKey" /> | ||
/// </summary> | ||
public interface IStoreKeySerializer | ||
{ | ||
/// <summary> | ||
/// Serialize a <see cref="StoreKey"/>. | ||
/// </summary> | ||
/// <param name="keyToSerialize">The <see cref="StoreKey"/> to be serialized.</param> | ||
/// <returns>The <param name="keyToSerialize"/> serialized to a <see cref="string"/>.</returns> | ||
///<exception cref="ArgumentNullException">thrown when the <paramref name="keyToSerialize"/> passed in is <c>null</c>.</exception> | ||
string SerializeStoreKey(StoreKey keyToSerialize); | ||
|
||
/// <summary> | ||
/// Deserialize a <see cref="StoreKey"/> from a <see cref="string"/>. | ||
/// </summary> | ||
/// <param name="storeKeyJson">The Json representation of a <see cref="StoreKey"/> to be deserialized.</param> | ||
/// <returns>The <param name="storeKeyJson"/> deserialized to a <see cref="StoreKey"/>.</returns> | ||
///<exception cref="ArgumentNullException">thrown when the <paramref name="storeKeyJson"/> passed in is <c>null</c>.</exception> | ||
///<exception cref="ArgumentException">thrown when the <paramref name="storeKeyJson"/> passed in is an empty string.</exception> | ||
///<exception cref="JsonException">thrown when the <paramref name="storeKeyJson"/> passed in cannot be deserialized to a <see cref="StoreKey"/>.</exception> | ||
StoreKey DeserializeStoreKey(string storeKeyJson); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Marvin.Cache.Headers/Serialization/DefaultStoreKeySerializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Any comments, input: @KevinDockx | ||
// Any issues, requests: https://github.com/KevinDockx/HttpCacheHeaders | ||
|
||
using System; | ||
using System.Text.Json; | ||
using Marvin.Cache.Headers.Interfaces; | ||
|
||
namespace Marvin.Cache.Headers.Serialization | ||
{ | ||
/// <summary> | ||
/// Serializes a <see cref="StoreKey"/> to JSON./// </summary> | ||
public class DefaultStoreKeySerializer : IStoreKeySerializer | ||
{ | ||
///<inheritDoc/> | ||
public string SerializeStoreKey(StoreKey keyToSerialize) | ||
{ | ||
ArgumentNullException.ThrowIfNull(keyToSerialize); | ||
return JsonSerializer.Serialize(keyToSerialize); | ||
} | ||
|
||
///<inheritDoc/> | ||
public StoreKey DeserializeStoreKey(string storeKeyJson) | ||
{ | ||
if (storeKeyJson == null) | ||
{ | ||
throw new ArgumentNullException(nameof(storeKeyJson)); | ||
} | ||
else if (storeKeyJson.Length == 0) | ||
{ | ||
throw new ArgumentException("The storeKeyJson parameter cannot be an empty string.", nameof(storeKeyJson)); | ||
} | ||
|
||
return JsonSerializer.Deserialize<StoreKey>(storeKeyJson); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
test/Marvin.Cache.Headers.Test/Serialization/DefaultStoreKeySerializerFacts.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System; | ||
using System.Text.Json; | ||
using Marvin.Cache.Headers.Serialization; | ||
using Quibble.Xunit; | ||
using Xunit; | ||
|
||
namespace Marvin.Cache.Headers.Test.Serialization; | ||
|
||
public class DefaultStoreKeySerializerFacts | ||
{ | ||
private readonly DefaultStoreKeySerializer _storeKeySerializer =new(); | ||
|
||
[Fact] | ||
public void SerializeStoreKey_ThrowsArgumentNullException_WhenKeyToSerializeIsNull() | ||
{ | ||
StoreKey keyToSerialize = null; | ||
Assert.Throws<ArgumentNullException>(() =>_storeKeySerializer.SerializeStoreKey(keyToSerialize)); | ||
} | ||
|
||
[Fact] | ||
public void SerializeStoreKey_ReturnsTheKeyToSerializeAsJson_WhenStoreKeyIsNotNull() | ||
{ | ||
var keyToSerialize = new StoreKey | ||
{ | ||
{ "testKey", "TestValue" } | ||
}; | ||
const string expectedStoreKeyJson = "{\"testKey\":\"TestValue\"}"; | ||
|
||
var serializedStoreKey = _storeKeySerializer. | ||
SerializeStoreKey(keyToSerialize); | ||
|
||
JsonAssert.Equal(expectedStoreKeyJson, serializedStoreKey); | ||
} | ||
|
||
[Fact] | ||
public void DeserializeStoreKey_ThrowsArgumentNullException_WhenStoreKeyJsonIsNull() | ||
{ | ||
string storeKeyJson = null; | ||
Assert.Throws<ArgumentNullException>(() => _storeKeySerializer.DeserializeStoreKey(storeKeyJson)); | ||
} | ||
|
||
[Fact] | ||
public void DeserializeStoreKey_ThrowsArgumentException_WhenStoreKeyJsonIsAnEmptyString() | ||
{ | ||
var storeKeyJson = String.Empty; | ||
Assert.Throws<ArgumentException>(() => _storeKeySerializer.DeserializeStoreKey(storeKeyJson)); | ||
} | ||
[Fact] | ||
public void DeserializeStoreKey_ThrowsJsonException_WhenStoreKeyJsonIsInvalid() | ||
{ | ||
const string storeKeyJson = "{"; | ||
Assert.Throws<JsonException>(() => _storeKeySerializer.DeserializeStoreKey(storeKeyJson)); | ||
} | ||
|
||
[Fact] | ||
public void DeserializeStoreKey_ReturnsTheStoreKeyJsonAsAStoreKey_WhenTheStoreKeyJsonIsValidJson() | ||
{ | ||
var expectedStoreKey = new StoreKey | ||
{ | ||
{ "testKey", "TestValue" } | ||
}; | ||
const string storeKeyJson = "{\"testKey\":\"TestValue\"}"; | ||
|
||
var deserializedStoreKey = _storeKeySerializer.DeserializeStoreKey(storeKeyJson); | ||
|
||
Assert.Equal(expectedStoreKey, deserializedStoreKey); | ||
} | ||
} |
Oops, something went wrong.