Comments (2)
Yes, you can do this using the IYamlConvertible interface,
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
var data = new Config();
data.Receivers.Add(new Receiver());
data.Receivers.Add(new Receiver());
data.Receivers.Add(new Receiver());
var serializer = new SerializerBuilder().Build();
var yaml = serializer.Serialize(data);
Console.WriteLine(yaml);
public class Config : IYamlConvertible
{
public List<Receiver> Receivers { get; } = [];
public void Read(IParser parser, Type expectedType, ObjectDeserializer nestedObjectDeserializer)
{
}
public void Write(IEmitter emitter, ObjectSerializer nestedObjectSerializer)
{
emitter.Emit(new YamlDotNet.Core.Events.MappingStart());
emitter.Emit(new Scalar("Receivers"));
emitter.Emit(new YamlDotNet.Core.Events.MappingStart());
for (var ordinal = 0; ordinal < Receivers.Count; ordinal++)
{
emitter.Emit(new Scalar($"otlp/{ordinal + 1}"));
nestedObjectSerializer(Receivers[ordinal], typeof(Receiver));
}
emitter.Emit(new YamlDotNet.Core.Events.MappingEnd());
emitter.Emit(new YamlDotNet.Core.Events.MappingEnd());
}
}
public class Receiver
{
public string ReceiverType = Guid.NewGuid().ToString();
public string ReceiverName = Guid.NewGuid().ToString();
}
Results in
Receivers:
otlp/1:
ReceiverType: ca5a6d4f-7faa-4420-8257-e97a5bdbda00
ReceiverName: 900e3564-2859-4277-9f29-674b16f597ed
otlp/2:
ReceiverType: ce04b24a-96f3-4a8c-a331-43474a3fb6ea
ReceiverName: a13e3668-f6b3-49ba-a2a5-9947182e08f9
otlp/3:
ReceiverType: 55e810bb-6e44-43c5-90db-6e25e7f6258e
ReceiverName: fdf65d79-906f-4927-be96-37701a47177d
from yamldotnet.
Since I gave a working example and it's been a few weeks I'm going to close this.
from yamldotnet.
Related Issues (20)
- Invalid date format used in serialized JSON HOT 2
- YamlDotNet does not write accurately equal to yaml being loaded HOT 1
- JsonCompatible serializer is not Json compatible HOT 4
- Is the IDeserializer thread safe? HOT 2
- Update benchmark file
- 15.1.0 EnsureRoundTrip no longer works without tag mapping and protected default constructors HOT 4
- yamldotnet is missing NuGet package README file
- Json Compatible Serialization does not produce JSON for nodes.
- Including (parts of) another YAML file HOT 12
- Serilizing an object with tags similar to AWS Cloudformation HOT 4
- FSharp deserialization of records with optional record fields HOT 3
- ObjectNodeDeserializer: Naming Convention Error on Deserialization HOT 2
- is it possible to style a string in a special way? HOT 3
- Set license expression on nuget package HOT 1
- Improve nuget package readme HOT 2
- Serialization / deserialization is not thread safe HOT 2
- Very large files fail to parse HOT 6
- Using ```YamlStream``` will cause "property not found" in ```IDeserializer.Deserialize()``` HOT 1
- Empty line in comments HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from yamldotnet.