Skip to content

Commit

Permalink
Merge pull request #50 from bingenito/enum-serialization
Browse files Browse the repository at this point in the history
Serialized Fdc3 enum values should be camel case
  • Loading branch information
psmulovics authored May 22, 2023
2 parents 934484a + b3703cc commit 028e2f3
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<RootNamespace>MorganStanley.Fdc3.NewtonsoftJson</RootNamespace>
<AssemblyName>MorganStanley.Fdc3.NewtonsoftJson</AssemblyName>
<VersionPrefix>2.0.0</VersionPrefix>
<VersionSuffix>alpha.4</VersionSuffix>
<VersionSuffix>alpha.5</VersionSuffix>
<Product>.NET FDC3 Newtonsoft JSON</Product>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\keypair.snk</AssemblyOriginatorKeyFile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Fdc3JsonSerializerSettings()
{
NamingStrategy = new Fdc3CamelCaseNamingStrategy()
};
this.Converters = new JsonConverter[] { new StringEnumConverter(), new RecipientJsonConverter() };
this.Converters = new JsonConverter[] { new StringEnumConverter(new CamelCaseNamingStrategy()), new RecipientJsonConverter() };
}
}
}
2 changes: 1 addition & 1 deletion src/Fdc3/MorganStanley.Fdc3.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<RootNamespace>MorganStanley.Fdc3</RootNamespace>
<AssemblyName>MorganStanley.Fdc3</AssemblyName>
<VersionPrefix>2.0.0</VersionPrefix>
<VersionSuffix>alpha.4</VersionSuffix>
<VersionSuffix>alpha.5</VersionSuffix>
<Product>.NET FDC3</Product>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\keypair.snk</AssemblyOriginatorKeyFile>
Expand Down
93 changes: 93 additions & 0 deletions src/Tests/MorganStanley.Fdc3.NewtonsoftJson.Tests/ChannelTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Morgan Stanley makes this available to you under the Apache License,
* Version 2.0 (the "License"). You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.
*
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership. 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 MorganStanley.Fdc3.Context;
using MorganStanley.Fdc3.NewtonsoftJson.Serialization;
using Newtonsoft.Json;

namespace MorganStanley.Fdc3.NewtonsoftJson.Tests;

public class ChannelTests
{
[Fact]
public void Channel_SerializedJsonHasCamelCaseId()
{
IChannel channel = new MockChannel();
string serializedChannel = JsonConvert.SerializeObject(channel, new Fdc3JsonSerializerSettings());
Assert.Contains("\"id\":", serializedChannel);
}

[Fact]
public void Channel_SerializedJsonHasCamelCaseEnumValue()
{
IChannel channel = new MockChannel(ChannelType.App);
string serializedChannel = JsonConvert.SerializeObject(channel, new Fdc3JsonSerializerSettings());
Assert.Contains("\"app\"", serializedChannel);

channel = new MockChannel(ChannelType.User);
serializedChannel = JsonConvert.SerializeObject(channel, new Fdc3JsonSerializerSettings());
Assert.Contains("\"user\"", serializedChannel);

channel = new MockChannel(ChannelType.Private);
serializedChannel = JsonConvert.SerializeObject(channel, new Fdc3JsonSerializerSettings());
Assert.Contains("\"private\"", serializedChannel);
}

[Fact]
public void Channel_ParsedChannelTypeFromJson()
{
string json = "{\"id\":\"value\",\"type\":\"user\",\"displayMetadata\":{}}";
IChannel? channel = JsonConvert.DeserializeObject<MockChannel>(json);
Assert.Equal("value", channel?.Id);
Assert.Equal(ChannelType.User, channel?.Type);

json = "{\"id\":\"value\",\"type\":\"app\",\"displayMetadata\":{}}";
channel = JsonConvert.DeserializeObject<MockChannel>(json);
Assert.Equal(ChannelType.App, channel?.Type);
}


public class MockChannel : IChannel
{
public MockChannel()
{
}

internal MockChannel(ChannelType channelType)
{
this.Type = channelType;
}

public string Id { get; set; } = "value";

public ChannelType Type { get; set; }

public IDisplayMetadata? DisplayMetadata { get; set; } = new DisplayMetadata();

public Task<IListener> AddContextListener<T>(string? contextType, ContextHandler<T> handler) where T : IContext
{
throw new NotImplementedException();
}

public Task Broadcast(IContext context)
{
throw new NotImplementedException();
}

public Task<IContext?> GetCurrentContext(string? contextType)
{
throw new NotImplementedException();
}
}
}

0 comments on commit 028e2f3

Please sign in to comment.