using Umbraco.Cms.Core.PropertyEditors.ValueConverters;
using Umbraco.Cms.Core.Serialization;
using Umbraco.Extensions;
namespace Umbraco.Cms.Infrastructure.PropertyEditors;
///
/// Handles the parsing of raw values to objects.
///
internal sealed class FileUploadValueParser
{
private readonly IJsonSerializer _jsonSerializer;
///
/// Initializes a new instance of the class.
///
///
public FileUploadValueParser(IJsonSerializer jsonSerializer) => _jsonSerializer = jsonSerializer;
///
/// Parses raw value to a .
///
/// The editor value.
/// value
///
public FileUploadValue? Parse(object? editorValue)
{
if (editorValue is null)
{
return null;
}
if (editorValue is string sourceString && sourceString.DetectIsJson() is false)
{
return new FileUploadValue()
{
Src = sourceString,
};
}
return _jsonSerializer.TryDeserialize(editorValue, out FileUploadValue? modelValue)
? modelValue
: throw new ArgumentException($"Could not parse editor value to a {nameof(FileUploadValue)} object.");
}
}