Open gcampbell-epiq opened 7 years ago
What are the workarounds?
@YarekTyshchenko This sample works well
[TestMethod, AutoDomainData]
public void Sut_is_deserialized_correctly()
{
// Arrange
string sourceJson = "{ id: \"112d761b-abb6-4c80-823e-8f6d28df813b:roomRateIda870fc97-d669-4382-9677-59ee0a269a30\" }";
// Act
RoomRateSource source = JsonConvert.DeserializeObject<RoomRateSource>(sourceJson);
// Assert
Assert.AreEqual(source.Id.SupplierId, Guid.Parse("112d761b-abb6-4c80-823e-8f6d28df813b"));
Assert.AreEqual(source.Id.RoomRateId, "roomRateIda870fc97-d669-4382-9677-59ee0a269a30");
}
public sealed class RoomRateSource
{
public RoomRateSource(
RoomRateSourceId id)
{
Id = id;
}
public RoomRateSourceId Id { get; }
}
public struct RoomRateSourceId
{
private const string _discriminator = ":";
public RoomRateSourceId(
Guid supplierId,
string roomRateId)
{
SupplierId = supplierId;
RoomRateId = roomRateId;
}
public Guid SupplierId { get; }
public string RoomRateId { get; }
public override string ToString() =>
$"{SupplierId}{_discriminator}{RoomRateId}";
public static implicit operator RoomRateSourceId(string sourceId)
{
if (string.IsNullOrWhiteSpace(sourceId))
{
throw new ArgumentException($"'{nameof(sourceId)}' cannot be null or whitespace", nameof(sourceId));
}
string[] segments = sourceId.Split(_discriminator, 2, StringSplitOptions.RemoveEmptyEntries);
if (segments.Length < 2)
{
throw new ArgumentException($"The form of '{nameof(sourceId)}' is invalid. It should be form of `{{supplierId}}{_discriminator}{{roomRateId}}`", nameof(sourceId));
}
return new RoomRateSourceId(
supplierId: Guid.Parse(segments[0]),
roomRateId: segments[1]);
}
public static implicit operator string(RoomRateSourceId id) =>
id.ToString();
}
I have a type that I'd like deserialized from a json string to another type. This example demonstrates the issue.
The following is thrown from the call to DeserializeObject.
I know there are workarounds. This is a matter of API cosmetics. Thanks.