forked from MvvmCross/MvvmCross
-
Notifications
You must be signed in to change notification settings - Fork 0
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 MvvmCross#753 from pcmichaels/v3.1
Add serialize to JSON Serializer loop handling to prevent crashing for self referencing objects
- Loading branch information
Showing
1 changed file
with
46 additions
and
45 deletions.
There are no files selected for viewing
91 changes: 46 additions & 45 deletions
91
Cirrious/Json/Cirrious.MvvmCross.Plugins.Json/MvxJsonConverter.cs
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 |
---|---|---|
@@ -1,46 +1,47 @@ | ||
// MvxJsonConverter.cs | ||
// (c) Copyright Cirrious Ltd. http://www.cirrious.com | ||
// MvvmCross is licensed using Microsoft Public License (Ms-PL) | ||
// Contributions and inspirations noted in readme.md and license.txt | ||
// | ||
// Project Lead - Stuart Lodge, @slodge, [email protected] | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Cirrious.CrossCore.Platform; | ||
using Newtonsoft.Json; | ||
|
||
namespace Cirrious.MvvmCross.Plugins.Json | ||
{ | ||
public class MvxJsonConverter : IMvxJsonConverter | ||
{ | ||
private static readonly JsonSerializerSettings Settings; | ||
|
||
static MvxJsonConverter() | ||
{ | ||
Settings = new JsonSerializerSettings | ||
{ | ||
Converters = new List<JsonConverter> | ||
{ | ||
new MvxEnumJsonConverter(), | ||
}, | ||
DateFormatHandling = DateFormatHandling.IsoDateFormat, | ||
}; | ||
} | ||
|
||
public T DeserializeObject<T>(string inputText) | ||
{ | ||
return JsonConvert.DeserializeObject<T>(inputText, Settings); | ||
} | ||
|
||
public string SerializeObject(object toSerialise) | ||
{ | ||
return JsonConvert.SerializeObject(toSerialise, Formatting.None, Settings); | ||
} | ||
|
||
public object DeserializeObject(Type type, string inputText) | ||
{ | ||
return JsonConvert.DeserializeObject(inputText, type, Settings); | ||
} | ||
} | ||
// MvxJsonConverter.cs | ||
// (c) Copyright Cirrious Ltd. http://www.cirrious.com | ||
// MvvmCross is licensed using Microsoft Public License (Ms-PL) | ||
// Contributions and inspirations noted in readme.md and license.txt | ||
// | ||
// Project Lead - Stuart Lodge, @slodge, [email protected] | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Cirrious.CrossCore.Platform; | ||
using Newtonsoft.Json; | ||
|
||
namespace Cirrious.MvvmCross.Plugins.Json | ||
{ | ||
public class MvxJsonConverter : IMvxJsonConverter | ||
{ | ||
private static readonly JsonSerializerSettings Settings; | ||
|
||
static MvxJsonConverter() | ||
{ | ||
Settings = new JsonSerializerSettings | ||
{ | ||
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize, | ||
Converters = new List<JsonConverter> | ||
{ | ||
new MvxEnumJsonConverter(), | ||
}, | ||
DateFormatHandling = DateFormatHandling.IsoDateFormat, | ||
}; | ||
} | ||
|
||
public T DeserializeObject<T>(string inputText) | ||
{ | ||
return JsonConvert.DeserializeObject<T>(inputText, Settings); | ||
} | ||
|
||
public string SerializeObject(object toSerialise) | ||
{ | ||
return JsonConvert.SerializeObject(toSerialise, Formatting.None, Settings); | ||
} | ||
|
||
public object DeserializeObject(Type type, string inputText) | ||
{ | ||
return JsonConvert.DeserializeObject(inputText, type, Settings); | ||
} | ||
} | ||
} |