Skip to content
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: Serialize dynamic dictionary #440

Open
j-carvalho opened this issue Oct 17, 2019 · 3 comments
Open

Question: Serialize dynamic dictionary #440

j-carvalho opened this issue Oct 17, 2019 · 3 comments
Labels

Comments

@j-carvalho
Copy link

j-carvalho commented Oct 17, 2019

I'm trying to serialize an object that contains a property which is a dictionary of type Dictionary<string, object>.

Basically this property will be an arbitrary json object, which can have different shapes, but my function should be unaware of the shape and simply accept the input and serialize it.

So basically, something in the format:

public class Input {
   public string Name {get;set;}
  public Dictionary<string, object> Annotation {get;set;}
}

The issue I have, is when I try to serialize this class to Yaml, the property Annotation contains the value: KindObject.

Any idea how I can better use the library to achieve this feature?

Thank you for the support.

@j-carvalho
Copy link
Author

j-carvalho commented Oct 17, 2019

Upon further debugging, I've found that the issue is elsewhere. Serializing an instance of this class directly is not an issue, the problem is the json serialization in between.

I have some E2E tests and I think the request dto is causing the issue.

My request dto is an instance of Input. When inspecting the object, I can see that the value of the property is a JsonElement.

image

Is maybe the case that YamlDotNet is not able to serialize a JsonElement?

@aaubry
Copy link
Owner

aaubry commented Oct 19, 2019

I don't know what is a JsonElement, but assuming that it is a representation of part of a JSON syntax, you will need to register a IYamlTypeConverter that will take care of converting it to YAML. I can't really help much because the implementation of the converter is dependent of what your goal is.

@IvanJosipovic
Copy link

I have started a library with a IYamlTypeConverter for System.Text.Json here, https://github.com/IvanJosipovic/YamlDotNet.System.Text.Json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants