aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid <daullmer@gmail.com>2020-07-12 20:11:59 +0200
committerDavid <daullmer@gmail.com>2020-07-12 20:11:59 +0200
commitee03b919f98032d2c49bd1613a5ca0874790062d (patch)
treef127ca26c4c70dce87d022ab1debea4779d3968a
parentf7c7b1e7e15678bb8ba92f082b9bc2882c87baae (diff)
Fix parsing
-rw-r--r--Jellyfin.Api/Controllers/ConfigurationController.cs3
-rw-r--r--Jellyfin.Api/Controllers/PluginsController.cs3
-rw-r--r--MediaBrowser.Common/Json/Converters/JsonDoubleConverter.cs56
-rw-r--r--MediaBrowser.Common/Json/JsonDefaults.cs1
-rw-r--r--MediaBrowser.Model/Configuration/BaseApplicationConfiguration.cs8
5 files changed, 68 insertions, 3 deletions
diff --git a/Jellyfin.Api/Controllers/ConfigurationController.cs b/Jellyfin.Api/Controllers/ConfigurationController.cs
index 13933cb33..d3c29969b 100644
--- a/Jellyfin.Api/Controllers/ConfigurationController.cs
+++ b/Jellyfin.Api/Controllers/ConfigurationController.cs
@@ -2,6 +2,7 @@ using System.Text.Json;
using System.Threading.Tasks;
using Jellyfin.Api.Constants;
using Jellyfin.Api.Models.ConfigurationDtos;
+using MediaBrowser.Common.Json;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Model.Configuration;
@@ -87,7 +88,7 @@ namespace Jellyfin.Api.Controllers
public async Task<ActionResult> UpdateNamedConfiguration([FromRoute] string? key)
{
var configurationType = _configurationManager.GetConfigurationType(key);
- var configuration = await JsonSerializer.DeserializeAsync(Request.Body, configurationType).ConfigureAwait(false);
+ var configuration = await JsonSerializer.DeserializeAsync(Request.Body, configurationType, JsonDefaults.GetOptions()).ConfigureAwait(false);
_configurationManager.SaveConfiguration(key, configuration);
return NoContent();
}
diff --git a/Jellyfin.Api/Controllers/PluginsController.cs b/Jellyfin.Api/Controllers/PluginsController.cs
index 056395a51..9b5529c37 100644
--- a/Jellyfin.Api/Controllers/PluginsController.cs
+++ b/Jellyfin.Api/Controllers/PluginsController.cs
@@ -7,6 +7,7 @@ using System.Threading.Tasks;
using Jellyfin.Api.Constants;
using Jellyfin.Api.Models.PluginDtos;
using MediaBrowser.Common;
+using MediaBrowser.Common.Json;
using MediaBrowser.Common.Plugins;
using MediaBrowser.Common.Updates;
using MediaBrowser.Model.Plugins;
@@ -118,7 +119,7 @@ namespace Jellyfin.Api.Controllers
return NotFound();
}
- var configuration = (BasePluginConfiguration)await JsonSerializer.DeserializeAsync(Request.Body, plugin.ConfigurationType)
+ var configuration = (BasePluginConfiguration)await JsonSerializer.DeserializeAsync(Request.Body, plugin.ConfigurationType, JsonDefaults.GetOptions())
.ConfigureAwait(false);
plugin.UpdateConfiguration(configuration);
diff --git a/MediaBrowser.Common/Json/Converters/JsonDoubleConverter.cs b/MediaBrowser.Common/Json/Converters/JsonDoubleConverter.cs
new file mode 100644
index 000000000..e5e9f28da
--- /dev/null
+++ b/MediaBrowser.Common/Json/Converters/JsonDoubleConverter.cs
@@ -0,0 +1,56 @@
+using System;
+using System.Buffers;
+using System.Buffers.Text;
+using System.Globalization;
+using System.Text.Json;
+using System.Text.Json.Serialization;
+
+namespace MediaBrowser.Common.Json.Converters
+{
+ /// <summary>
+ /// Double to String JSON converter.
+ /// Web client send quoted doubles.
+ /// </summary>
+ public class JsonDoubleConverter : JsonConverter<double>
+ {
+ /// <summary>
+ /// Read JSON string as double.
+ /// </summary>
+ /// <param name="reader"><see cref="Utf8JsonReader"/>.</param>
+ /// <param name="typeToConvert">Type.</param>
+ /// <param name="options">Options.</param>
+ /// <returns>Parsed value.</returns>
+ public override double Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
+ {
+ if (reader.TokenType == JsonTokenType.String)
+ {
+ // try to parse number directly from bytes
+ var span = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan;
+ if (Utf8Parser.TryParse(span, out double number, out var bytesConsumed) && span.Length == bytesConsumed)
+ {
+ return number;
+ }
+
+ // try to parse from a string if the above failed, this covers cases with other escaped/UTF characters
+ if (double.TryParse(reader.GetString(), out number))
+ {
+ return number;
+ }
+ }
+
+ // fallback to default handling
+ return reader.GetDouble();
+ }
+
+ /// <summary>
+ /// Write double to JSON string.
+ /// </summary>
+ /// <param name="writer"><see cref="Utf8JsonWriter"/>.</param>
+ /// <param name="value">Value to write.</param>
+ /// <param name="options">Options.</param>
+ public override void Write(Utf8JsonWriter writer, double value, JsonSerializerOptions options)
+ {
+ writer.WriteStringValue(value.ToString(NumberFormatInfo.InvariantInfo));
+ }
+ }
+}
diff --git a/MediaBrowser.Common/Json/JsonDefaults.cs b/MediaBrowser.Common/Json/JsonDefaults.cs
index 13f2f060b..36ab6d900 100644
--- a/MediaBrowser.Common/Json/JsonDefaults.cs
+++ b/MediaBrowser.Common/Json/JsonDefaults.cs
@@ -32,6 +32,7 @@ namespace MediaBrowser.Common.Json
options.Converters.Add(new JsonStringEnumConverter());
options.Converters.Add(new JsonNonStringKeyDictionaryConverterFactory());
options.Converters.Add(new JsonInt64Converter());
+ options.Converters.Add(new JsonDoubleConverter());
return options;
}
diff --git a/MediaBrowser.Model/Configuration/BaseApplicationConfiguration.cs b/MediaBrowser.Model/Configuration/BaseApplicationConfiguration.cs
index cdd322c94..db06c06fc 100644
--- a/MediaBrowser.Model/Configuration/BaseApplicationConfiguration.cs
+++ b/MediaBrowser.Model/Configuration/BaseApplicationConfiguration.cs
@@ -44,7 +44,13 @@ namespace MediaBrowser.Model.Configuration
public string PreviousVersionStr
{
get => PreviousVersion?.ToString();
- set => PreviousVersion = Version.Parse(value);
+ set
+ {
+ if (Version.TryParse(value, out var version))
+ {
+ PreviousVersion = version;
+ }
+ }
}
/// <summary>