Skip to content

Commit

Permalink
Merge pull request MvvmCross#753 from pcmichaels/v3.1
Browse files Browse the repository at this point in the history
Add serialize to JSON Serializer loop handling to prevent crashing for self referencing objects
  • Loading branch information
slodge committed Aug 18, 2014
2 parents 8dbb8d1 + 541d906 commit 87bd67c
Showing 1 changed file with 46 additions and 45 deletions.
91 changes: 46 additions & 45 deletions Cirrious/Json/Cirrious.MvvmCross.Plugins.Json/MvxJsonConverter.cs
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);
}
}
}

0 comments on commit 87bd67c

Please sign in to comment.