C# YamlDotNet无法正确反序列化双字unicode字符
我正在开发一个twitter文本c#库,twitter在一致性测试中添加了一个双字unicode字符测试 下面是针对上述文件运行的nUnit测试方法C# YamlDotNet无法正确反序列化双字unicode字符,c#,twitter,unicode,yamldotnet,C#,Twitter,Unicode,Yamldotnet,我正在开发一个twitter文本c#库,twitter在一致性测试中添加了一个双字unicode字符测试 下面是针对上述文件运行的nUnit测试方法 [Test] public void TestDoubleWordUnicodeYamlRetrieval() { var yamlFile = "validate.yml"; Assert.IsTrue(File.Exists(conformanceDir + yamlFile), "Ya
[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++;
}
}
[测试]
public void TestDoubleWordUnicodeYamlRetrieval()
{
var yamlFile=“validate.yml”;
Assert.IsTrue(File.Exists(conformanceDir+yamlFile),“Yaml File”+conformanceDir+yamlFile+“不存在”);
var stream=新的StreamReader(Path.Combine(conformanceDir,yamlFile));
var yaml=新的YamlStream();
yaml.负载(流);
var root=yaml.Documents[0]。RootNode作为YamlMappingNode;
var testNode=新的YamlScalarNode(“测试”);
Assert.IsTrue(root.Children.ContainsKey(testNode),“文档缺少测试节点”);
var tests=root.Children[testNode]作为YamlMappingNode;
IsNotNull(测试,“测试节点不是YamlMappingNode”);
var typeNode=新的YamlScalarNode(“长度”);
Assert.IsTrue(tests.Children.ContainsKey(typeNode),“在测试中找不到测试类型长度”);
var typeTests=tests.Children[typeNode]作为YamlSequenceNode;
IsNotNull(类型测试,“长度测试不是YamlSequenceNode”);
var list=新列表();
var计数=0;
foreach(类型测试中的YamlMappingNode项)
{
var text=ConvertNode(item.Children.Single(x=>x.Key.ToString()==“text”).Value)作为字符串;
var description=ConvertNode(item.Children.Single(x=>x.Key.ToString()==“description”).Value)作为字符串;
Assert.DoesNotThrow(()=>{text.Normalize(NormalizationForm.FormC);},String.Format(“Yaml在测试{0}-{1}时无法解析双字unicode字符串,计数,描述));
计数++;
}
}
这是产生的错误:
Vocus.TwitterText.Tests.ConformanceTest.TestDoubleWordUnicodeYamlRetrieval:
Yaml无法在测试5时解析双字unicode字符串-在基本多语言平面(双字)外计数unicode字符。
意外异常:System.ArgumentException我认为这不是yaml解析器,请尝试以下操作:
using (var stream = new StreamReader(path, Encoding.UTF8))
{
var yaml = new YamlStream();
yaml.Load(stream);
//Do the rest of your code
}
很抱歉这么晚才回复,但这没有帮助。有问题的特定行实际上不是UTF8字符,而是Unicode字符表示:text:“\U00010000\U0010ffff”使用流读取器将文件输出为字符串时,字符是正确的。使用yaml检索节点时,输出为\0。