Skip to content

Commit

Permalink
Merge pull request #50 from digirati-co-uk/feature/serialisation/expa…
Browse files Browse the repository at this point in the history
…nd_customisation

Expand serialization customisation options
  • Loading branch information
p-kaczynski authored Sep 23, 2024
2 parents b8e1cbe + c6fb3c7 commit 8c44376
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/IIIF/IIIF/IIIF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<PackageId>iiif-net</PackageId>
<LangVersion>10.0</LangVersion>
<Authors>Donald Gray,Tom Crane</Authors>
<Company>Digirati</Company>
<Description>IIIF Library for .NET Core</Description>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using IIIF.Auth.V2;
using IIIF.ImageApi.V3;
using IIIF.Presentation.V3;
Expand All @@ -20,13 +21,13 @@ public class ResourceBaseV3Converter : ReadOnlyConverter<ResourceBase>
{
var jsonObject = JObject.Load(reader);

var resourceBase = IdentifyConcreteType(jsonObject);
var resourceBase = IdentifyConcreteType(jsonObject, serializer);

serializer.Populate(jsonObject.CreateReader(), resourceBase);
return resourceBase;
}

private static ResourceBase? IdentifyConcreteType(JObject jsonObject)
private static ResourceBase? IdentifyConcreteType(JObject jsonObject, JsonSerializer serializer)
{
ResourceBase? resourceBase = null;
if (!jsonObject.ContainsKey("type"))
Expand Down Expand Up @@ -54,7 +55,7 @@ public class ResourceBaseV3Converter : ReadOnlyConverter<ResourceBase>
nameof(TextualBody) => new TextualBody(jsonObject["value"].Value<string>()),
_ => null
};

if (resourceBase != null) return resourceBase;

if (jsonObject.ContainsKey("motivation"))
Expand All @@ -68,6 +69,14 @@ public class ResourceBaseV3Converter : ReadOnlyConverter<ResourceBase>
_ => new UnknownMotivation(motivation)
};
}

// Look for consumer-provided mapping
if (type != null
&& serializer.Context.Context is IDictionary<string, Func<JObject, ResourceBase>> customMappings
&& customMappings.TryGetValue(type, out var customMapping))
{
resourceBase = customMapping(jsonObject);
}

if (resourceBase == null) return new ExternalResource(type);

Expand Down
11 changes: 6 additions & 5 deletions src/IIIF/IIIF/Serialisation/IIIFSerialiserX.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace IIIF.Serialisation;
/// </summary>
public static class IIIFSerialiserX
{
private static readonly JsonSerializerSettings SerializerSettings = new()
public static JsonSerializerSettings SerializerSettings { get; set; } = new()
{
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new PrettyIIIFContractResolver(),
Expand All @@ -23,16 +23,17 @@ public static class IIIFSerialiserX
}
};

private static readonly JsonSerializerSettings DeserializerSettings = new()
public static JsonSerializerSettings DeserializerSettings { get; set; } = new()
{
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new PrettyIIIFContractResolver(),
Formatting = Formatting.Indented,
Converters = new List<JsonConverter>
{
new ImageService2Converter(), new AnnotationV3Converter(), new ResourceBaseV3Converter(),
new StructuralLocationConverter(), new ExternalResourceConverter(), new PaintableConverter(),
new SelectorConverter(), new ServiceConverter(), new ResourceConverter(), new CollectionItemConverter()
new ExternalResourceConverter(), new ImageService2Converter(), new AnnotationV3Converter(),
new StructuralLocationConverter(), new PaintableConverter(),
new SelectorConverter(), new ResourceBaseV3Converter(), new ServiceConverter(),
new CollectionItemConverter(), new ResourceConverter()
}
};

Expand Down

0 comments on commit 8c44376

Please sign in to comment.