-
-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite the SearchMulti function using Json trickery
- Loading branch information
Showing
12 changed files
with
129 additions
and
101 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
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 |
---|---|---|
|
@@ -10,6 +10,9 @@ public enum MediaType | |
Movie = 1, | ||
|
||
[EnumValue("tv")] | ||
Tv = 2 | ||
Tv = 2, | ||
|
||
[EnumValue("person")] | ||
Person = 3 | ||
} | ||
} |
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,19 @@ | ||
using Newtonsoft.Json; | ||
using TMDbLib.Objects.General; | ||
using TMDbLib.Utilities; | ||
|
||
namespace TMDbLib.Objects.Search | ||
{ | ||
[JsonConverter(typeof(SearchBaseConverter))] | ||
public class SearchBase | ||
{ | ||
[JsonProperty("id")] | ||
public int Id { get; set; } | ||
|
||
[JsonProperty("popularity")] | ||
public double Popularity { get; set; } | ||
|
||
[JsonIgnore] | ||
public MediaType MediaType { 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
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 was deleted.
Oops, something went wrong.
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,58 @@ | ||
using System; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using TMDbLib.Objects.General; | ||
using TMDbLib.Objects.Search; | ||
|
||
namespace TMDbLib.Utilities | ||
{ | ||
internal class SearchBaseConverter : JsonConverter | ||
{ | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return objectType == typeof(SearchBase); | ||
} | ||
|
||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
JObject jObject = JObject.Load(reader); | ||
|
||
SearchBase result; | ||
if (jObject["media_type"] == null) | ||
{ | ||
// We cannot determine the correct type, let's hope we were provided one | ||
result = (SearchBase)Activator.CreateInstance(objectType); | ||
} | ||
else | ||
{ | ||
// Determine the type based on the media_type | ||
MediaType mediaType = jObject["media_type"].ToObject<MediaType>(); | ||
|
||
switch (mediaType) | ||
{ | ||
case MediaType.Movie: | ||
result = new SearchMovie(); | ||
break; | ||
case MediaType.Tv: | ||
result = new SearchTv(); | ||
break; | ||
case MediaType.Person: | ||
result = new SearchPerson(); | ||
break; | ||
default: | ||
throw new ArgumentOutOfRangeException(); | ||
} | ||
} | ||
|
||
// Populate the result | ||
serializer.Populate(jObject.CreateReader(), result); | ||
|
||
return result; | ||
} | ||
|
||
public override void WriteJson(JsonWriter writer, object 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