YamlDotNet没有正确反序列化双字unicode字符

时间:2014-04-22 18:59:10

标签: c# twitter unicode yamldotnet

我正在开发一个twitter文本c#库,而twitter已经在他们的一致性测试中添加了双字unicode字符测试。

https://github.com/twitter/twitter-text-conformance/blob/master/validate.yml

这是针对上述文件运行的nUnit测试方法。

    [Test]
    public void TestDoubleWordUnicodeYamlRetrieval()
    {
        var yamlFile = "validate.yml";
        Assert.IsTrue(File.Exists(conformanceDir + yamlFile), "Yaml file " + conformanceDir + yamlFile + " does not exist.");

        var stream = new StreamReader(Path.Combine(conformanceDir, yamlFile));
        var yaml = new YamlStream();
        yaml.Load(stream);

        var root = yaml.Documents[0].RootNode as YamlMappingNode;
        var testNode = new YamlScalarNode("tests");
        Assert.IsTrue(root.Children.ContainsKey(testNode), "Document is missing test node.");
        var tests = root.Children[testNode] as YamlMappingNode;
        Assert.IsNotNull(tests, "Test node is not YamlMappingNode");

        var typeNode = new YamlScalarNode("lengths");
        Assert.IsTrue(tests.Children.ContainsKey(typeNode), "Test type lengths not found in tests.");
        var typeTests = tests.Children[typeNode] as YamlSequenceNode;
        Assert.IsNotNull(typeTests, "lengths tests are not YamlSequenceNode");

        var list = new List<dynamic>();
        var count = 0;
        foreach (YamlMappingNode item in typeTests)
        {
            var text = ConvertNode<string>(item.Children.Single(x => x.Key.ToString() == "text").Value) as string;
            var description = ConvertNode<string>(item.Children.Single(x => x.Key.ToString() == "description").Value) as string;
            Assert.DoesNotThrow(() => {text.Normalize(NormalizationForm.FormC);}, String.Format("Yaml couldn't parse a double word unicode string at test {0} - {1}.", count, description));
            count++;
        }
    }

这是产生的错误: Vocus.TwitterText.Tests.ConformanceTest.TestDoubleWordUnicodeYamlRetrieval:   在测试5时,Yaml无法解析双字unicode字符串 - 在基本多语言平面(双字)之外计算unicode字符。 意外异常:System.ArgumentException

1 个答案:

答案 0 :(得分:0)

我不认为它是yaml解析器,尝试类似:

using (var stream = new StreamReader(path, Encoding.UTF8))
{
    var yaml = new YamlStream();
    yaml.Load(stream);
    //Do the rest of your code
}