Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand serialization customisation options #50

Merged
merged 5 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it worth pointing out somewhere that the language version is being pinned to 10?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's kinda implied by the target framework, can be bumped at some point along with framework when 6 goes out of support. Which is /very/ soon :D

<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
Loading