mirror of
https://github.com/LukeHagar/plexcsharp.git
synced 2025-12-08 20:47:48 +00:00
migrated to v15
This commit is contained in:
@@ -15,24 +15,52 @@ namespace PlexAPI.Utils
|
||||
using System.Net.Http.Headers;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Numerics;
|
||||
using Newtonsoft.Json;
|
||||
using NodaTime;
|
||||
using System.Collections;
|
||||
|
||||
public class Utilities
|
||||
{
|
||||
public static string SerializeJSON(object obj)
|
||||
public static JsonConverter[] GetJsonConverters(Type type, string format = "")
|
||||
{
|
||||
if (format == "string")
|
||||
{
|
||||
if (type == typeof(BigInteger))
|
||||
{
|
||||
return new JsonConverter[] { new BigIntSerializer() };
|
||||
}
|
||||
if (type == typeof(Decimal))
|
||||
{
|
||||
return new JsonConverter[] { new DecimalSerializer() };
|
||||
}
|
||||
}
|
||||
|
||||
return new JsonConverter[]
|
||||
{
|
||||
new IsoDateTimeSerializer(),
|
||||
new EnumSerializer(),
|
||||
};
|
||||
}
|
||||
|
||||
public static string SerializeJSON(object obj, string format = "")
|
||||
{
|
||||
var type = obj.GetType();
|
||||
if (IsList(obj))
|
||||
{
|
||||
type = type.GetGenericArguments().Single();
|
||||
}
|
||||
else if (IsDictionary(obj))
|
||||
{
|
||||
type = type.GetGenericArguments().Last();
|
||||
}
|
||||
|
||||
return JsonConvert.SerializeObject(
|
||||
obj,
|
||||
new JsonSerializerSettings()
|
||||
{
|
||||
NullValueHandling = NullValueHandling.Ignore,
|
||||
Converters = new JsonConverter[]
|
||||
{
|
||||
new IsoDateTimeSerializer(),
|
||||
new EnumSerializer()
|
||||
}
|
||||
Converters = GetJsonConverters(type, format)
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -82,11 +110,11 @@ namespace PlexAPI.Utils
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsPrimitive(object obj) => obj != null && obj.GetType().IsPrimitive;
|
||||
public static bool IsPrimitive(object? obj) => obj != null && obj.GetType().IsPrimitive;
|
||||
|
||||
public static bool IsEnum(object obj) => obj != null && obj.GetType().IsEnum;
|
||||
public static bool IsEnum(object? obj) => obj != null && obj.GetType().IsEnum;
|
||||
|
||||
public static bool IsDate(object obj) =>
|
||||
public static bool IsDate(object? obj) =>
|
||||
obj != null && (obj.GetType() == typeof(DateTime) || obj.GetType() == typeof(LocalDate));
|
||||
|
||||
private static string StripSurroundingQuotes(string input)
|
||||
@@ -238,4 +266,4 @@ namespace PlexAPI.Utils
|
||||
return template;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user