Skip to content

Commit 43291ff

Browse files
committed
Merge branch 'release/0.17.0'
2 parents 4c9ad30 + 62a43f7 commit 43291ff

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

src/LitJson/JsonMapper.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ private static object ReadValue (Type inst_type, JsonReader reader)
405405
list = new ArrayList ();
406406
elem_type = inst_type.GetElementType ();
407407
}
408-
408+
409409
list.Clear();
410410

411411
while (true) {
@@ -829,10 +829,20 @@ private static void WriteValue (object obj, JsonWriter writer,
829829
if (obj is Enum) {
830830
Type e_type = Enum.GetUnderlyingType (obj_type);
831831

832-
if (e_type == typeof (long)
833-
|| e_type == typeof (uint)
834-
|| e_type == typeof (ulong))
832+
if (e_type == typeof (long))
833+
writer.Write ((long) obj);
834+
else if (e_type == typeof (uint))
835+
writer.Write ((uint) obj);
836+
else if (e_type == typeof (ulong))
835837
writer.Write ((ulong) obj);
838+
else if (e_type == typeof(ushort))
839+
writer.Write ((ushort)obj);
840+
else if (e_type == typeof(short))
841+
writer.Write ((short)obj);
842+
else if (e_type == typeof(byte))
843+
writer.Write ((byte)obj);
844+
else if (e_type == typeof(sbyte))
845+
writer.Write ((sbyte)obj);
836846
else
837847
writer.Write ((int) obj);
838848

@@ -920,7 +930,7 @@ public static T ToObject<T> (string json)
920930

921931
return (T) ReadValue (typeof (T), reader);
922932
}
923-
933+
924934
public static object ToObject(string json, Type ConvertType )
925935
{
926936
JsonReader reader = new JsonReader(json);

test/JsonMapperTest.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,5 +1099,40 @@ public void EmptyArrayInJsonDataTest()
10991099
string expectedJson = "{\"array\":[],\"name\":\"testName\"}";
11001100
Assert.AreEqual(toJsonResult, expectedJson);
11011101
}
1102+
public enum UshortEnum : ushort
1103+
{
1104+
Test = 0,
1105+
}
1106+
public enum ShortEnum : short
1107+
{
1108+
Test = 0,
1109+
}
1110+
public enum ByteEnum : byte
1111+
{
1112+
Test = 0,
1113+
}
1114+
1115+
public enum SbyteEnum : sbyte
1116+
{
1117+
Test = 0,
1118+
}
1119+
1120+
public class EnumWrapper
1121+
{
1122+
public UshortEnum ushortEnum;
1123+
public ShortEnum shortEnum;
1124+
public ByteEnum byteEnum;
1125+
public SbyteEnum sbyteEnum;
1126+
}
1127+
1128+
[Test]
1129+
public void shortAndByteBasedEnumToJsonTest()
1130+
{
1131+
var enumWrapper = new EnumWrapper();
1132+
Assert.DoesNotThrow(() =>
1133+
{
1134+
var json = JsonMapper.ToJson(enumWrapper);
1135+
});
1136+
}
11021137
}
11031138
}

0 commit comments

Comments
 (0)