mirror of
				https://github.com/Nioux/AideDeJeu.git
				synced 2025-10-30 23:16:09 +00:00 
			
		
		
		
	Sérialize/déserialize yaml
This commit is contained in:
		
							parent
							
								
									c490ea8854
								
							
						
					
					
						commit
						71d62d146f
					
				
					 2 changed files with 52 additions and 4 deletions
				
			
		|  | @ -185,9 +185,29 @@ namespace AideDeJeuLib | ||||||
| 
 | 
 | ||||||
|         [IgnoreDataMember] |         [IgnoreDataMember] | ||||||
|         [YamlIgnore] |         [YamlIgnore] | ||||||
|         public Dictionary<string, Type> ClassMapping = new Dictionary<string, Type>() |         public static Dictionary<string, Type> ClassMapping = new Dictionary<string, Type>() | ||||||
|         { |         { | ||||||
|             { nameof(Generic), typeof(Generic) }, |             { "Generic", typeof(Generic) }, | ||||||
|  |             { "Monster", typeof(Monster) }, | ||||||
|  |             { "MonsterHD", typeof(MonsterHD) }, | ||||||
|  |             { "MonsterVO", typeof(MonsterVO) }, | ||||||
|  |             { "Monsters", typeof(Monsters) }, | ||||||
|  |             { "MonstersHD", typeof(MonstersHD) }, | ||||||
|  |             { "MonstersVO", typeof(MonstersVO) }, | ||||||
|  |             { "Spell", typeof(Spell) }, | ||||||
|  |             { "SpellHD", typeof(SpellHD) }, | ||||||
|  |             { "SpellVO", typeof(SpellVO) }, | ||||||
|  |             { "Spells", typeof(Spells) }, | ||||||
|  |             { "SpellsHD", typeof(SpellsHD) }, | ||||||
|  |             { "SpellsVO", typeof(SpellsVO) }, | ||||||
|  |             { "Equipment", typeof(Equipment) }, | ||||||
|  |             { "Equipments", typeof(Equipments) }, | ||||||
|  |             { "Item", typeof(Item) }, | ||||||
|  |             { "Items", typeof(Items) }, | ||||||
|  |             { "LinkItem", typeof(LinkItem) }, | ||||||
|  |             { "MagicItem", typeof(MagicItem) }, | ||||||
|  |             { "MagicItems", typeof(MagicItems) }, | ||||||
|  |             { "PageItem", typeof(PageItem) }, | ||||||
|         }; |         }; | ||||||
| 
 | 
 | ||||||
|         [IgnoreDataMember] |         [IgnoreDataMember] | ||||||
|  | @ -196,8 +216,12 @@ namespace AideDeJeuLib | ||||||
|         { |         { | ||||||
|             get |             get | ||||||
|             { |             { | ||||||
|                 var serializer = new SerializerBuilder() |                 var builder = new SerializerBuilder(); | ||||||
|                     .WithTagMapping($"!{nameof(MonsterHD)}", typeof(MonsterHD)) |                 foreach(var mapping in ClassMapping) | ||||||
|  |                 { | ||||||
|  |                     builder = builder.WithTagMapping($"!{mapping.Key}", mapping.Value); | ||||||
|  |                 } | ||||||
|  |                 var serializer = builder | ||||||
|                     .EnsureRoundtrip() |                     .EnsureRoundtrip() | ||||||
|                     .WithNamingConvention(new PascalCaseNamingConvention()) |                     .WithNamingConvention(new PascalCaseNamingConvention()) | ||||||
|                     .Build(); |                     .Build(); | ||||||
|  | @ -214,5 +238,27 @@ namespace AideDeJeuLib | ||||||
|                 return $"---\n{Yaml}---\n{Markdown}"; |                 return $"---\n{Yaml}---\n{Markdown}"; | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  | 
 | ||||||
|  |         public static Item ParseYamlMarkdown(string yamlmd) | ||||||
|  |         { | ||||||
|  |             var builder = new DeserializerBuilder(); | ||||||
|  |             foreach (var mapping in ClassMapping) | ||||||
|  |             { | ||||||
|  |                 builder = builder.WithTagMapping($"!{mapping.Key}", mapping.Value); | ||||||
|  |             } | ||||||
|  |             var yamlDeserializer = builder | ||||||
|  |                 .WithNamingConvention(new PascalCaseNamingConvention()) | ||||||
|  |                 .Build(); | ||||||
|  | 
 | ||||||
|  |             var parser = new Parser(new System.IO.StringReader(yamlmd)); | ||||||
|  |             parser.Expect<StreamStart>(); | ||||||
|  |             parser.Expect<DocumentStart>(); | ||||||
|  |             var post = yamlDeserializer.Deserialize(parser); | ||||||
|  |             parser.Expect<DocumentEnd>(); | ||||||
|  |             //parser.MoveNext(); | ||||||
|  |             var item = post as Item; | ||||||
|  |             item.Markdown = yamlmd.Substring(parser.Current.End.Index + 1); | ||||||
|  |             return post as Item; | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -337,6 +337,8 @@ namespace AideDeJeuCmd | ||||||
|                 var yaml = serializer.Serialize(monsters); |                 var yaml = serializer.Serialize(monsters); | ||||||
|                 var sr = new StringReader(yaml); |                 var sr = new StringReader(yaml); | ||||||
|                 var deser = deserializer.Deserialize(sr); |                 var deser = deserializer.Deserialize(sr); | ||||||
|  | 
 | ||||||
|  |                 var truc = Item.ParseYamlMarkdown(monsters.FirstOrDefault().YamlMarkdown); | ||||||
|                 Console.WriteLine(yaml); |                 Console.WriteLine(yaml); | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Yan Maniez
						Yan Maniez