Esempio n. 1
0
        /// <summary>
        /// Writes the content of this YAML node to the specified writer.
        /// </summary>
        /// <param name="writer">The writer to output YAML to.</param>
        public void WriteTo(TextWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }
            var preferredIndent = YamlSerializer.GetSerializerSettings().PreferredIndent;

            yamlStream.Save(writer, true, preferredIndent);
            writer.Flush();
        }
Esempio n. 2
0
        public static IDynamicYamlNode ConvertFrom <T>(T dataObject)
        {
            using (var stream = new MemoryStream())
            {
                // convert data to string
                YamlSerializer.Serialize(stream, dataObject);

                stream.Position = 0;

                // convert string to Yaml nodes
                using (var reader = new StreamReader(stream))
                {
                    var yamlStream = new YamlStream();
                    yamlStream.Load(reader);
                    return((IDynamicYamlNode)DynamicYamlObject.ConvertToDynamic(yamlStream.Documents[0].RootNode));
                }
            }
        }
Esempio n. 3
0
        public static T ConvertTo <T>(IDynamicYamlNode yamObject)
        {
            using (var memoryStream = new MemoryStream())
            {
                // convert Yaml nodes to string
                using (var streamWriter = new StreamWriter(memoryStream))
                {
                    var yamlStream = new YamlStream {
                        new YamlDocument(yamObject.Node)
                    };
                    yamlStream.Save(streamWriter, true, YamlSerializer.GetSerializerSettings().PreferredIndent);

                    streamWriter.Flush();
                    memoryStream.Position = 0;

                    // convert string to object
                    return((T)YamlSerializer.Deserialize(memoryStream, typeof(T), null));
                }
            }
        }