46 lines
1 KiB
C#
46 lines
1 KiB
C#
|
using System;
|
||
|
|
||
|
using Gallium;
|
||
|
|
||
|
using Nitride.Contents;
|
||
|
|
||
|
using YamlDotNet.Serialization;
|
||
|
|
||
|
namespace Nitride.Yaml;
|
||
|
|
||
|
public static class NitrideYamlEntityExtensions
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// Parses the entity text as a YAML file and returns the results.
|
||
|
/// </summary>
|
||
|
public static TType? GetYaml<TType>(
|
||
|
this Entity entity,
|
||
|
Action<DeserializerBuilder>? configure = null)
|
||
|
{
|
||
|
DeserializerBuilder builder = new();
|
||
|
|
||
|
configure?.Invoke(builder);
|
||
|
|
||
|
IDeserializer deserializer = builder.Build();
|
||
|
|
||
|
return entity.GetYaml<TType>(deserializer);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Parses the entity text as a YAML file and returns the results.
|
||
|
/// </summary>
|
||
|
public static TType? GetYaml<TType>(
|
||
|
this Entity entity,
|
||
|
IDeserializer deserializer)
|
||
|
{
|
||
|
if (!entity.TryGet(out ITextContent content))
|
||
|
{
|
||
|
return default;
|
||
|
}
|
||
|
|
||
|
string text = content.GetText();
|
||
|
|
||
|
return deserializer.Deserialize<TType>(text);
|
||
|
}
|
||
|
}
|