public static YamlNode ToYamlNode(this object val) { Type type = val.GetType(); YamlMapping node = new YamlMapping(); List <PropertyInfo> properties = new List <PropertyInfo>(type.GetProperties()); properties.Sort((l, r) => l.MetadataToken.CompareTo(r.MetadataToken)); foreach (PropertyInfo property in properties) { object propVal = property.GetValue(val); if (propVal != null) { if (property.IsEnumerable()) { YamlSequence yamlSequence = new YamlSequence(); node.Add(property.Name, yamlSequence); foreach (object v in ((IEnumerable)propVal)) { yamlSequence.Add(v.ToYamlNode()); } } else { if (property.PropertyType == typeof(bool) || property.PropertyType == typeof(bool?) || property.PropertyType == typeof(int) || property.PropertyType == typeof(int?) || property.PropertyType == typeof(long) || property.PropertyType == typeof(long?) || property.PropertyType == typeof(decimal) || property.PropertyType == typeof(decimal?) || property.PropertyType == typeof(double) || property.PropertyType == typeof(double?) || property.PropertyType == typeof(string) || property.PropertyType == typeof(byte[]) || property.PropertyType == typeof(DateTime) || property.PropertyType == typeof(DateTime?)) { node.Add(property.Name, new YamlScalar(propVal.ToString())); } else { node.Add(property.Name, propVal.ToYamlNode()); } } } } return(node); }
private void DictionaryToMap(object obj, YamlMapping mapping) { var accessor = ObjectMemberAccessor.FindFor(obj.GetType()); var iter = ((IEnumerable)obj).GetEnumerator(); // When this is a pure dictionary, we can directly add key,value to YamlMapping var dictionary = accessor.IsPureDictionary ? mapping : map(); Func <object, object> key = null, value = null; while (iter.MoveNext()) { if (key == null) { var keyvalue = iter.Current.GetType(); var keyprop = keyvalue.GetProperty("Key"); var valueprop = keyvalue.GetProperty("Value"); key = o => keyprop.GetValue(o, new object[0]); value = o => valueprop.GetValue(o, new object[0]); } dictionary.Add( ObjectToNode(key(iter.Current), accessor.KeyType), ObjectToNode(value(iter.Current), accessor.ValueType) ); } if (!accessor.IsPureDictionary && dictionary.Count > 0) { mapping.Add(MapKey("~Items"), dictionary); } }
public void TestChangingMappingKey() { var key = (YamlScalar)"def"; var map = new YamlMapping(key, 3); key.Value = "ghi"; Assert.IsTrue(map.ContainsKey(key)); Assert.IsTrue(map.Remove(key)); map[map] = 4; Assert.IsTrue(map.ContainsKey(map)); Assert.AreEqual((YamlNode)4, map[map]); var map2 = new YamlMapping(key, 3); map2[map2] = 2; map2[map] = 4; Assert.IsTrue(map2.ContainsKey(map2)); Assert.AreEqual((YamlNode)2, map2[map2]); Assert.AreEqual((YamlNode)4, map2[map]); map[map2] = 2; Assert.IsTrue(map2.ContainsKey(map2)); Assert.AreEqual((YamlNode)2, map2[map2]); Assert.AreEqual((YamlNode)4, map2[map]); Assert.IsTrue(map.ContainsKey(map)); Assert.AreEqual((YamlNode)4, map[map]); map.Tag = YamlNode.DefaultTagPrefix + "map"; Assert.IsTrue(map2.ContainsKey(map2)); Assert.AreEqual((YamlNode)2, map2[map2]); Assert.AreEqual((YamlNode)4, map2[map]); Assert.IsTrue(map.ContainsKey(map)); Assert.AreEqual((YamlNode)4, map[map]); Assert.IsTrue(map.ContainsKey(map2)); Assert.AreEqual((YamlNode)2, map[map2]); var seq = new YamlSequence(map); map.Add(seq, 4); Assert.IsTrue(map2.ContainsKey(map2)); Assert.AreEqual((YamlNode)2, map2[map2]); Assert.AreEqual((YamlNode)4, map2[map]); Assert.IsTrue(map.ContainsKey(map)); Assert.AreEqual((YamlNode)4, map[map]); Assert.IsTrue(map.ContainsKey(map2)); Assert.AreEqual((YamlNode)2, map[map2]); seq.Add(map); Assert.IsTrue(map2.ContainsKey(map2)); Assert.AreEqual((YamlNode)2, map2[map2]); Assert.AreEqual((YamlNode)4, map2[map]); Assert.IsTrue(map.ContainsKey(map)); Assert.AreEqual((YamlNode)4, map[map]); Assert.IsTrue(map.ContainsKey(map2)); Assert.AreEqual((YamlNode)2, map[map2]); }
public static string Save() { YamlMapping Map = new YamlMapping(); YamlSerializer Serializer = new YamlSerializer(); ForeachSetting((F, A) => { // HAX Map.Add(F.Name, YamlNode.FromYaml(Serializer.Serialize(F.GetValue(null)))[0]); }); return Map.ToYaml(); }
public void MergeKey() { var map = new YamlMapping("existing", "value"); var mergeKey = new YamlScalar("!!merge", "<<"); map.Add(mergeKey, new YamlMapping("existing", "new value")); map.OnLoaded(); Assert.AreEqual(1, map.Count); Assert.IsTrue(map.ContainsKey("existing")); Assert.AreEqual((YamlNode)"value", map["existing"]); map.Add(mergeKey, new YamlMapping("not existing", "new value")); map.OnLoaded(); Assert.AreEqual(2, map.Count); Assert.IsTrue(map.ContainsKey("existing")); Assert.AreEqual((YamlNode)"value", map["existing"]); Assert.IsTrue(map.ContainsKey("not existing")); Assert.AreEqual((YamlNode)"new value", map["not existing"]); map.Add(mergeKey, new YamlMapping("key1", "value1", 2, 2, 3.0, 3.0)); map.OnLoaded(); Assert.AreEqual(5, map.Count); Assert.IsTrue(map.ContainsKey("existing")); Assert.AreEqual((YamlNode)"value", map["existing"]); Assert.IsTrue(map.ContainsKey("not existing")); Assert.AreEqual((YamlNode)"new value", map["not existing"]); Assert.IsTrue(map.ContainsKey(2)); Assert.AreEqual((YamlNode)2, map[2]); Assert.IsTrue(map.ContainsKey(3.0)); Assert.AreEqual((YamlNode)3.0, map[3.0]); map = new YamlMapping( "existing", "value", mergeKey, new YamlMapping("not existing", "new value")); map.OnLoaded(); Assert.AreEqual(2, map.Count); Assert.IsTrue(map.ContainsKey("existing")); Assert.AreEqual((YamlNode)"value", map["existing"]); Assert.IsTrue(map.ContainsKey("not existing")); Assert.AreEqual((YamlNode)"new value", map["not existing"]); map = (YamlMapping)YamlNode.FromYaml( "key1: value1\r\n" + "key2: value2\r\n" + "<<: \r\n" + " key2: value2 modified\r\n" + " key3: value3\r\n" + " <<: \r\n" + " key4: value4\r\n" + " <<: value5\r\n" + "key6: <<\r\n")[0]; Assert.AreEqual( "%YAML 1.2\r\n" + "---\r\n" + "<<: value5\r\n" + "key6: <<\r\n" + "key3: value3\r\n" + "key2: value2\r\n" + "key4: value4\r\n" + "key1: value1\r\n" + "...\r\n", map.ToYaml() ); Assert.IsTrue(map.ContainsKey(mergeKey)); Assert.AreEqual(mergeKey, map["key6"]); map.Remove(mergeKey); map.Add(mergeKey, map); // recursive map.OnLoaded(); Assert.AreEqual( // nothing has been changed "%YAML 1.2\r\n" + "---\r\n" + "key6: <<\r\n" + "key3: value3\r\n" + "key2: value2\r\n" + "key4: value4\r\n" + "key1: value1\r\n" + "...\r\n", map.ToYaml() ); }
private void DictionaryToMap(object obj, YamlMapping mapping) { var accessor = ObjectMemberAccessor.FindFor(obj.GetType()); var iter = ((IEnumerable)obj).GetEnumerator(); // When this is a pure dictionary, we can directly add key,value to YamlMapping var dictionary = accessor.IsPureDictionary ? mapping : map(); Func<object, object> key = null, value = null; while (iter.MoveNext()) { if (key == null) { var keyvalue = iter.Current.GetType(); var keyprop = keyvalue.GetProperty("Key"); var valueprop = keyvalue.GetProperty("Value"); key = o => keyprop.GetValue(o, new object[0]); value = o => valueprop.GetValue(o, new object[0]); } dictionary.Add( ObjectToNode(key(iter.Current), accessor.KeyType), ObjectToNode(value(iter.Current), accessor.ValueType) ); } if (!accessor.IsPureDictionary && dictionary.Count > 0) mapping.Add(MapKey("~Items"), dictionary); }
public void saveYaml() { if (string.IsNullOrEmpty(Global.projectName)) { MessageBox.Show("Please check the project", "Project Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } //네비 시작점 확인 if (!checkStart()) { MessageBox.Show("Please check the starting point.", "Start Point Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } //종점 확인 if (!checkend()) { MessageBox.Show("Please check the end point.", "End Point Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } saveNaviTemp.Clear(); var startshape = NavinObsDiagram.Instance.Navidiagram.Items.OfType <NaviShape>().Where(x => x.PointType == "0").ToList(); saveNaviTemp.Add(startshape[0]); SetNaviPoistion(startshape[0]); //리스트에 시작 끝점이 존재하는지 체크 if (!checkStartEnd(saveNaviTemp)) { MessageBox.Show("It is not connected from start point to end point.", "End Point Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } //저장을 시작한다. System.Windows.Forms.SaveFileDialog save = new System.Windows.Forms.SaveFileDialog(); save.Title = ""; save.Filter = "Yaml file|*.yaml"; if (save.ShowDialog() == System.Windows.Forms.DialogResult.OK) { using (new WaitCursor()) { string filePath = save.FileName; YamlMapping map = new YamlMapping(); //point 갯수 map.Add("size_points", saveNaviTemp.Count); //장애물 갯수 List <ObsShape> obsdata = NavinObsDiagram.Instance.Navidiagram.Items.OfType <ObsShape>().ToList(); map.Add("size_objects", obsdata.Count); //네비포인트 갯수 for (int i = 0; i < saveNaviTemp.Count; i++) { //points0_x string Xkey = "points" + i + "_x"; string Ykey = "points" + i + "_y"; map.Add(Xkey, saveNaviTemp[i].NaviPointX); map.Add(Ykey, saveNaviTemp[i].NaviPointY); } //장애물 갯수 for (int i = 0; i < obsdata.Count; i++) { //x y name comment type //objects0_pos_x string Xkey = "objects" + obsdata[i].Index.ToString() + "_pos_x"; string Ykey = "objects" + obsdata[i].Index.ToString() + "_pos_y"; string NameKey = "objects" + obsdata[i].Index.ToString() + "_name"; string commKey = "objects" + obsdata[i].Index.ToString() + "_comment"; string typeKey = "objects" + obsdata[i].Index.ToString() + "_type"; map.Add(Xkey, obsdata[i].ObsPointX); map.Add(Ykey, obsdata[i].ObsPointY); map.Add(NameKey, Global.getObsCodeName(int.Parse(obsdata[i].PointType))); map.Add(commKey, obsdata[i].Description); map.Add(typeKey, int.Parse(obsdata[i].PointType)); } map.Add("wayWidth", float.Parse("2.5000000000000000e+00")); map.Add("nextDirDist", float.Parse("10.00000")); map.Add("nextDirThres", float.Parse("20.00000")); YamlNode node = map; node.ToYamlFile(filePath); MessageBox.Show("yaml file creation is complete.", "Yaml Create.", MessageBoxButton.OK, MessageBoxImage.Information); } } }
public void TestChangingMappingKey() { var key = (YamlScalar)"def"; var map = new YamlMapping(key, 3); key.Value = "ghi"; Assert.IsTrue(map.ContainsKey(key)); Assert.IsTrue(map.Remove(key)); map[map] = 4; Assert.IsTrue(map.ContainsKey(map)); Assert.AreEqual((YamlNode)4, map[map]); var map2 = new YamlMapping(key, 3); map2[map2] = 2; map2[map] = 4; Assert.IsTrue(map2.ContainsKey(map2)); Assert.AreEqual((YamlNode)2, map2[map2]); Assert.AreEqual((YamlNode)4, map2[map]); map[map2] = 2; Assert.IsTrue(map2.ContainsKey(map2)); Assert.AreEqual((YamlNode)2, map2[map2]); Assert.AreEqual((YamlNode)4, map2[map]); Assert.IsTrue(map.ContainsKey(map)); Assert.AreEqual((YamlNode)4, map[map]); map.Tag = YamlNode.DefaultTagPrefix + "map"; Assert.IsTrue(map2.ContainsKey(map2)); Assert.AreEqual((YamlNode)2, map2[map2]); Assert.AreEqual((YamlNode)4, map2[map]); Assert.IsTrue(map.ContainsKey(map)); Assert.AreEqual((YamlNode)4, map[map]); Assert.IsTrue(map.ContainsKey(map2)); Assert.AreEqual((YamlNode)2, map[map2]); var seq = new YamlSequence(map); map.Add(seq, 4); Assert.IsTrue(map2.ContainsKey(map2)); Assert.AreEqual((YamlNode)2, map2[map2]); Assert.AreEqual((YamlNode)4, map2[map]); Assert.IsTrue(map.ContainsKey(map)); Assert.AreEqual((YamlNode)4, map[map]); Assert.IsTrue(map.ContainsKey(map2)); Assert.AreEqual((YamlNode)2, map[map2]); seq.Add(map); Assert.IsTrue(map2.ContainsKey(map2)); Assert.AreEqual((YamlNode)2, map2[map2]); Assert.AreEqual((YamlNode)4, map2[map]); Assert.IsTrue(map.ContainsKey(map)); Assert.AreEqual((YamlNode)4, map[map]); Assert.IsTrue(map.ContainsKey(map2)); Assert.AreEqual((YamlNode)2, map[map2]); }
public void MergeKey() { var map = new YamlMapping("existing", "value"); var mergeKey = new YamlScalar("!!merge", "<<"); map.Add(mergeKey, new YamlMapping("existing", "new value")); map.OnLoaded(); Assert.AreEqual(1, map.Count); Assert.IsTrue(map.ContainsKey("existing")); Assert.AreEqual((YamlNode) "value", map["existing"]); map.Add(mergeKey, new YamlMapping("not existing", "new value")); map.OnLoaded(); Assert.AreEqual(2, map.Count); Assert.IsTrue(map.ContainsKey("existing")); Assert.AreEqual((YamlNode) "value", map["existing"]); Assert.IsTrue(map.ContainsKey("not existing")); Assert.AreEqual((YamlNode) "new value", map["not existing"]); map.Add(mergeKey, new YamlMapping("key1", "value1", 2, 2, 3.0, 3.0)); map.OnLoaded(); Assert.AreEqual(5, map.Count); Assert.IsTrue(map.ContainsKey("existing")); Assert.AreEqual((YamlNode) "value", map["existing"]); Assert.IsTrue(map.ContainsKey("not existing")); Assert.AreEqual((YamlNode) "new value", map["not existing"]); Assert.IsTrue(map.ContainsKey(2)); Assert.AreEqual((YamlNode) 2, map[2]); Assert.IsTrue(map.ContainsKey(3.0)); Assert.AreEqual((YamlNode) 3.0, map[3.0]); map = new YamlMapping( "existing", "value", mergeKey, new YamlMapping("not existing", "new value")); map.OnLoaded(); Assert.AreEqual(2, map.Count); Assert.IsTrue(map.ContainsKey("existing")); Assert.AreEqual((YamlNode) "value", map["existing"]); Assert.IsTrue(map.ContainsKey("not existing")); Assert.AreEqual((YamlNode) "new value", map["not existing"]); map = (YamlMapping) YamlNode.FromYaml( "key1: value1\r\n" + "key2: value2\r\n" + "<<: \r\n" + " key2: value2 modified\r\n" + " key3: value3\r\n" + " <<: \r\n" + " key4: value4\r\n" + " <<: value5\r\n" + "key6: <<\r\n")[0]; var mapBack = map.ToYaml(); Assert.AreEqual(@"%YAML 1.2 --- <<: value5 key1: value1 key2: value2 key3: value3 key4: value4 key6: << ... ", mapBack); Assert.IsTrue(map.ContainsKey(mergeKey)); Assert.AreEqual(mergeKey, map["key6"]); map.Remove(mergeKey); map.Add(mergeKey, map); // recursive map.OnLoaded(); // nothing has been changed mapBack = map.ToYaml(); Assert.AreEqual(@"%YAML 1.2 --- key1: value1 key2: value2 key3: value3 key4: value4 key6: << ... ", mapBack); }