Skip to content

Encoding

Apq.Cfg provides intelligent encoding detection and handling for configuration files.

Automatic Detection

By default, Apq.Cfg automatically detects file encoding:

  1. BOM (Byte Order Mark) detection
  2. UTF.Unknown library detection
  3. Fallback to UTF-8

Specify Encoding

csharp
var cfg = new CfgBuilder()
    .AddJsonFile("config.json", level: 0, encoding: Encoding.UTF8)
    .Build();

Encoding Mapping

Configure encoding rules for specific files or patterns:

csharp
var cfg = new CfgBuilder()
    .ConfigureEncodingMapping(config =>
    {
        // Exact path
        config.AddReadMapping("/path/to/config.json", EncodingMappingType.ExactPath, Encoding.UTF8);

        // Wildcard pattern
        config.AddReadMapping("*.ps1", EncodingMappingType.Wildcard, Encoding.UTF8);

        // Regex pattern
        config.AddReadMapping(@"logs[/\\].*\.log$", EncodingMappingType.Regex, Encoding.Unicode);
    })
    .AddJsonFile("config.json", level: 0)
    .Build();

Priority Order

Match TypeDefault PriorityExample
ExactPath100/path/to/config.json
Wildcard0*.ps1
Regex0logs[/\\].*\.log$
Built-in PowerShell-100*.ps1, *.psm1

Common Encodings

EncodingUse Case
UTF-8Default, most common
UTF-8 with BOMWindows PowerShell scripts
UTF-16 LEWindows native
GB2312/GBKChinese legacy systems

Next Steps

Released under the MIT License