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

Add ability to deserialize custom collection items #53

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
237 changes: 237 additions & 0 deletions src/IIIF/IIIF.Tests/Serialisation/CustomClassSerializationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using IIIF.Presentation.V3;
using IIIF.Presentation.V3.Content;
using IIIF.Presentation.V3.Strings;
using IIIF.Serialisation;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Collection = IIIF.Presentation.V3.Collection;
using ResourceBase = IIIF.Presentation.V3.ResourceBase;

namespace IIIF.Tests.Serialisation;

public class CustomClassSerializationTests
{
private Collection sampleCollection = new()
{
Id = $"someId",
Context = "http://iiif.io/api/presentation/3/context.json",
Label = new LanguageMap
{
{"en", new List<string>
{
"root"
}}
},
Behavior = new List<string>
{
"some-behaviour"
},
Items = new List<ICollectionItem>
{
new CustomCollectionItem
{
Id = "child",
Label = new LanguageMap
{
{"en", new List<string>
{
"child"
}}
},
CustomField = "some-custom-field"
},
new CustomItem
{
Id = "custom-item-child",
Label = new LanguageMap
{
{"en", new List<string>
{
"child"
}}
},
CustomField = "some-custom-field"
}
},
PartOf = new List<ResourceBase>
{
new ExternalResource("Collection")
{
Id = $"PartOf",
Label = new LanguageMap
{
{"en", new List<string>
{
"some collection"
}}
}
}
},
SeeAlso = new List<ExternalResource>
{
new ("SeeAlso")
{
Id = "see also",
Label = new LanguageMap
{
{"en", new List<string>
{
"child"
}}
},
Profile = "Public"
}
},
};

private Collection sampleCollectionOnlyInheritance = new()
{
Id = $"someId",
Context = "http://iiif.io/api/presentation/3/context.json",
Label = new LanguageMap
{
{"en", new List<string>
{
"root"
}}
},
Behavior = new List<string>
{
"some-behaviour"
},
Items = new List<ICollectionItem>
{
new CustomCollectionItem
{
Id = "child",
Label = new LanguageMap
{
{"en", new List<string>
{
"child"
}}
},
CustomField = "some-custom-field"
}
},
PartOf = new List<ResourceBase>
{
new ExternalResource("Collection")
{
Id = $"PartOf",
Label = new LanguageMap
{
{"en", new List<string>
{
"some collection"
}}
}
}
},
SeeAlso = new List<ExternalResource>
{
new ("SeeAlso")
{
Id = "see also",
Label = new LanguageMap
{
{"en", new List<string>
{
"child"
}}
},
Profile = "Public"
}
},
};

[Fact]
public async Task CanDeserialiseCustomSerialisedCollection()
{
var serialisedCollection = sampleCollection.AsJson();

var deserialised = await serialisedCollection.AsJson<Collection>();
var itemAsCustomCollection = deserialised.Items[0] as CustomCollectionItem;
var itemAsCustomItem = deserialised.Items[1] as CustomItem;

deserialised.Should().BeEquivalentTo(sampleCollection);
itemAsCustomCollection.Should().NotBeNull();
itemAsCustomItem.Should().NotBeNull();
}

[Fact]
public void CanDeserialiseCustomSerialisedCollectionWithOnlyStandardLibrary()
{
var serialisedCollection = sampleCollectionOnlyInheritance.AsJson();

var deserialised = serialisedCollection.FromJson<Collection>();

var itemAsCustomCollection = deserialised.Items[0] as CustomCollectionItem;
var itemAsCollection = deserialised.Items[0] as Collection;

deserialised.Should().BeEquivalentTo(sampleCollectionOnlyInheritance);
itemAsCustomCollection.Should().BeNull();
itemAsCollection.Should().NotBeNull();
}
}

public static class CustomSerializerX
{
public static async Task<T?> AsJson<T>(this string content, JsonSerializerSettings? settings = null)
where T : JsonLdBase, new()
{
using var streamReader = new StringReader(content);
return await DeserializeStream<T>(settings, streamReader);
}

private static async Task<T?> DeserializeStream<T>(JsonSerializerSettings? settings, TextReader streamReader)
where T : JsonLdBase, new()
{
await using var jsonReader = new JsonTextReader(streamReader);

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

var serializer = JsonSerializer.Create(settings);

try
{
var result = new T();
serializer.Populate(jsonReader, result);
return result;
}
catch (JsonException)
{
return default;
}
}
}

public class CustomCollectionItem : Collection
{
public string CustomField { get; set; } = "custom";
}

public class CustomItem : StructureBase, ICollectionItem
{
public string CustomField { get; set; } = "custom";

public List<IService> Services { get; set; }
public override string Type { get; } = nameof(CustomItem);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using IIIF.Presentation.V3;
using Newtonsoft.Json.Linq;

Expand All @@ -11,12 +12,30 @@ public class CollectionItemConverter : ReadOnlyConverter<ICollectionItem>
{
var jsonObject = JObject.Load(reader);

ICollectionItem collectionItem = jsonObject["type"].Value<string>() switch
var type = jsonObject["type"].Value<string>();
ICollectionItem collectionItem = null;

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

if (collectionItem == null)
{
collectionItem = type switch
{
nameof(Collection) => new Collection(),
nameof(Manifest) => new Manifest(),
_ => null
};
}

serializer.Populate(jsonObject.CreateReader(), collectionItem);
return collectionItem;
Expand Down
Loading