#nullable enable using BCG; using Spark.Core.Json; using System; using System.Collections.Generic; using System.Linq; using System.Text.Json; using System.Text.Json.Serialization; namespace Bts.DataStreamCore; class DataPacketJsonConverter : JsonConverter { /// /// Whether or not to include a field for current rows (i.e., snapshot of current state). By /// default, only incremental changes are included ('added' and 'deleted'). /// public bool IncludeCurrent { get; set; } /// /// Whether to treat rows as objects (name/value pairs), or arrays (list of values). Arrays /// result in smaller payloads, but aren't as easy to read as name/value pairs (for mere humans). /// public bool UseObjectRows { get; set; } public override DataPacket? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (!reader.ReadObject()) { return null; } if (!reader.ReadPropertyName(out var prop)) { return null; } if (!reader.Read()) { return null; } if (prop == "added") { var added = new List(); if (reader.TokenType != JsonTokenType.StartArray) { return null; } while (true) { if (!reader.Read()) { break; } if (reader.TokenType != JsonTokenType.StartArray || !reader.Read()) { return null; } switch (reader.TokenType) { case JsonTokenType.String: // DAMNIT! Need the schema. break; } } } return null; } public override void Write(Utf8JsonWriter writer, DataPacket value, JsonSerializerOptions options) { using var _ = writer.WriteObject(); using (writer.WriteArray("schema")) { foreach (var col in value.schema.columnInfo) { using (writer.WriteObject()) { writer.WriteString("name", col.colName); writer.WriteNumber("index", col.colNo); // TODO: use language agnostic type names (e.g., 'string', 'integer', 'float', 'date' etc.) writer.WriteString("type", col.colType.ToString()); } } } var cellWriters = value.schema.columnInfo.Select(getCellWriter).ToList(); using (writer.WriteArray("added")) { foreach (IBlueRow row in value.addedRows) { using (writer.WriteArray()) { for (int i = 0; i < cellWriters.Count; i++) { cellWriters[i](row[i]); } } } } using (writer.WriteArray("deleted")) { foreach (IBlueRow row in value.deletedRows) { using (writer.WriteArray()) { for (int i = 0; i < cellWriters.Count; i++) { cellWriters[i](row[i]); } } } } if (IncludeCurrent) { using (writer.WriteArray("current")) { foreach (IBlueRow row in value.newRows) { using (writer.WriteArray()) { for (int i = 0; i < cellWriters.Count; i++) { cellWriters[i](row[i]); } } } } } Action getCellWriter(ColumnInfo colInfo) { var writeVal = getCellValueWriter(colInfo); if (!UseObjectRows) { return writeVal; } return v => { writer.WritePropertyName(colInfo.colName); writeVal(v); }; } Action getCellValueWriter(ColumnInfo colInfo) { var t = colInfo.colType; if (t == typeof(int)) { return nullCheckWrapper(v => writer.WriteNumberValue((int)v)); } if (t == typeof(uint)) { return nullCheckWrapper(v => writer.WriteNumberValue((uint)v)); } if (t == typeof(long)) { return nullCheckWrapper(v => writer.WriteNumberValue((long)v)); } if (t == typeof(ulong)) { return nullCheckWrapper(v => writer.WriteNumberValue((ulong)v)); } if (t == typeof(short)) { return nullCheckWrapper(v => writer.WriteNumberValue((short)v)); } if (t == typeof(ushort)) { return nullCheckWrapper(v => writer.WriteNumberValue((ushort)v)); } if (t == typeof(double)) { return nullCheckWrapper(v => writer.WriteNumberValue((double)v)); } if (t == typeof(float)) { return nullCheckWrapper(v => writer.WriteNumberValue((float)v)); } if (t == typeof(bool)) { return nullCheckWrapper(v => writer.WriteBooleanValue((bool)v)); } return nullCheckWrapper(v => writer.WriteStringValue(v?.ToString())); Action nullCheckWrapper(Action writeNonNull) => v => { if (v is null) { writer.WriteNullValue(); } else { writeNonNull(v); } }; } } }