-
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 #95 from FirelyTeam/feature/parse-complex-author-o…
…bject Support complex Author objects in manifest.json
- Loading branch information
Showing
5 changed files
with
271 additions
and
2 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -40,6 +40,82 @@ public void RoundtripsAllCommonProperties() | |
manif2.Should().BeEquivalentTo(manif); | ||
} | ||
|
||
[TestMethod] | ||
public void TestAuthorSerialization() | ||
{ | ||
PackageManifest manif = new(name: "author.test", version: "1.0.1") | ||
{ | ||
Author = "Marten" | ||
}; | ||
|
||
var json = PackageParser.SerializeManifest(manif); | ||
|
||
json.Should().Contain("\"author\": \"Marten\""); | ||
var manif2 = PackageParser.ParseManifest(json); | ||
manif2.Should().BeEquivalentTo(manif); | ||
manif2?.AuthorInformation?.Name.Should().Be("Marten"); | ||
|
||
//Test the other way around | ||
|
||
manif = new(name: "author.test", version: "1.0.1") | ||
{ | ||
AuthorInformation = new() { Name = "Marten", Url = "https://foo.bar", Email = "[email protected]" } | ||
}; | ||
|
||
json = PackageParser.SerializeManifest(manif); | ||
|
||
json.Should().Contain(" \"author\": {\r\n \"name\": \"Marten\",\r\n \"email\": \"[email protected]\",\r\n \"url\": \"https://foo.bar\"\r\n }"); | ||
manif2 = PackageParser.ParseManifest(json); | ||
manif2.Should().BeEquivalentTo(manif); | ||
manif2?.Author?.Should().Be("Marten <[email protected]> (https://foo.bar)"); | ||
} | ||
|
||
[TestMethod] | ||
public void TestReadingAComplexAuthorProperty() | ||
{ | ||
var json = File.ReadAllText($"TestData/unknown-properties-package.json"); | ||
var manifest = PackageParser.ParseManifest(json); | ||
//manifest.AuthorInformation() | ||
manifest.Should().NotBeNull(); | ||
} | ||
|
||
[DataRow("foo <foo@bar> (http://foo.bar)", "foo", "foo@bar", "http://foo.bar")] | ||
[DataRow("foo (http://foo.bar)", "foo", null, "http://foo.bar")] | ||
[DataRow("foo <foo@bar>", "foo", "foo@bar", null)] | ||
[DataRow("<foo@bar> (http://foo.bar)", null, "foo@bar", "http://foo.bar")] | ||
[DataTestMethod] | ||
public void TestAuthorStringParsing(string? fullAuthor, string? name, string? email, string? url) | ||
{ | ||
PackageManifest manif = new(name: "authot.test", version: "1.0.1") | ||
{ | ||
Author = fullAuthor | ||
}; | ||
|
||
manif.AuthorInformation?.Name.Should().Be(name); | ||
manif.AuthorInformation?.Email.Should().Be(email); | ||
manif.AuthorInformation?.Url.Should().Be(url); | ||
} | ||
|
||
[DataRow("foo <foo@bar> (http://foo.bar)", "foo", "foo@bar", "http://foo.bar")] | ||
[DataRow("foo (http://foo.bar)", "foo", null, "http://foo.bar")] | ||
[DataRow("foo <foo@bar>", "foo", "foo@bar", null)] | ||
[DataRow("<foo@bar> (http://foo.bar)", null, "foo@bar", "http://foo.bar")] | ||
[DataTestMethod] | ||
public void TestAuthorStringSerializing(string? fullAuthor, string? name, string? email, string? url) | ||
{ | ||
PackageManifest manif = new(name: "authot.test", version: "1.0.1") | ||
{ | ||
AuthorInformation = new() | ||
{ | ||
Name = name, | ||
Email = email, | ||
Url = url | ||
} | ||
}; | ||
|
||
manif.Author.Should().Be(fullAuthor); | ||
} | ||
|
||
[TestMethod] | ||
public void DoesNotWriteNulls() | ||
{ | ||
|
13 changes: 13 additions & 0 deletions
13
Firely.Fhir.Packages.Tests/TestData/unknown-properties-package.json
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,13 @@ | ||
{ | ||
"dependencies": { | ||
"hl7.fhir.r4.core": "4.0.1" | ||
}, | ||
"fhirVersions": [ | ||
"4.0.1" | ||
], | ||
"author": { | ||
"name": "Barney Rubble", | ||
"email": "[email protected]", | ||
"url": "http://barnyrubble.tumblr.com/" | ||
} | ||
} |
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,57 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
|
||
namespace Firely.Fhir.Packages | ||
{ | ||
/// <summary> | ||
/// Only does something custom for the author element, otherwise just do the regular serialization. | ||
/// </summary> | ||
internal class AuthorJsonConverter : JsonConverter | ||
{ | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return objectType == typeof(AuthorInfo) || objectType == typeof(string); | ||
} | ||
|
||
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) | ||
{ | ||
|
||
if (reader.TokenType == JsonToken.StartObject) | ||
{ | ||
|
||
if (objectType == typeof(AuthorInfo)) | ||
{ | ||
// Use DummyDictionary to fool JsonSerializer into not using this converter recursively | ||
var author = serializer.Deserialize<DummyDictionary>(reader); | ||
return author; | ||
} | ||
} | ||
else if (reader.TokenType == JsonToken.String && reader.Path == "author") | ||
{ | ||
if (reader.Value?.ToString() is { } value) | ||
{ | ||
var author = AuthorSerializer.Deserialize(value); | ||
return author; | ||
} | ||
} | ||
return serializer.Deserialize(reader); | ||
} | ||
|
||
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) | ||
{ | ||
if (value is AuthorInfo author) | ||
{ | ||
serializer.Serialize(writer, author.ParsedFromString ? AuthorSerializer.Serialize(author) : value); | ||
} | ||
else | ||
{ | ||
serializer.Serialize(writer, value); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Dummy to fool JsonSerializer into not using this converter recursively | ||
/// </summary> | ||
private class DummyDictionary : AuthorInfo { } | ||
} | ||
} |
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
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