Skip to content

Commit

Permalink
Support comments in recipes (#15386)
Browse files Browse the repository at this point in the history
Co-authored-by: Sébastien Ros <sebastienros@gmail.com>
  • Loading branch information
MikeAlhayek and sebastienros authored Feb 21, 2024
1 parent 3bd5ae8 commit fe2ba6e
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.IO;
using System.IO.Compression;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
Expand Down Expand Up @@ -129,7 +130,7 @@ public async Task<IActionResult> Json(ImportJsonViewModel model)
return Forbid();
}

if (!model.Json.IsJson())
if (!model.Json.IsJson(JOptions.Document))
{
ModelState.AddModelError(nameof(model.Json), S["The recipe is written in an incorrect json format."]);
}
Expand Down
4 changes: 2 additions & 2 deletions src/OrchardCore/OrchardCore.Abstractions/Json/JOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ static JOptions()

Document = new JsonDocumentOptions
{
CommentHandling = Default.ReadCommentHandling,
AllowTrailingCommas = Default.AllowTrailingCommas,
CommentHandling = JsonCommentHandling.Skip,
AllowTrailingCommas = true,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,10 @@ private JsonConfigurationParser() { }

private async Task<IDictionary<string, string?>> ParseStreamAsync(Stream input)
{
var jsonDocumentOptions = new JsonDocumentOptions
{
CommentHandling = JsonCommentHandling.Skip,
AllowTrailingCommas = true,
};

try
{
using (var doc = await JsonDocument.ParseAsync(input, jsonDocumentOptions))
// Use JOptions.Document to allow comments and trailing commas
using (var doc = await JsonDocument.ParseAsync(input, JOptions.Document))
{
if (doc.RootElement.ValueKind != JsonValueKind.Object)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Globalization;
using System.Net;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
using Cysharp.Text;
Expand Down Expand Up @@ -501,11 +502,11 @@ public static string ToPascalCase(this string attribute, char upperAfterDelimite
/// <summary>
/// Tests if a string is valid json.
/// </summary>
public static bool IsJson(this string json)
public static bool IsJson(this string json, JsonDocumentOptions jsonDocumentOptions = default)
{
try
{
JsonNode.Parse(json);
JsonNode.Parse(json, null, jsonDocumentOptions);
return true;
}
catch
Expand Down

0 comments on commit fe2ba6e

Please sign in to comment.