using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public double[,] PressureMatrix { get; set; }
public class CalibrationConfiguration
public class RepresentationConfiguration
public class RecordedDataHeader
public string SoftwareVersion { get; set; }
public CalibrationConfiguration CalibrationConfiguration { get; set; }
public RepresentationConfiguration RepresentationConfiguration { get; set; }
public class RecordedData
[JsonProperty(Order = 1)]
public RecordedDataHeader RecordedDataHeader { get; set; }
[JsonProperty(Order = 2)]
public IEnumerable<PressureMap> PressureData { get; set; }
public static void Test()
var filePath = "Question52311362.json";
new TestClass { filePath = filePath }.SampleAndWritePressureData();
Console.WriteLine(File.ReadAllText(filePath));
public void SampleAndWritePressureData()
var calibrationConfiguration = new CalibrationConfiguration();
var representationConfiguration = new RepresentationConfiguration();
var softwareVersion = Assembly.GetExecutingAssembly().GetName().Version.Major.ToString() + "." + Assembly.GetExecutingAssembly().GetName().Version.Minor.ToString();
using (var pressureData = new BlockingCollection<PressureMap>())
using (Task t1 = Task.Factory.StartNew(() =>
for (int i = 0; i < sampleCount; i++)
var data = GetPressureMap(i);
Console.WriteLine("Generated sample {0}", i);
System.Threading.Thread.Sleep(sampleInterval);
pressureData.CompleteAdding();
using (Task t2 = Task.Factory.StartNew(() =>
var recordedDataHeader = new RecordedDataHeader
SoftwareVersion = softwareVersion,
CalibrationConfiguration = calibrationConfiguration,
RepresentationConfiguration = representationConfiguration,
var settings = new JsonSerializerSettings
ContractResolver = new CamelCasePropertyNamesContractResolver(),
using (var stream = new FileStream(this.filePath, FileMode.Create))
using (var textWriter = new StreamWriter(stream))
using (var jsonWriter = new JsonTextWriter(textWriter))
.GetConsumingEnumerable()
Console.WriteLine("Serializing item {0}", j++);
var recordedData = new RecordedData
RecordedDataHeader = recordedDataHeader,
Console.WriteLine("Beginning serialization of {0} to {1}:", recordedData, this.filePath);
JsonSerializer.CreateDefault(settings).Serialize(textWriter, recordedData);
Console.WriteLine("Finished serialization of {0} to {1}.", recordedData, this.filePath);
PressureMap GetPressureMap(int count)
PressureMatrix = new double[,] { { (double)count } }
public static void Main()
Console.WriteLine("Environment version: " + Environment.Version);
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");