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.
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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":
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using Umbraco.Core.Models.PublishedContent;
|
||||
using Umbraco.Core.Models.PublishedContent;
|
||||
|
||||
namespace Umbraco.Core.PropertyEditors.ValueConverters
|
||||
{
|
||||
|
||||
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// It is a converter for any value type that is "JSON"
|
||||
/// </summary>
|
||||
/// <param name="propertyType"></param>
|
||||
/// <returns></returns>
|
||||
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<JsonValueConverter>("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!
|
||||
}
|
||||
}
|
||||
@@ -334,6 +334,7 @@
|
||||
<Compile Include="PropertyEditors\SupportTagsAttribute.cs" />
|
||||
<Compile Include="PropertyEditors\TagValueType.cs" />
|
||||
<Compile Include="PropertyEditors\ValueConverters\IntegerValueConverter.cs" />
|
||||
<Compile Include="PropertyEditors\ValueConverters\JsonValueConverter.cs" />
|
||||
<Compile Include="PropertyEditors\ValueConverters\MultipleTextStringValueConverter.cs" />
|
||||
<Compile Include="PropertyEditors\ValueConverters\SimpleEditorValueConverter.cs" />
|
||||
<Compile Include="PropertyEditors\ValueConverters\TextStringValueConverter.cs" />
|
||||
|
||||
Reference in New Issue
Block a user