From 865d8e633e05361798e5a1aab7f4b0ffe7ee48d8 Mon Sep 17 00:00:00 2001 From: Shannon Date: Wed, 23 Oct 2013 20:27:48 +1100 Subject: [PATCH] Created a json converter so we don't have to use DarkMagic - this is based on a new "JSON" value type that is applied to a property editor. --- .../PublishedContent/PublishedPropertyType.cs | 14 +----- .../PropertyEditors/PropertyValueEditor.cs | 2 + .../ValueConverters/IntegerValueConverter.cs | 3 +- .../ValueConverters/JsonValueConverter.cs | 49 +++++++++++++++++++ src/Umbraco.Core/Umbraco.Core.csproj | 1 + 5 files changed, 54 insertions(+), 15 deletions(-) create mode 100644 src/Umbraco.Core/PropertyEditors/ValueConverters/JsonValueConverter.cs diff --git a/src/Umbraco.Core/Models/PublishedContent/PublishedPropertyType.cs b/src/Umbraco.Core/Models/PublishedContent/PublishedPropertyType.cs index 5e72cb6b2f..e6f180e63c 100644 --- a/src/Umbraco.Core/Models/PublishedContent/PublishedPropertyType.cs +++ b/src/Umbraco.Core/Models/PublishedContent/PublishedPropertyType.cs @@ -188,19 +188,7 @@ namespace Umbraco.Core.Models.PublishedContent if (bool.TryParse(stringSource, out b)) return b; - //try json - expensive - if (stringSource.DetectIsJson()) - { - try - { - var obj = JsonConvert.DeserializeObject(stringSource); - return obj; - } - catch - { - //swallow, continue trying other things - } - } + //TODO: We can change this just like we do for the JSON converter - but to maintain compatibility might mean this still has to remain here // try xml - that is expensive, performance-wise XElement elt; diff --git a/src/Umbraco.Core/PropertyEditors/PropertyValueEditor.cs b/src/Umbraco.Core/PropertyEditors/PropertyValueEditor.cs index becbf785ac..86de15d309 100644 --- a/src/Umbraco.Core/PropertyEditors/PropertyValueEditor.cs +++ b/src/Umbraco.Core/PropertyEditors/PropertyValueEditor.cs @@ -105,6 +105,8 @@ namespace Umbraco.Core.PropertyEditors case "STRING": return DataTypeDatabaseType.Nvarchar; case "TEXT": + case "JSON": + case "XML": return DataTypeDatabaseType.Ntext; case "DATETIME": case "DATE": diff --git a/src/Umbraco.Core/PropertyEditors/ValueConverters/IntegerValueConverter.cs b/src/Umbraco.Core/PropertyEditors/ValueConverters/IntegerValueConverter.cs index 445b333d4b..96c908295b 100644 --- a/src/Umbraco.Core/PropertyEditors/ValueConverters/IntegerValueConverter.cs +++ b/src/Umbraco.Core/PropertyEditors/ValueConverters/IntegerValueConverter.cs @@ -1,5 +1,4 @@ -using System; -using Umbraco.Core.Models.PublishedContent; +using Umbraco.Core.Models.PublishedContent; namespace Umbraco.Core.PropertyEditors.ValueConverters { diff --git a/src/Umbraco.Core/PropertyEditors/ValueConverters/JsonValueConverter.cs b/src/Umbraco.Core/PropertyEditors/ValueConverters/JsonValueConverter.cs new file mode 100644 index 0000000000..bcecf4fd7a --- /dev/null +++ b/src/Umbraco.Core/PropertyEditors/ValueConverters/JsonValueConverter.cs @@ -0,0 +1,49 @@ +using System; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Umbraco.Core.Logging; +using Umbraco.Core.Models.PublishedContent; + +namespace Umbraco.Core.PropertyEditors.ValueConverters +{ + [PropertyValueType(typeof(JToken))] + [PropertyValueCache(PropertyCacheValue.All, PropertyCacheLevel.Content)] + public class JsonValueConverter : PropertyValueConverterBase + { + /// + /// It is a converter for any value type that is "JSON" + /// + /// + /// + public override bool IsConverter(PublishedPropertyType propertyType) + { + var propertyEditor = PropertyEditorResolver.Current.GetByAlias(propertyType.PropertyEditorAlias); + if (propertyEditor == null) return false; + return propertyEditor.ValueEditor.ValueType.InvariantEquals("json"); + } + + public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview) + { + if (source == null) return null; + var sourceString = source.ToString(); + + if (sourceString.DetectIsJson()) + { + try + { + var obj = JsonConvert.DeserializeObject(sourceString); + return obj; + } + catch (Exception ex) + { + LogHelper.Error("Could not parse the string " + sourceString + " to a json object", ex); + } + } + + //it's not json, just return the string + return sourceString; + } + + //TODO: Now to convert that to XPath! + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index c592970273..eb647abd21 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -334,6 +334,7 @@ +