Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Move to Newstonsoft.JSON for JSON serialisation
  • Loading branch information
goya committed Apr 2, 2015
1 parent ac15e13 commit 418e51ea4f6c6cb644f18f0660efdd8b058644a5
Showing 5 changed files with 14 additions and 55 deletions.
@@ -8,6 +8,7 @@ Thumbs.db
/temp/*

*.dll
!template/lib/*

# Ignore template zip files
*.zip
@@ -199,6 +199,11 @@
<ItemGroup>
<Folder Include="Plugins\" />
</ItemGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\template\lib\Newtonsoft.Json.dll</HintPath>
</Reference>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\$(TargetFrameworkIdentifier)\$(TargetFrameworkVersion)\Microsoft.$(TargetFrameworkIdentifier).$(TargetFrameworkVersion).Overrides.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\$(TargetFrameworkIdentifier)\$(TargetFrameworkVersion)\Microsoft.$(TargetFrameworkIdentifier).CSharp.targets" />
<ProjectExtensions />
@@ -226,6 +226,11 @@
<ItemGroup>
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
</ItemGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>lib\Newtonsoft.Json.dll</HintPath>
</Reference>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\$(TargetFrameworkIdentifier)\$(TargetFrameworkVersion)\Microsoft.$(TargetFrameworkIdentifier).$(TargetFrameworkVersion).Overrides.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\$(TargetFrameworkIdentifier)\$(TargetFrameworkVersion)\Microsoft.$(TargetFrameworkIdentifier).CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
@@ -12,21 +12,7 @@
limitations under the License.
*/

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Newtonsoft.Json;

namespace WPCordovaClassLib.Cordova.JSON
{
@@ -42,29 +28,7 @@ public static class JsonHelper
/// <returns>JSON representation of the object. Returns 'null' string for null passed as argument</returns>
public static string Serialize(object obj)
{
if (obj == null)
{
return "null";
}

DataContractJsonSerializer ser = new DataContractJsonSerializer(obj.GetType());

MemoryStream ms = new MemoryStream();
ser.WriteObject(ms, obj);

ms.Position = 0;

string json = String.Empty;

using (StreamReader sr = new StreamReader(ms))
{
json = sr.ReadToEnd();
}

ms.Close();

return json;

return JsonConvert.SerializeObject(obj);
}

/// <summary>
@@ -75,23 +39,7 @@ public static string Serialize(object obj)
/// <returns>Deserialized object instance</returns>
public static T Deserialize<T>(string json)
{
DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(T));
object result = null;
try
{
using (MemoryStream mem = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
result = deserializer.ReadObject(mem);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
Debug.WriteLine("Failed to deserialize " + typeof(T) + " with JSON value :: " + json);
}

return (T)result;

return JsonConvert.DeserializeObject<T>(json);
}
}
}
Binary file not shown.

0 comments on commit 418e51e

Please sign in to comment.