Netcore: Migration of Model classes from Umbraco.Infrastructure to Core (#9404)
* Migrating more model, mapping and tree classes * Migrating files from Mapping dir without Newtonsoft dependency * Migrating files from PublishedContent and Editors dirs without Newtonsoft dependency + some more of the same kind * Migrating DataType class without the usage of Newtonsoft.Json and making the corresponding changes to all classes affected * Combining 3 ContentExtensions files into 1 * Refactoring from migrating ContentExtensions * Migrating more classes * Migrating ContentRepositoryExtensions - combining it with existing file in Umbraco.Core * removing Newtonsoft json dependency & migrating file. Adding partial migration of ConfigurationEditor, so PropertyTagsExtensions can be migrated * Migrating ContentTagsExtensions, and refactoring from changes in PropertyTagsExtensions * Changes that should be reverted once ConfigurationEditor class is fully migrated * VS couldn't find Composing, so build was failing. Removing the using solves the problem * Handling a single case for deserializing a subset of an input * Small changes and added tests to JsonNetSerializer Signed-off-by: Bjarke Berg <mail@bergmania.dk> * Migrated ConfigurationEditor Signed-off-by: Bjarke Berg <mail@bergmania.dk> Co-authored-by: Bjarke Berg <mail@bergmania.dk>
This commit is contained in:
committed by
GitHub
parent
d498c1a2cd
commit
dd5f400cf3
@@ -0,0 +1,37 @@
|
||||
using System.Reflection;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Umbraco.Core.PropertyEditors;
|
||||
|
||||
namespace Umbraco.Core.Serialization
|
||||
{
|
||||
public class ConfigurationEditorJsonSerializer : JsonNetSerializer, IConfigurationEditorJsonSerializer
|
||||
{
|
||||
public ConfigurationEditorJsonSerializer()
|
||||
{
|
||||
JsonSerializerSettings.Converters.Add(new FuzzyBooleanConverter());
|
||||
JsonSerializerSettings.ContractResolver = new ConfigurationCustomContractResolver();
|
||||
}
|
||||
|
||||
private class ConfigurationCustomContractResolver : DefaultContractResolver
|
||||
{
|
||||
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
|
||||
{
|
||||
// base.CreateProperty deals with [JsonProperty("name")]
|
||||
var property = base.CreateProperty(member, memberSerialization);
|
||||
|
||||
// override with our custom attribute, if any
|
||||
var attribute = member.GetCustomAttribute<ConfigurationFieldAttribute>();
|
||||
if (attribute != null) property.PropertyName = attribute.Key;
|
||||
|
||||
// for value types,
|
||||
// don't try to deserialize nulls (in legacy json)
|
||||
// no impact on serialization (value cannot be null)
|
||||
if (member is PropertyInfo propertyInfo && propertyInfo.PropertyType.IsValueType)
|
||||
property.NullValueHandling = NullValueHandling.Ignore;
|
||||
|
||||
return property;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Umbraco.Core.Serialization
|
||||
{
|
||||
public class JsonNetSerializer : IJsonSerializer
|
||||
{
|
||||
private static readonly JsonConverter[] _defaultConverters = new JsonConverter[]
|
||||
protected static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings()
|
||||
{
|
||||
new StringEnumConverter()
|
||||
Converters = new List<JsonConverter>()
|
||||
{
|
||||
new StringEnumConverter()
|
||||
}
|
||||
};
|
||||
|
||||
public string Serialize(object input)
|
||||
{
|
||||
return JsonConvert.SerializeObject(input, _defaultConverters);
|
||||
return JsonConvert.SerializeObject(input, JsonSerializerSettings);
|
||||
}
|
||||
|
||||
public T Deserialize<T>(string input)
|
||||
{
|
||||
return JsonConvert.DeserializeObject<T>(input, _defaultConverters);
|
||||
return JsonConvert.DeserializeObject<T>(input, JsonSerializerSettings);
|
||||
}
|
||||
|
||||
public T DeserializeSubset<T>(string input, string key)
|
||||
{
|
||||
if (key == null) throw new ArgumentNullException(nameof(key));
|
||||
|
||||
var root = JsonConvert.DeserializeObject<JObject>(input);
|
||||
|
||||
var jToken = root.SelectToken(key);
|
||||
|
||||
return jToken switch
|
||||
{
|
||||
JArray jArray => jArray.ToObject<T>(),
|
||||
JObject jObject => jObject.ToObject<T>(),
|
||||
_ => jToken is null ? default : jToken.Value<T>()
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user