2020-07-01 17:19:56 +12:00
|
|
|
|
using Newtonsoft.Json;
|
2020-09-25 00:32:11 +10:00
|
|
|
|
using System;
|
2020-07-01 17:19:56 +12:00
|
|
|
|
using System.Collections.Generic;
|
2021-01-28 13:50:18 +11:00
|
|
|
|
using Umbraco.Core.Models;
|
2020-07-01 17:19:56 +12:00
|
|
|
|
using Umbraco.Core.Serialization;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.PublishedCache.NuCache.DataSource
|
|
|
|
|
|
{
|
2020-07-03 12:11:05 +10:00
|
|
|
|
|
2020-09-25 00:32:11 +10:00
|
|
|
|
public class JsonContentNestedDataSerializer : IContentCacheDataSerializer
|
2020-07-01 17:19:56 +12:00
|
|
|
|
{
|
2021-01-25 15:56:01 +11:00
|
|
|
|
// by default JsonConvert will deserialize our numeric values as Int64
|
|
|
|
|
|
// which is bad, because they were Int32 in the database - take care
|
|
|
|
|
|
private readonly JsonSerializerSettings _jsonSerializerSettings = new JsonSerializerSettings
|
|
|
|
|
|
{
|
|
|
|
|
|
Converters = new List<JsonConverter> { new ForceInt32Converter() },
|
|
|
|
|
|
|
|
|
|
|
|
// Explicitly specify date handling so that it's consistent and follows the same date handling as MessagePack
|
|
|
|
|
|
DateParseHandling = DateParseHandling.DateTime,
|
|
|
|
|
|
DateFormatHandling = DateFormatHandling.IsoDateFormat,
|
|
|
|
|
|
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
|
|
|
|
|
|
DateFormatString = "o"
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2021-01-28 13:50:18 +11:00
|
|
|
|
public ContentCacheDataModel Deserialize(IReadOnlyContentBase content, string stringData, byte[] byteData)
|
2020-07-01 17:19:56 +12:00
|
|
|
|
{
|
2020-12-16 16:52:43 +11:00
|
|
|
|
if (stringData == null && byteData != null)
|
2020-09-25 00:32:11 +10:00
|
|
|
|
throw new NotSupportedException($"{typeof(JsonContentNestedDataSerializer)} does not support byte[] serialization");
|
|
|
|
|
|
|
2021-01-25 15:56:01 +11:00
|
|
|
|
return JsonConvert.DeserializeObject<ContentCacheDataModel>(stringData, _jsonSerializerSettings);
|
2020-07-01 17:19:56 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-28 13:50:18 +11:00
|
|
|
|
public ContentCacheDataSerializationResult Serialize(IReadOnlyContentBase content, ContentCacheDataModel model)
|
2020-07-01 17:19:56 +12:00
|
|
|
|
{
|
2020-07-03 12:11:05 +10:00
|
|
|
|
// note that numeric values (which are Int32) are serialized without their
|
|
|
|
|
|
// type (eg "value":1234) and JsonConvert by default deserializes them as Int64
|
|
|
|
|
|
|
2020-09-25 00:32:11 +10:00
|
|
|
|
var json = JsonConvert.SerializeObject(model);
|
|
|
|
|
|
return new ContentCacheDataSerializationResult(json, null);
|
2020-07-01 17:19:56 +12:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|