mirror of
https://github.com/space-wizards/space-station-14.git
synced 2026-02-14 19:29:53 +01:00
* initial visual nubody * oops overlay * im so pheeming rn * conversion... * tests * comeback of the underwear * oops eyes * blabbl * zeds * yaml linted * search and visible count constraints * reordering * preserve previously selected markings colors * fix test * some ui niceties * ordering * make DB changes backwards-compatible/downgrade-friendly * fix things again * fix migration * vulpkanin markings limit increase * wrapping * code cleanup and more code cleanup and more code cleanup and more code cleanup and * fix slop ports * better sampling API * make filter work + use the method i made for its intended purpose * fix test fails real quick * magic mirror cleanup, remove TODO * don't 0-init the organ profile data * remove deltastates --------- Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com>
62 lines
2.5 KiB
C#
62 lines
2.5 KiB
C#
using System.Linq;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Nodes;
|
|
using Robust.Shared.Serialization.Markdown;
|
|
using Robust.Shared.Serialization.Markdown.Mapping;
|
|
using Robust.Shared.Serialization.Markdown.Sequence;
|
|
using Robust.Shared.Serialization.Markdown.Value;
|
|
using YamlDotNet.RepresentationModel;
|
|
|
|
namespace Content.Server.Database;
|
|
|
|
public static class DataNodeJsonExtensions
|
|
{
|
|
private static JsonNode ToJsonNode(this MappingDataNode node)
|
|
{
|
|
return new JsonObject(node.Children.Select(kvp => new KeyValuePair<string, JsonNode?>(kvp.Key, kvp.Value.ToJsonNode())));
|
|
}
|
|
|
|
private static JsonNode ToJsonNode(this SequenceDataNode node)
|
|
{
|
|
return new JsonArray(node.Select(ToJsonNode).ToArray());
|
|
}
|
|
|
|
public static JsonNode? ToJsonNode(this DataNode node)
|
|
{
|
|
return node switch
|
|
{
|
|
ValueDataNode valueDataNode => JsonValue.Create(valueDataNode.IsNull ? null : valueDataNode.Value),
|
|
MappingDataNode mappingDataNode => mappingDataNode.ToJsonNode(),
|
|
SequenceDataNode sequenceNode => sequenceNode.ToJsonNode(),
|
|
_ => throw new ArgumentOutOfRangeException(nameof(node))
|
|
};
|
|
}
|
|
|
|
public static DataNode ToDataNode(this JsonElement element)
|
|
{
|
|
return element.ValueKind switch
|
|
{
|
|
JsonValueKind.Object => new MappingDataNode(element.EnumerateObject().ToDictionary(kvp => kvp.Name, kvp => kvp.Value.ToDataNode())),
|
|
JsonValueKind.Array => new SequenceDataNode(element.EnumerateArray().Select(item => item.ToDataNode()).ToList()),
|
|
JsonValueKind.Number => new ValueDataNode(element.GetRawText()),
|
|
JsonValueKind.String => new ValueDataNode(element.GetString()),
|
|
JsonValueKind.True => new ValueDataNode("true"),
|
|
JsonValueKind.False => new ValueDataNode("false"),
|
|
JsonValueKind.Null => new ValueDataNode("null"),
|
|
_ => throw new ArgumentOutOfRangeException(nameof(element)),
|
|
};
|
|
}
|
|
|
|
public static DataNode ToDataNode(this JsonNode? node)
|
|
{
|
|
return node switch
|
|
{
|
|
null => ValueDataNode.Null(),
|
|
JsonValue value => new ValueDataNode(value.GetValue<string>()),
|
|
JsonArray array => new SequenceDataNode(array.Select(item => item.ToDataNode()).ToList()),
|
|
JsonObject obj => new MappingDataNode(obj.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.ToDataNode())),
|
|
_ => throw new ArgumentOutOfRangeException(nameof(node))
|
|
};
|
|
}
|
|
}
|