2019-04-22 19:49:49 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2018-04-25 15:25:47 +02:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using CSharpTest.Net.Serialization;
|
2021-02-18 11:06:02 +01:00
|
|
|
|
using Umbraco.Extensions;
|
2018-04-25 15:25:47 +02:00
|
|
|
|
|
2021-02-18 11:06:02 +01:00
|
|
|
|
namespace Umbraco.Cms.Infrastructure.PublishedCache.DataSource
|
2018-04-25 15:25:47 +02:00
|
|
|
|
{
|
2021-06-24 09:43:57 -06:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Serializes/Deserializes culture variant data as a dictionary for BTree
|
|
|
|
|
|
/// </summary>
|
2018-04-25 15:25:47 +02:00
|
|
|
|
internal class DictionaryOfCultureVariationSerializer : SerializerBase, ISerializer<IReadOnlyDictionary<string, CultureVariation>>
|
|
|
|
|
|
{
|
|
|
|
|
|
public IReadOnlyDictionary<string, CultureVariation> ReadFrom(Stream stream)
|
|
|
|
|
|
{
|
|
|
|
|
|
// read variations count
|
|
|
|
|
|
var pcount = PrimitiveSerializer.Int32.ReadFrom(stream);
|
2018-05-01 12:45:07 +02:00
|
|
|
|
if (pcount == 0) return Empty;
|
2018-04-25 15:25:47 +02:00
|
|
|
|
|
|
|
|
|
|
// read each variation
|
2019-04-22 19:49:49 +02:00
|
|
|
|
var dict = new Dictionary<string, CultureVariation>(StringComparer.InvariantCultureIgnoreCase);
|
2018-04-25 15:25:47 +02:00
|
|
|
|
for (var i = 0; i < pcount; i++)
|
|
|
|
|
|
{
|
2021-06-24 09:43:57 -06:00
|
|
|
|
var languageId = string.Intern(PrimitiveSerializer.String.ReadFrom(stream));
|
|
|
|
|
|
var cultureVariation = new CultureVariation
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = ReadStringObject(stream),
|
|
|
|
|
|
UrlSegment = ReadStringObject(stream),
|
|
|
|
|
|
Date = ReadDateTime(stream)
|
|
|
|
|
|
};
|
2018-04-25 15:25:47 +02:00
|
|
|
|
dict[languageId] = cultureVariation;
|
|
|
|
|
|
}
|
|
|
|
|
|
return dict;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static readonly IReadOnlyDictionary<string, CultureVariation> Empty = new Dictionary<string, CultureVariation>();
|
|
|
|
|
|
|
|
|
|
|
|
public void WriteTo(IReadOnlyDictionary<string, CultureVariation> value, Stream stream)
|
|
|
|
|
|
{
|
|
|
|
|
|
var variations = value ?? Empty;
|
|
|
|
|
|
|
|
|
|
|
|
// write variations count
|
|
|
|
|
|
PrimitiveSerializer.Int32.WriteTo(variations.Count, stream);
|
|
|
|
|
|
|
|
|
|
|
|
// write each variation
|
|
|
|
|
|
foreach (var (culture, variation) in variations)
|
|
|
|
|
|
{
|
2019-01-27 01:17:32 -05:00
|
|
|
|
// TODO: it's weird we're dealing with cultures here, and languageId in properties
|
2018-04-25 15:25:47 +02:00
|
|
|
|
|
|
|
|
|
|
PrimitiveSerializer.String.WriteTo(culture, stream); // should never be null
|
|
|
|
|
|
WriteObject(variation.Name, stream); // write an object in case it's null (though... should not happen)
|
2019-03-05 11:15:30 +01:00
|
|
|
|
WriteObject(variation.UrlSegment, stream); // write an object in case it's null (though... should not happen)
|
2018-04-28 21:57:07 +02:00
|
|
|
|
PrimitiveSerializer.DateTime.WriteTo(variation.Date, stream);
|
2018-04-25 15:25:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|