Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use System.Text.Json in ColorPickerValueConverter #13311

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text.Json;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Extensions;

Expand All @@ -22,25 +21,28 @@ public override PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType

public override object? ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object? source, bool preview)
{
var useLabel = UseLabel(propertyType);
bool useLabel = UseLabel(propertyType);

if (source == null)
{
return useLabel ? null : string.Empty;
}

var ssource = source.ToString()!;
string ssource = source.ToString()!;
if (ssource.DetectIsJson())
{
try
{
JObject? jo = JsonConvert.DeserializeObject<JObject>(ssource);
if (useLabel)
using (JsonDocument doc = JsonDocument.Parse(ssource))
{
return new PickedColor(jo!["value"]!.ToString(), jo["label"]!.ToString());
}
JsonElement json = doc.RootElement;
if (useLabel)
{
return new PickedColor(json.GetProperty("value").GetString()!, json.GetProperty("label").GetString()!);
}

return jo!["value"]!.ToString();
return json.GetProperty("value").GetString()!;
}
}
catch
{
Expand Down