Add json facilities for DataStream websocket api

This commit is contained in:
John Colvin committed 2024-07-07 17:43:45 -04:00
1 parent 1531c6e794
commit a86d4490e4
6 files changed
+436 -88

No files matched your search

@@ -0,0 +1,138 @@
#nullable enable
namespace Spark.Core.Json;
using Spark.Core.Disposables;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Nodes;
public static class JsonExtensions {
public static T As<T>(this JsonNode node) =>
node.GetValue<T>();
public static JsonArray AsArrayOrEmpty(this JsonNode node) =>
node as JsonArray ?? new();
public static TEnum AsEnum<TEnum>(this JsonNode node) where TEnum : struct, Enum {
var str = node.As<string>();
return Enum.Parse<TEnum>(str);
}
public static string AsString(this JsonNode node) =>
node.GetValue<string>();
public static string AsStringOrEmpty(this JsonNode node) =>
node is JsonValue value && value.TryGetValue(out string? str) ? str : string.Empty;
public static JsonArray GetArrayOrEmpty(this JsonObject obj, string propertyName) =>
obj.TryGetArray(propertyName, out var res) ? res : new();
public static T GetValue<T>(this JsonObject obj, string propertyName) =>
obj[propertyName]!.GetValue<T>();
public static JsonObject GetObjectOrEmpty(this JsonObject obj, string propertyName) =>
obj.TryGetObject(propertyName, out var res) ? res : new();
public static string GetStringOrEmpty(this JsonObject obj, string propertyName) =>
obj.TryGetString(propertyName, out var res) ? res : string.Empty;
public static bool ReadArray(
this Utf8JsonReader reader, [NotNullWhen(true)] out JsonArray? array) =>
ReadArray(reader, JsonSerializerOptions.Default, out array);
public static bool ReadArray(
this Utf8JsonReader reader, JsonSerializerOptions options, [NotNullWhen(true)] out JsonArray? array) {
array = JsonSerializer.Deserialize<JsonArray>(ref reader, options);
return array is not null;
}
public static bool ReadObject(this Utf8JsonReader reader) {
return reader.Read() && reader.TokenType == JsonTokenType.StartObject;
}
public static bool ReadObject(this Utf8JsonReader reader, [NotNullWhen(true)] out JsonObject? obj) {
obj = JsonSerializer.Deserialize<JsonObject>(ref reader);
return obj is not null;
}
public static bool ReadPropertyName(
this Utf8JsonReader reader, [NotNullWhen(true)] out string? propertyName) {
if (reader.Read() && reader.TokenType == JsonTokenType.PropertyName) {
propertyName = reader.GetString()!;
return true;
}
propertyName = null;
return false;
}
public static bool ReadString(
this Utf8JsonReader reader, [NotNullWhen(true)] out string? str) =>
ReadString(reader, JsonSerializerOptions.Default, out str);
public static bool ReadString(
this Utf8JsonReader reader, JsonSerializerOptions options, [NotNullWhen(true)] out string? str) {
str = JsonSerializer.Deserialize<string>(ref reader, options);
return str is not null;
}
public static bool TryGetArray(this JsonObject obj, string propertyName, [NotNullWhen(true)] out JsonArray? result) {
result = obj.TryGetPropertyValue(propertyName, out var v) ? v as JsonArray : null;
return result is not null;
}
public static bool TryGetObject(
this JsonObject obj, string propertyName, [NotNullWhen(true)] out JsonObject? result) {
result = obj.TryGetPropertyValue(propertyName, out var v) ? v as JsonObject : null;
return result is not null;
}
public static bool TryGetString(
this JsonObject obj, string propertyName, [NotNullWhen(true)] out string? result) {
if (obj.TryGetPropertyValue(propertyName, out var v) && v is JsonValue value) {
return value.TryGetValue(out result);
}
result = null;
return false;
}
public static IDisposable WriteArray(this Utf8JsonWriter writer) {
writer.WriteStartArray();
return Disposable.Create(writer.WriteEndArray);
}
public static IDisposable WriteArray(this Utf8JsonWriter writer, string propertyName) {
writer.WriteStartArray(propertyName);
return Disposable.Create(writer.WriteEndArray);
}
public static void WriteArray(this Utf8JsonWriter writer, string propertyName, IEnumerable<string> items) {
writer.WriteStartArray(propertyName);
foreach (var item in items) {
writer.WriteStringValue(item);
}
writer.WriteEndArray();
}
public static void Write<T>(this Utf8JsonWriter writer, string propertyName, T node) {
writer.WritePropertyName(propertyName);
JsonSerializer.Serialize(writer, node);
}
public static IDisposable WriteObject(this Utf8JsonWriter writer) {
writer.WriteStartObject();
return Disposable.Create(writer.WriteEndObject);
}
public static IDisposable WriteObject(this Utf8JsonWriter writer, string propertyName) {
writer.WritePropertyName(propertyName);
return writer.WriteObject();
}
public static void WriteObject(this Utf8JsonWriter writer, string propertyName, JsonObject obj) {
writer.WritePropertyName(propertyName);
JsonSerializer.Serialize(writer, obj);
}
}
@@ -0,0 +1,29 @@
#nullable enable
namespace Spark.Core.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
/// <summary>
/// A JsonConverter that can parse input in the form of a (single) value, or an array.
/// </summary>
public class JsonValueOrArrayConverter<T> : JsonConverter<List<T>> {
public override List<T>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
reader.TokenType switch {
JsonTokenType.StartArray =>
reader.ReadArray(options, out var array)
? array.Select(n => n is null ? throw invalidJsonEx() : n.As<T>()).ToList()
: throw invalidJsonEx(),
JsonTokenType.Null => null,
_ => new() { JsonSerializer.Deserialize<T>(ref reader, options) ?? throw invalidJsonEx() }
};
public override void Write(Utf8JsonWriter writer, List<T> value, JsonSerializerOptions options) =>
JsonSerializer.Serialize(writer, value, options);
static JsonException invalidJsonEx() =>
new($"Failed to parse json (expected value or array of type {typeof(T)})");
}