Static utility that parses a JSON array of packets into APPacket instances. It reads a JSON document, iterates the root array, looks up a .GetProperty("cmd") value, resolves a concrete type via PacketRegistry.Get and deserializes each element into that type, yielding APPacket instances.
using System.Collections.Generic;
using System.Text.Json;
namespace APLib.Protocol;
public static class PacketReader
{
public static IEnumerable<APPacket> Read( string json )
{
using var doc = JsonDocument.Parse( json );
foreach ( var element in doc.RootElement.EnumerateArray() )
{
var cmd = element.GetProperty( "cmd" ).GetString();
var type = PacketRegistry.Get( cmd );
var packet = (APPacket)element.Deserialize( type );
yield return packet;
}
}
}