Skip to content

Commit

Permalink
Update to use new dictionary with more specific typing
Browse files Browse the repository at this point in the history
  • Loading branch information
JackLewis-digirati committed Nov 15, 2024
1 parent 018905c commit 42a2b33
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,16 @@ public static class CustomSerializerX

settings ??= new(IIIFSerialiserX.DeserializerSettings);
settings.Context = new StreamingContext(StreamingContextStates.Other,
new Dictionary<string, Func<JObject, ICollectionItem>>
new Dictionary<Type, IDictionary<string, Func<JObject, object>>>
{
{ "Collection", p => new CustomCollectionItem() },
{ "CustomItem", p => new CustomItem() }
{
typeof(ICollectionItem),
new Dictionary<string, Func<JObject, object>>
{
{ "Collection", _ => new CustomCollectionItem() },
{ "CustomItem", _ => new CustomItem() }
}
}
});

var serializer = JsonSerializer.Create(settings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ public class CollectionItemConverter : ReadOnlyConverter<ICollectionItem>
ICollectionItem collectionItem = null;

// Look for consumer-provided mapping
if (serializer.Context.Context is IDictionary<string, Func<JObject, ICollectionItem>> customMappings
&& customMappings.TryGetValue(type, out var customMapping))
if (serializer.Context.Context is IDictionary<Type, IDictionary<string, Func<JObject, object>>> ctx)
{
collectionItem = customMapping(jsonObject);
if (ctx.TryGetValue(typeof(ICollectionItem), out var customMappings))
{
if (customMappings.TryGetValue(type, out var customMapping))
{
collectionItem = (ICollectionItem) customMapping(jsonObject);
}
}
}

if (collectionItem == null)
Expand Down

0 comments on commit 42a2b33

Please sign in to comment.