Add JSON and MessagePack serialization for HostProject

This commit is contained in:
Dustin Campbell 2024-10-15 16:01:09 -07:00
Родитель b1bfaadde7
Коммит b6b95ac29e
3 изменённых файлов: 64 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,41 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.
using MessagePack;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
namespace Microsoft.AspNetCore.Razor.Serialization.MessagePack.Formatters;
internal sealed class HostProjectFormatter : ValueFormatter<HostProject>
{
public static readonly ValueFormatter<HostProject> Instance = new HostProjectFormatter();
private HostProjectFormatter()
{
}
public override HostProject Deserialize(ref MessagePackReader reader, SerializerCachingOptions options)
{
reader.ReadArrayHeaderAndVerify(5);
var filePath = CachedStringFormatter.Instance.Deserialize(ref reader, options).AssumeNotNull();
var intermediateOutputPath = CachedStringFormatter.Instance.Deserialize(ref reader, options).AssumeNotNull();
var configuration = reader.Deserialize<RazorConfiguration>(options);
var rootNamespace = CachedStringFormatter.Instance.Deserialize(ref reader, options);
var displayName = CachedStringFormatter.Instance.Deserialize(ref reader, options).AssumeNotNull();
return new HostProject(filePath, intermediateOutputPath, configuration, rootNamespace, displayName);
}
public override void Serialize(ref MessagePackWriter writer, HostProject value, SerializerCachingOptions options)
{
writer.WriteArrayHeader(5);
CachedStringFormatter.Instance.Serialize(ref writer, value.FilePath, options);
CachedStringFormatter.Instance.Serialize(ref writer, value.IntermediateOutputPath, options);
writer.Serialize(value.Configuration, options);
CachedStringFormatter.Instance.Serialize(ref writer, value.RootNamespace, options);
CachedStringFormatter.Instance.Serialize(ref writer, value.DisplayName, options);
}
}

Просмотреть файл

@ -100,6 +100,17 @@ internal static partial class ObjectReaders
return new HostDocument(filePath, targetPath, fileKind);
}
public static HostProject ReadHostProjectFromProperties(JsonDataReader reader)
{
var filePath = reader.ReadNonNullString(nameof(HostProject.FilePath));
var intermediateOutputPath = reader.ReadNonNullString(nameof(HostProject.IntermediateOutputPath));
var configuration = reader.ReadNonNullObject(nameof(HostProject.Configuration), ReadConfigurationFromProperties);
var rootNamespace = reader.ReadStringOrNull(nameof(HostProject.RootNamespace));
var displayName = reader.ReadNonNullString(nameof(HostProject.DisplayName));
return new HostProject(filePath, intermediateOutputPath, configuration, rootNamespace, displayName);
}
public static ProjectWorkspaceState ReadProjectWorkspaceStateFromProperties(JsonDataReader reader)
{
var tagHelpers = reader.ReadImmutableArrayOrEmpty(nameof(ProjectWorkspaceState.TagHelpers), static r => ReadTagHelper(r, useCache: true));

Просмотреть файл

@ -79,6 +79,18 @@ internal static class ObjectWriters
writer.Write(nameof(value.FileKind), value.FileKind);
}
public static void Write(JsonDataWriter writer, HostProject? value)
=> writer.WriteObject(value, WriteProperties);
public static void WriteProperties(JsonDataWriter writer, HostProject value)
{
writer.Write(nameof(value.FilePath), value.FilePath);
writer.Write(nameof(value.IntermediateOutputPath), value.IntermediateOutputPath);
writer.WriteObject(nameof(value.Configuration), value.Configuration, WriteProperties);
writer.WriteIfNotNull(nameof(value.RootNamespace), value.RootNamespace);
writer.Write(nameof(value.DisplayName), value.DisplayName);
}
public static void Write(JsonDataWriter writer, ProjectWorkspaceState? value)
=> writer.WriteObject(value, WriteProperties);