using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public List<Event> events { get; set; } = new();
public int id { get; set; }
public List<ActionHolder> actions { get; set; } = new();
[JsonConverter(typeof(ActionHolderConverter))]
public class ActionHolder
public int id => (int)(values?.id ?? ActionValueType.None);
public ActionValueBase values { get; set; }
public enum ActionValueType
public abstract class ActionValueBase
[JsonIgnore] public abstract ActionValueType id { get; }
public int obj_id { get; set; }
public class OpenURLActionValue : ActionValueBase
public override ActionValueType id => ActionValueType.OpenURL;
public string url { get; set; }
public class ScaleActionValue : ActionValueBase
public override ActionValueType id => ActionValueType.Scale;
public float scale { get; set; } = 1;
class ActionHolderConverter : JsonConverter<ActionHolder>
public override ActionHolder ReadJson(JsonReader reader, Type objectType, ActionHolder existingValue, bool hasExistingValue, JsonSerializer serializer)
JObject joAction = JObject.Load(reader);
var values = (ActionValueType)joAction[nameof(ActionHolder.id)].Value<int>() switch
ActionValueType.Scale => joAction[nameof(ActionHolder.values)]?.ToObject<ScaleActionValue>(serializer),
ActionValueType.OpenURL => joAction[nameof(ActionHolder.values)]?.ToObject<OpenURLActionValue>(serializer),
ActionValueType.None => (ActionValueBase)null,
var t => throw new JsonSerializationException($"Unknown ActionValueTypes {t}"),
var holder = existingValue ?? new ActionHolder();
public override bool CanWrite => false;
public override void WriteJson(JsonWriter writer, ActionHolder value, JsonSerializer serializer) => throw new NotImplementedException();
public static void Test()
var settings = new JsonSerializerSettings
ContractResolver = new CamelCasePropertyNamesContractResolver(),
var root = JsonConvert.DeserializeObject<Root>(json, settings);
var json2 = JsonConvert.SerializeObject(root, Formatting.Indented, settings);
Console.WriteLine("Re-serialized {0}:", root);
Console.WriteLine(json2);
Assert.That(JToken.DeepEquals(JToken.Parse(json), JToken.Parse(json2)));
static string GetJson() =>
"url":"https://www.myurl.com/"
public static void Main()
Console.WriteLine("Environment version: {0} ({1}), {2}", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , Environment.Version, Environment.OSVersion);
Console.WriteLine("{0} version: {1}", typeof(JsonSerializer).Namespace, typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");