Skip to content

Commit

Permalink
Replace Newtonsoft.Json.Schema with NJsonSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
erri120 committed Jul 7, 2024
1 parent a7a9a74 commit 052f252
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 22 deletions.
2 changes: 1 addition & 1 deletion build/deploy-local-smapi.targets
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ This assumes `find-game-folder.targets` has already been imported and validated.
<Copy SourceFiles="$(TargetDir)\SMAPI.config.json" DestinationFiles="$(GamePath)\smapi-internal\config.json" />
<Copy SourceFiles="$(TargetDir)\SMAPI.metadata.json" DestinationFiles="$(GamePath)\smapi-internal\metadata.json" />
<Copy SourceFiles="$(TargetDir)\Newtonsoft.Json.dll" DestinationFolder="$(GamePath)\smapi-internal" />
<Copy SourceFiles="$(TargetDir)\Newtonsoft.Json.Schema.dll" DestinationFolder="$(GamePath)\smapi-internal" />
<Copy SourceFiles="$(TargetDir)\NJsonSchema.dll" DestinationFolder="$(GamePath)\smapi-internal" />
<Copy SourceFiles="$(TargetDir)\TMXTile.dll" DestinationFolder="$(GamePath)\smapi-internal" />
<Copy SourceFiles="$(TargetDir)\Pintail.dll" DestinationFolder="$(GamePath)\smapi-internal" />
<Copy SourceFiles="@(TranslationFiles)" DestinationFolder="$(GamePath)\smapi-internal\i18n" />
Expand Down
2 changes: 1 addition & 1 deletion src/SMAPI.Toolkit/SMAPI.Toolkit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<ItemGroup>
<PackageReference Include="HtmlAgilityPack" Version="1.11.52" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Newtonsoft.Json.Schema" Version="3.0.15" />
<PackageReference Include="NJsonSchema" Version="11.0.1" />
<PackageReference Include="Pathoschild.Http.FluentClient" Version="4.3.0" />
<PackageReference Include="System.Management" Version="5.0.0" Condition="'$(OS)' == 'Windows_NT'" />
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" Condition="'$(OS)' == 'Windows_NT'" />
Expand Down
24 changes: 4 additions & 20 deletions src/SMAPI.Toolkit/Serialization/JsonHelper.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,16 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Schema;
using Newtonsoft.Json.Schema.Generation;
using NJsonSchema;
using StardewModdingAPI.Toolkit.Serialization.Converters;

namespace StardewModdingAPI.Toolkit.Serialization
{
/// <summary>Encapsulates SMAPI's JSON file parsing.</summary>
public class JsonHelper
{
/*********
** Fields
*********/
/// <summary>The JSON schema generator to use when creating a schema file.</summary>
private readonly JSchemaGenerator SchemaGenerator = new();

/// <summary>The JSON settings to use when creating a schema file.</summary>
private readonly JSchemaWriterSettings SchemaWriterSettings = new()
{
Version = SchemaVersion.Draft2019_09,
ReferenceHandling = JSchemaWriterReferenceHandling.Never
};


/*********
** Accessors
*********/
Expand Down Expand Up @@ -61,7 +45,7 @@ public static JsonSerializerSettings CreateDefaultSettings()
/// <exception cref="JsonReaderException">The file contains invalid JSON.</exception>
public bool ReadJsonFileIfExists<TModel>(string fullPath,
#if NET6_0_OR_GREATER
[NotNullWhen(true)]
[System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
out TModel? result
)
Expand Down Expand Up @@ -142,8 +126,8 @@ public void WriteJsonSchemaFile<TModel>(string fullPath)
Directory.CreateDirectory(dir);

// write file
JSchema schema = this.SchemaGenerator.Generate(typeof(TModel));
string json = schema.ToString(this.SchemaWriterSettings);
JsonSchema schema = JsonSchema.FromType<TModel>();
string json = schema.ToJson();
File.WriteAllText(fullPath, json);
}

Expand Down

0 comments on commit 052f252

Please sign in to comment.