Add csharp JSON deserializer example

https://github.com/Hamlib/Hamlib/issues/695
pull/712/head
Mike Black W9MDB 2021-05-20 17:29:21 -05:00
rodzic 3a2531ad68
commit 8b22a5a9a8
4 zmienionych plików z 154 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,22 @@
Works on Windows and Linux
Requires you get dotnet installed of course
For Wiundows install Visual Studio or such to get dotnet
On Ubuntu 20.04 it was this
apt install snap
apt install mono-complete
snap install dotnet-sdk --classic --channel=5.0
snap alias dotnet-sdk.dotnet dotnet
snap install dotnet-runtime-50 --classic
snap alias dotnet-runtime-50.dotnet dotnet
export DOTNET_ROOT=/snap/dotnet-sdk/current
Once dotnet is OK
dotnet build
You should then be able to run
./bin/Debug/net5.0/multicast

Wyświetl plik

@ -0,0 +1,68 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.IO;
namespace HamlibMultiCast
{
public class HamlibMulticastClient
{
public class VFO
{
public string Name;
public ulong Freq;
public string Mode;
public int Width;
public bool RX;
public bool TX;
}
public class SpectrumClass
{
public int Length;
public string Data;
public string Type;
public int MinLevel;
public int MaxLevel;
public int MinStrength;
public int MaxStrength;
public double CenterFreq;
public int Span;
public double LowFreq;
public double HighFreq;
}
public string ID;
public List<VFO> VFOs { get; set; }
public bool Split;
public bool SatMode;
public string Rig;
public string App;
public string Version;
public UInt32 Seq;
public string CRC;
public SpectrumClass Spectrum { get; set; }
}
class Program
{
static int Main(string[] args)
{
Console.WriteLine("HamlibMultiCast Test Json Deserialize");
ITraceWriter traceWriter = new MemoryTraceWriter();
try
{
string json = File.ReadAllText("test.json");
var client = JsonConvert.DeserializeObject<HamlibMulticastClient>(json, new JsonSerializerSettings { TraceWriter = traceWriter, Converters = { new JavaScriptDateTimeConverter() } });
Console.WriteLine(traceWriter);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return 1;
}
return 0;
}
}
}

Wyświetl plik

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
</Project>

Wyświetl plik

@ -0,0 +1,52 @@
{
"__comment1__": "customizable rig identification -- will allow multiple rigs to be on the multicast",
"ID": "Rig#1",
"VFOs": [
{
"Name": "VFOA",
"Freq": 14074000,
"Mode": "USB",
"Width": 5000,
"RX": true,
"TX": false
},
{
"Name": "VFOB",
"Freq": 14076000,
"Mode": "USB",
"Width": 5000,
"RX": false,
"TX": false
}],
"__comment_spectrum__": "Rigs that have spectrum output may include this data",
"Spectrum": {
"Length": 475,
"__comment_spectrum_data__": "2-char hex bytes so data len=2*Length",
"Data": "00AAFF75BD2AAA...",
"Type": "FIXED|CENTER",
"MinLevel": 0,
"MaxLevel": 140,
"MinStrength": -100,
"MaxStrength": 0,
"__comment_spectrum_center__": "If Type=CENTER, the following fields will be present:",
"CenterFreq": 14267000,
"Span": 25000,
"__comment_spectrum_fixed__": "If SpectrumType=FIXED, the following fields will be present:",
"LowFreq": 14000000,
"HighFreq": 14250000
},
"Split": true,
"SatMode": false,
"Rig": "Dummy",
"App": "Hamlib",
"__comment_version__": "protocol version date YYYYMMDD",
"Version": "20210520",
"__comment_seq__": "Seq is 1-up sequence number 32-bit -- wraps around to 1 from 2^32-1",
"Seq": 1,
"__comment_crc__": "32-bit CRC of entire JSON record replacing the CRC value with 0x00000000",
"CRC": "0x00000000"
}