https://stackoverflow.com/questions/76263968/c-sharp-parse-byte-to-json-without-converting-to-base64-string

CSharp · Console

/Program.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using System.Text.Json.Nodes;

using NUnit.Framework; // Throws NUnit.Framework.AssertionException

#nullable enable

//https://stackoverflow.com/questions/76263968/c-sharp-parse-byte-to-json-without-converting-to-base64-string

	public class MyModel
	{
		public int Id { get; set; }
		public IEnumerable<byte>? Data { get; set; }
	}

    class TestClass
    {
        public static void Test()
        {
			var options = new JsonSerializerOptions { };
			
			MyModel myObject = new MyModel
			{
				Id = 1,                
				Data = new byte[] { 0x01, 0x02, 0x03 }
			};
	        string json = JsonSerializer.Serialize(myObject, options);
			
			Console.WriteLine(json);
		}	
	}

	public class Program
	{
		public static void Main()
		{
			Console.WriteLine("Environment version: {0} ({1}), {2}", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , Environment.Version, Environment.OSVersion);
			Console.WriteLine("System.Text.Json version: " + typeof(JsonSerializer).Assembly.FullName);
			Console.WriteLine();

			try
			{
				TestClass.Test();
			}
			catch (Exception ex)
			{
				Console.WriteLine("Failed with unhandled exception: ");
				Console.WriteLine(ex);
				throw;
			}
		}
	}
;