From 0547c4c07e0b8de16a33649fee07e47787d59bb8 Mon Sep 17 00:00:00 2001 From: Tom Fulton Date: Tue, 29 Apr 2014 18:18:14 -0600 Subject: [PATCH] Ensure we only save the "cleaned" model to the DB * Extract the model cleaning logic to a method for re-use * Call cleaning method from ConvertEditorToDb (we were only calling on ConvertDbToString (cache)) * Update ConvertDbToEditor to route through new deserialization helper, so that it gets the "virtual" properties it needs that were lost in the cleaning --- .../Umbraco.Archetype/Models/Archetype.cs | 18 +++++++++++++++++- .../PropertyEditors/ArcheTypePropertyEditor.cs | 14 +++----------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/app/Umbraco/Umbraco.Archetype/Models/Archetype.cs b/app/Umbraco/Umbraco.Archetype/Models/Archetype.cs index 207e9f6..4df3c7a 100644 --- a/app/Umbraco/Umbraco.Archetype/Models/Archetype.cs +++ b/app/Umbraco/Umbraco.Archetype/Models/Archetype.cs @@ -1,6 +1,9 @@ -using System.Collections; +using System; +using System.Collections; using System.Collections.Generic; +using System.Linq; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; namespace Archetype.Umbraco.Models { @@ -24,5 +27,18 @@ namespace Archetype.Umbraco.Models { return this.GetEnumerator(); } + + internal string SerializeForPersistence() + { + var json = JObject.Parse(JsonConvert.SerializeObject(this)); + var propertiesToRemove = new String[] { "propertyEditorAlias", "dataTypeId", "dataTypeGuid" }; + + json.Descendants().OfType() + .Where(p => propertiesToRemove.Contains(p.Name)) + .ToList() + .ForEach(x => x.Remove()); + + return json.ToString(); + } } } diff --git a/app/Umbraco/Umbraco.Archetype/PropertyEditors/ArcheTypePropertyEditor.cs b/app/Umbraco/Umbraco.Archetype/PropertyEditors/ArcheTypePropertyEditor.cs index 81414dc..e55ed1d 100644 --- a/app/Umbraco/Umbraco.Archetype/PropertyEditors/ArcheTypePropertyEditor.cs +++ b/app/Umbraco/Umbraco.Archetype/PropertyEditors/ArcheTypePropertyEditor.cs @@ -72,15 +72,7 @@ namespace Archetype.Umbraco.PropertyEditors } } - var json = JObject.Parse(JsonConvert.SerializeObject(archetype)); - var propertiesToRemove = new String[] { "propertyEditorAlias", "dataTypeId", "dataTypeGuid" }; - - json.Descendants().OfType() - .Where(p => propertiesToRemove.Contains(p.Name)) - .ToList() - .ForEach(x => x.Remove()); - - return json.ToString(); + return archetype.SerializeForPersistence(); } public override object ConvertDbToEditor(Property property, PropertyType propertyType, IDataTypeService dataTypeService) @@ -88,7 +80,7 @@ namespace Archetype.Umbraco.PropertyEditors if (property.Value == null || property.Value.ToString() == "") return string.Empty; - var archetype = JsonConvert.DeserializeObject(property.Value.ToString(), _jsonSettings); + var archetype = new ArchetypeHelper().DeserializeJsonToArchetype(property.Value.ToString(), propertyType.DataTypeDefinitionId); foreach (var fieldset in archetype.Fieldsets) { @@ -123,7 +115,7 @@ namespace Archetype.Umbraco.PropertyEditors } } - return JsonConvert.SerializeObject(archetype); + return archetype.SerializeForPersistence(); } }