From a649272be9536d1f72a388cae4bf1225d7a22f55 Mon Sep 17 00:00:00 2001 From: Greg Dennis Date: Thu, 23 Feb 2023 11:31:57 +1300 Subject: [PATCH 1/3] add back-to-yaml functionality --- Yaml2JsonNode/Yaml2JsonNode.csproj | 4 +-- Yaml2JsonNode/YamlConverter.cs | 32 +++++++++++++++++ .../wwwroot/xml/Yaml2JsonNode.xml | 35 ------------------- 3 files changed, 34 insertions(+), 37 deletions(-) delete mode 100644 json-everything.net/wwwroot/xml/Yaml2JsonNode.xml diff --git a/Yaml2JsonNode/Yaml2JsonNode.csproj b/Yaml2JsonNode/Yaml2JsonNode.csproj index 7a172230a6..dd5fac464a 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 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/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. - - - From 54cbca3b88a2e13ba64102c275ddf3ef69486f32 Mon Sep 17 00:00:00 2001 From: Greg Dennis Date: Thu, 23 Feb 2023 11:35:40 +1300 Subject: [PATCH 2/3] update release notes --- Yaml2JsonNode/Yaml2JsonNode.csproj | 2 +- json-everything.net/wwwroot/md/release-notes/yaml.md | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Yaml2JsonNode/Yaml2JsonNode.csproj b/Yaml2JsonNode/Yaml2JsonNode.csproj index dd5fac464a..8c345a1eeb 100644 --- a/Yaml2JsonNode/Yaml2JsonNode.csproj +++ b/Yaml2JsonNode/Yaml2JsonNode.csproj @@ -34,7 +34,7 @@ - + True 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 From 8d0446314f5f5badc204cfb3c0cfbd12771e07af Mon Sep 17 00:00:00 2001 From: Greg Dennis Date: Thu, 23 Feb 2023 11:44:55 +1300 Subject: [PATCH 3/3] update docs --- json-everything.net/wwwroot/md/yaml.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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