Skip to content

Commit 37d8191

Browse files
committed
added specific deserializer for the interface
1 parent f1b6a00 commit 37d8191

File tree

9 files changed

+130
-11
lines changed

9 files changed

+130
-11
lines changed

Branding.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1616
EndProject
1717
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreHelpers.Branding.AspNet", "CoreHelpers.Branding.AspNet\CoreHelpers.Branding.AspNet.csproj", "{C84C8CF3-0226-437D-9475-A92FA5E3CAAB}"
1818
EndProject
19+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreHelpers.Branding.Stores.AzureStorage.Tests", "CoreHelpers.Branding.Stores.AzureStorage.Tests\CoreHelpers.Branding.Stores.AzureStorage.Tests.csproj", "{802BB680-BF47-4B74-A7DF-3FC81B07D6E9}"
20+
EndProject
1921
Global
2022
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2123
Debug|Any CPU = Debug|Any CPU
@@ -38,6 +40,10 @@ Global
3840
{C84C8CF3-0226-437D-9475-A92FA5E3CAAB}.Debug|Any CPU.Build.0 = Debug|Any CPU
3941
{C84C8CF3-0226-437D-9475-A92FA5E3CAAB}.Release|Any CPU.ActiveCfg = Release|Any CPU
4042
{C84C8CF3-0226-437D-9475-A92FA5E3CAAB}.Release|Any CPU.Build.0 = Release|Any CPU
43+
{802BB680-BF47-4B74-A7DF-3FC81B07D6E9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
44+
{802BB680-BF47-4B74-A7DF-3FC81B07D6E9}.Debug|Any CPU.Build.0 = Debug|Any CPU
45+
{802BB680-BF47-4B74-A7DF-3FC81B07D6E9}.Release|Any CPU.ActiveCfg = Release|Any CPU
46+
{802BB680-BF47-4B74-A7DF-3FC81B07D6E9}.Release|Any CPU.Build.0 = Release|Any CPU
4147
EndGlobalSection
4248
GlobalSection(SolutionProperties) = preSolution
4349
HideSolutionNode = FALSE
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
13+
<PackageReference Include="xunit" Version="2.4.1" />
14+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
15+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
16+
<PrivateAssets>all</PrivateAssets>
17+
</PackageReference>
18+
<PackageReference Include="coverlet.collector" Version="3.1.2">
19+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
20+
<PrivateAssets>all</PrivateAssets>
21+
</PackageReference>
22+
</ItemGroup>
23+
24+
<ItemGroup>
25+
<None Remove="DataFiles\" />
26+
</ItemGroup>
27+
<ItemGroup>
28+
<Folder Include="DataFiles\" />
29+
</ItemGroup>
30+
<ItemGroup>
31+
<None Update="DataFiles\BasicCompanyBranding.json">
32+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
33+
</None>
34+
</ItemGroup>
35+
<ItemGroup>
36+
<ProjectReference Include="..\CoreHelpers.Branding.Stores.AzureStorage\CoreHelpers.Branding.Stores.AzureStorage.csproj" />
37+
</ItemGroup>
38+
</Project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "Demo App",
3+
"logos": {
4+
"small": "https://acme.org/demo.png"
5+
},
6+
"legals": {
7+
"imprint": "https://acme.org/imprint",
8+
"dataPrivacy": "https://acme.org/privacy"
9+
},
10+
"colors": {
11+
"primary": "#e20074",
12+
"PrimaryFont": "white"
13+
}
14+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
global using Xunit;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Reflection.PortableExecutable;
2+
using CoreHelpers.Branding.Runtime;
3+
using Newtonsoft.Json;
4+
5+
namespace CoreHelpers.Branding.Stores.AzureStorage.Tests;
6+
7+
public class UnitTest1
8+
{
9+
[Fact]
10+
public void Test1()
11+
{
12+
// read the data
13+
var dataAsString = File.ReadAllText("./DataFiles/BasicCompanyBranding.json");
14+
15+
// deserialize
16+
var dataModel = JsonConvert.DeserializeObject<AzureStorageCompanyBranding>(dataAsString);
17+
18+
// verify the properties
19+
Assert.Equal("https://acme.org/imprint", dataModel?.Legals[nLegalItems.imprint]);
20+
Assert.Equal("https://acme.org/privacy", dataModel?.Legals[nLegalItems.dataPrivacy]);
21+
Assert.Equal("https://acme.org/demo.png", dataModel?.Logos[nLogoSize.small]);
22+
23+
Assert.Equal("#e20074", dataModel?.Colors.Primary);
24+
Assert.Equal("white", dataModel?.Colors.PrimaryFont);
25+
}
26+
}

CoreHelpers.Branding.Stores.AzureStorage/AzureStorageBrandingStore.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Net.NetworkInformation;
34
using System.Text.Json;
45
using System.Threading.Tasks;
56
using Azure.Storage.Blobs;
@@ -8,7 +9,7 @@
89

910
namespace CoreHelpers.Branding.Stores.AzureStorage
1011
{
11-
public class AzureStorageBrandingStore : IBrandingStore
12+
internal class AzureStorageBrandingStore : IBrandingStore
1213
{
1314
private string _connectionString;
1415
private string _containerName;
@@ -39,7 +40,7 @@ public async Task<ICompanyBranding> LoadBranding(string applicationId, string br
3940
using (StreamReader reader = new StreamReader(blobStream.Value.Content))
4041
{
4142
using (JsonTextReader jsonReader = new JsonTextReader(reader))
42-
{
43+
{
4344
var ser = new Newtonsoft.Json.JsonSerializer();
4445
return ser.Deserialize<AzureStorageCompanyBranding>(jsonReader);
4546
}
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
using System;
22
using System.Collections.Generic;
33
using CoreHelpers.Branding.Runtime;
4+
using Newtonsoft.Json;
45

56
namespace CoreHelpers.Branding.Stores.AzureStorage
67
{
7-
public class AzureStorageCompanyBrandingColors : ICompanyBrandingColors
8+
internal class AzureStorageCompanyBrandingColors : ICompanyBrandingColors
89
{
9-
public string Font { get; }
10+
public string Font { get; set; }
1011

11-
public string FontHover { get; }
12+
public string FontHover { get; set; }
1213

13-
public string FontActive { get; }
14+
public string FontActive { get; set; }
1415

15-
public string Primary { get; }
16+
public string Primary { get; set; }
1617

17-
public string PrimaryHover { get; }
18+
public string PrimaryHover { get; set; }
1819

19-
public string PrimaryFont { get; }
20+
public string PrimaryFont { get; set; }
2021
}
2122

22-
public class AzureStorageCompanyBranding : ICompanyBranding
23+
internal class AzureStorageCompanyBranding : ICompanyBranding
2324
{
2425
public string Name { get; set; }
2526

2627
public Dictionary<nLogoSize, string> Logos { get; set; } = new Dictionary<nLogoSize, string>();
2728

2829
public Dictionary<nLegalItems, string> Legals { get; set; } = new Dictionary<nLegalItems, string>();
2930

30-
public ICompanyBrandingColors Colors { get; set; } = new AzureStorageCompanyBrandingColors();
31+
[JsonConverter(typeof(ConcreteTypeConverter<AzureStorageCompanyBrandingColors>))]
32+
public ICompanyBrandingColors Colors { get; set; }
3133
}
3234
}
3335

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using Newtonsoft.Json;
3+
4+
namespace CoreHelpers.Branding.Stores.AzureStorage
5+
{
6+
public class ConcreteTypeConverter<TConcrete> : JsonConverter
7+
{
8+
public override bool CanConvert(Type objectType)
9+
{
10+
//assume we can convert to anything for now
11+
return true;
12+
}
13+
14+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
15+
{
16+
//explicitly specify the concrete type we want to create
17+
return serializer.Deserialize<TConcrete>(reader);
18+
}
19+
20+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
21+
{
22+
//use the default serialization - it works fine
23+
serializer.Serialize(writer, value);
24+
}
25+
}
26+
}
27+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
4+
[assembly: InternalsVisibleTo("CoreHelpers.Branding.Stores.AzureStorage.Tests")]

0 commit comments

Comments
 (0)