public override void WriteJson(JsonWriter writer, TypedValue value, JsonSerializer serializer) { var valueToken = value.Value == null?JValue.CreateNull() : JToken.FromObject(value.Value, serializer); var o = new JObject(); o.AddTypeProperty(value.Type); if (valueToken.Type == JTokenType.Object) { o.Merge(valueToken, new JsonMergeSettings() { MergeArrayHandling = MergeArrayHandling.Union }); } else { o["Value"] = valueToken; } serializer.Serialize(writer, o); }
public void Handle(AppSettingsMessage message) { if (message.Action == SettingsAction.Saving) { if (!message.Settings.EnsureContainsObjects("OutputTarget") || !message.Settings.TryGetObject(out var settings, "OutputTarget")) { return; } settings[nameof(ContentVisible)] = ContentVisible; settings[nameof(ScanDelay)] = ScanDelay; settings[nameof(ScanInterval)] = ScanInterval; if (ActiveItem != null) { settings[nameof(ActiveItem)] = ActiveItem.Identifier; } settings[nameof(Items)] = JArray.FromObject(Items.Select(x => { var o = new JObject() { ["$index"] = x.InstanceIndex }; o.AddTypeProperty(x.GetType()); x.HandleSettings(o, message.Action); return(o); })); } else if (message.Action == SettingsAction.Loading) { if (!message.Settings.TryGetObject(out var settings, "OutputTarget")) { return; } if (settings.TryGetValue <bool>(nameof(ContentVisible), out var contentVisible)) { ContentVisible = contentVisible; } if (settings.TryGetValue <int>(nameof(ScanDelay), out var scanDelay)) { ScanDelay = scanDelay; } if (settings.TryGetValue <int>(nameof(ScanInterval), out var scanInterval)) { ScanInterval = scanInterval; } if (settings.TryGetValue(nameof(Items), out var itemsToken) && itemsToken is JArray items) { foreach (var item in items.OfType <JObject>()) { var type = item.GetTypeProperty(); var index = item["$index"].ToObject <int>(); item.Remove("$type"); var usedIndices = Items.Where(x => x.GetType() == type) .Select(x => x.InstanceIndex) .ToList(); if (usedIndices.Contains(index)) { Logger.Warn("Index {0} is already used for type {1}", index, type); continue; } var instance = _outputTargetFactory.CreateOutputTarget(type, index); if (instance == null) { continue; } instance.HandleSettings(item, message.Action); AddItem(instance); } } if (settings.TryGetValue <string>(nameof(ActiveItem), out var selectedItem)) { ChangeActiveItem(Items.FirstOrDefault(x => string.Equals(x.Identifier, selectedItem)) ?? Items.FirstOrDefault(), closePrevious: false); } } }