-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented snapshot stream support. (#173)
- Loading branch information
Showing
14 changed files
with
278 additions
and
51 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/Snapshooter/Core/Serialization/MemoryStreamJsonConverter.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 @@ | ||
using System; | ||
using System.IO; | ||
using Newtonsoft.Json; | ||
|
||
namespace Snapshooter.Core.Serialization; | ||
|
||
public class MemoryStreamJsonConverter : JsonConverter | ||
{ | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return typeof(MemoryStream).IsAssignableFrom(objectType); | ||
} | ||
|
||
public override object ReadJson( | ||
JsonReader reader, | ||
Type objectType, | ||
object existingValue, | ||
JsonSerializer serializer) | ||
{ | ||
var bytes = serializer.Deserialize<byte[]>(reader); | ||
|
||
return bytes != null ? | ||
new MemoryStream(bytes) : | ||
new MemoryStream(); | ||
} | ||
|
||
public override void WriteJson( | ||
JsonWriter writer, | ||
object value, | ||
JsonSerializer serializer) | ||
{ | ||
var bytes = ((MemoryStream)value).ToArray(); | ||
|
||
serializer.Serialize(writer, bytes); | ||
} | ||
} |
83 changes: 44 additions & 39 deletions
83
src/Snapshooter/Core/Serialization/SnapshotSerializerSettings.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 |
---|---|---|
@@ -1,60 +1,65 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Linq; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Converters; | ||
using Newtonsoft.Json.Serialization; | ||
using Snapshooter.Extensions; | ||
|
||
namespace Snapshooter.Core.Serialization | ||
namespace Snapshooter.Core.Serialization; | ||
|
||
/// <summary> | ||
/// </summary> | ||
public abstract class SnapshotSerializerSettings | ||
{ | ||
/// <summary> | ||
/// </summary> | ||
public abstract class SnapshotSerializerSettings | ||
{ | ||
public virtual int Order { get; } = 1; | ||
public virtual int Order { get; } = 1; | ||
|
||
public virtual bool Active { get; } = true; | ||
public virtual bool Active { get; } = true; | ||
|
||
/// <summary> | ||
/// </summary> | ||
public static JsonSerializerSettings DefaultJsonSerializerSettings => | ||
new JsonSerializerSettings | ||
{ | ||
ReferenceLoopHandling = ReferenceLoopHandling.Ignore, | ||
Formatting = Formatting.Indented, | ||
NullValueHandling = NullValueHandling.Include, | ||
DateFormatHandling = DateFormatHandling.IsoDateFormat, | ||
Culture = CultureInfo.InvariantCulture, | ||
ContractResolver = ChildFirstContractResolver.Instance, | ||
Converters = new List<JsonConverter> {new StringEnumConverter()} | ||
}; | ||
|
||
public abstract JsonSerializerSettings Extend(JsonSerializerSettings settings); | ||
|
||
private class ChildFirstContractResolver : DefaultContractResolver | ||
/// <summary> | ||
/// </summary> | ||
public static JsonSerializerSettings DefaultJsonSerializerSettings => | ||
new JsonSerializerSettings | ||
{ | ||
static ChildFirstContractResolver() { Instance = new ChildFirstContractResolver(); } | ||
ReferenceLoopHandling = ReferenceLoopHandling.Ignore, | ||
Formatting = Formatting.Indented, | ||
NullValueHandling = NullValueHandling.Include, | ||
DateFormatHandling = DateFormatHandling.IsoDateFormat, | ||
Culture = CultureInfo.InvariantCulture, | ||
ContractResolver = ChildFirstContractResolver.Instance, | ||
Converters = new List<JsonConverter> | ||
{ | ||
new StringEnumConverter(), | ||
new MemoryStreamJsonConverter(), | ||
new StreamJsonConverter() | ||
} | ||
}; | ||
|
||
public static ChildFirstContractResolver Instance { get; } | ||
public abstract JsonSerializerSettings Extend(JsonSerializerSettings settings); | ||
|
||
protected override IList<JsonProperty> CreateProperties( | ||
Type type, MemberSerialization memberSerialization) | ||
{ | ||
IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization); | ||
private class ChildFirstContractResolver : DefaultContractResolver | ||
{ | ||
public static ChildFirstContractResolver Instance { get; } | ||
= new ChildFirstContractResolver(); | ||
|
||
if (properties != null) | ||
{ | ||
properties = properties.OrderBy(p => | ||
{ | ||
IEnumerable<Type> d = ((Type)p.DeclaringType).BaseTypesAndSelf().ToList(); | ||
return 1000 - d.Count(); | ||
}).ToList(); | ||
} | ||
protected override IList<JsonProperty> CreateProperties( | ||
Type type, MemberSerialization memberSerialization) | ||
{ | ||
IList<JsonProperty> properties = | ||
base.CreateProperties(type, memberSerialization); | ||
|
||
return properties; | ||
if (properties != null) | ||
{ | ||
properties = properties | ||
.OrderBy(p => 1000 - ((Type)p.DeclaringType) | ||
.BaseTypesAndSelf() | ||
.Count()) | ||
.ToList(); | ||
} | ||
|
||
return properties; | ||
} | ||
} | ||
} |
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,39 @@ | ||
using System; | ||
using System.IO; | ||
using Newtonsoft.Json; | ||
|
||
namespace Snapshooter.Core.Serialization; | ||
|
||
public class StreamJsonConverter : JsonConverter | ||
{ | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return typeof(Stream).IsAssignableFrom(objectType); | ||
} | ||
|
||
public override object ReadJson( | ||
JsonReader reader, | ||
Type objectType, | ||
object existingValue, | ||
JsonSerializer serializer) | ||
{ | ||
var bytes = serializer.Deserialize<byte[]>(reader); | ||
|
||
return bytes != null ? | ||
new MemoryStream(bytes) : | ||
new MemoryStream(); | ||
} | ||
|
||
public override void WriteJson( | ||
JsonWriter writer, | ||
object value, | ||
JsonSerializer serializer) | ||
{ | ||
using var stream = (Stream)value; | ||
|
||
byte[] bytes = new byte[stream.Length]; | ||
stream.Read(bytes, 0, bytes.Length); | ||
|
||
serializer.Serialize(writer, bytes); | ||
} | ||
} |
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
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
1 change: 1 addition & 0 deletions
1
....Tests/__snapshots__/SnapshotTests.Match_FactMatchFileStreamSnapshot_SuccessfulMatch.snap
Large diffs are not rendered by default.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
...ests/__snapshots__/SnapshotTests.Match_FactMatchMemoryStreamSnapshot_SuccessfulMatch.snap
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 @@ | ||
"Rm9vIEJhciAzNQ==" |
7 changes: 7 additions & 0 deletions
7
...napshots__/SnapshotTests.Match_FactMatchObjectWithAllStreamsSnapshot_SuccessfulMatch.snap
Large diffs are not rendered by default.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
...napshots__/SnapshotTests.Match_FactMatchObjectWithFileStreamSnapshot_SuccessfulMatch.snap
Large diffs are not rendered by default.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
...pshots__/SnapshotTests.Match_FactMatchObjectWithMemoryStreamSnapshot_SuccessfulMatch.snap
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,5 @@ | ||
{ | ||
"FirstName": "Foo", | ||
"Age": 35, | ||
"Picture": "Rm9vIEJhciAzNQ==" | ||
} |
5 changes: 5 additions & 0 deletions
5
.../__snapshots__/SnapshotTests.Match_FactMatchObjectWithStreamSnapshot_SuccessfulMatch.snap
Large diffs are not rendered by default.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
...unit.Tests/__snapshots__/SnapshotTests.Match_FactMatchStreamSnapshot_SuccessfulMatch.snap
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.