From 2576b24b7d569887bec363add9b9cdc2a71aa4bd Mon Sep 17 00:00:00 2001 From: kdw9502 Date: Fri, 18 Dec 2020 01:48:14 +0900 Subject: [PATCH] Fix byte,short underlying Enum Write issue --- src/LitJson/JsonMapper.cs | 20 +++++++++++++++----- test/JsonMapperTest.cs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/LitJson/JsonMapper.cs b/src/LitJson/JsonMapper.cs index 153cc70..99946cf 100644 --- a/src/LitJson/JsonMapper.cs +++ b/src/LitJson/JsonMapper.cs @@ -405,7 +405,7 @@ private static object ReadValue (Type inst_type, JsonReader reader) list = new ArrayList (); elem_type = inst_type.GetElementType (); } - + list.Clear(); while (true) { @@ -829,10 +829,20 @@ private static void RegisterBaseImporters () if (obj is Enum) { Type e_type = Enum.GetUnderlyingType (obj_type); - if (e_type == typeof (long) - || e_type == typeof (uint) - || e_type == typeof (ulong)) + if (e_type == typeof (long)) + writer.Write ((long) obj); + else if (e_type == typeof (uint)) + writer.Write ((uint) obj); + else if (e_type == typeof (ulong)) writer.Write ((ulong) obj); + else if (e_type == typeof(ushort)) + writer.Write ((ushort)obj); + else if (e_type == typeof(short)) + writer.Write ((short)obj); + else if (e_type == typeof(byte)) + writer.Write ((byte)obj); + else if (e_type == typeof(sbyte)) + writer.Write ((sbyte)obj); else writer.Write ((int) obj); @@ -920,7 +930,7 @@ public static T ToObject (string json) return (T) ReadValue (typeof (T), reader); } - + public static object ToObject(string json, Type ConvertType ) { JsonReader reader = new JsonReader(json); diff --git a/test/JsonMapperTest.cs b/test/JsonMapperTest.cs index 63ee5c3..7b761e4 100644 --- a/test/JsonMapperTest.cs +++ b/test/JsonMapperTest.cs @@ -1099,5 +1099,40 @@ public void EmptyArrayInJsonDataTest() string expectedJson = "{\"array\":[],\"name\":\"testName\"}"; Assert.AreEqual(toJsonResult, expectedJson); } + public enum UshortEnum : ushort + { + Test = 0, + } + public enum ShortEnum : short + { + Test = 0, + } + public enum ByteEnum : byte + { + Test = 0, + } + + public enum SbyteEnum : sbyte + { + Test = 0, + } + + public class EnumWrapper + { + public UshortEnum ushortEnum; + public ShortEnum shortEnum; + public ByteEnum byteEnum; + public SbyteEnum sbyteEnum; + } + + [Test] + public void shortAndByteBasedEnumToJsonTest() + { + var enumWrapper = new EnumWrapper(); + Assert.DoesNotThrow(() => + { + var json = JsonMapper.ToJson(enumWrapper); + }); + } } }