Skip to content

Commit

Permalink
Add and implement IsJson() string extension method (#6614)
Browse files Browse the repository at this point in the history
  • Loading branch information
deanmarcussen authored Jul 9, 2020
1 parent 4c5c843 commit 1ee808f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Localization;
using Newtonsoft.Json.Linq;
using OrchardCore.ContentFields.Fields;
using OrchardCore.ContentFields.ViewModels;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentTypes.Editors;
using OrchardCore.DisplayManagement.ModelBinding;
using OrchardCore.DisplayManagement.Views;
using OrchardCore.Mvc.Utilities;

namespace OrchardCore.ContentFields.Settings
{
Expand Down Expand Up @@ -39,20 +40,17 @@ public override async Task<IDisplayResult> UpdateAsync(ContentPartFieldDefinitio

await context.Updater.TryUpdateModelAsync(model, Prefix);

settings.InsertMediaWithUrl = model.InsertMediaWithUrl;

try
if (!model.Options.IsJson())
{
settings.Options = model.Options;
JObject.Parse(settings.Options);
context.Updater.ModelState.AddModelError(Prefix + '.' + nameof(TrumbowygSettingsViewModel.Options), S["The options are written in an incorrect format."]);
}
catch
else
{
context.Updater.ModelState.AddModelError(Prefix, S["The options are written in an incorrect format."]);
return Edit(partFieldDefinition);
}
settings.InsertMediaWithUrl = model.InsertMediaWithUrl;
settings.Options = model.Options;

context.Builder.WithSettings(settings);
context.Builder.WithSettings(settings);
}
}

return Edit(partFieldDefinition);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Localization;
using Newtonsoft.Json.Linq;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentTypes.Editors;
using OrchardCore.DisplayManagement.ModelBinding;
using OrchardCore.DisplayManagement.Views;
using OrchardCore.Html.Models;
using OrchardCore.Html.ViewModels;
using OrchardCore.Mvc.Utilities;

namespace OrchardCore.Html.Settings
{
Expand Down Expand Up @@ -51,20 +51,16 @@ public override async Task<IDisplayResult> UpdateAsync(ContentTypePartDefinition

await context.Updater.TryUpdateModelAsync(model, Prefix);

settings.InsertMediaWithUrl = model.InsertMediaWithUrl;

try
if (!model.Options.IsJson())
{
settings.Options = model.Options;
JObject.Parse(settings.Options);
context.Updater.ModelState.AddModelError(Prefix + "." + nameof(TrumbowygSettingsViewModel.Options), S["The options are written in an incorrect format."]);
}
catch
else
{
context.Updater.ModelState.AddModelError(Prefix, S["The options are written in an incorrect format."]);
return Edit(contentTypePartDefinition, context.Updater);
settings.InsertMediaWithUrl = model.InsertMediaWithUrl;
settings.Options = model.Options;
context.Builder.WithSettings(settings);
}

context.Builder.WithSettings(settings);
}

return Edit(contentTypePartDefinition, context.Updater);
Expand Down
17 changes: 17 additions & 0 deletions src/OrchardCore/OrchardCore.Mvc.Core/Utilities/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.Extensions.Localization;
using Newtonsoft.Json.Linq;

namespace OrchardCore.Mvc.Utilities
{
Expand Down Expand Up @@ -473,5 +474,21 @@ public static string ToPascalCase(this string attribute, char upperAfterDelimite

return result.ToString();
}

/// <summary>
/// Tests if a string is valid json.
/// </summary>
public static bool IsJson(this string json)
{
try
{
JToken.Parse(json);
return true;
}
catch
{
return false;
}
}
}
}

0 comments on commit 1ee808f

Please sign in to comment.