Skip to content

Commit

Permalink
Add system text json support for actor serialization
Browse files Browse the repository at this point in the history
Signed-off-by: Erik O'Leary <erik.m.oleary@gmail.com>
  • Loading branch information
onionhammer committed Jul 10, 2023
1 parent c99475b commit 8d25615
Show file tree
Hide file tree
Showing 19 changed files with 506 additions and 111 deletions.
11 changes: 10 additions & 1 deletion src/Dapr.Actors/Client/ActorProxyFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Dapr.Actors.Client
using System;
using System.Net.Http;
using Dapr.Actors.Builder;
using Dapr.Actors.Communication;
using Dapr.Actors.Communication.Client;

/// <summary>
Expand Down Expand Up @@ -79,7 +80,15 @@ public object CreateActorProxy(ActorId actorId, Type actorInterfaceType, string
options ??= this.DefaultOptions;

var daprInteractor = new DaprHttpInteractor(this.handler, options.HttpEndpoint, options.DaprApiToken, options.RequestTimeout);
var remotingClient = new ActorRemotingClient(daprInteractor);

// provide a serializer if 'useJsonSerialization' is true and no serialization provider is provided.
IActorMessageBodySerializationProvider serializationProvider = null;
if (options.UseJsonSerialization)
{
serializationProvider = new ActorMessageBodyJsonSerializationProvider(options.JsonSerializerOptions);

Check warning on line 88 in src/Dapr.Actors/Client/ActorProxyFactory.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Client/ActorProxyFactory.cs#L88

Added line #L88 was not covered by tests
}

var remotingClient = new ActorRemotingClient(daprInteractor, serializationProvider);
var proxyGenerator = ActorCodeBuilder.GetOrCreateProxyGenerator(actorInterfaceType);
var actorProxy = proxyGenerator.CreateActorProxy();
actorProxy.Initialize(remotingClient, actorId, actorType, options);
Expand Down
5 changes: 5 additions & 0 deletions src/Dapr.Actors/Client/ActorProxyOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,10 @@ public JsonSerializerOptions JsonSerializerOptions
/// The timeout allowed for an actor request. Can be set to System.Threading.Timeout.InfiniteTimeSpan to disable any timeouts.
/// </summary>
public TimeSpan? RequestTimeout { get; set; } = null;

/// <summary>
/// Enable JSON serialization for actor proxy message serialization in both remoting and non-remoting invocations.
/// </summary>
public bool UseJsonSerialization { get; set; }

Check warning on line 69 in src/Dapr.Actors/Client/ActorProxyOptions.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Client/ActorProxyOptions.cs#L69

Added line #L69 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace Dapr.Actors.Communication
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using System.Xml;

/// <summary>
Expand Down Expand Up @@ -185,21 +186,21 @@ byte[] IActorRequestMessageBodySerializer.Serialize(IActorRequestMessageBody act
return stream.ToArray();
}

IActorRequestMessageBody IActorRequestMessageBodySerializer.Deserialize(Stream stream)
ValueTask<IActorRequestMessageBody> IActorRequestMessageBodySerializer.DeserializeAsync(Stream stream)
{
if (stream == null)
{
return null;
return default;

Check warning on line 193 in src/Dapr.Actors/Communication/ActorMessageBodyDataContractSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyDataContractSerializationProvider.cs#L193

Added line #L193 was not covered by tests
}

if (stream.Length == 0)
{
return null;
return default;

Check warning on line 198 in src/Dapr.Actors/Communication/ActorMessageBodyDataContractSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyDataContractSerializationProvider.cs#L198

Added line #L198 was not covered by tests
}

stream.Position = 0;
using var reader = this.CreateXmlDictionaryReader(stream);
return (TRequest)this.serializer.ReadObject(reader);
return new ValueTask<IActorRequestMessageBody>((TRequest)this.serializer.ReadObject(reader));

Check warning on line 203 in src/Dapr.Actors/Communication/ActorMessageBodyDataContractSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyDataContractSerializationProvider.cs#L203

Added line #L203 was not covered by tests
}

byte[] IActorResponseMessageBodySerializer.Serialize(IActorResponseMessageBody actorResponseMessageBody)
Expand All @@ -217,11 +218,11 @@ byte[] IActorResponseMessageBodySerializer.Serialize(IActorResponseMessageBody a
return stream.ToArray();
}

IActorResponseMessageBody IActorResponseMessageBodySerializer.Deserialize(Stream messageBody)
ValueTask<IActorResponseMessageBody> IActorResponseMessageBodySerializer.DeserializeAsync(Stream messageBody)
{
if (messageBody == null)
{
return null;
return default;

Check warning on line 225 in src/Dapr.Actors/Communication/ActorMessageBodyDataContractSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyDataContractSerializationProvider.cs#L225

Added line #L225 was not covered by tests
}

// TODO check performance
Expand All @@ -231,11 +232,11 @@ IActorResponseMessageBody IActorResponseMessageBodySerializer.Deserialize(Stream

if (stream.Capacity == 0)
{
return null;
return default;

Check warning on line 235 in src/Dapr.Actors/Communication/ActorMessageBodyDataContractSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyDataContractSerializationProvider.cs#L235

Added line #L235 was not covered by tests
}

using var reader = this.CreateXmlDictionaryReader(stream);
return (TResponse)this.serializer.ReadObject(reader);
return new ValueTask<IActorResponseMessageBody>((TResponse)this.serializer.ReadObject(reader));

Check warning on line 239 in src/Dapr.Actors/Communication/ActorMessageBodyDataContractSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyDataContractSerializationProvider.cs#L239

Added line #L239 was not covered by tests
}

/// <summary>
Expand Down
100 changes: 100 additions & 0 deletions src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// ------------------------------------------------------------------------
// Copyright 2021 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Dapr.Actors.Communication
{
internal class ActorMessageBodyJsonConverter<T> : JsonConverter<T>
{
private readonly List<Type> methodRequestParameterTypes;
private readonly List<Type> wrappedRequestMessageTypes;
private readonly Type wrapperMessageType;

public ActorMessageBodyJsonConverter(
List<Type> methodRequestParameterTypes,
List<Type> wrappedRequestMessageTypes = null
)

Check warning on line 30 in src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs#L27-L30

Added lines #L27 - L30 were not covered by tests
{
this.methodRequestParameterTypes = methodRequestParameterTypes;
this.wrappedRequestMessageTypes = wrappedRequestMessageTypes;

Check warning on line 33 in src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs#L32-L33

Added lines #L32 - L33 were not covered by tests

if (this.wrappedRequestMessageTypes != null && this.wrappedRequestMessageTypes.Count == 1)
{
this.wrapperMessageType = this.wrappedRequestMessageTypes[0];

Check warning on line 37 in src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs#L37

Added line #L37 was not covered by tests
}

// If T is of WrappedMessageBody, then get the 'Value' property.
if (typeof(T).IsAssignableFrom(typeof(WrappedMessageBody)))

Check warning on line 41 in src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs#L41

Added line #L41 was not covered by tests
{
}
}

Check warning on line 44 in src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs#L44

Added line #L44 was not covered by tests

public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
// Ensure start-of-object, then advance
if (reader.TokenType != JsonTokenType.StartObject) throw new JsonException();
reader.Read();

Check warning on line 50 in src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs#L50

Added line #L50 was not covered by tests

// Ensure property name, then advance
if (reader.TokenType != JsonTokenType.PropertyName || reader.GetString() != "value") throw new JsonException();
reader.Read();

Check warning on line 54 in src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs#L54

Added line #L54 was not covered by tests

// If the value is null, return null.
if (reader.TokenType == JsonTokenType.Null)
{
// Read the end object token.
reader.Read();
return default;

Check warning on line 61 in src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs#L60-L61

Added lines #L60 - L61 were not covered by tests
}

// If the value is an object, deserialize it to wrapper message type
if (this.wrapperMessageType != null)
{
var value = JsonSerializer.Deserialize(ref reader, this.wrapperMessageType, options);

Check warning on line 67 in src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs#L67

Added line #L67 was not covered by tests

// Construct a new WrappedMessageBody with the deserialized value.
var wrapper = new WrappedMessageBody()
{
Value = value,
};

Check warning on line 73 in src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs#L70-L73

Added lines #L70 - L73 were not covered by tests

// Read the end object token.
reader.Read();

Check warning on line 76 in src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs#L76

Added line #L76 was not covered by tests

// Coerce the type to T; required because WrappedMessageBody inherits from two separate interfaces, which
// cannot both be used as generic constraints
return (T)((object)wrapper);

Check warning on line 80 in src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs#L80

Added line #L80 was not covered by tests
}

return JsonSerializer.Deserialize<T>(ref reader, options);

Check warning on line 83 in src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs#L83

Added line #L83 was not covered by tests
}

public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WritePropertyName("value");

Check warning on line 89 in src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs#L88-L89

Added lines #L88 - L89 were not covered by tests

if (value is WrappedMessageBody body)
{
JsonSerializer.Serialize(writer, body.Value, body.Value.GetType(), options);

Check warning on line 93 in src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs#L93

Added line #L93 was not covered by tests
}
else
writer.WriteNullValue();
writer.WriteEndObject();
}

Check warning on line 98 in src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonConverter.cs#L96-L98

Added lines #L96 - L98 were not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
// ------------------------------------------------------------------------
// Copyright 2021 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------

namespace Dapr.Actors.Communication
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
using System.Xml;

/// <summary>
/// This is the implmentation for <see cref="IActorMessageBodySerializationProvider"/>used by remoting service and client during
/// request/response serialization . It uses request Wrapping and data contract for serialization.
/// </summary>
internal class ActorMessageBodyJsonSerializationProvider : IActorMessageBodySerializationProvider
{
public JsonSerializerOptions Options { get; }

Check warning on line 29 in src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs#L29

Added line #L29 was not covered by tests

/// <summary>
/// Initializes a new instance of the <see cref="ActorMessageBodyJsonSerializationProvider"/> class.
/// </summary>
public ActorMessageBodyJsonSerializationProvider(JsonSerializerOptions options)

Check warning on line 34 in src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs#L34

Added line #L34 was not covered by tests
{
Options = options;
}

Check warning on line 37 in src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs#L36-L37

Added lines #L36 - L37 were not covered by tests

/// <summary>
/// Creates a MessageFactory for Wrapped Message Json Remoting Types. This is used to create Remoting Request/Response objects.
/// </summary>
/// <returns>
/// <see cref="IActorMessageBodyFactory" /> that provides an instance of the factory for creating
/// remoting request and response message bodies.
/// </returns>
public IActorMessageBodyFactory CreateMessageBodyFactory()
{
return new WrappedRequestMessageFactory();

Check warning on line 48 in src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs#L48

Added line #L48 was not covered by tests
}

/// <summary>
/// Creates IActorRequestMessageBodySerializer for a serviceInterface using Wrapped Message Json implementation.
/// </summary>
/// <param name="serviceInterfaceType">The remoted service interface.</param>
/// <param name="methodRequestParameterTypes">The union of parameter types of all of the methods of the specified interface.</param>
/// <param name="wrappedRequestMessageTypes">Wrapped Request Types for all Methods.</param>
/// <returns>
/// An instance of the <see cref="IActorRequestMessageBodySerializer" /> that can serialize the service
/// actor request message body to a messaging body for transferring over the transport.
/// </returns>
public IActorRequestMessageBodySerializer CreateRequestMessageBodySerializer(
Type serviceInterfaceType,
IEnumerable<Type> methodRequestParameterTypes,
IEnumerable<Type> wrappedRequestMessageTypes = null)
{
return new MemoryStreamMessageBodySerializer<WrappedMessageBody, WrappedMessageBody>(Options, serviceInterfaceType, methodRequestParameterTypes, wrappedRequestMessageTypes);

Check warning on line 66 in src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs#L66

Added line #L66 was not covered by tests
}

/// <summary>
/// Creates IActorResponseMessageBodySerializer for a serviceInterface using Wrapped Message Json implementation.
/// </summary>
/// <param name="serviceInterfaceType">The remoted service interface.</param>
/// <param name="methodReturnTypes">The return types of all of the methods of the specified interface.</param>
/// <param name="wrappedResponseMessageTypes">Wrapped Response Types for all remoting methods.</param>
/// <returns>
/// An instance of the <see cref="IActorResponseMessageBodySerializer" /> that can serialize the service
/// actor response message body to a messaging body for transferring over the transport.
/// </returns>
public IActorResponseMessageBodySerializer CreateResponseMessageBodySerializer(
Type serviceInterfaceType,
IEnumerable<Type> methodReturnTypes,
IEnumerable<Type> wrappedResponseMessageTypes = null)
{
return new MemoryStreamMessageBodySerializer<WrappedMessageBody, WrappedMessageBody>(Options, serviceInterfaceType, methodReturnTypes, wrappedResponseMessageTypes);

Check warning on line 84 in src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs#L84

Added line #L84 was not covered by tests
}

/// <summary>
/// Default serializer for service remoting request and response message body that uses the
/// memory stream to create outgoing message buffers.
/// </summary>
private class MemoryStreamMessageBodySerializer<TRequest, TResponse> :
IActorRequestMessageBodySerializer,
IActorResponseMessageBodySerializer
where TRequest : IActorRequestMessageBody
where TResponse : IActorResponseMessageBody
{
private readonly JsonSerializerOptions serializerOptions;

public MemoryStreamMessageBodySerializer(
JsonSerializerOptions serializerOptions,
Type serviceInterfaceType,
IEnumerable<Type> methodRequestParameterTypes,
IEnumerable<Type> wrappedRequestMessageTypes = null)

Check warning on line 103 in src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs#L99-L103

Added lines #L99 - L103 were not covered by tests
{
var _methodRequestParameterTypes = new List<Type>(methodRequestParameterTypes);
var _wrappedRequestMessageTypes = new List<Type>(wrappedRequestMessageTypes);

Check warning on line 106 in src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs#L105-L106

Added lines #L105 - L106 were not covered by tests

this.serializerOptions = new(serializerOptions)
{
// Workaround since WrappedMessageBody creates an object
// with parameters as fields
IncludeFields = true,
};

Check warning on line 113 in src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs#L108-L113

Added lines #L108 - L113 were not covered by tests

this.serializerOptions.Converters.Add(new ActorMessageBodyJsonConverter<TRequest>(_methodRequestParameterTypes, _wrappedRequestMessageTypes));
this.serializerOptions.Converters.Add(new ActorMessageBodyJsonConverter<TResponse>(_methodRequestParameterTypes, _wrappedRequestMessageTypes));
}

Check warning on line 117 in src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs#L115-L117

Added lines #L115 - L117 were not covered by tests

byte[] IActorRequestMessageBodySerializer.Serialize(IActorRequestMessageBody actorRequestMessageBody)
{
if (actorRequestMessageBody == null)
{
return null;

Check warning on line 123 in src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs#L123

Added line #L123 was not covered by tests
}

using var stream = new MemoryStream();
using var writer = new Utf8JsonWriter(stream);
JsonSerializer.Serialize<object>(writer, actorRequestMessageBody, this.serializerOptions);
writer.Flush();

Check warning on line 129 in src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs#L126-L129

Added lines #L126 - L129 were not covered by tests

return stream.ToArray();
}

Check warning on line 132 in src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs#L131-L132

Added lines #L131 - L132 were not covered by tests

async ValueTask<IActorRequestMessageBody> IActorRequestMessageBodySerializer.DeserializeAsync(Stream stream)
{
if (stream == null)
{
return default;

Check warning on line 138 in src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs#L138

Added line #L138 was not covered by tests
}

if (stream.Length == 0)
{
return default;

Check warning on line 143 in src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs#L143

Added line #L143 was not covered by tests
}

stream.Position = 0;

Check warning on line 146 in src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs#L146

Added line #L146 was not covered by tests
return await JsonSerializer.DeserializeAsync<TRequest>(stream, this.serializerOptions);
}

Check warning on line 148 in src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs#L148

Added line #L148 was not covered by tests

byte[] IActorResponseMessageBodySerializer.Serialize(IActorResponseMessageBody actorResponseMessageBody)
{
if (actorResponseMessageBody == null)
{
return null;

Check warning on line 154 in src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs#L154

Added line #L154 was not covered by tests
}

using var stream = new MemoryStream();
using var writer = new Utf8JsonWriter(stream);
JsonSerializer.Serialize<object>(writer, actorResponseMessageBody, this.serializerOptions);
writer.Flush();

Check warning on line 160 in src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs#L157-L160

Added lines #L157 - L160 were not covered by tests

return stream.ToArray();
}

Check warning on line 163 in src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs#L162-L163

Added lines #L162 - L163 were not covered by tests

async ValueTask<IActorResponseMessageBody> IActorResponseMessageBodySerializer.DeserializeAsync(Stream messageBody)
{
if (messageBody == null)
{
return null;

Check warning on line 169 in src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs#L169

Added line #L169 was not covered by tests
}

using var stream = new MemoryStream();
messageBody.CopyTo(stream);
stream.Position = 0;

Check warning on line 174 in src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs#L172-L174

Added lines #L172 - L174 were not covered by tests

if (stream.Capacity == 0)
{
return null;

Check warning on line 178 in src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs#L178

Added line #L178 was not covered by tests
}

return await JsonSerializer.DeserializeAsync<TResponse>(stream, this.serializerOptions);
}

Check warning on line 182 in src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Communication/ActorMessageBodyJsonSerializationProvider.cs#L182

Added line #L182 was not covered by tests
}
}
}
Loading

0 comments on commit 8d25615

Please sign in to comment.