Files
XplorePlane/XP.Hardware.RaySource.Comet.Messages/MessageSerializer.cs
T
XplorePlane Developer 957157ae80 fix: 修复编译错误、BGA向导预览、算子重复插入及状态栏消息
## Compilation fixes
- Fix XAML namespace reference XplorePlane.Converters → XplorePlane.Helpers (EventLogView, StateDisplayView)
- Add missing using for ISampleTypeRepository / JsonSampleTypeRepository in App.xaml.cs
- Fix ILoggerService.Warn() calls using Exception as first arg instead of message string
- Serialize CncProgram to JSON when passing to MatrixLayout (expects string, not CncProgram object)
- Suppress CS8632 nullable annotation warnings project-wide

## BGA wizard fixes
- Fix preview not showing on step 3: force RefreshBgaPreview() when entering final step
- Add step validation in CanNext() to prevent skipping to preview with invalid inputs
- Notify NextCommand.CanExecuteChanged when inputs change
- Fix RunProgress runtime binding error: set Mode=OneWay for ProgressBar.Value

## Operator toolbox: prevent duplicate insertion
- Add static OperatorTarget property for direct dispatch to active pipeline
- PipelineEditorView registers as toolbox target on load (floating window only)
- CNC page sets initial target; PipelineEditorWindow resets to CNC pipeline on close
- '+' button inserts only to the active pipeline instead of broadcasting to all subscribers

## Pipeline editor status bar messages
- Add StatusBarMessageEvent / StatusBarMessagePayload to CommonEvents.cs
- PipelineEditorViewModel publishes status on all ops (add/remove/reorder/save/execute)
- MainViewModel subscribes and displays on main window bottom status bar
- Auto-clear after timeout (3s info, 5s error)

## docs
- Merge three architecture docs into consolidated XplorePlane-架构与结构说明.md
2026-07-24 15:59:16 +08:00

93 lines
3.2 KiB
C#

using System;
using Newtonsoft.Json;
using XP.Hardware.RaySource.Comet.Messages.Commands;
using XP.Hardware.RaySource.Comet.Messages.Responses;
namespace XP.Hardware.RaySource.Comet.Messages
{
/// <summary>
/// 消息序列化工具类
/// 封装 Newtonsoft.Json 序列化/反序列化逻辑
/// </summary>
public static class MessageSerializer
{
/// <summary>
/// JSON 序列化设置
/// TypeNameHandling.Auto:保留类型信息以支持多态反序列化
/// NullValueHandling.Ignore:忽略 null 值减少传输量
/// Formatting.None:单行输出配合行协议
/// </summary>
private static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All,
NullValueHandling = NullValueHandling.Ignore,
Formatting = Formatting.None
};
/// <summary>
/// 将对象序列化为 JSON 字符串
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="obj">要序列化的对象</param>
/// <returns>JSON 字符串</returns>
public static string Serialize<T>(T obj)
{
return JsonConvert.SerializeObject(obj, Settings);
}
/// <summary>
/// 将 JSON 字符串反序列化为指定类型的对象
/// </summary>
/// <typeparam name="T">目标类型</typeparam>
/// <param name="json">JSON 字符串</param>
/// <returns>反序列化后的对象,失败时返回 null</returns>
public static T Deserialize<T>(string json) where T : class
{
try
{
return JsonConvert.DeserializeObject<T>(json, Settings);
}
catch (JsonException)
{
return null;
}
}
/// <summary>
/// 将 JSON 字符串反序列化为命令对象
/// 根据类型信息还原为正确的命令子类实例
/// </summary>
/// <param name="json">JSON 字符串</param>
/// <returns>命令对象,失败时返回 null</returns>
public static RaySourceCommand DeserializeCommand(string json)
{
try
{
return JsonConvert.DeserializeObject<RaySourceCommand>(json, Settings);
}
catch (JsonException)
{
return null;
}
}
/// <summary>
/// 将 JSON 字符串反序列化为响应对象
/// 根据类型信息还原为正确的响应子类实例
/// </summary>
/// <param name="json">JSON 字符串</param>
/// <returns>响应对象,失败时返回 null</returns>
public static RaySourceResponse DeserializeResponse(string json)
{
try
{
return JsonConvert.DeserializeObject<RaySourceResponse>(json, Settings);
}
catch (JsonException)
{
return null;
}
}
}
}