-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add musician example for derived types deserialization
- Loading branch information
nolan.sedley
committed
Jun 8, 2020
1 parent
abac941
commit 4e273ab
Showing
7 changed files
with
105 additions
and
24 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Collections.Generic; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
[ApiController] | ||
[Route("musicians")] | ||
public class MusiciansController : ControllerBase | ||
{ | ||
public ActionResult Post(IEnumerable<Musician> musiciansToCreate) => Accepted(musiciansToCreate); | ||
} |
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 |
---|---|---|
@@ -1,7 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
</Project> | ||
<ItemGroup> | ||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3"/> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.4"/> | ||
<PackageReference Include="JsonSubTypes" Version="1.7.0"/> | ||
</ItemGroup> | ||
</Project> |
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,27 @@ | ||
using JsonSubTypes; | ||
using Newtonsoft.Json; | ||
|
||
// one possible solution is to use JsonSubTypes attributes | ||
// [JsonConverter(typeof(JsonSubtypes), nameof(Musician.InstrumentType))] | ||
// [JsonSubtypes.KnownSubType(typeof(Guitarist), "Guitar")] | ||
// [JsonSubtypes.KnownSubType(typeof(Pianist), "Piano")] | ||
public abstract class Musician | ||
{ | ||
public abstract string InstrumentType { get; } | ||
public string FirstName { get; set; } | ||
public string LastName { get; set; } | ||
|
||
public class Guitarist : Musician | ||
{ | ||
public override string InstrumentType { get; } = "Guitar"; | ||
public int StringCount { get; set; } | ||
public bool CanPlayWonderwall { get; set; } | ||
} | ||
|
||
public class Pianist : Musician | ||
{ | ||
public override string InstrumentType { get; } = "Piano"; | ||
public int HandSpanRating { get; set; } | ||
} | ||
} | ||
|
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,28 @@ | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
// You could write a custom JsonConverter to tell the serializer how to act. | ||
public class MusicianConverter : JsonConverter<Musician> | ||
{ | ||
public override Musician ReadJson(JsonReader reader, Type objectType, [AllowNull] Musician existingValue, bool hasExistingValue, JsonSerializer serializer) | ||
{ | ||
var jObject = JObject.Load(reader); | ||
var typeDiscriminator = jObject["instrumentType"].Value<string>(); | ||
switch (typeDiscriminator) | ||
{ | ||
case "Guitar": | ||
return serializer.Deserialize<Musician.Guitarist>(reader); | ||
case "Piano": | ||
return serializer.Deserialize<Musician.Pianist>(reader); | ||
default: | ||
throw new NotSupportedException(); | ||
} | ||
} | ||
|
||
public override void WriteJson(JsonWriter writer, [AllowNull] Musician value, JsonSerializer serializer) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
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
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,18 @@ | ||
POST http://localhost:5000/musicians HTTP/1.1 | ||
Content-Type: application/json | ||
|
||
[ | ||
{ | ||
"instrumentType": "Guitar", | ||
"stringCount": 6, | ||
"canPlayWonderwall": true, | ||
"firstName": "Noel", | ||
"lastName": "Gallagher" | ||
}, | ||
{ | ||
"instrumentType": "Piano", | ||
"handSpanRating": 15, | ||
"firstName": "Claude", | ||
"lastName": "Debussy" | ||
} | ||
] |