using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using System.Text.Json.Nodes;
using OASISLevel2TrackingPacket;
namespace OASISLevel2TrackingPacket
[global::ProtoBuf.ProtoContract]
public partial class EntityTracking
[global::ProtoBuf.ProtoMember(2, Name = @"TrackingData")]
public global::System.Collections.Generic.List<EntityTrackingActivity> TrackingDatas { get; } = new global::System.Collections.Generic.List<EntityTrackingActivity>();
[global::ProtoBuf.ProtoContract]
public partial class Person : EntityTracking
[global::ProtoBuf.ProtoMember(1, Name = @"Name")]
public string? Name { get; set; }
[global::ProtoBuf.ProtoContract]
public partial class EntityTrackingActivity
[global::ProtoBuf.ProtoMember(1, Name = @"Id")]
public int Id { get; set; }
public static partial class JsonExtensions
public static Action<JsonTypeInfo> InitializeProtoMemberNames(Type type) => typeInfo =>
if (typeInfo.Kind != JsonTypeInfoKind.Object)
if (!type.IsAssignableFrom(typeInfo.Type))
foreach (var property in typeInfo.Properties)
var name = property.AttributeProvider?.GetCustomAttributes(typeof(global::ProtoBuf.ProtoMemberAttribute), true)
.OfType<global::ProtoBuf.ProtoMemberAttribute>()
public static async Task Test()
var filename = "hugefile.json";
[{"Name":"My name is 0","TrackingData":[{"Id":0},{"Id":1}]},{"Name":"My name is 1","TrackingData":[{"Id":2},{"Id":3}]},{"Name":"My name is 2","TrackingData":[{"Id":4},{"Id":5}]},{"Name":"My name is 3","TrackingData":[{"Id":6},{"Id":7}]}]
await File.WriteAllTextAsync(filename, json);
var options = new JsonSerializerOptions
PreferredObjectCreationHandling = JsonObjectCreationHandling.Populate,
TypeInfoResolver = new DefaultJsonTypeInfoResolver()
.WithAddedModifier(JsonExtensions.InitializeProtoMemberNames(typeof(Person))),
await using var fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, useAsync: true);
var people = JsonSerializer.DeserializeAsyncEnumerable<Person?>(fileStream, options);
await foreach (var person in people)
Console.WriteLine($"Hello, my name is \"{person?.Name}\", my tracking data is {JsonSerializer.Serialize(person?.TrackingDatas.Select(t => t.Id))}!");
Assert.That(person?.TrackingDatas.Count > 0);
public static async Task Main(string[] args)
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , Environment.Version);
Console.WriteLine("{0} version: {1}", typeof(JsonSerializer).Assembly.GetName().Name, typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");