Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions SysML2.NET.Serializer.Json.Tests/PsmDeSerializerTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public void VerifyDeSerializeResponse()

Assert.That(() => this.deSerializer.DeSerializeResponse<Project>(notAnObject, SerializationModeKind.JSON, false),
Throws.TypeOf<JsonException>());

using var unknownType = ToStream("""{ "@type": "ProjectResponse", "name": "the name" }""");

Assert.That(() => this.deSerializer.DeSerializeResponse<Project>(unknownType, SerializationModeKind.JSON, false),
Throws.TypeOf<JsonException>().With.Message.Contains("ProjectResponse"));
}

[Test]
Expand Down Expand Up @@ -121,6 +126,11 @@ public void VerifyDeSerializeRequest()

Assert.That(() => this.deSerializer.DeSerializeRequest<BranchRequest>(mismatched, SerializationModeKind.JSON, false),
Throws.TypeOf<JsonException>());

using var unknownType = ToStream("""{ "@type": "BranchRequest", "name": "feature" }""");

Assert.That(() => this.deSerializer.DeSerializeRequest<BranchRequest>(unknownType, SerializationModeKind.JSON, false),
Throws.TypeOf<JsonException>().With.Message.Contains("BranchRequest"));
}

[Test]
Expand Down
32 changes: 32 additions & 0 deletions SysML2.NET.Serializer.Json/DeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ public async Task<IEnumerable<IIdentified>> DeSerializeAsync(Stream stream, Seri
/// <param name="serializationModeKind">The <see cref="SerializationModeKind"/> to use</param>
/// <param name="deserializeDerivedProperties">Asserts that the deserializer should deserialize derived properties if present or if they are ignored</param>
/// <returns>an instance of <typeparamref name="T"/></returns>
/// <exception cref="JsonException">Thrown when the payload is not valid JSON, declares an unknown <c>@type</c>, or is not a <typeparamref name="T"/></exception>
/// <exception cref="SerializationException">Thrown when an object carries no <c>@type</c> discriminator or the payload exceeds the maximum length</exception>
/// <exception cref="InvalidOperationException">Thrown when a property value has a JSON token type that the property does not accept</exception>
/// <exception cref="NotSupportedException">Thrown when the <paramref name="serializationModeKind"/> is not supported</exception>
public T DeSerializeRequest<T>(Stream stream, SerializationModeKind serializationModeKind, bool deserializeDerivedProperties) where T : IRequest
{
return ReadPooled(stream, (byte[] buffer, int length) => this.ReadSingle<T>(buffer, length, serializationModeKind, deserializeDerivedProperties, true));
Expand All @@ -176,6 +180,10 @@ public T DeSerializeRequest<T>(Stream stream, SerializationModeKind serializatio
/// <param name="serializationModeKind">The <see cref="SerializationModeKind"/> to use</param>
/// <param name="deserializeDerivedProperties">Asserts that the deserializer should deserialize derived properties if present or if they are ignored</param>
/// <returns>an <see cref="IEnumerable{T}"/></returns>
/// <exception cref="JsonException">Thrown when the payload is not valid JSON, declares an unknown <c>@type</c>, or is not a <typeparamref name="T"/></exception>
/// <exception cref="SerializationException">Thrown when an object carries no <c>@type</c> discriminator or the payload exceeds the maximum length</exception>
/// <exception cref="InvalidOperationException">Thrown when a property value has a JSON token type that the property does not accept</exception>
/// <exception cref="NotSupportedException">Thrown when the <paramref name="serializationModeKind"/> is not supported</exception>
public IEnumerable<T> DeSerializeRequests<T>(Stream stream, SerializationModeKind serializationModeKind, bool deserializeDerivedProperties) where T : IRequest
{
return ReadPooled(stream, (byte[] buffer, int length) => this.ReadMany<T>(buffer, length, serializationModeKind, deserializeDerivedProperties, true));
Expand All @@ -189,6 +197,10 @@ public IEnumerable<T> DeSerializeRequests<T>(Stream stream, SerializationModeKin
/// <param name="serializationModeKind">The <see cref="SerializationModeKind"/> to use</param>
/// <param name="deserializeDerivedProperties">Asserts that the deserializer should deserialize derived properties if present or if they are ignored</param>
/// <returns>an instance of <typeparamref name="T"/></returns>
/// <exception cref="JsonException">Thrown when the payload is not valid JSON, declares an unknown <c>@type</c>, or is not a <typeparamref name="T"/></exception>
/// <exception cref="SerializationException">Thrown when an object carries no <c>@type</c> discriminator or the payload exceeds the maximum length</exception>
/// <exception cref="InvalidOperationException">Thrown when a property value has a JSON token type that the property does not accept</exception>
/// <exception cref="NotSupportedException">Thrown when the <paramref name="serializationModeKind"/> is not supported</exception>
public T DeSerializeResponse<T>(Stream stream, SerializationModeKind serializationModeKind, bool deserializeDerivedProperties) where T : IResponse
{
return ReadPooled(stream, (byte[] buffer, int length) => this.ReadSingle<T>(buffer, length, serializationModeKind, deserializeDerivedProperties, false));
Expand All @@ -202,6 +214,10 @@ public T DeSerializeResponse<T>(Stream stream, SerializationModeKind serializati
/// <param name="serializationModeKind">The <see cref="SerializationModeKind"/> to use</param>
/// <param name="deserializeDerivedProperties">Asserts that the deserializer should deserialize derived properties if present or if they are ignored</param>
/// <returns>an <see cref="IEnumerable{T}"/></returns>
/// <exception cref="JsonException">Thrown when the payload is not valid JSON, declares an unknown <c>@type</c>, or is not a <typeparamref name="T"/></exception>
/// <exception cref="SerializationException">Thrown when an object carries no <c>@type</c> discriminator or the payload exceeds the maximum length</exception>
/// <exception cref="InvalidOperationException">Thrown when a property value has a JSON token type that the property does not accept</exception>
/// <exception cref="NotSupportedException">Thrown when the <paramref name="serializationModeKind"/> is not supported</exception>
public IEnumerable<T> DeSerializeResponses<T>(Stream stream, SerializationModeKind serializationModeKind, bool deserializeDerivedProperties) where T : IResponse
{
return ReadPooled(stream, (byte[] buffer, int length) => this.ReadMany<T>(buffer, length, serializationModeKind, deserializeDerivedProperties, false));
Expand All @@ -216,6 +232,10 @@ public IEnumerable<T> DeSerializeResponses<T>(Stream stream, SerializationModeKi
/// <param name="deserializeDerivedProperties">Asserts that the deserializer should deserialize derived properties if present or if they are ignored</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to cancel the operation</param>
/// <returns>an instance of <typeparamref name="T"/></returns>
/// <exception cref="JsonException">Thrown when the payload is not valid JSON, declares an unknown <c>@type</c>, or is not a <typeparamref name="T"/></exception>
/// <exception cref="SerializationException">Thrown when an object carries no <c>@type</c> discriminator or the payload exceeds the maximum length</exception>
/// <exception cref="InvalidOperationException">Thrown when a property value has a JSON token type that the property does not accept</exception>
/// <exception cref="NotSupportedException">Thrown when the <paramref name="serializationModeKind"/> is not supported</exception>
public Task<T> DeSerializeRequestAsync<T>(Stream stream, SerializationModeKind serializationModeKind, bool deserializeDerivedProperties, CancellationToken cancellationToken) where T : IRequest
{
return ReadPooledAsync(stream, (byte[] buffer, int length) => this.ReadSingle<T>(buffer, length, serializationModeKind, deserializeDerivedProperties, true), cancellationToken);
Expand All @@ -230,6 +250,10 @@ public Task<T> DeSerializeRequestAsync<T>(Stream stream, SerializationModeKind s
/// <param name="deserializeDerivedProperties">Asserts that the deserializer should deserialize derived properties if present or if they are ignored</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to cancel the operation</param>
/// <returns>an <see cref="IEnumerable{T}"/></returns>
/// <exception cref="JsonException">Thrown when the payload is not valid JSON, declares an unknown <c>@type</c>, or is not a <typeparamref name="T"/></exception>
/// <exception cref="SerializationException">Thrown when an object carries no <c>@type</c> discriminator or the payload exceeds the maximum length</exception>
/// <exception cref="InvalidOperationException">Thrown when a property value has a JSON token type that the property does not accept</exception>
/// <exception cref="NotSupportedException">Thrown when the <paramref name="serializationModeKind"/> is not supported</exception>
public async Task<IEnumerable<T>> DeSerializeRequestsAsync<T>(Stream stream, SerializationModeKind serializationModeKind, bool deserializeDerivedProperties, CancellationToken cancellationToken) where T : IRequest
{
return await ReadPooledAsync(stream, (byte[] buffer, int length) => this.ReadMany<T>(buffer, length, serializationModeKind, deserializeDerivedProperties, true), cancellationToken);
Expand All @@ -244,6 +268,10 @@ public async Task<IEnumerable<T>> DeSerializeRequestsAsync<T>(Stream stream, Ser
/// <param name="deserializeDerivedProperties">Asserts that the deserializer should deserialize derived properties if present or if they are ignored</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to cancel the operation</param>
/// <returns>an instance of <typeparamref name="T"/></returns>
/// <exception cref="JsonException">Thrown when the payload is not valid JSON, declares an unknown <c>@type</c>, or is not a <typeparamref name="T"/></exception>
/// <exception cref="SerializationException">Thrown when an object carries no <c>@type</c> discriminator or the payload exceeds the maximum length</exception>
/// <exception cref="InvalidOperationException">Thrown when a property value has a JSON token type that the property does not accept</exception>
/// <exception cref="NotSupportedException">Thrown when the <paramref name="serializationModeKind"/> is not supported</exception>
public Task<T> DeSerializeResponseAsync<T>(Stream stream, SerializationModeKind serializationModeKind, bool deserializeDerivedProperties, CancellationToken cancellationToken) where T : IResponse
{
return ReadPooledAsync(stream, (byte[] buffer, int length) => this.ReadSingle<T>(buffer, length, serializationModeKind, deserializeDerivedProperties, false), cancellationToken);
Expand All @@ -258,6 +286,10 @@ public Task<T> DeSerializeResponseAsync<T>(Stream stream, SerializationModeKind
/// <param name="deserializeDerivedProperties">Asserts that the deserializer should deserialize derived properties if present or if they are ignored</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to cancel the operation</param>
/// <returns>an <see cref="IEnumerable{T}"/></returns>
/// <exception cref="JsonException">Thrown when the payload is not valid JSON, declares an unknown <c>@type</c>, or is not a <typeparamref name="T"/></exception>
/// <exception cref="SerializationException">Thrown when an object carries no <c>@type</c> discriminator or the payload exceeds the maximum length</exception>
/// <exception cref="InvalidOperationException">Thrown when a property value has a JSON token type that the property does not accept</exception>
/// <exception cref="NotSupportedException">Thrown when the <paramref name="serializationModeKind"/> is not supported</exception>
public async Task<IEnumerable<T>> DeSerializeResponsesAsync<T>(Stream stream, SerializationModeKind serializationModeKind, bool deserializeDerivedProperties, CancellationToken cancellationToken) where T : IResponse
{
return await ReadPooledAsync(stream, (byte[] buffer, int length) => this.ReadMany<T>(buffer, length, serializationModeKind, deserializeDerivedProperties, false), cancellationToken);
Expand Down
Loading
Loading