diff --git a/Yaml2JsonNode/Yaml2JsonNode.csproj b/Yaml2JsonNode/Yaml2JsonNode.csproj index 7a172230a6..8c345a1eeb 100644 --- a/Yaml2JsonNode/Yaml2JsonNode.csproj +++ b/Yaml2JsonNode/Yaml2JsonNode.csproj @@ -4,9 +4,9 @@ netstandard2.0 Yaml2JsonNode Greg Dennis - 1.0.0 + 1.1.0 1.0.0.0 - 1.0.0.0 + 1.1.0.0 Allows conversion of YamlDotNet's YAML models to JsonNodes. LICENSE https://github.com/gregsdennis/json-everything @@ -34,7 +34,7 @@ - + True diff --git a/Yaml2JsonNode/YamlConverter.cs b/Yaml2JsonNode/YamlConverter.cs index 32a38b742d..6c12f00e50 100644 --- a/Yaml2JsonNode/YamlConverter.cs +++ b/Yaml2JsonNode/YamlConverter.cs @@ -49,6 +49,23 @@ public static JsonNode ToJsonNode(this YamlNode yaml) }; } + /// + /// Converts a single JSON node to a . + /// + /// + /// + /// + public static YamlNode ToYamlNode(this JsonNode json) + { + return json switch + { + JsonObject obj => obj.ToYamlMapping(), + JsonArray arr => arr.ToYamlSequence(), + JsonValue val => val.ToYamlScalar(), + _ => throw new NotSupportedException("This isn't a supported JsonNode") + }; + } + private static JsonObject ToJsonObject(this YamlMappingNode yaml) { var node = new JsonObject(); @@ -61,6 +78,11 @@ private static JsonObject ToJsonObject(this YamlMappingNode yaml) return node; } + private static YamlMappingNode ToYamlMapping(this JsonObject obj) + { + return new YamlMappingNode(obj.ToDictionary(x => (YamlNode)new YamlScalarNode(x.Key), x => x.Value.ToYamlNode())); + } + private static JsonArray ToJsonArray(this YamlSequenceNode yaml) { var node = new JsonArray(); @@ -72,6 +94,11 @@ private static JsonArray ToJsonArray(this YamlSequenceNode yaml) return node; } + private static YamlSequenceNode ToYamlSequence(this JsonArray arr) + { + return new YamlSequenceNode(arr.Select(x => x.ToYamlNode())); + } + private static JsonValue ToJsonValue(this YamlScalarNode yaml) { switch (yaml.Style) @@ -92,4 +119,9 @@ private static JsonValue ToJsonValue(this YamlScalarNode yaml) throw new ArgumentOutOfRangeException(); } } + + private static YamlScalarNode ToYamlScalar(this JsonValue val) + { + return new YamlScalarNode(val.ToJsonString()); + } } \ No newline at end of file diff --git a/json-everything.net/wwwroot/md/release-notes/yaml.md b/json-everything.net/wwwroot/md/release-notes/yaml.md index c45e73c05f..ad51a5c138 100644 --- a/json-everything.net/wwwroot/md/release-notes/yaml.md +++ b/json-everything.net/wwwroot/md/release-notes/yaml.md @@ -1,3 +1,7 @@ +# [1.1.0](https://github.com/gregsdennis/json-everything/pull/387) + +[#381](https://github.com/gregsdennis/json-everything/issues/381) - Adds conversions from `JsonNode` back to YAML. + # [1.0.0](https://github.com/gregsdennis/json-everything/pull/358) Initial release. \ No newline at end of file diff --git a/json-everything.net/wwwroot/md/yaml.md b/json-everything.net/wwwroot/md/yaml.md index e5d3c6d980..d9d4961b70 100644 --- a/json-everything.net/wwwroot/md/yaml.md +++ b/json-everything.net/wwwroot/md/yaml.md @@ -1,4 +1,4 @@ -Yaml2JsonNode is pretty simple: it converts models of YAML text that has been parsed by YamlDotNet into `JsonNode`s. That's it. +Yaml2JsonNode is pretty simple: it converts models of YAML text that has been parsed by YamlDotNet into `JsonNode`s (and can also convert `JsonNode`s back into `YamlNode`s). That's it. ## Why? @@ -36,3 +36,7 @@ var specificJsonNode = specificYamlNode.ToJsonNode(); ``` There's really not much to this. Just remember one method: `.ToJsonNode()`. It'll take care of you. + +If you want to go back to YAML for some reason, `.ToYamlNode()` is your friend. + +***NOTE** This library will get JSON into the `YamlNode` model. You'll need to understand how to get that back into a string using YamlDotNet. Link to their docs above.* \ No newline at end of file diff --git a/json-everything.net/wwwroot/xml/Yaml2JsonNode.xml b/json-everything.net/wwwroot/xml/Yaml2JsonNode.xml deleted file mode 100644 index cb0c522433..0000000000 --- a/json-everything.net/wwwroot/xml/Yaml2JsonNode.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - Yaml2JsonNode - - - - - Provides extensions to convert YAML models to JSON models. - - - - - Converts all of the documents in a YAML stream to s. - - The YAML stream. - A collection of nodes representing the YAML documents in the stream. - - - - Converts a single YAML document to a . - - The YAML document. - A `JsonNode` representative of the YAML document. - - - - Converts a single YAML node to a . - - The YAML node. - A `JsonNode` representative of the YAML node. - Thrown for YAML that is not compatible with JSON. - - -