Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Net.Http;
using System.Security;
Expand Down Expand Up @@ -34,6 +35,13 @@ public class TestJsonCommand : PSCmdlet

#endregion

#region Json Document Option Constants

private const string IgnoreCommentsOption = "IgnoreComments";
private const string AllowTrailingCommasOption = "AllowTrailingCommas";

#endregion

#region Parameters

/// <summary>
Expand Down Expand Up @@ -97,12 +105,21 @@ public string LiteralPath
[ValidateNotNullOrEmpty]
public string SchemaFile { get; set; }

/// <summary>
/// Gets or sets JSON document options.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty]
[ValidateSet(IgnoreCommentsOption, AllowTrailingCommasOption)]
public string[] Options { get; set; } = Array.Empty<string>();

#endregion

#region Private Members

private bool _isLiteralPath = false;
private JsonSchema _jschema;
private JsonDocumentOptions _documentOptions;

#endregion

Expand Down Expand Up @@ -200,6 +217,14 @@ e is SecurityException
Exception exception = new(TestJsonCmdletStrings.InvalidJsonSchema, e);
ThrowTerminatingError(new ErrorRecord(exception, "InvalidJsonSchema", ErrorCategory.InvalidData, resolvedpath));
}

_documentOptions = new JsonDocumentOptions
{
CommentHandling = Options.Contains(IgnoreCommentsOption, StringComparer.OrdinalIgnoreCase)
? JsonCommentHandling.Skip
: JsonCommentHandling.Disallow,
AllowTrailingCommas = Options.Contains(AllowTrailingCommasOption, StringComparer.OrdinalIgnoreCase)
};
}

/// <summary>
Expand Down Expand Up @@ -235,7 +260,7 @@ protected override void ProcessRecord()
try
{

var parsedJson = JsonNode.Parse(jsonToParse);
var parsedJson = JsonNode.Parse(jsonToParse, nodeOptions: null, _documentOptions);

if (_jschema != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ Describe "Test-Json" -Tags "CI" {
}
'@

$jsonWithComments = @'
{
// A Json comment
"string": "test"
}
'@

$jsonWithTrailingComma = @'
{
"string": "test",
}
'@

$jsonWithCommentsAndTrailingComma = @'
{
// A Json comment
"string": "test",
}
'@

$validJsonPath = Join-Path -Path $TestDrive -ChildPath 'validJson.json'
$validLiteralJsonPath = Join-Path -Path $TestDrive -ChildPath "[valid]Json.json"
$invalidNodeInJsonPath = Join-Path -Path $TestDrive -ChildPath 'invalidNodeInJson.json'
Expand Down Expand Up @@ -309,4 +329,18 @@ Describe "Test-Json" -Tags "CI" {
Test-Json -Json $value -Schema $schema -ErrorAction SilentlyContinue
} | Should -Be $expected
}

It "Test-Json returns True with document options '<options>'" -TestCases @(
@{ Json = $jsonWithComments; Options = 'IgnoreComments' }
@{ Json = $jsonWithTrailingComma; Options = 'AllowTrailingCommas'}
@{ Json = $jsonWithCommentsAndTrailingComma; Options = 'IgnoreComments', 'AllowTrailingCommas'}
) {
param($Json, $Options)

# Without options should fail
($Json | Test-Json -ErrorAction SilentlyContinue) | Should -BeFalse

# With options should pass
($Json | Test-Json -Option $Options -ErrorAction SilentlyContinue) | Should -BeTrue
}
}