-
-
Notifications
You must be signed in to change notification settings - Fork 483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question about generic serialization #36
Comments
This is marked as "completed". May I ask, how was this completed? Because I am facing the same issue. |
I bet it was marked as complete because it serializes the object correctly. As for deserialization, that could be a bit trickier. You would need to build some kind of type factory or something that would read the yaml and determine what kind of object to create. We couldn't do that in yamldotnet for you, I do know that the extension points are there to do it. I'll see if I can drudge up an example. Until then, here is a working example where it serializes/deserializes (when you know the data type) using System;
using System.Text.Json;
using YamlDotNet.Serialization;
var serializer = new SerializerBuilder()
.Build();
var deserializer = new DeserializerBuilder()
.Build();
var msg = new MessageEnvelope<ConfigMessage>()
{
Type = "config",
Payload = new ConfigMessage()
{
SettingA = "red",
SettingB = 5
}
};
var yaml = serializer.Serialize(msg);
Console.WriteLine(yaml);
Console.WriteLine("=====");
var deserialized = deserializer.Deserialize<MessageEnvelope<ConfigMessage>>(yaml);
Console.WriteLine(JsonSerializer.Serialize(deserialized));
class RequestMessage
{
[YamlMember(Alias = "request")]
public string Request { get; set; }
[YamlMember(Alias = "request")]
public string RequesterName { get; set; }
}
class ConfigMessage
{
[YamlMember(Alias = "a")]
public string SettingA { get; set; }
[YamlMember(Alias = "b")]
public int SettingB { get; set; }
}
class MessageEnvelope<TPayload>
{
[YamlMember(Alias = "message_type")]
public string Type { get; set; }
[YamlMember(Alias = "payload")]
public TPayload Payload { get; set; }
} With the following output
|
I think this will answer your question about how to determine what type to use based on a value of a scalar |
This reply went above and beyond and is extremely helpful. Thank you. |
Here's a contrived example to demonstrate my question:
Say I have some messages defind by their payloads, and a generic envelope
that I stick a payload in and send/receive with. This is generally how I would
structure that classes:
One issue I've been trying to figure out is how do you make this work well
with serialization, or more accurrately deserialization. If I create and object
and serialize it like so:
Ideally it creates yaml documents that looks like:
Which it does now (I just tested, it use to just give 'payload: {}', but must
have been fixed in a recent update).
The question is, how do you deserialize this in one pass to an object, when you
don't know what the payload type is before deserializing?
Currently I'm treating the payload as a separate serialized object, which yields
yaml like this:
This isn't nearly as clean and readible, and I was hoping for a good way to
do this, does anyone have any ideas?
The text was updated successfully, but these errors were encountered: