Skip to content

Commit

Permalink
Refactor json serialiation helpers into a new package for developer c…
Browse files Browse the repository at this point in the history
…onsumption (#30)
  • Loading branch information
bingenito authored Feb 13, 2023
1 parent 7538229 commit a6694ff
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 46 deletions.
45 changes: 45 additions & 0 deletions src/Fdc3.Json/MorganStanley.Fdc3.Json.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<RootNamespace>MorganStanley.Fdc3.Json</RootNamespace>
<AssemblyName>MorganStanley.Fdc3.Json</AssemblyName>
<VersionPrefix>2.0.0</VersionPrefix>
<VersionSuffix>alpha.1</VersionSuffix>
<Product>fdc3-dotnet</Product>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\keypair.snk</AssemblyOriginatorKeyFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
<TargetFramework>netstandard2.0</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>8.0</LangVersion>
<RepositoryUrl>https://github.com/morganstanley/fdc3-dotnet</RepositoryUrl>
<Description>.NET Standard 2.0 declarations to implement concrete FDC3 compatible .NET desktop agents and usage of intents/contexts.</Description>
<Tags>FDC3</Tags>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningLevel>9999</WarningLevel>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<WarningLevel>9999</WarningLevel>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\LICENSE">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
<None Include="..\..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

</Project>
47 changes: 47 additions & 0 deletions src/Fdc3.Json/Serialization/Fdc3CamelCaseNamingStrategy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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 Newtonsoft.Json.Serialization;

namespace MorganStanley.Fdc3.Json.Serialization
{
public class Fdc3CamelCaseNamingStrategy : CamelCaseNamingStrategy
{
public override string GetPropertyName(string name, bool hasSpecifiedName)
{
switch (name)
{
case "CURRENCY_ISOCODE":
case "ISOALPHA2":
case "ISOALPHA3":
case "COUNTRY_ISOALPHA2":
case "COUNTRY_ISOALPHA3":
case "FDS_ID":
case "BBG":
case "CUSIP":
case "FIGI":
case "ISIN":
case "PERMID":
case "RIC":
case "SEDOL":
case "LEI":
return name;

default:
return base.GetPropertyName(name, hasSpecifiedName);
}
}
}

}
37 changes: 37 additions & 0 deletions src/Fdc3.Json/Serialization/Fdc3JsonSerializerSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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 Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;

namespace MorganStanley.Fdc3.Json.Serialization
{
public class Fdc3JsonSerializerSettings : JsonSerializerSettings
{
public Fdc3JsonSerializerSettings()
{
this.TypeNameHandling = TypeNameHandling.None;
this.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
this.DefaultValueHandling = DefaultValueHandling.Populate;
this.NullValueHandling = NullValueHandling.Ignore;
this.ContractResolver = new DefaultContractResolver()
{
NamingStrategy = new Fdc3CamelCaseNamingStrategy()
};
this.Converters = new JsonConverter[] { new StringEnumConverter() };

}
}
}
2 changes: 1 addition & 1 deletion src/Fdc3/MorganStanley.Fdc3.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<VersionSuffix>alpha.1</VersionSuffix>
<Product>fdc3-dotnet</Product>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>keypair.snk</AssemblyOriginatorKeyFile>
<AssemblyOriginatorKeyFile>..\keypair.snk</AssemblyOriginatorKeyFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
<TargetFramework>netstandard2.0</TargetFramework>
<Nullable>enable</Nullable>
Expand Down
47 changes: 3 additions & 44 deletions src/Tests/MorganStanley.Fdc3.Tests/Context/ContextSchemaTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,22 @@
*/

using MorganStanley.Fdc3.Context;
using MorganStanley.Fdc3.Json.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using Newtonsoft.Json.Serialization;

namespace MorganStanley.Fdc3.Tests;

public abstract class ContextSchemaTest
public abstract partial class ContextSchemaTest
{
protected JSchema? Schema { get; private set; }
protected string SchemaUrl { get; private set; }
protected JsonSerializerSettings SerializerSettings { get; }

public ContextSchemaTest(string schemaUrl)
{
this.SerializerSettings = new JsonSerializerSettings()
{
TypeNameHandling = TypeNameHandling.None,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Populate,
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new DefaultContractResolver()
{
NamingStrategy = new Fdc3CamelCaseNamingStrategy()
},
Converters = { new StringEnumConverter() }
};

this.SerializerSettings = new Fdc3JsonSerializerSettings();
this.SchemaUrl = schemaUrl;
}

Expand All @@ -56,32 +43,4 @@ protected async Task<string> ValidateSchema(IContext context)
Assert.True(isValid, String.Join(",", errorMessages.ToArray<string>()));
return serializedContext;
}

public class Fdc3CamelCaseNamingStrategy : CamelCaseNamingStrategy
{
public override string GetPropertyName(string name, bool hasSpecifiedName)
{
switch (name)
{
case "CURRENCY_ISOCODE":
case "ISOALPHA2":
case "ISOALPHA3":
case "COUNTRY_ISOALPHA2":
case "COUNTRY_ISOALPHA3":
case "FDS_ID":
case "BBG":
case "CUSIP":
case "FIGI":
case "ISIN":
case "PERMID":
case "RIC":
case "SEDOL":
case "LEI":
return name;

default:
return base.GetPropertyName(name, hasSpecifiedName);
}
}
}
}
9 changes: 8 additions & 1 deletion src/Tests/MorganStanley.Fdc3.Tests/Context/EmailTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,16 @@ public EmailTests()
}

[Fact]
public async void Email_SerializedJsonMatchesSchema()
public async void Email_Contact_SerializedJsonMatchesSchema()
{
Email email = new Email(new Contact(new ContactID() { Email = "email", FdsId = "fdsid" }), "subject", "body", null, "email");
await this.ValidateSchema(email);
}

[Fact]
public async void Email_ContactList_SerializedJsonMatchesSchema()
{
Email email = new Email(new ContactList(new Contact[] { new Contact(new ContactID() { Email = "email", FdsId = "fdsid" }) }), "subject", "body", null, "email");
await this.ValidateSchema(email);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Fdc3.Json\MorganStanley.Fdc3.Json.csproj" />
<ProjectReference Include="..\..\Fdc3\MorganStanley.Fdc3.csproj" />
</ItemGroup>

Expand Down
6 changes: 6 additions & 0 deletions src/fdc3-dotnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MorganStanley.Fdc3.Tests", "Tests\MorganStanley.Fdc3.Tests\MorganStanley.Fdc3.Tests.csproj", "{D932070B-937D-440F-9D79-B51C8F850E9E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MorganStanley.Fdc3.Json", "Fdc3.Json\MorganStanley.Fdc3.Json.csproj", "{ED9F79A1-05F9-4324-8F5F-AE3C418C813E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -50,6 +52,10 @@ Global
{D932070B-937D-440F-9D79-B51C8F850E9E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D932070B-937D-440F-9D79-B51C8F850E9E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D932070B-937D-440F-9D79-B51C8F850E9E}.Release|Any CPU.Build.0 = Release|Any CPU
{ED9F79A1-05F9-4324-8F5F-AE3C418C813E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ED9F79A1-05F9-4324-8F5F-AE3C418C813E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ED9F79A1-05F9-4324-8F5F-AE3C418C813E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ED9F79A1-05F9-4324-8F5F-AE3C418C813E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
File renamed without changes.

0 comments on commit a6694ff

Please sign in to comment.