-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from digirati-co-uk/fature/collectionSerializa…
…tion Add ability to deserialize collections correctly
- Loading branch information
Showing
3 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
src/IIIF/IIIF.Tests/Serialisation/CollectionSerializationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using IIIF.Presentation.V3; | ||
using IIIF.Presentation.V3.Content; | ||
using IIIF.Presentation.V3.Strings; | ||
using IIIF.Serialisation; | ||
|
||
namespace IIIF.Tests.Serialisation; | ||
|
||
public class CollectionSerializationTests | ||
{ | ||
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 Collection | ||
{ | ||
Id = "child", | ||
Label = new LanguageMap | ||
{ | ||
{"en", new List<string> | ||
{ | ||
"child" | ||
}} | ||
} | ||
}, | ||
new Manifest | ||
{ | ||
Id = "child", | ||
Label = new LanguageMap | ||
{ | ||
{"en", new List<string> | ||
{ | ||
"child manifest" | ||
}} | ||
} | ||
} | ||
}, | ||
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 void CanDeserialiseSerialisedCollection() | ||
{ | ||
var serialisedCollection = sampleCollection.AsJson(); | ||
|
||
var deserialised = serialisedCollection.FromJson<Collection>(); | ||
|
||
deserialised.Should().BeEquivalentTo(sampleCollection); | ||
} | ||
|
||
[Fact] | ||
public void CanDeserialiseSerialisedCollection_Stream() | ||
{ | ||
using var memoryStream = new MemoryStream(); | ||
sampleCollection.AsJsonStream(memoryStream); | ||
|
||
memoryStream.Position = 0; | ||
var deserialised = memoryStream.FromJsonStream<Collection>(); | ||
|
||
deserialised.Should().BeEquivalentTo(sampleCollection); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/IIIF/IIIF/Serialisation/Deserialisation/CollectionItemConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using IIIF.Presentation.V3; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace IIIF.Serialisation.Deserialisation; | ||
|
||
public class CollectionItemConverter : ReadOnlyConverter<ICollectionItem> | ||
{ | ||
public override ICollectionItem? ReadJson(JsonReader reader, Type objectType, ICollectionItem? existingValue, bool hasExistingValue, | ||
JsonSerializer serializer) | ||
{ | ||
var jsonObject = JObject.Load(reader); | ||
|
||
ICollectionItem collectionItem = jsonObject["type"].Value<string>() switch | ||
{ | ||
nameof(Collection) => new Collection(), | ||
nameof(Manifest) => new Manifest(), | ||
_ => null | ||
}; | ||
|
||
serializer.Populate(jsonObject.CreateReader(), collectionItem); | ||
return collectionItem; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters