网站开发的问题有哪些,wordpress釆集插件,在建项目查询在哪里查,帮彩票网站做流量提升文章目录 1. 定义配置模型类2. 创建示例config.json文件3. 实现配置读取器4. 使用示例5. 扩展ModbusTcpHelper类6. 配置验证增强版7. 处理特殊需求 配置文件种类及其比较#xff1a;JSON配置文件的优势一、常见配置文件类型及示例1. JSON (JavaScript Object Notation)2. XML … 文章目录 1. 定义配置模型类2. 创建示例config.json文件3. 实现配置读取器4. 使用示例5. 扩展ModbusTcpHelper类6. 配置验证增强版7. 处理特殊需求 配置文件种类及其比较JSON配置文件的优势一、常见配置文件类型及示例1. JSON (JavaScript Object Notation)2. XML (eXtensible Markup Language)3. YAML (YAML Aint Markup Language)4. INI (Initialization File)5. 环境变量6. 二进制格式 二、JSON配置文件的优势1. 可读性强2. 数据结构丰富3. 跨平台兼容性好4. 工具生态完善5. 性能优异6. 现代开发标准7. 支持注释通过变通方式 三、各类型配置文件对比表四、JSON配置的典型应用场景1. 应用程序配置2. IoT设备配置3. 网络服务配置 五、JSON配置的最佳实践六、何时不选择JSON七、总结 下面我将展示如何从config.json文件读取配置并转换为强类型的Settings对象使用.NET 8和System.Text.Json。
1. 定义配置模型类
首先创建表示配置结构的模型类
public class ModbusSettings
{public string IpAddress { get; set; }public int Port { get; set; }public byte SlaveId { get; set; }public int TimeoutMs { get; set; } 5000;public ListRegisterMap RegisterMaps { get; set; }
}public class RegisterMap
{public string Name { get; set; }public ushort Address { get; set; }public DataType DataType { get; set; }public float ScalingFactor { get; set; } 1.0f;
}public enum DataType
{UInt16,Int16,UInt32,Int32,Float
}2. 创建示例config.json文件
{IpAddress: 192.168.1.202,Port: 4196,SlaveId: 40,TimeoutMs: 3000,RegisterMaps: [{Name: Temperature,Address: 22,DataType: Float,ScalingFactor: 0.1},{Name: Pressure,Address: 26,DataType: Float}]
}3. 实现配置读取器
using System.Text.Json;
using System.Text.Json.Serialization;public static class ConfigLoader
{private static readonly JsonSerializerOptions _options new(){PropertyNameCaseInsensitive true,Converters { new JsonStringEnumConverter() },AllowTrailingCommas true,ReadCommentHandling JsonCommentHandling.Skip};public static ModbusSettings LoadConfig(string filePath config.json){try{if (!File.Exists(filePath)){throw new FileNotFoundException($配置文件 {filePath} 不存在);}string json File.ReadAllText(filePath);var settings JsonSerializer.DeserializeModbusSettings(json, _options);if (settings null){throw new InvalidOperationException(配置文件内容为空或格式不正确);}// 验证必要配置if (string.IsNullOrWhiteSpace(settings.IpAddress)){throw new InvalidDataException(IP地址不能为空);}if (settings.Port 0 || settings.Port 65535){throw new InvalidDataException(端口号必须在1-65535范围内);}return settings;}catch (JsonException ex){throw new InvalidOperationException($配置文件解析失败: {ex.Message}, ex);}}// 可选保存配置的方法public static void SaveConfig(ModbusSettings settings, string filePath config.json){var options new JsonSerializerOptions{WriteIndented true,Encoder System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,Converters { new JsonStringEnumConverter() }};string json JsonSerializer.Serialize(settings, options);File.WriteAllText(filePath, json);}
}4. 使用示例
class Program
{static async Task Main(){try{// 加载配置var settings ConfigLoader.LoadConfig();Console.WriteLine($成功加载配置: {settings.IpAddress}:{settings.Port});// 使用配置初始化Modbus帮助类var modbusHelper new ModbusTcpHelper(settings.IpAddress, settings.Port);await modbusHelper.ConnectAsync(settings.TimeoutMs);// 读取配置中定义的寄存器foreach (var map in settings.RegisterMaps){try{// 根据数据类型读取数据object value map.DataType switch{DataType.Float await ReadFloatRegister(modbusHelper, settings.SlaveId, map),_ await ReadStandardRegister(modbusHelper, settings.SlaveId, map)};// 应用缩放因子if (value is float floatValue map.ScalingFactor ! 1.0f){value floatValue * map.ScalingFactor;}Console.WriteLine(${map.Name} ({map.Address}): {value});}catch (Exception ex){Console.WriteLine($读取 {map.Name} 失败: {ex.Message});}}}catch (Exception ex){Console.WriteLine($发生错误: {ex.Message});}}private static async Taskobject ReadFloatRegister(ModbusTcpHelper helper, byte slaveId, RegisterMap map){// 浮点数需要读取2个寄存器(4字节)float[] values await helper.ReadFloatRegistersAsync(slaveId, map.Address, 2);return values[0];}private static async Taskobject ReadStandardRegister(ModbusTcpHelper helper, byte slaveId, RegisterMap map){// 读取单个寄存器ushort[] values await helper.ReadRegistersAsync(slaveId, map.Address, 1);return map.DataType switch{DataType.UInt16 values[0],DataType.Int16 (short)values[0],_ throw new NotSupportedException($不支持的数据类型: {map.DataType})};}
}5. 扩展ModbusTcpHelper类
在之前的ModbusTcpHelper类中添加以下方法以支持更多数据类型
/// summary
/// 读取标准寄存器值
/// /summary
public async Taskushort[] ReadRegistersAsync(byte slaveId, ushort startAddress, ushort registerCount)
{byte[] request new byte[6];request[0] slaveId;request[1] 0x03; // 功能码: 读保持寄存器request[2] (byte)(startAddress 8);request[3] (byte)(startAddress 0xFF);request[4] (byte)(registerCount 8);request[5] (byte)(registerCount 0xFF);byte[] response await SendRequestAsync(request);// 提取寄存器数据(每个寄存器2字节)ushort[] registers new ushort[registerCount];for (int i 0; i registerCount; i){int offset 3 i * 2;registers[i] (ushort)((response[offset] 8) | response[offset 1]);}return registers;
}6. 配置验证增强版
可以添加更详细的配置验证
public class ModbusSettingsValidator
{public static void Validate(ModbusSettings settings){if (settings null)throw new ArgumentNullException(nameof(settings));if (string.IsNullOrWhiteSpace(settings.IpAddress))throw new InvalidDataException(IP地址不能为空);if (!IPAddress.TryParse(settings.IpAddress, out _))throw new InvalidDataException(IP地址格式不正确);if (settings.Port 1 || settings.Port 65535)throw new InvalidDataException(端口号必须在1-65535范围内);if (settings.SlaveId 1 || settings.SlaveId 247)throw new InvalidDataException(从站ID必须在1-247范围内);if (settings.TimeoutMs 100)throw new InvalidDataException(超时时间不能小于100ms);if (settings.RegisterMaps null || settings.RegisterMaps.Count 0)throw new InvalidDataException(至少需要配置一个寄存器映射);foreach (var map in settings.RegisterMaps){if (string.IsNullOrWhiteSpace(map.Name))throw new InvalidDataException(寄存器名称不能为空);if (map.Address 65535)throw new InvalidDataException($寄存器地址 {map.Name} 超出范围);if (map.ScalingFactor 0)throw new InvalidDataException($缩放因子 {map.Name} 不能为零);}}
}// 在ConfigLoader中使用
var settings JsonSerializer.DeserializeModbusSettings(json, _options);
ModbusSettingsValidator.Validate(settings);7. 处理特殊需求
如果需要支持更复杂的配置如
自定义字节序在RegisterMap中添加ByteOrder属性位操作支持读取线圈和离散输入批量读取优化配置中定义哪些寄存器可以批量读取
示例扩展配置
{BatchReads: [{Name: SensorGroup1,StartAddress: 22,Count: 4,MapsTo: [Temperature, Humidity, Pressure, Voltage]}]
}这种架构提供了灵活的配置方式同时保持了类型安全和良好的错误处理。 配置文件种类及其比较JSON配置文件的优势
一、常见配置文件类型及示例
1. JSON (JavaScript Object Notation)
示例
{appSettings: {name: 数据采集系统,version: 1.2.0,debugMode: false,sampleRate: 1000},modbus: {ip: 192.168.1.202,port: 502,timeout: 3000}
}2. XML (eXtensible Markup Language)
示例
configurationappSettingsname数据采集系统/nameversion1.2.0/versiondebugModefalse/debugModesampleRate1000/sampleRate/appSettingsmodbusip192.168.1.202/ipport502/porttimeout3000/timeout/modbus
/configuration3. YAML (YAML Ain’t Markup Language)
示例
appSettings:name: 数据采集系统version: 1.2.0debugMode: falsesampleRate: 1000modbus:ip: 192.168.1.202port: 502timeout: 30004. INI (Initialization File)
示例
[appSettings]
name数据采集系统
version1.2.0
debugModefalse
sampleRate1000[modbus]
ip192.168.1.202
port502
timeout30005. 环境变量
示例
APP_NAME数据采集系统
APP_VERSION1.2.0
MODBUS_IP192.168.1.202
MODBUS_PORT5026. 二进制格式
示例通常不可读如Windows注册表、Protocol Buffers等
二、JSON配置文件的优势
1. 可读性强
JSON采用键值对结构和缩进格式比XML更简洁
{user: {name: 张三,age: 30,active: true}
}对比XML
username张三/nameage30/ageactivetrue/active
/user2. 数据结构丰富
支持多种数据类型
对象嵌套结构数组字符串数字布尔值null
{devices: [{id: 1,type: sensor,registers: [40001, 40002, 40003]},{id: 2,type: actuator,enabled: true}]
}3. 跨平台兼容性好
所有现代编程语言都内置JSON支持Web应用天然支持移动端和嵌入式系统也能轻松处理
4. 工具生态完善
验证工具JSON Schema验证格式化工具VS Code等编辑器内置支持转换工具可轻松转换为其他格式在线工具各种JSON在线解析和美化工具
5. 性能优异
解析速度比XML快比YAML更高效体积通常比XML小30%-50%
6. 现代开发标准
REST API的标准数据格式NoSQL数据库如MongoDB使用类似JSON的BSON前端框架React/Vue等直接支持
7. 支持注释通过变通方式
虽然标准JSON不支持注释但可以通过以下方式实现
{//note: 这是设备配置,device: {ip: 192.168.1.100,//status: active|inactive,status: active}
}三、各类型配置文件对比表
特性JSONXMLYAMLINI环境变量二进制可读性★★★★★★★★☆☆★★★★★★★★☆☆★★☆☆☆☆☆☆☆☆数据结构★★★★★★★★★★★★★★★★★☆☆☆★☆☆☆☆★★★★★跨平台支持★★★★★★★★★★★★★★☆★★★☆☆★★★★★★★★☆☆工具生态★★★★★★★★★★★★★★☆★★★☆☆★★★☆☆★★☆☆☆解析性能★★★★★★★★☆☆★★★☆☆★★★★★★★★★★★★★★★适合场景通用复杂结构人工维护简单配置容器化高性能
四、JSON配置的典型应用场景
1. 应用程序配置
{logging: {level: debug,path: /var/log/app.log},database: {connectionString: Serverdb;Databaseappdb,timeout: 30}
}2. IoT设备配置
{deviceId: SN-12345,sensors: [{type: temperature,address: 40001,unit: °C},{type: humidity,address: 40003,scale: 0.1}]
}3. 网络服务配置
{endpoints: {api: https://api.example.com/v1,auth: https://auth.example.com},retryPolicy: {maxAttempts: 3,delay: 1000}
}五、JSON配置的最佳实践 使用有意义的键名 // 不好
t: 30// 好
temperature: 30合理组织层次结构 {modbus: {tcp: {ip: 192.168.1.100,port: 502},rtu: {port: COM3,baudRate: 9600}}
}添加配置版本控制 {configVersion: 1.1,settings: {// ...}
}为重要配置添加默认值 {timeout: 5000, // 默认5秒retries: 3 // 默认3次
}使用JSON Schema验证 {$schema: ./config-schema.json,// 实际配置内容
}六、何时不选择JSON
虽然JSON有很多优点但在以下情况可能需要考虑其他方案
需要注释考虑YAML极简配置考虑INI或环境变量高性能场景考虑二进制格式人工编辑频繁考虑YAML更友好的格式
七、总结
JSON作为配置文件格式具有以下核心优势
完美的平衡性在可读性、功能性和性能之间取得最佳平衡语言无关性几乎所有现代编程语言都原生支持层次化结构能清晰表达复杂配置关系扩展性强支持数组、嵌套对象等复杂数据结构工具链完善从编辑器支持到验证工具一应俱全
对于大多数应用场景特别是需要表达结构化数据、跨平台使用或与现代Web技术栈集成的场景JSON都是配置文件的最佳选择。