-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* [Issue #44] Update Json parsing to ignore datetime in string * Add tests using object snapshots for datetime parsing * Updating changelog for the 2.2.1 release
- Loading branch information
Showing
9 changed files
with
117 additions
and
11 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.IO; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Snapper.Json | ||
{ | ||
internal static class JObjectHelper | ||
{ | ||
private static readonly JsonSerializerSettings JsonSettings = new JsonSerializerSettings | ||
{ | ||
DateParseHandling = DateParseHandling.None | ||
}; | ||
|
||
public static JObject ParseFromString(string jsonString) | ||
{ | ||
return JsonConvert.DeserializeObject(jsonString, JsonSettings) as JObject; | ||
} | ||
|
||
public static JObject FromObject(object obj) | ||
{ | ||
if (obj is JObject result) | ||
return result; | ||
|
||
return JObject.FromObject(obj, JsonSerializer.Create(JsonSettings)); | ||
} | ||
} | ||
} |
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
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
project/Tests/Snapper.Tests/SnapperNewtonsoftNuisancesTests.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.Runtime.CompilerServices; | ||
using Snapper.Attributes; | ||
using Xunit; | ||
|
||
namespace Snapper.Tests | ||
{ | ||
/// <summary> | ||
/// This class contains tests which test that the nuisances of newtonsoft are avoided in Snapper | ||
/// </summary> | ||
[StoreSnapshotsPerClass] | ||
public class SnapperNewtonsoftNuisancesTests | ||
{ | ||
[Fact] | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
// Related issue https://github.com/JamesNK/Newtonsoft.Json/issues/862 | ||
public void DateTimeIsParsedAsStringBySnapper_UsingStringSnapshot_FileSnapshot() | ||
{ | ||
const string snapshot = "{" + | ||
"\"Key\" : \"2010-12-31T00:00:00+00:00\"" + | ||
"}"; | ||
|
||
snapshot.ShouldMatchSnapshot(); | ||
} | ||
|
||
[Fact] | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
// Related issue https://github.com/JamesNK/Newtonsoft.Json/issues/862 | ||
public void DateTimeIsParsedAsStringBySnapper_UsingObjectSnapshot_FileSnapshot() | ||
{ | ||
var snapshot = new | ||
{ | ||
Key = new DateTime(2010, 12, 31, 0, 0, 0, DateTimeKind.Utc) | ||
}; | ||
|
||
snapshot.ShouldMatchSnapshot(); | ||
} | ||
|
||
[Fact] | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
// Related issue https://github.com/JamesNK/Newtonsoft.Json/issues/862 | ||
public void DateTimeIsParsedAsStringBySnapper_UsingStringSnapshot_InlineSnapshot() | ||
{ | ||
const string snapshot = "{" + | ||
"\"Key\" : \"2010-12-31T00:00:00+00:00\"" + | ||
"}"; | ||
|
||
snapshot.ShouldMatchInlineSnapshot("{" + | ||
"\"Key\" : \"2010-12-31T00:00:00+00:00\"" + | ||
"}"); | ||
} | ||
|
||
[Fact] | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
// Related issue https://github.com/JamesNK/Newtonsoft.Json/issues/862 | ||
public void DateTimeIsParsedAsStringBySnapper_UsingObjectSnapshot_InlineSnapshot() | ||
{ | ||
var snapshot = new | ||
{ | ||
Key = new DateTime(2010, 12, 31, 0, 0, 0, DateTimeKind.Utc) | ||
}; | ||
|
||
snapshot.ShouldMatchInlineSnapshot("{" + | ||
"\"Key\" : \"2010-12-31T00:00:00Z\"" + | ||
"}"); | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
project/Tests/Snapper.Tests/_snapshots/SnapperNewtonsoftNuisancesTests.json
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,8 @@ | ||
{ | ||
"DateTimeIsParsedAsStringBySnapper_UsingStringSnapshot_FileSnapshot": { | ||
"Key": "2010-12-31T00:00:00+00:00" | ||
}, | ||
"DateTimeIsParsedAsStringBySnapper_UsingObjectSnapshot_FileSnapshot": { | ||
"Key": "2010-12-31T00:00:00Z" | ||
} | ||
} |