From 4449af7e4968e524842759fcc3aab246968fdc53 Mon Sep 17 00:00:00 2001 From: p-kaczynski Date: Mon, 23 Sep 2024 12:20:09 +0100 Subject: [PATCH 1/5] Make (de)serialisation settings accessible and settable --- src/IIIF/IIIF/Serialisation/IIIFSerialiserX.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/IIIF/IIIF/Serialisation/IIIFSerialiserX.cs b/src/IIIF/IIIF/Serialisation/IIIFSerialiserX.cs index 9358a6d..ea99361 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,7 +23,7 @@ public static class IIIFSerialiserX } }; - private static readonly JsonSerializerSettings DeserializerSettings = new() + public static JsonSerializerSettings DeserializerSettings { get; set; } = new() { NullValueHandling = NullValueHandling.Ignore, ContractResolver = new PrettyIIIFContractResolver(), From 8a3dcab8e7db5cf0dfba1f51c49236537dcb61a2 Mon Sep 17 00:00:00 2001 From: p-kaczynski Date: Mon, 23 Sep 2024 12:20:33 +0100 Subject: [PATCH 2/5] Reorder converters to an order that prioritizes concrete implementations --- src/IIIF/IIIF/Serialisation/IIIFSerialiserX.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/IIIF/IIIF/Serialisation/IIIFSerialiserX.cs b/src/IIIF/IIIF/Serialisation/IIIFSerialiserX.cs index ea99361..f8d0b8b 100644 --- a/src/IIIF/IIIF/Serialisation/IIIFSerialiserX.cs +++ b/src/IIIF/IIIF/Serialisation/IIIFSerialiserX.cs @@ -30,9 +30,10 @@ public static class IIIFSerialiserX 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 ServiceConverter(), new ResourceBaseV3Converter(), + new CollectionItemConverter(), new ResourceConverter() } }; From dd8be7879b37f376201ccd53304fd43b9dc1cd01 Mon Sep 17 00:00:00 2001 From: p-kaczynski Date: Mon, 23 Sep 2024 12:21:14 +0100 Subject: [PATCH 3/5] Allow passing custom ResourceBase concrete mappings in serialization context --- .../Deserialisation/ResourceBaseV3Converter.cs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) 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); From f07f57d6c65ca9db6ee4f77d8edc84681050bc3e Mon Sep 17 00:00:00 2001 From: p-kaczynski Date: Mon, 23 Sep 2024 12:23:38 +0100 Subject: [PATCH 4/5] Specify max lang version for the net6.0 to avoid issues with legacy builders --- src/IIIF/IIIF/IIIF.csproj | 1 + 1 file changed, 1 insertion(+) 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 From c6fb3c7785228af46bb8f127147825ceb5756db5 Mon Sep 17 00:00:00 2001 From: p-kaczynski Date: Mon, 23 Sep 2024 15:33:31 +0100 Subject: [PATCH 5/5] Fix converter order again. --- src/IIIF/IIIF/Serialisation/IIIFSerialiserX.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/IIIF/IIIF/Serialisation/IIIFSerialiserX.cs b/src/IIIF/IIIF/Serialisation/IIIFSerialiserX.cs index f8d0b8b..de05029 100644 --- a/src/IIIF/IIIF/Serialisation/IIIFSerialiserX.cs +++ b/src/IIIF/IIIF/Serialisation/IIIFSerialiserX.cs @@ -32,7 +32,7 @@ public static class IIIFSerialiserX { new ExternalResourceConverter(), new ImageService2Converter(), new AnnotationV3Converter(), new StructuralLocationConverter(), new PaintableConverter(), - new SelectorConverter(), new ServiceConverter(), new ResourceBaseV3Converter(), + new SelectorConverter(), new ResourceBaseV3Converter(), new ServiceConverter(), new CollectionItemConverter(), new ResourceConverter() } };