使用 YamlDotNet 序列化动态模型时,更改用于所有多行字符串的标量样式 [英] Change the scalar style used for all multi-line strings when serialising a dynamic model using YamlDotNet

查看:18
本文介绍了使用 YamlDotNet 序列化动态模型时,更改用于所有多行字符串的标量样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码片段将项目的动态模型序列化为字符串(最终导出到 YAML 文件).

I am using the following code snippet to serialise a dynamic model of a project to a string (which is eventually exported to a YAML file).

    dynamic exportModel = exportModelConvertor.ToDynamicModel(project);
    var serializerBuilder = new SerializerBuilder();
    var serializer = serializerBuilder.EmitDefaults().DisableAliases().Build();

    using (var sw = new StringWriter())
    {
        serializer.Serialize(sw, exportModel);
        string result = sw.ToString();
    }

任何多行字符串,例如:

Any multi-line strings such as the following:

propertyName = "One line of text
followed by another line
and another line"

以下列格式导出:

propertyName: >
  One line of text

  followed by another line

  and another line

注意额外的(不需要的)换行符.

Note the extra (unwanted) line breaks.

根据这个YAML Multiline guide,这里使用的格式是折叠块标量样式.有没有办法使用 YamlDotNet 将所有多行字符串属性的输出样式更改为文字块标量样式或流标量样式之一?

According to this YAML Multiline guide, the format used here is the folded block scalar style. Is there a way using YamlDotNet to change the style of this output for all multi-line string properties to literal block scalar style or one of the flow scalar styles?

YamlDotNet 文档 展示了如何将 ScalarStyle.DoubleQuoted 应用于特定的使用 WithAttributeOverride 的属性,但这需要一个类名,并且要序列化的模型是动态的.这还需要列出要更改的每个属性(其中有很多).我想一次更改所有多行字符串属性的样式.

The YamlDotNet documentation shows how to apply ScalarStyle.DoubleQuoted to a particular property using WithAttributeOverride but this requires a class name and the model to be serialised is dynamic. This also requires listing every property to change (of which there are many). I would like to change the style for all multi-line string properties at once.

推荐答案

为了回答我自己的问题,我现在已经弄清楚如何通过从 ChainedEventEmitter 类派生并覆盖 void Emit(ScalarEventInfo eventInfo, IEmitter发射器).请参阅下面的代码示例.

To answer my own question, I've now worked out how to do this by deriving from the ChainedEventEmitter class and overriding void Emit(ScalarEventInfo eventInfo, IEmitter emitter). See code sample below.

public class MultilineScalarFlowStyleEmitter : ChainedEventEmitter
{
    public MultilineScalarFlowStyleEmitter(IEventEmitter nextEmitter)
        : base(nextEmitter) { }

    public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
    {

        if (typeof(string).IsAssignableFrom(eventInfo.Source.Type))
        {
            string value = eventInfo.Source.Value as string;
            if (!string.IsNullOrEmpty(value))
            {
                bool isMultiLine = value.IndexOfAny(new char[] { '\r', '\n', '\x85', '\x2028', '\x2029' }) >= 0;
                if (isMultiLine)
                    eventInfo = new ScalarEventInfo(eventInfo.Source)
                    {
                        Style = ScalarStyle.Literal
                    };
            }
        }

        nextEmitter.Emit(eventInfo, emitter);
    }
}

这篇关于使用 YamlDotNet 序列化动态模型时,更改用于所有多行字符串的标量样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆