diff --git a/src/IIIF/IIIF/IIIF.csproj b/src/IIIF/IIIF/IIIF.csproj index d8cb475..93df607 100644 --- a/src/IIIF/IIIF/IIIF.csproj +++ b/src/IIIF/IIIF/IIIF.csproj @@ -3,6 +3,7 @@ net6.0 iiif-net + 10.0 Donald Gray,Tom Crane Digirati IIIF Library for .NET Core diff --git a/src/IIIF/IIIF/Serialisation/Deserialisation/ResourceBaseV3Converter.cs b/src/IIIF/IIIF/Serialisation/Deserialisation/ResourceBaseV3Converter.cs index c9f03fb..1c49518 100644 --- a/src/IIIF/IIIF/Serialisation/Deserialisation/ResourceBaseV3Converter.cs +++ b/src/IIIF/IIIF/Serialisation/Deserialisation/ResourceBaseV3Converter.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using IIIF.Auth.V2; using IIIF.ImageApi.V3; using IIIF.Presentation.V3; @@ -20,13 +21,13 @@ public class ResourceBaseV3Converter : ReadOnlyConverter { 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")) @@ -54,7 +55,7 @@ public class ResourceBaseV3Converter : ReadOnlyConverter nameof(TextualBody) => new TextualBody(jsonObject["value"].Value()), _ => null }; - + if (resourceBase != null) return resourceBase; if (jsonObject.ContainsKey("motivation")) @@ -68,6 +69,14 @@ public class ResourceBaseV3Converter : ReadOnlyConverter _ => new UnknownMotivation(motivation) }; } + + // Look for consumer-provided mapping + if (type != null + && serializer.Context.Context is IDictionary> customMappings + && customMappings.TryGetValue(type, out var customMapping)) + { + resourceBase = customMapping(jsonObject); + } if (resourceBase == null) return new ExternalResource(type); diff --git a/src/IIIF/IIIF/Serialisation/IIIFSerialiserX.cs b/src/IIIF/IIIF/Serialisation/IIIFSerialiserX.cs index 9358a6d..de05029 100644 --- a/src/IIIF/IIIF/Serialisation/IIIFSerialiserX.cs +++ b/src/IIIF/IIIF/Serialisation/IIIFSerialiserX.cs @@ -10,7 +10,7 @@ namespace IIIF.Serialisation; /// public static class IIIFSerialiserX { - private static readonly JsonSerializerSettings SerializerSettings = new() + public static JsonSerializerSettings SerializerSettings { get; set; } = new() { NullValueHandling = NullValueHandling.Ignore, ContractResolver = new PrettyIIIFContractResolver(), @@ -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 { - 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() } };