[Fork] [Fork] [Fork] https://stackoverflow.com/questions/58780225/how-to-save-a-serialized-messagepack-binary-file-in-c

CSharp · Console

/Program.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters;
using System.IO;
using System.Data;
using System.Globalization;
using System.Reflection;
using System.Runtime.Serialization;

using MessagePack;

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

//https://stackoverflow.com/questions/58780225/how-to-save-a-serialized-messagepack-binary-file-in-c
	
	[MessagePackObject]
	public struct struct_realTime
	{
		[Key(0)]
		public int indexNum { get; set; }
		[Key(1)]
		public string currentTime { get; set; }
		[Key(2)]
		public string currentType { get; set; }
	}

    class TestClass
    {
        public static void Test()
        {
			var path = "Question58780225.bin";
			foreach (var count in new [] { 1, 2, 14, 15, 16, 17, ushort.MaxValue - 4, ushort.MaxValue + 4 })
			{
				try
				{
					File.Delete(path);
					Test(path, count);
				}
				finally
				{
					File.Delete(path);
				}
			}
		}	
	
        static void Test(string filename, int expectedNumberOfRootItemsInFile)
        {
			int innerCount = 3;

			List<struct_realTime> list_temp = new List<struct_realTime>(innerCount);

			for (int num=0; num < innerCount; num++)
			{
				list_temp.Add(new struct_realTime
							  {
								  indexNum = 1,
								  currentTime = "time",
								  currentType = "type",
							  });
			}
			
			Console.WriteLine("Testing: {0} repetitions of {1}", expectedNumberOfRootItemsInFile, MessagePackSerializer.ToJson(list_temp));

			for (int i = 0; i < expectedNumberOfRootItemsInFile; i++)
			{
				// Append each list_temp sequentially
				using (var fileStream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite))
				{
					MessagePackSerializer.Serialize(fileStream, list_temp);
				}
			}

			List<List<struct_realTime>> allItemsInFile;
			using (var fileStream = File.OpenRead(filename))
			{
				allItemsInFile = MessagePackSerializer.Deserialize<List<List<struct_realTime>>>(fileStream);
			}
			Assert.IsTrue(allItemsInFile.Count == expectedNumberOfRootItemsInFile);
			Assert.IsTrue(allItemsInFile.All(i => i.SequenceEqual(list_temp)));
			
			Console.WriteLine("   Tests passed.");
		}	
	}

	public class Program
	{
		public static void Main()
		{
			Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
			Console.WriteLine("MessagePack version: " + typeof(MessagePackSerializer).Assembly.FullName);
			Console.WriteLine();

			try
			{
				TestClass.Test();
			}
			catch (Exception ex)
			{
				Console.WriteLine("Failed with unhandled exception: ");
				Console.WriteLine(ex);
				throw;
			}
		}
		
		public static string GetNetCoreVersion()
		{
			//https://techblog.dorogin.com/how-to-detect-net-core-version-in-runtime-ecd65ad695be
			//
			var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
			var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
			int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
			if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
				return assemblyPath[netCoreAppIndex + 1];
			return null;
		}
	}
;