From cbc1f546bc045c5aa6b20cf43865c27cf846954a Mon Sep 17 00:00:00 2001 From: unrealTOM Date: Mon, 19 Nov 2018 21:26:55 +0800 Subject: [PATCH 1/2] fix singleton object serialization exception if prop object is the same as current object, just ignore it to avoid infinite recursion. --- src/LitJson/JsonMapper.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/LitJson/JsonMapper.cs b/src/LitJson/JsonMapper.cs index 0fc106a1..93fbb7e7 100644 --- a/src/LitJson/JsonMapper.cs +++ b/src/LitJson/JsonMapper.cs @@ -832,11 +832,11 @@ private static void WriteValue (object obj, JsonWriter writer, } else { PropertyInfo p_info = (PropertyInfo) p_data.Info; + object prop_obj = p_info.GetValue(obj, null); - if (p_info.CanRead) { + if (prop_obj != obj && p_info.CanRead) { writer.WritePropertyName (p_data.Info.Name); - WriteValue (p_info.GetValue (obj, null), - writer, writer_is_private, depth + 1); + WriteValue (prop_obj, writer, writer_is_private, depth + 1); } } } From db640567c4fb6c67aed7742c0573abe87ca2758f Mon Sep 17 00:00:00 2001 From: unrealTOM Date: Tue, 20 Nov 2018 09:50:27 +0800 Subject: [PATCH 2/2] Update JsonMapper.cs --- src/LitJson/JsonMapper.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/LitJson/JsonMapper.cs b/src/LitJson/JsonMapper.cs index 93fbb7e7..4c261b89 100644 --- a/src/LitJson/JsonMapper.cs +++ b/src/LitJson/JsonMapper.cs @@ -832,11 +832,12 @@ private static void WriteValue (object obj, JsonWriter writer, } else { PropertyInfo p_info = (PropertyInfo) p_data.Info; - object prop_obj = p_info.GetValue(obj, null); - - if (prop_obj != obj && p_info.CanRead) { - writer.WritePropertyName (p_data.Info.Name); - WriteValue (prop_obj, writer, writer_is_private, depth + 1); + if (p_info.CanRead) { + object prop_obj = p_info.GetValue(obj, null); + if (prop_obj != obj) { + writer.WritePropertyName (p_data.Info.Name); + WriteValue (prop_obj, writer, writer_is_private, depth + 1); + } } } }