Skip to content

Commit

Permalink
Merge pull request #387 from gregsdennis/yaml/back2yaml
Browse files Browse the repository at this point in the history
add back-to-yaml functionality
  • Loading branch information
gregsdennis committed Feb 22, 2023
2 parents 93cb60d + 8d04463 commit 5e9faf4
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 39 deletions.
6 changes: 3 additions & 3 deletions Yaml2JsonNode/Yaml2JsonNode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>Yaml2JsonNode</PackageId>
<Authors>Greg Dennis</Authors>
<Version>1.0.0</Version>
<Version>1.1.0</Version>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<FileVersion>1.1.0.0</FileVersion>
<Description>Allows conversion of YamlDotNet's YAML models to JsonNodes.</Description>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageProjectUrl>https://github.com/gregsdennis/json-everything</PackageProjectUrl>
Expand Down Expand Up @@ -34,7 +34,7 @@
</ItemGroup>

<ItemGroup>
<None Include="..\json-everything.net\wwwroot\md\release-notes\json-yaml.md" Link="json-yaml.md" />
<None Include="..\json-everything.net\wwwroot\md\release-notes\yaml.md" Link="yaml.md" />
<None Include="..\LICENSE">
<Pack>True</Pack>
<PackagePath></PackagePath>
Expand Down
32 changes: 32 additions & 0 deletions Yaml2JsonNode/YamlConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ public static JsonNode ToJsonNode(this YamlNode yaml)
};
}

/// <summary>
/// Converts a single JSON node to a <see cref="YamlNode"/>.
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
/// <exception cref="NotSupportedException"></exception>
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();
Expand All @@ -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();
Expand All @@ -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)
Expand All @@ -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());
}
}
4 changes: 4 additions & 0 deletions json-everything.net/wwwroot/md/release-notes/yaml.md
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 5 additions & 1 deletion json-everything.net/wwwroot/md/yaml.md
Original file line number Diff line number Diff line change
@@ -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?

Expand Down Expand Up @@ -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.*
35 changes: 0 additions & 35 deletions json-everything.net/wwwroot/xml/Yaml2JsonNode.xml

This file was deleted.

0 comments on commit 5e9faf4

Please sign in to comment.