Skip to content

Commit 2711dc9

Browse files
committed
fix(string): 修复JSONC转换中$schema和多行注释处理问题
1 parent 5e37d37 commit 2711dc9

2 files changed

Lines changed: 50 additions & 5 deletions

File tree

psutils/modules/string.psm1

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,34 @@ function Convert-JsoncToJson {
9898
# 读取JSONC文件内容
9999
$jsoncContent = Get-Content -Path $Path -Raw
100100

101-
# 移除单行注释 (//...)
102-
$jsonContent = $jsoncContent -replace '(?m)\s*//.*$', ''
103-
104-
# 移除多行注释 (/*...*/)
101+
# 初始化$jsonContent变量
102+
$jsonContent = $jsoncContent
103+
104+
# 移除键名包含$schema的整行(最终修正版)
105+
$jsonContent = $jsonContent -replace '(?m)^\s*"[^"]*?\$schema[^"]*"\s*:\s*".*?"[,\r\n]*\s*$', ''
106+
Write-Debug "移除`$schema后:$jsonContent"
107+
# 移除单行注释 (//...) 但排除字符串中的//
108+
$jsonContent = $jsonContent -replace '(?m)(?<!"[^"]*)//.*$', ''
109+
Write-Debug "移除单行注释后:$jsonContent"
110+
111+
# 移除行尾注释 (/*...*/)
112+
# TODO
113+
$jsonContent = $jsonContent -replace '(?m)/\*.*?\*/\s*$', ''
114+
Write-Debug "移除行尾注释后:$jsonContent"
115+
116+
# 移除多行注释 (/*...*/)
105117
$jsonContent = $jsonContent -replace '(?s)/\*.*?\*/', ''
106-
118+
Write-Debug "移除多行注释后:$jsonContent"
107119
# 移除尾随逗号
108120
$jsonContent = $jsonContent -replace ',\s*([}\]])', '$1'
121+
Write-Debug "移除尾随逗号后:$jsonContent"
109122

110123
# 转换内容为JSON对象以验证有效性
111124
if ( -not (Test-Json -Json $jsonContent -ErrorAction SilentlyContinue)) {
112125
Write-Debug '目前7.5.0版本,Test-Json无法处理包含$schema的json'
126+
Write-Debug '暂未实现移除行尾注释的功能'
113127
Write-Error "转换失败: $_"
128+
return
114129
}
115130

116131
# 输出结果

psutils/tests/string.Tests.ps1

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ Describe "Convert-JsoncToJson 函数测试" {
4242
"@
4343
$testJsoncPath = "$TestDrive\test.jsonc"
4444
$testJsonc | Out-File -FilePath $testJsoncPath -Encoding utf8
45+
46+
# 新增带$schema的测试用例
47+
$testJsoncWithSchema = @"
48+
{
49+
"`$schema": "http://json-schema.org/draft-07/schema#",
50+
// 带schema的配置
51+
"name": "schema_test",
52+
/**
53+
* 测试多行需求**/
54+
"config": {
55+
"url": "http://example.com", // URL注释
56+
"path": "some//path" // 包含//的字符串
57+
}
58+
}
59+
"@
60+
$testJsoncWithSchemaPath = "$TestDrive\test_with_schema.jsonc"
61+
$testJsoncWithSchema | Out-File -FilePath $testJsoncWithSchemaPath -Encoding utf8
4562
}
4663

4764
It "成功转换JSONC到JSON" {
@@ -73,4 +90,17 @@ Describe "Convert-JsoncToJson 函数测试" {
7390
Test-Path $outputPath | Should -Be $true
7491
Get-Content $outputPath -Raw | Should -Not -Match "//"
7592
}
93+
94+
It "正确处理包含$schema的JSONC文件" {
95+
$result = Convert-JsoncToJson -Path $testJsoncWithSchemaPath -Debug
96+
$result | Should -Not -BeNullOrEmpty
97+
$result | ConvertFrom-Json | % { $_.name } | Should -Be "schema_test"
98+
$result -match '\$schema' | Should -Be $false
99+
}
100+
101+
It "保留字符串中的//不当作注释" {
102+
$result = Convert-JsoncToJson -Path $testJsoncWithSchemaPath
103+
$jsonObj = $result | ConvertFrom-Json
104+
$jsonObj.config.path | Should -Be "some//path"
105+
}
76106
}

0 commit comments

Comments
 (0)