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 collections correctly #49

Merged
merged 2 commits into from
Aug 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
104 changes: 104 additions & 0 deletions src/IIIF/IIIF.Tests/Serialisation/CollectionSerializationTests.cs
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);
}
}
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;
}
}
2 changes: 1 addition & 1 deletion src/IIIF/IIIF/Serialisation/IIIFSerialiserX.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static class IIIFSerialiserX
{
new ImageService2Converter(), new AnnotationV3Converter(), new ResourceBaseV3Converter(),
new StructuralLocationConverter(), new ExternalResourceConverter(), new PaintableConverter(),
new SelectorConverter(), new ServiceConverter(), new ResourceConverter(),
new SelectorConverter(), new ServiceConverter(), new ResourceConverter(), new CollectionItemConverter()
}
};

Expand Down
Loading