使用自定义类型解析器

时间:2014-04-20 11:15:10

标签: c# yamldotnet

我目前正在尝试使用YamlDotNet库将YAML文档反序列化为标准.NET对象,例如标量值为string,映射为Dictionary<string, object>

我猜Deserializer类是最佳选择,但其输出为objectDictionary<object>。我尝试像这样实现自定义INodeTypeResolver

class MyNodeTypeReslover : INodeTypeResolver
{
    bool INodeTypeResolver.Resolve(NodeEvent nodeEvent, ref Type currentType)
    {
        if (currentType == typeof(object))
        {
            if (nodeEvent is SequenceStart)
                currentType = typeof(List<object>);
            else if (nodeEvent is MappingStart)
                currentType = typeof(Dictionary<string, object>);
            else if (nodeEvent is Scalar)
                currentType = typeof(string);

            return true;
        }

        return false;
    }
}

并像这样使用它:

Deserializer deserializer = new Deserializer();
deserializer.TypeResolvers.Add(new MyNodeTypeReslover());
var res = deserializer.Deserialize(input);

但这似乎没有任何效果。有没有办法改变Deserializer生成的对象类型?

2 个答案:

答案 0 :(得分:0)

AFAIK,Deserialize接受一个类型参数,这非常好

%YAML 1.1
%TAG !namespace! _MyNamespace.NestedClass.Whatever.
---

entry_0: !namespace!MyMessage
  format: Alert
  desc: "Entry One! Uses the exact string representation of the desired type. (A bit fragile, IMHO)"

entry_1: !!message
  format: Default
  desc: "Entry Two! Uses a type registered beforehand."

entry_2:
  format: Default
  desc: "Entry Three! Just winging it, sometimes YamlDotNet is exceedingly clever."

...

可以通过

进行反序列化
var dict = new Deserializer().Deserialize<Dictionary<string,MyMessage>>(
    new StringReader(that_doc_up_there));

前提是MyMessage具有format和desc属性,并且只要它不在命名空间中。如果是,您可以事先使用反序列化器注册它,也可以为它创建一个新标签。 %TAG别名似乎吃了标签的第一个字符,所以我放了一个下划线。也许是个bug。 另一种方法是注册它,

deserializer.RegisterTagMapping(
    "tag:yaml.org,2002:message", typeof(MyMessage));

答案 1 :(得分:0)

您使用INodeTypeResolver的方向正确,但是您需要构建和使用自定义反序列化器:

DeserializerBuilder deserializerBuilder = new DeserializerBuilder()
    .WithNodeTypeResolver(new MyNodeTypeResolver());
IDeserializer deserializer = deserializerBuilder.Build();
var res = deserializer.Deserialize(input);