yamldotnet

How to deserialize YAML with YAMLDotNet?

微笑、不失礼 提交于 2021-01-29 06:32:03
问题 I want to deserialize this YAML with YAMLDotNet. It have sequence and nested mapping. data.yml people: - name: "John" age: 20 - name: "Michael" age: 21 - name: "William" age: 22 network: address: "192.168.1.1" port: 1234 param: paramNumber: 10 paramString: "text data" paramBool: true This is my code. But, It can't compile. I would like to know the following two things. How to define class to decerialize nested mapping? How to access it? Print Deserialized Data DeserializedObject obj =

In YamlDotNet, how can I deserialize a getter-only property?

天涯浪子 提交于 2020-02-06 08:49:55
问题 I would like to serialize/deserialize objects that have readonly private fields with public getter-only properties, whose readonly fields are set in the objects' constructor. However, the following code fails in unit testing: using System.ComponentModel; using FluentAssertions; using Xunit; using YamlDotNet.Serialization; namespace Utilities.ComputerInventory { public class CPU { readonly CPUMaker cPUMaker; public CPU() : this(CPUMaker.Generic) { } public CPU(CPUMaker cPUMaker) { this

Build a Yaml document dynamically from c#

笑着哭i 提交于 2019-12-23 09:06:18
问题 Is it possible to build a Yaml document dynamically from c# with Yaml.DotNet or another library? I understand how this can be done using serialisation, however that requires starting with an object structure. I'm looking to find a way to create the Yaml document nodes on the fly as you would with Xml using the XElement.Add(object) method for example. 回答1: You can do that using YamlDotNet. You start by creating a YamlStream, add one or more document to it, then you can add sequences, mappings

What does a single exclamation mark do in YAML?

冷暖自知 提交于 2019-12-17 16:00:34
问题 I'm working with the YamlDotNet library and I'm getting this error when loading a YAML file: While parsing a tag, did not find expected tag URI. The YAML file is supposed to be well-formed because it comes right from RoR. The error seems to be triggered by this code: formats: default: ! '%d-%m-%Y' long: ! '%d %B, %Y' short: ! '%d %b' I'm not an expert, but I see from the YAML spec that you can use an exclamation mark to indicate a custom object/type, and two exclamation marks to indicate an

YamlDotNet can not find property

你离开我真会死。 提交于 2019-12-13 16:31:17
问题 I am trying to create a simple model for parsing a yaml file to my domain object using YamlDotNet. The caveat is, that I want the domain model to be readonly, so I'm attempting to solve this through inheritance and internal setters. For some reason though, the library throws an exception stating: Property 'HtmlTemplate' not found on type 'ConsoleApplication1.Repositories.YamlTemplateRepository+DeserializeableTemplate'. I am using an alias, but even scratching that, and using a test class

Deserializing YAML using YamlDotNet when the root node of each object is named using it's ID?

自作多情 提交于 2019-12-11 04:15:27
问题 I'm using C# and I have a YAML file I want to deserialize. I've looked at using YamlDotNet, and it looks like it's pretty decent, but I can't find how to handle this situation. The YAML objects take the following format: 1: id: 1 name: foo 2: id: 2 name: foo Ideally this would be: (EDIT: Changed ideal format as per Anthon's comment.) - id: 1 name: foo - id: 2 name: foo But it's not ... oh well. I can of course revert to doing everything much more manually, by looping over each node and

Deserialize a YAML “Table” of data

时光怂恿深爱的人放手 提交于 2019-12-10 05:31:40
问题 I am using yamldotnet and c# to deserialize a file created by a third party software application. The following YAML file examples are both valid from the application: #File1 Groups: - Name: ATeam FirstName, LastName, Age, Height: - [Joe, Soap, 21, 184] - [Mary, Ryan, 20, 169] - [Alex, Dole, 24, 174] #File2 Groups: - Name: ATeam FirstName, LastName, Height: - [Joe, Soap, 184] - [Mary, Ryan, 169] - [Alex, Dole, 174] Notice that File2 doesnt have any Age column but the deserializer must still

How to convert YAML to JSON?

霸气de小男生 提交于 2019-12-07 10:56:53
问题 I'm looking to convert between a YAML file, and JSON. This was really difficult to find any information on. 回答1: If you do not need the features of Json.NET, you can also use the Serializer class directly to emit JSON: // now convert the object to JSON. Simple! var js = new Serializer(SerializationOptions.JsonCompatible); var w = new StringWriter(); js.Serialize(w, o); string jsonText = w.ToString(); You can check two working fiddles here: Convert YAML to JSON Convert YAML to JSON using Json

How to convert JSON to YAML using YamlDotNet

百般思念 提交于 2019-12-07 02:03:54
问题 I am trying to convert JSON to YAML using YamlDotNet. This is the code I have: class Program { static void Main(string[] args) { var json = "{\"swagger\":\"2.0\",\"info\":{\"title\":\"UberAPI\",\"description\":\"MoveyourappforwardwiththeUberAPI\",\"version\":\"1.0.0\"},\"host\":\"api.uber.com\",\"schemes\":[\"https\"],\"basePath\":\"/v1\",\"produces\":[\"application/json\"]}"; var swaggerDocument = JsonConvert.DeserializeObject(json); var serializer = new YamlDotNet.Serialization.Serializer()