This repository has been archived by the owner on Feb 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from zkxs/enum-handling
Fix for 1.9.0 Enum handling
- Loading branch information
Showing
6 changed files
with
77 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
|
||
namespace NeosModLoader.JsonConverters | ||
{ | ||
// serializes and deserializes enums as strings | ||
class EnumConverter : JsonConverter | ||
{ | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return objectType.IsEnum; | ||
} | ||
|
||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
// handle old behavior where enums were serialized as underlying type | ||
Type underlyingType = Enum.GetUnderlyingType(objectType); | ||
if (TryConvert(reader.Value, underlyingType, out object deserialized)) | ||
{ | ||
Logger.DebugInternal($"Deserializing a BaseX type: {objectType} from a {reader.Value.GetType()}"); | ||
return deserialized; | ||
} | ||
|
||
// handle new behavior where enums are serialized as strings | ||
if (reader.Value is string serialized) | ||
{ | ||
return Enum.Parse(objectType, serialized); | ||
} | ||
|
||
throw new ArgumentException($"Could not deserialize a BaseX type: {objectType} from a {reader.Value.GetType()}. Expected underlying type was {underlyingType}"); | ||
} | ||
|
||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
string serialized = Enum.GetName(value.GetType(), value); | ||
writer.WriteValue(serialized); | ||
} | ||
|
||
private bool TryConvert(object value, Type newType, out object converted) | ||
{ | ||
try | ||
{ | ||
converted = Convert.ChangeType(value, newType); | ||
return true; | ||
} | ||
catch | ||
{ | ||
converted = null; | ||
return false; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters