public static void SetupJsonProtocol(JsonHubProtocolOptions options) { options.PayloadSerializerOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; }
public void WriteMessage(string protocolTestDataName) { var testData = ProtocolTestData[protocolTestDataName]; var expectedOutput = Frame(testData.Json); var protocolOptions = new JsonHubProtocolOptions { PayloadSerializerSettings = new JsonSerializerSettings() { NullValueHandling = testData.NullValueHandling, ContractResolver = testData.CamelCase ? new CamelCasePropertyNamesContractResolver() : new DefaultContractResolver() } }; var protocol = new JsonHubProtocol(Options.Create(protocolOptions)); var writer = MemoryBufferWriter.Get(); try { protocol.WriteMessage(testData.Message, writer); var json = Encoding.UTF8.GetString(writer.ToArray()); Assert.Equal(expectedOutput, json); } finally { MemoryBufferWriter.Return(writer); } }
public GameHub(IServiceProvider sp, IOptions <JsonHubProtocolOptions> options) { this._sp = sp; JsonHubProtocolOptions opts = options.Value; this._jsonSerializerOptions = opts.PayloadSerializerOptions; }
protected override IHubProtocol GetProtocolWithOptions(bool useCamelCase, bool ignoreNullValues) { var protocolOptions = new JsonHubProtocolOptions() { IgnoreNullValues = ignoreNullValues, UseCamelCase = useCamelCase, }; return(new JsonHubProtocol(Options.Create(protocolOptions))); }
public async Task CamelCasedJsonIsPreservedAcrossMassTransitBoundary() { Harness = new InMemoryTestHarness(); var messagePackOptions = new MessagePackHubProtocolOptions(); var jsonOptions = new JsonHubProtocolOptions(); jsonOptions.PayloadSerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); var backplane1Harness = RegisterBusEndpoint("receiveEndpoint1"); var backplane2Harness = RegisterBusEndpoint("receiveEndpoint2"); await Harness.Start(); try { backplane1Harness.SetHubLifetimeManager(CreateLifetimeManager(messagePackOptions, jsonOptions)); backplane2Harness.SetHubLifetimeManager(CreateLifetimeManager()); using (var client1 = new TestClient()) using (var client2 = new TestClient()) { var manager1 = backplane1Harness.HubLifetimeManager; var manager2 = backplane2Harness.HubLifetimeManager; var connection1 = HubConnectionContextUtils.Create(client1.Connection); var connection2 = HubConnectionContextUtils.Create(client2.Connection); await manager1.OnConnectedAsync(connection1).OrTimeout(); await manager2.OnConnectedAsync(connection2).OrTimeout(); await manager1.SendAllAsync("Hello", new object[] { new TestObject { TestProperty = "Foo" } }); Assert.IsTrue(backplane2Harness.All.Consumed.Select <All <MyHub> >().Any()); var message = await client2.ReadAsync().OrTimeout() as InvocationMessage; Assert.NotNull(message); Assert.AreEqual("Hello", message.Target); CollectionAssert.AllItemsAreInstancesOfType(message.Arguments, typeof(JObject)); var jObject = message.Arguments[0] as JObject; Assert.NotNull(jObject); var firstProperty = jObject.Properties().First(); Assert.AreEqual("testProperty", firstProperty.Name); Assert.AreEqual("Foo", firstProperty.Value.Value <string>()); } } finally { await Harness.Stop(); } }
protected override IHubProtocol GetProtocolWithOptions(bool useCamelCase, bool ignoreNullValues) { var protocolOptions = new JsonHubProtocolOptions() { PayloadSerializerOptions = new JsonSerializerOptions() { IgnoreNullValues = ignoreNullValues, PropertyNamingPolicy = useCamelCase ? JsonNamingPolicy.CamelCase : null } }; return(new JsonHubProtocol(Options.Create(protocolOptions))); }
public void WithJsonHubProtocolSetsHubProtocolToJsonWithProvidedOptions() { var expectedOptions = new JsonHubProtocolOptions() { PayloadSerializerSettings = new JsonSerializerSettings() { DateFormatString = "JUST A TEST" } }; Assert.True(new HubConnectionBuilder().WithJsonProtocol(expectedOptions).TryGetSetting<IHubProtocol>(HubConnectionBuilderDefaults.HubProtocolKey, out var hubProtocol)); var actualProtocol = Assert.IsType<JsonHubProtocol>(hubProtocol); Assert.Equal("JUST A TEST", actualProtocol.PayloadSerializer.DateFormatString); }
protected override IHubProtocol GetProtocolWithOptions(bool useCamelCase, bool ignoreNullValues) { var protocolOptions = new JsonHubProtocolOptions() { PayloadSerializerOptions = new JsonSerializerOptions() { DefaultIgnoreCondition = ignoreNullValues ? System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault : System.Text.Json.Serialization.JsonIgnoreCondition.Never, PropertyNamingPolicy = useCamelCase ? JsonNamingPolicy.CamelCase : null, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, } }; return(new JsonHubProtocol(Options.Create(protocolOptions))); }
protected override IHubProtocol GetProtocolWithOptions(bool useCamelCase, bool ignoreNullValues) { var protocolOptions = new JsonHubProtocolOptions() { PayloadSerializerOptions = new JsonSerializerOptions() { IgnoreNullValues = ignoreNullValues, PropertyNamingPolicy = useCamelCase ? JsonNamingPolicy.CamelCase : null, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, } }; return(new JsonHubProtocol(Options.Create(protocolOptions))); }
public async Task CamelCasedJsonIsPreservedAcrossRedisBoundary() { var server = new TestRedisServer(); var messagePackOptions = new MessagePackHubProtocolOptions(); messagePackOptions.SerializationContext.DictionarySerlaizationOptions.KeyTransformer = DictionaryKeyTransformers.LowerCamel; var jsonOptions = new JsonHubProtocolOptions(); jsonOptions.PayloadSerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); using (var client1 = new TestClient()) using (var client2 = new TestClient()) { // The sending manager has serializer settings var manager1 = CreateLifetimeManager(server, messagePackOptions, jsonOptions); // The receiving one doesn't matter because of how we serialize! var manager2 = CreateLifetimeManager(server); var connection1 = HubConnectionContextUtils.Create(client1.Connection); var connection2 = HubConnectionContextUtils.Create(client2.Connection); await manager1.OnConnectedAsync(connection1).OrTimeout(); await manager2.OnConnectedAsync(connection2).OrTimeout(); await manager1.SendAllAsync("Hello", new object[] { new TestObject { TestProperty = "Foo" } }); var message = Assert.IsType <InvocationMessage>(await client2.ReadAsync().OrTimeout()); Assert.Equal("Hello", message.Target); Assert.Collection( message.Arguments, arg0 => { var dict = Assert.IsType <JObject>(arg0); Assert.Collection(dict.Properties(), prop => { Assert.Equal("testProperty", prop.Name); Assert.Equal("Foo", prop.Value.Value <string>()); }); }); } }
protected MassTransitHubLifetimeManager <THub> CreateLifetimeManager(MessagePackHubProtocolOptions messagePackOptions = null, JsonHubProtocolOptions jsonOptions = null) { messagePackOptions ??= new MessagePackHubProtocolOptions(); jsonOptions ??= new JsonHubProtocolOptions(); var manager = new MassTransitHubLifetimeManager <THub>( new HubLifetimeManagerOptions <THub> { ServerName = $"{_prefix}_{Guid.NewGuid():N}" }, new BusHubLifetimeScopeProvider(Harness.Bus), new DefaultHubProtocolResolver( new IHubProtocol[] { new JsonHubProtocol(Options.Create(jsonOptions)), new MessagePackHubProtocol(Options.Create(messagePackOptions)) }, NullLogger <DefaultHubProtocolResolver> .Instance) ); return(manager); }
public void ParseMessage(HubMessage expectedMessage, bool camelCase, NullValueHandling nullValueHandling, string input) { input = Frame(input); var protocolOptions = new JsonHubProtocolOptions { PayloadSerializerSettings = new JsonSerializerSettings { NullValueHandling = nullValueHandling, ContractResolver = camelCase ? new CamelCasePropertyNamesContractResolver() : new DefaultContractResolver() } }; var binder = new TestBinder(expectedMessage); var protocol = new JsonHubProtocol(Options.Create(protocolOptions)); protocol.TryParseMessages(Encoding.UTF8.GetBytes(input), binder, out var messages); Assert.Equal(expectedMessage, messages[0], TestHubMessageEqualityComparer.Instance); }
public void ParseMessage(string protocolTestDataName) { var testData = ProtocolTestData[protocolTestDataName]; var input = Frame(testData.Json); var protocolOptions = new JsonHubProtocolOptions { PayloadSerializerSettings = new JsonSerializerSettings { NullValueHandling = testData.NullValueHandling, ContractResolver = testData.CamelCase ? new CamelCasePropertyNamesContractResolver() : new DefaultContractResolver() } }; var binder = new TestBinder(testData.Message); var protocol = new JsonHubProtocol(Options.Create(protocolOptions)); var data = new ReadOnlySequence <byte>(Encoding.UTF8.GetBytes(input)); protocol.TryParseMessage(ref data, binder, out var message); Assert.Equal(testData.Message, message, TestHubMessageEqualityComparer.Instance); }
public void WriteMessage(HubMessage message, bool camelCase, NullValueHandling nullValueHandling, string expectedOutput) { expectedOutput = Frame(expectedOutput); var protocolOptions = new JsonHubProtocolOptions { PayloadSerializerSettings = new JsonSerializerSettings() { NullValueHandling = nullValueHandling, ContractResolver = camelCase ? new CamelCasePropertyNamesContractResolver() : new DefaultContractResolver() } }; var protocol = new JsonHubProtocol(Options.Create(protocolOptions)); using (var ms = new MemoryStream()) { protocol.WriteMessage(message, ms); var json = Encoding.UTF8.GetString(ms.ToArray()); Assert.Equal(expectedOutput, json); } }
public static IHubConnectionBuilder WithJsonProtocol(this IHubConnectionBuilder hubConnectionBuilder, JsonHubProtocolOptions options) { return(hubConnectionBuilder.WithHubProtocol(new JsonHubProtocol(Options.Create(options)))); }
public HubLifetimeManager <THub> CreateLifetimeManager(MessagePackHubProtocolOptions messagePackOptions = null, JsonHubProtocolOptions jsonOptions = null) { messagePackOptions = messagePackOptions ?? new MessagePackHubProtocolOptions(); jsonOptions = jsonOptions ?? new JsonHubProtocolOptions(); var manager = new MassTransitHubLifetimeManager <THub>( Harness.Bus, Harness.Bus.CreateClientFactory(TimeSpan.FromSeconds(5)), new DefaultHubProtocolResolver(new IHubProtocol[] { new JsonHubProtocol(Options.Create(jsonOptions)), new MessagePackHubProtocol(Options.Create(messagePackOptions)), }, NullLogger <DefaultHubProtocolResolver> .Instance)); return(manager); }
private RedisHubLifetimeManager <MyHub> CreateLifetimeManager(TestRedisServer server, MessagePackHubProtocolOptions messagePackOptions = null, JsonHubProtocolOptions jsonOptions = null) { var options = new RedisOptions() { ConnectionFactory = async(t) => await Task.FromResult(new TestConnectionMultiplexer(server)) }; messagePackOptions = messagePackOptions ?? new MessagePackHubProtocolOptions(); jsonOptions = jsonOptions ?? new JsonHubProtocolOptions(); return(new RedisHubLifetimeManager <MyHub>( NullLogger <RedisHubLifetimeManager <MyHub> > .Instance, Options.Create(options), new DefaultHubProtocolResolver(new IHubProtocol[] { new JsonHubProtocol(Options.Create(jsonOptions)), new MessagePackHubProtocol(Options.Create(messagePackOptions)), }, NullLogger <DefaultHubProtocolResolver> .Instance))); }