Skip to content

Commit 815de91

Browse files
committed
Update to version 1.4.4, Fixed null reference issues. Details in release note
Signed-off-by: TJ Bakre <olamide.bakre@gmail.com>
1 parent 070bc0d commit 815de91

File tree

15 files changed

+78
-57
lines changed

15 files changed

+78
-57
lines changed

NetJSON.Net6_0.Tests/NetJSONTest.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44

55
namespace NetJSON.Net6_0.Tests
66
{
7+
public class Person
8+
{
9+
public int Id { get; set; }
10+
11+
public List<Address> Addresses { get; set; } // basically any class type
12+
}
13+
public class Address
14+
{
15+
public int Id { get; set; } = 1;
16+
}
17+
718
public class NetJSONTest
819
{
920
[Fact]
@@ -13,5 +24,28 @@ public void Test1()
1324
var result = NetJSON.Deserialize<Dictionary<string, Dictionary<string, string>>>(jsonString);
1425
Assert.True(result.ContainsKey("All") && result["All"].ContainsKey("ym") && result["All"]["ym"].Equals("ss"));
1526
}
27+
28+
[Fact]
29+
public void TestNullValueInArrayShouldBeSkipped()
30+
{
31+
var p = new Person()
32+
{
33+
Id = 1,
34+
Addresses = new List<Address> { new Address(), null, new Address() }
35+
};
36+
37+
var json = NetJSON.Serialize(p);
38+
var p2 = NetJSON.Deserialize<Person>(json);
39+
Assert.True(p2.Addresses.Count == 3);
40+
}
41+
42+
[Fact]
43+
public void TestForNullShouldNotThrowInvalidJson()
44+
{
45+
Person nullModel = null;
46+
var json = NetJSON.Serialize(nullModel, NetJSONSettings.CurrentSettings);
47+
var result = NetJSON.Deserialize<Person>(json);
48+
Assert.Null(result);
49+
}
1650
}
1751
}

NetJSON/NetJSON.Internals.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ internal unsafe static void ThrowIfInvalidJSON(string json, char chr)
190190

191191
var endChar = chr == '[' ? ']' : '}';
192192

193+
// Ignore null string from json serialization
194+
if (json == "null")
195+
{
196+
return;
197+
}
198+
193199
if(json[0] != chr)
194200
{
195201
throw new NetJSONInvalidJSONException();

NetJSON/NetJSON.cs

Lines changed: 32 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3108,57 +3108,6 @@ internal static void WritePropertiesFor(TypeBuilder typeBuilder, Type type, ILGe
31083108

31093109
il.MarkLabel(skipDefaultValueTrueAndHasValueLabel);
31103110
}
3111-
3112-
#region
3113-
3114-
//if (_skipDefaultValue) {
3115-
3116-
// if (isNullable) {
3117-
// var hasValueMethod = originPropType.GetMethod("get_HasValue");
3118-
// il.Emit(OpCodes.Ldloca, nullablePropValue);
3119-
// il.Emit(OpCodes.Call, hasValueMethod);
3120-
// il.Emit(OpCodes.Brfalse, propNullLabel);
3121-
// }
3122-
3123-
// if (isStruct)
3124-
// il.Emit(OpCodes.Ldloca, propValue);
3125-
// else
3126-
// il.Emit(OpCodes.Ldloc, propValue);
3127-
// if (isValueType && isPrimitive) {
3128-
// LoadDefaultValueByType(il, propType);
3129-
// } else {
3130-
// if (!isValueType)
3131-
// il.Emit(OpCodes.Ldnull);
3132-
// }
3133-
3134-
// if (equalityMethod != null) {
3135-
// il.Emit(OpCodes.Call, equalityMethod);
3136-
// il.Emit(OpCodes.Brtrue, propNullLabel);
3137-
// } else {
3138-
// if (isStruct) {
3139-
3140-
// var tempValue = il.DeclareLocal(propType);
3141-
3142-
// il.Emit(OpCodes.Ldloca, tempValue);
3143-
// il.Emit(OpCodes.Initobj, propType);
3144-
// il.Emit(OpCodes.Ldloc, tempValue);
3145-
// il.Emit(OpCodes.Box, propType);
3146-
// il.Emit(OpCodes.Constrained, propType);
3147-
3148-
// il.Emit(OpCodes.Callvirt, _objectEquals);
3149-
3150-
// il.Emit(OpCodes.Brtrue, propNullLabel);
3151-
3152-
// } else
3153-
// il.Emit(OpCodes.Beq, propNullLabel);
3154-
// }
3155-
//}
3156-
3157-
#endregion
3158-
3159-
//if (_skipDefaultValue) {
3160-
// il.MarkLabel(propNullLabel);
3161-
//}
31623111
}
31633112
counter++;
31643113
}
@@ -3318,6 +3267,11 @@ internal static NetJSONSerializer<T> GetSerializer<T>() {
33183267
/// <param name="value"></param>
33193268
/// <returns></returns>
33203269
public static string Serialize(Type type, object value) {
3270+
if (value == null)
3271+
{
3272+
return null;
3273+
}
3274+
33213275
return _serializeWithTypes.GetOrAdd(type, _ => {
33223276
lock (GetDictLockObject("SerializeType", type.Name)) {
33233277
var name = String.Concat(SerializeStr, type.FullName);
@@ -3354,6 +3308,11 @@ public static string Serialize(Type type, object value) {
33543308
/// <returns></returns>
33553309
public static string Serialize(Type type, object value, NetJSONSettings settings)
33563310
{
3311+
if (value == null)
3312+
{
3313+
return null;
3314+
}
3315+
33573316
return _serializeWithTypesSettings.GetOrAdd(type, _ => {
33583317
lock (GetDictLockObject("SerializeTypeSetting", type.Name))
33593318
{
@@ -3388,6 +3347,11 @@ public static string Serialize(Type type, object value, NetJSONSettings settings
33883347
/// <param name="value"></param>
33893348
/// <returns></returns>
33903349
public static string Serialize(object value) {
3350+
if (value == null)
3351+
{
3352+
return null;
3353+
}
3354+
33913355
return Serialize(value.GetType(), value);
33923356
}
33933357

@@ -3398,6 +3362,11 @@ public static string Serialize(object value) {
33983362
/// <param name="value"></param>
33993363
/// <returns></returns>
34003364
public static object Deserialize(Type type, string value) {
3365+
if (value == null)
3366+
{
3367+
return null;
3368+
}
3369+
34013370
return _deserializeWithTypes.GetOrAdd(type.FullName, _ => {
34023371
lock (GetDictLockObject("DeserializeType", type.Name)) {
34033372
var name = String.Concat(DeserializeStr, type.FullName);
@@ -3436,6 +3405,11 @@ public static object Deserialize(Type type, string value) {
34363405
/// <returns></returns>
34373406
public static object Deserialize(Type type, string value, NetJSONSettings settings)
34383407
{
3408+
if (value == null)
3409+
{
3410+
return null;
3411+
}
3412+
34393413
return _deserializeWithTypesSettings.GetOrAdd(type.FullName, _ => {
34403414
lock (GetDictLockObject("DeserializeTypeSettings", type.Name))
34413415
{
@@ -3620,6 +3594,11 @@ public static void Serialize<T>(T value, TextWriter writer, NetJSONSettings sett
36203594
/// <returns>String</returns>
36213595
public static string SerializeObject(object value, NetJSONSettings settings)
36223596
{
3597+
if (value == null)
3598+
{
3599+
return null;
3600+
}
3601+
36233602
return Serialize(value.GetType(), value, settings);
36243603
}
36253604

@@ -5167,7 +5146,7 @@ private static MethodInfo GenerateGetClassOrDictFor(TypeBuilder typeBuilder, Typ
51675146

51685147

51695148
//if (count == 0 && current == 'n') {
5170-
// index += 3;
5149+
// index += 4;
51715150
// return null;
51725151
//}
51735152
il.Emit(OpCodes.Ldc_I4_0);
@@ -5178,7 +5157,7 @@ private static MethodInfo GenerateGetClassOrDictFor(TypeBuilder typeBuilder, Typ
51785157
il.Emit(OpCodes.Ldloc, current);
51795158
il.Emit(OpCodes.Bne_Un, isNullObjectLabel);
51805159

5181-
IncrementIndexRef(il, count: 3);
5160+
IncrementIndexRef(il, count: 4);
51825161

51835162
if (isTypeValueType) {
51845163
var nullLocal = il.DeclareLocal(type);

NetJSON/NetJSON.nuspec

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
<id>NetJSON</id>
55
<title>NetJSON</title>
66
<tags>json json-serializer javascript JSON serializer binary</tags>
7-
<version>1.4.3</version>
7+
<version>1.4.4</version>
88
<authors>TJ Bakre</authors>
99
<description>Faster than Any Binary?</description>
1010
<releaseNotes>
11-
- Fixed bug with non string based array such as boolean been last item in array
11+
- Fixed issue with null value in array causing invalid json exception
12+
- Fixed null reference exception when using serialize object method
13+
- Fixed issue with literal null string causing invalid json exception
1214
</releaseNotes>
1315
<language>en-US</language>
1416
<licenseUrl>https://github.com/rpgmaker/NetJSON/blob/master/LICENSE.txt</licenseUrl>

NetJSON/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
// You can specify all the values or you can default the Build and Revision Numbers
3434
// by using the '*' as shown below:
3535
// [assembly: AssemblyVersion("1.0.*")]
36-
[assembly: AssemblyVersion("1.4.3")]
37-
[assembly: AssemblyFileVersion("1.4.3")]
36+
[assembly: AssemblyVersion("1.4.4")]
37+
[assembly: AssemblyFileVersion("1.4.4")]
3838
#if !NET_35 && !NET_STANDARD
3939
[assembly: SecurityRules(SecurityRuleSet.Level2, SkipVerificationInFullTrust = true)]
4040
#endif

NetJSON/lib/net5.0/NetJSON.dll

512 Bytes
Binary file not shown.

NetJSON/lib/net5.0/NetJSON.pdb

820 Bytes
Binary file not shown.

NetJSON/lib/net6.0/NetJSON.dll

512 Bytes
Binary file not shown.

NetJSON/lib/net6.0/NetJSON.pdb

820 Bytes
Binary file not shown.

NetJSON/lib/net7.0/NetJSON.dll

1 KB
Binary file not shown.

0 commit comments

Comments
 (0)