Skip to content

Commit

Permalink
Using Manatee.Json (dotnet#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros committed Jul 14, 2020
1 parent f384da9 commit 196a052
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 20 deletions.
2 changes: 1 addition & 1 deletion eng/Signing.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<FileSignInfo Include="CsvHelper.dll" CertificateName="3PartySHA2" />
<FileSignInfo Include="Fluid.dll" CertificateName="3PartySHA2" />
<FileSignInfo Include="Irony.dll" CertificateName="3PartySHA2" />
<FileSignInfo Include="Manatee.Json.dll" CertificateName="3PartySHA2" />
<FileSignInfo Include="Newtonsoft.Json.dll" CertificateName="3PartySHA2" />
<FileSignInfo Include="Newtonsoft.Json.Schema.dll" CertificateName="3PartySHA2" />
<FileSignInfo Include="YamlDotNet.dll" CertificateName="3PartySHA2" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<PackageReference Include="CsvHelper" Version="12.1.2" />
<PackageReference Include="Fluid.Core" Version="1.0.0-beta-9660" />
<PackageReference Include="YamlDotNet" Version="8.1.2" />
<PackageReference Include="Newtonsoft.Json.Schema" Version="3.0.13" />
<PackageReference Include="Manatee.Json" Version="13.0.1" />
<PackageReference Include="NuGet.Versioning" Version="5.6.0" />
</ItemGroup>

Expand Down
32 changes: 14 additions & 18 deletions src/Microsoft.Crank.Controller/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
using McMaster.Extensions.CommandLineUtils;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using Newtonsoft.Json.Serialization;
using NuGet.Versioning;
using YamlDotNet.Serialization;
using System.Reflection;
using System.Text;
using Manatee.Json.Schema;
using Manatee.Json;

namespace Microsoft.Crank.Controller
{
Expand Down Expand Up @@ -1180,31 +1181,26 @@ public static async Task<JObject> LoadConfigurationAsync(string configurationFil
var json = serializer.Serialize(yamlObject);
// Format json in case the schema validation fails and we need to render error line numbers
localconfiguration = JObject.Parse(json);
localconfiguration.AddFirst(new JProperty("$schema", "https://raw.githubusercontent.com/aspnet/Benchmarks/master/src/BenchmarksDriver2/benchmarks.schema.json"));
json = localconfiguration.ToString(Formatting.Indented);
localconfiguration = JObject.Parse(json);

var schemaJson = File.ReadAllText(Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location), "benchmarks.schema.json"));
var schema = JSchema.Parse(schemaJson);
bool valid = localconfiguration.IsValid(schema, out IList<ValidationError> errorMessages);
var schema = new Manatee.Json.Serialization.JsonSerializer().Deserialize<JsonSchema>(JsonValue.Parse(schemaJson));

var jsonToValidate = JsonValue.Parse(json);
var validationResults = schema.Validate(jsonToValidate, new JsonSchemaOptions { OutputFormat = SchemaValidationOutputFormat.Detailed });

if (!valid)
if (!validationResults.IsValid)
{
var validationFilename = Path.Combine(Path.GetTempPath(), "crank-debug.json");
File.WriteAllText(validationFilename, json);
// Create a json debug file with the schema
localconfiguration.AddFirst(new JProperty("$schema", "https://raw.githubusercontent.com/aspnet/Benchmarks/master/src/BenchmarksDriver2/benchmarks.schema.json"));

var lines = json.Split(new [] { '\n', '\r'}, StringSplitOptions.RemoveEmptyEntries);
var debugFilename = Path.Combine(Path.GetTempPath(), "crank-debug.json");
File.WriteAllText(debugFilename, localconfiguration.ToString(Formatting.Indented));

var errorBuilder = new StringBuilder();

errorBuilder.AppendLine($"Invalid configuration file '{configurationFilenameOrUrl}'");
errorBuilder.AppendLine($"Debug file created at '{validationFilename}");

foreach (var error in errorMessages)
{
errorBuilder.AppendLine($"at [{error.LineNumber}, {error.LinePosition}]: {error.Message}");
errorBuilder.AppendLine($" {lines[error.LineNumber - 1]}");
}
errorBuilder.AppendLine($"Invalid configuration file '{configurationFilenameOrUrl}' at '{validationResults.InstanceLocation}'");
errorBuilder.AppendLine($"{validationResults.ErrorMessage}");
errorBuilder.AppendLine($"Debug file created at '{debugFilename}'");

throw new ControllerException(errorBuilder.ToString());
}
Expand Down
48 changes: 48 additions & 0 deletions src/NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -629,3 +629,51 @@ SOFTWARE.
-------------------------------------------------------------------

-------------------------------------------------------------------

Manatee.Json 13.0.2 - MIT

Copyright (c) 2016 Little Crab Solutions (Greg Dennis)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

-------------------------------------------------------------------

-------------------------------------------------------------------

NuGet.Client 5.6.0 - Apache 2.0

Copyright (c) .NET Foundation and Contributors.

All rights reserved.


Licensed under the Apache License, Version 2.0 (the "License"); you may not use
these files except in compliance with the License. You may obtain a copy of the
License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.

-------------------------------------------------------------------

-------------------------------------------------------------------

0 comments on commit 196a052

Please sign in to comment.