Need help on deserialization #929
Unanswered
AlexCastroAlex
asked this question in
Q&A
Replies: 1 comment 1 reply
-
You'll want to use a type discriminator to determine what type of path to resolve to. Here's an example: using YamlDotNet.Serialization;
using YamlDotNet.Serialization.BufferedDeserialization;
var yaml = @"paths:
cam1hd:
record: 'yes'
recordPath: \\192.168.10.32\recordings\%path\%Y-%m-%d_%H-%M-%S-%f
recordFormat: fmp4
recordPartDuration: 100ms
recordSegmentDuration: 3m
recordDeleteAfter: 30m
source: 'rtsp://admin:[email protected]/Streaming/Channels/1'
cam2hd:
source: 'rtsp://admin:[email protected]/main'";
var deserializer = new DeserializerBuilder()
.WithTypeDiscriminatingNodeDeserializer((options) =>
{
var descrimators = new Dictionary<string, Type>();
descrimators.Add("record", typeof(Cam1Remote));
options.AddUniqueKeyTypeDiscriminator<CamVt>(descrimators);
}).Build();
var deserialized = deserializer.Deserialize<Root>(yaml);
Console.WriteLine(string.Join(", ", deserialized.Paths.Select(p => p.Key + "-" + p.Value.GetType().ToString())));
public class Root
{
[YamlMember(Alias = "paths", ApplyNamingConventions = false)]
public Paths Paths { get; set; }
}
public partial class Paths : Dictionary<string, CamVt>
{
[YamlMember(Alias = "all_others", ApplyNamingConventions = false)]
public object AllOthers { get; set; }
}
public partial class CamVt
{
[YamlMember(Alias = "source", ApplyNamingConventions = false)]
public string Source { get; set; }
}
public partial class Cam1Remote : CamVt
{
[YamlMember(Alias = "record", ApplyNamingConventions = false)]
public string Record { get; set; }
[YamlMember(Alias = "recordPath", ApplyNamingConventions = false)]
public string RecordPath { get; set; }
[YamlMember(Alias = "recordFormat", ApplyNamingConventions = false)]
public string RecordFormat { get; set; }
[YamlMember(Alias = "recordPartDuration", ApplyNamingConventions = false)]
public string RecordPartDuration { get; set; }
[YamlMember(Alias = "recordSegmentDuration", ApplyNamingConventions = false)]
public string RecordSegmentDuration { get; set; }
[YamlMember(Alias = "recordDeleteAfter", ApplyNamingConventions = false)]
public string RecordDeleteAfter { get; set; }
} Results in
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello all ,
I need help on deserialization of a yaml file with properties like these :
The representation of the C# entity is :
The idea is that cam1hd could be whatever name and has properties below, that's why i created a Dictionary<string, CamVt>.
And the camVt entity could be inherited to be a Dictionary<string, Cam1Remote>.
I was not able to deserialize this file because of this specificity.
Anybody could help please ? 🙏
Regards
Beta Was this translation helpful? Give feedback.
All reactions