Skip to content
This repository has been archived by the owner on Jan 7, 2023. It is now read-only.

Commit

Permalink
Add hard reference to JSON.NET to the project.
Browse files Browse the repository at this point in the history
Fix up CallbackHandler to use direct references to JSON.NET to avoid version conflicts the old dynamic load code used to cause. Added since most web projects already include NewtonSoft.Json anyway.
  • Loading branch information
RickStrahl committed May 20, 2015
1 parent 2e72cd0 commit 2a47fef
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 23 deletions.
11 changes: 8 additions & 3 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
<small>Westwind.Web</small><br/>
The JsonVariables utility that allows embedding of server side data into client script has been updated to generate < and > tags as encoded strings to prevent XSS attacks when rendering.

* **CallbackHandler improved JSON.NET Suppport**<br/>
<small>Westwind.Web</small><br/>
Switched to hard linked JSON.NET support in CallbackHandler instead of the previous dynamic loading to avoid the assembly reference to JSON.NET. This fixes odd version incompatibilities that have been reported as well as improving JSON performance slightly.

* **Add Async Support for HttpClient**
<small>Westwind.Utilities</small><br/>
Added support for Async methods to the HttpClient Class for DownloadBytesAsync() and DownloadStringAsync(). Also optimized throughput and fixed explicit disposal of one of the internal streams that previously slowed down high volume requests.

* **ImageUtils.NormalizeJpgImageRotation**<br/>
<small>Westwind.Utilities</small><br/>
Method that looks at Exif Orientation data in a jpeg file and rotates the image to match the orientation before removing the Exif data. Useful when capturing images from mobile device which often are natively rotated and contain.
Expand All @@ -15,9 +23,6 @@ Method that looks at Exif Orientation data in a jpeg file and rotates the image
<small>Westwind.Utilities</small><br/>
Removes Exif data from Jpg images. Helps reduce size of images and normalizes images and keeps them from auto-rotating.

* **Add Async Support for HttpClient**
<small>Westwind.Utilities</small><br/>
Added support for Async methods to the HttpClient Class for DownloadBytesAsync() and DownloadStringAsync(). Also optimized throughput and fixed explicit disposal of one of the internal streams that previously slowed down high volume requests.

### Version 2.63
* April 30th, 2015
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public JSONSerializer(SupportedJsonParserTypes parserType)
#if (true) //JSONNET_REFERENCE)
else if (parserType == SupportedJsonParserTypes.JsonNet)
_serializer = new JsonNetJsonSerializer(this);
//_serializer = new JsonNetJsonSerializerOld(this);
#endif
else if (parserType == SupportedJsonParserTypes.JavaScriptSerializer)
_serializer = new WebExtensionsJavaScriptSerializer(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
using System.Text.RegularExpressions;
using System.Reflection;
using System.Collections.Specialized;

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Westwind.Utilities;

//using Newtonsoft.Json;
Expand Down Expand Up @@ -66,7 +67,7 @@ public JsonNetJsonSerializer(JSONSerializer serializer)
masterSerializer = serializer;
}

private static dynamic JsonNetJson;
private static JsonSerializer JsonNetJson;
private static object JsonNetLock = new object();

public dynamic CreateJsonNetInstance(bool forceReload = false)
Expand All @@ -79,28 +80,27 @@ public dynamic CreateJsonNetInstance(bool forceReload = false)
lock (JsonNetLock)
{
if (JsonNetJson == null)
JsonNetJson = ReflectionUtils.CreateInstanceFromString("Newtonsoft.Json.JsonSerializer");
JsonNetJson = new JsonSerializer();
}
}

JsonNetJson.ObjectCreationHandling = (dynamic) ReflectionUtils.GetStaticProperty("Newtonsoft.Json.ObjectCreationHandling", "Replace");
JsonNetJson.MissingMemberHandling = (dynamic) ReflectionUtils.GetStaticProperty("Newtonsoft.Json.MissingMemberHandling", "Ignore");
JsonNetJson.ReferenceLoopHandling = (dynamic) ReflectionUtils.GetStaticProperty("Newtonsoft.Json.ReferenceLoopHandling","Ignore");
JsonNetJson.ObjectCreationHandling = ObjectCreationHandling.Replace;
JsonNetJson.MissingMemberHandling = MissingMemberHandling.Ignore;
JsonNetJson.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

//JsonNetJson.Converters.Add((dynamic) ReflectionUtils.CreateInstanceFromString("Newtonsoft.Json.Converters.IsoDateTimeConverter") );
JsonNetJson.Converters.Add((dynamic)ReflectionUtils.CreateInstanceFromString("Newtonsoft.Json.Converters.StringEnumConverter"));
JsonNetJson.Converters.Add(new StringEnumConverter());

return JsonNetJson;
}

public dynamic CreateJsonNetWriter(StringWriter sw)
{
dynamic writer = ReflectionUtils.CreateInstanceFromString("Newtonsoft.Json.JsonTextWriter", sw);
public JsonTextWriter CreateJsonNetWriter(StringWriter sw)
{
var writer = new JsonTextWriter(sw);

if (FormatJsonOutput)
writer.Formatting = (dynamic)ReflectionUtils.GetStaticProperty("Newtonsoft.Json.Formatting", "Indented");
writer.Formatting = Formatting.Indented;
else
writer.Formatting = (dynamic)ReflectionUtils.GetStaticProperty("Newtonsoft.Json.Formatting", "None");
writer.Formatting = Formatting.None;

return writer;
}
Expand Down Expand Up @@ -134,12 +134,12 @@ public string Serialize(object value)
{
Type type = value.GetType();

CreateJsonNetInstance();
var json = CreateJsonNetInstance();

StringWriter sw = new StringWriter();
var writer = CreateJsonNetWriter(sw);

writer.QuoteChar = '"';

JsonNetJson.Serialize(writer, value);

string output = sw.ToString();
Expand Down Expand Up @@ -176,9 +176,11 @@ public object Deserialize(string jsonText, Type valueType)
CreateJsonNetInstance();

StringReader sr = new StringReader(jsonText);
dynamic reader = ReflectionUtils.CreateInstanceFromString("Newtonsoft.Json.JsonTextReader",sr);
object result = JsonNetJson.Deserialize(reader, valueType);
reader.Close();
object result;
using (var reader = new JsonTextReader(sr))
{
result = JsonNetJson.Deserialize(reader, valueType);
}

return result;
}
Expand Down
4 changes: 2 additions & 2 deletions Westwind.Web/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.62.0")]
[assembly: AssemblyFileVersion("2.62.0")]
[assembly: AssemblyVersion("2.64.0")]
[assembly: AssemblyFileVersion("2.64.0")]
5 changes: 5 additions & 0 deletions Westwind.Web/Westwind.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json">
<HintPath>..\..\..\Temp\wwgtest\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
Expand All @@ -84,6 +87,7 @@
<Compile Include="CallbackHandler\CallbackHandler\JsonCallbackMethodProcessor.cs" />
<Compile Include="CallbackHandler\CallbackHandler\XmlCallbackMethodProcessor.cs" />
<Compile Include="CallbackHandler\JsonSerializers\IJSONSerializer.cs" />
<Compile Include="CallbackHandler\JsonSerializers\JsonNetJsonSerializer - Copy.cs" />
<Compile Include="CallbackHandler\JsonSerializers\JsonNetJsonSerializer.cs" />
<Compile Include="CallbackHandler\JsonSerializers\JSONSerializer.cs" />
<Compile Include="CallbackHandler\JsonSerializers\WebExtensionsSerializer.cs" />
Expand Down Expand Up @@ -127,6 +131,7 @@
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="Readme.md" />
</ItemGroup>
<ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions Westwind.Web/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net45" />
</packages>

0 comments on commit 2a47fef

Please sign in to comment.