This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some custom converters as a hack to get serialization/deserializa…
…tion working. Upgrading to .NET 5.0 should fix this as it supports working with classes without default constructors dotnet/runtime#29895
- Loading branch information
Showing
4 changed files
with
93 additions
and
8 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
37 changes: 37 additions & 0 deletions
37
src/Examples/Shopping.Infrastructure/JsonSerialization/ItemAddedToShoppingCartConverter.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,37 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Shopping.Domain.Events; | ||
|
||
namespace Shopping.Infrastructure.JsonSerialization | ||
{ | ||
// HACK: Add a temporary manual converter. .NET Core 3.1 doesn't support constructors with parameters | ||
public class ItemAddedToShoppingCartConverter : JsonConverter<ItemAddedToShoppingCart> | ||
{ | ||
public override ItemAddedToShoppingCart Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
reader.Read(); // Id property name | ||
reader.Read(); // Id property value | ||
var id = reader.GetGuid(); | ||
|
||
reader.Read(); // Item property name | ||
reader.Read(); // Item property value | ||
var item = reader.GetString(); | ||
|
||
// Read the rest of the object. We don't need any of this | ||
while (reader.Read()) | ||
{ | ||
} | ||
|
||
return new ItemAddedToShoppingCart(id, item); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, ItemAddedToShoppingCart value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStartObject(); | ||
writer.WriteString("Id",value.Id); | ||
writer.WriteString("Item", value.Item); | ||
writer.WriteEndObject(); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/Examples/Shopping.Infrastructure/JsonSerialization/ShoppingCartCreatedConverter.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,32 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Shopping.Domain.Events; | ||
|
||
namespace Shopping.Infrastructure.JsonSerialization | ||
{ | ||
// HACK: Add a temporary manual converter. .NET Core 3.1 doesn't support constructors with parameters | ||
public class ShoppingCartCreatedConverter : JsonConverter<ShoppingCartCreated> | ||
{ | ||
public override ShoppingCartCreated Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
reader.Read(); // Id property name | ||
reader.Read(); // Id property value | ||
var id = reader.GetGuid(); | ||
|
||
// Read the rest of the object. We don't need any of this | ||
while (reader.Read()) | ||
{ | ||
} | ||
|
||
return new ShoppingCartCreated(id); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, ShoppingCartCreated value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStartObject(); | ||
writer.WriteString("Id",value.Id); | ||
writer.WriteEndObject(); | ||
} | ||
} | ||
} |
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