-
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.
Switch to using JsonNode rather than JsonElement
- Loading branch information
Showing
19 changed files
with
180 additions
and
269 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
This file was deleted.
Oops, something went wrong.
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,51 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
|
||
namespace Snapper.Json; | ||
|
||
internal static class JsonNodeHelper | ||
{ | ||
public static JsonNode ParseFromString(string jsonString, SnapshotSettings snapshotSettings) | ||
{ | ||
return JsonSerializer.Deserialize<JsonNode>(jsonString, CreateSerialiserSettings(snapshotSettings)) | ||
?? throw new InvalidOperationException(); | ||
} | ||
|
||
public static JsonNode FromObject(object? obj, SnapshotSettings snapshotSettings) | ||
{ | ||
if (obj is JsonNode node) | ||
return node; | ||
|
||
return JsonSerializer.SerializeToNode(obj, CreateSerialiserSettings(snapshotSettings)) | ||
?? throw new InvalidOperationException(); | ||
} | ||
|
||
public static string ToString(JsonNode json, SnapshotSettings? snapshotSettings = null) | ||
{ | ||
return JsonSerializer.Serialize(json, CreateSerialiserSettings(snapshotSettings ?? SnapshotSettings.New())); | ||
} | ||
|
||
public static bool JsonEquals(JsonNode x, JsonNode y) | ||
=> JsonNode.DeepEquals(x, y); | ||
|
||
private static JsonSerializerOptions CreateSerialiserSettings(SnapshotSettings snapshotSettings) | ||
{ | ||
var serializerSettings = new JsonSerializerOptions | ||
{ | ||
WriteIndented = true | ||
}; | ||
|
||
SnapshotSettings.GlobalSnapshotSerialiserSettings?.Invoke(serializerSettings); | ||
snapshotSettings.SnapshotSerialiserSettings?.Invoke(serializerSettings); | ||
|
||
return serializerSettings; | ||
} | ||
|
||
public static bool TryGetValue(this JsonNode node, string propertyName, out JsonNode value) | ||
{ | ||
var prop = node[propertyName]; | ||
value = prop ?? new JsonObject(); | ||
return prop != null; | ||
} | ||
} |
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,13 +1,13 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
using Snapper.Core; | ||
|
||
namespace Snapper.Json; | ||
|
||
internal record JsonSnapshot(SnapshotId Id, JsonElement Value) | ||
internal record JsonSnapshot(SnapshotId Id, JsonNode Value) | ||
{ | ||
public bool CompareValues(JsonSnapshot other) | ||
{ | ||
return JsonElementHelper.JsonEquals(Value, other.Value); | ||
return JsonNodeHelper.JsonEquals(Value, other.Value); | ||
} | ||
} | ||
|
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
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
Oops, something went wrong.