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

Deserializing arbitrary YAML without creating types? #332

Closed
fj opened this issue Jun 25, 2018 · 6 comments
Closed

Deserializing arbitrary YAML without creating types? #332

fj opened this issue Jun 25, 2018 · 6 comments

Comments

@fj
Copy link

fj commented Jun 25, 2018

Is there a way to use YamlDotNet to deserialize YAML to raw strings, lists, or dictionaries when the structure of the YAML isn't known in advance and can be arbitrary? For example, is ExpandoObject a possibility?

@aaubry
Copy link
Owner

aaubry commented Jun 25, 2018

Sure, either deserialize to Object, or use YamlStream to load the document(s).

Deserializing as object:

var deserializer = new DeserializerBuilder().Build();
var result = deserializer.Deserialize<object>(yaml);

Using YamlStream:

var stream = new YamlStream();
stream.Load(new StringReader(yaml));
// Read stream.Documents

@aaubry
Copy link
Owner

aaubry commented Jun 25, 2018

Id you deserialize to object, the following default types will be used:

Node Type CLR type
scalar String
sequence List<Object>
mapping Dictionary<Object, Object>

@fj
Copy link
Author

fj commented Jun 25, 2018

Thanks! In this case the Dictionary's values would always be one of String, List, or another Dictionary, right?

Does YamlDotNet provide any jq-DSL-like facility for fetching specific paths in the YAML, e.g., "please list all the values of //items/name" or is that work which is expected to be handled by a custom deserializer?

@aaubry
Copy link
Owner

aaubry commented Jun 25, 2018

Exactly. Unless a node has a tag, the above types will be used depending on the node's type. If the node has a tag, you will need to register a tag mapping, indicating which .NET type corresponds to that tag.

@bjorg
Copy link

bjorg commented Dec 7, 2018

Is there an easy what to change the default type for mapping to Dictionary<string, object>?

@aaubry aaubry closed this as completed Sep 25, 2019
@ohads-MSFT
Copy link

ohads-MSFT commented May 28, 2023

Id you deserialize to object, the following default types will be used:

Node Type CLR type
scalar String
sequence List<Object>
mapping Dictionary<Object, Object>

Thank you, this is really useful information - is it documented anywhere?

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

No branches or pull requests

4 participants