Hi mcalsyn. Definitely appreciate you having a look if you are getting this stuff to work. Here is my configuration class. You can see that the config object (for now) consists of 3 pumps and 1 network object. I run into several issues depending on exactly what I try. If I just go for the gold on this sample, I get a System.Exception unsupported type at var bson = result.ToBson(); If think this has to do with the byte[]s in the network object? If I remove the network object, It creates the Jobject, but when I look at the string, it is a partial and has no values for any of the variables. I have never worked with Json before, so part of the problem is that I just don’t know what I can expect it to be capable of. Any insight would be appreciated as this would be the best way to do serialization for my purposes (vs an SQLite DB).
using System;
using System.IO;
using System.Reflection;
using GHIElectronics.TinyCLR.Data.Json;
using IPT;
namespace SampleStream_TinyCLR
{
[Serializable]
public class SSConfiguration
{
public Pump pump1;
public Pump pump2;
public Pump pump3;
public Network network;
public SSConfiguration()
{
pump1 = new Pump();
pump2 = new Pump();
pump3 = new Pump();
network = new Network();
}
}
public static class ConfigurationUtilities
{
public static void SaveConfiguration(string fileName, SSConfiguration config)
{
var result = JsonConverter.Serialize(config);
//var jObject = JObject.Serialize(typeof(SSConfiguration), config);
var bson = result.ToBson();
var compare = (SSConfiguration)JsonConverter.FromBson(bson, typeof(SSConfiguration));
//SDHelpers.WriteFile(fileName, bson);
}
public static SSConfiguration GetConfiguration(string configPath)
{
SSConfiguration config;
string[] files = SDHelpers.GetFiles();
foreach(string item in files)
{
if(string.Compare(configPath, item) == 0)
{
using(FileStream fs = new FileStream(configPath, FileMode.Open))
{
config = (SSConfiguration)JsonConverter.DeserializeObject(fs, typeof(SSConfiguration), CreateInstance);
}
return config;
}
}
return null;
}
private static object CreateInstance(string path, string name, int length)
{
if (name == "intArray")
return new int[length];
else if (name == "stringArray")
return new string[length];
else if (name == "byteArray")
return new byte[length];
else
return null;
}
}
[Serializable]
public class Pump
{
public double SyringeVolume { get; set; } = 0.250;
public double SyringeSteps { get; set; } = 6000;
}
[Serializable]
public class Network
{
public byte[] IPAddress { get; set; } = { 172, 16, 0, 106 };
public int Port { get; set; } = 64000;
public byte[] SubnetMask { get; set; } = { 255, 255, 255, 0 };
public byte[] DefaultGateway { get; set; } = { 172, 16, 0, 255 };
public byte[] TlsEntropy { get; set; } = { 0, 1, 2, 3 };
public byte[] DNSAddress1 { get; set; } = { 8, 8, 8, 8 };
public byte[] DNSAddress2 { get; set; } = { 8, 8, 4, 4 };
public byte[] MACAddress { get; set; } = { 0x76, 0xCA, 0x75, 0x29, 0x57, 0x50 };
public bool IsDHCP { get; set; } = false;
public bool IsDynamicDNS { get; set; } = false;
}
}
Adding a bit, here is the string I get:
{{\r\n “pump2” : {\r\n “SyringeSteps” : {\r\n\r\n },\r\n “SyringeVolume” : {\r\n\r\n }\r\n },\r\n “network” : {\r\n "P}
It looks like it is getting truncated somehow.