Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Dec 14, 2019
1 parent 7678e8f commit a8c17ce
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/WireMock.Net/ResponseBuilders/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public IResponseBuilder WithBody(string body, string destination = BodyDestinati

case BodyDestinationFormat.Json:
ResponseMessage.BodyData.DetectedBodyType = BodyType.Json;
ResponseMessage.BodyData.BodyAsJson = JsonConvert.DeserializeObject(body);
ResponseMessage.BodyData.BodyAsJson = JsonUtils.DeserializeObject(body);
break;

default:
Expand Down
16 changes: 9 additions & 7 deletions src/WireMock.Net/Server/FluentMockServer.Admin.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using JetBrains.Annotations;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using WireMock.Admin.Mappings;
using WireMock.Admin.Scenarios;
using WireMock.Admin.Settings;
Expand Down Expand Up @@ -179,8 +179,10 @@ public void WatchStaticMappings([CanBeNull] string folder = null)

_settings.Logger.Info($"Watching folder '{folder}'{includeSubdirectoriesText} for new, updated and deleted MappingFiles.");

var watcher = new EnhancedFileSystemWatcher(folder, "*.json", EnhancedFileSystemWatcherTimeoutMs);
watcher.IncludeSubdirectories = includeSubdirectories;
var watcher = new EnhancedFileSystemWatcher(folder, "*.json", EnhancedFileSystemWatcherTimeoutMs)
{
IncludeSubdirectories = includeSubdirectories
};

watcher.Created += (sender, args) =>
{
Expand Down Expand Up @@ -229,7 +231,7 @@ public bool ReadStaticMappingAndAddOrUpdate([NotNull] string path)

if (FileHelper.TryReadMappingFileWithRetryAndDelay(_settings.FileSystemHandler, path, out string value))
{
var mappingModels = DeserializeObjectToArray<MappingModel>(JsonConvert.DeserializeObject(value));
var mappingModels = DeserializeObjectToArray<MappingModel>(JsonUtils.DeserializeObject(value));
foreach (var mappingModel in mappingModels)
{
if (mappingModels.Length == 1 && Guid.TryParse(filenameWithoutExtension, out Guid guidFromFilename))
Expand Down Expand Up @@ -882,7 +884,7 @@ private T DeserializeObject<T>(RequestMessage requestMessage)
{
if (requestMessage?.BodyData?.DetectedBodyType == BodyType.String)
{
return JsonConvert.DeserializeObject<T>(requestMessage.BodyData.BodyAsString);
return JsonUtils.DeserializeObject<T>(requestMessage.BodyData.BodyAsString);
}

if (requestMessage?.BodyData?.DetectedBodyType == BodyType.Json)
Expand Down
4 changes: 1 addition & 3 deletions src/WireMock.Net/Util/BodyParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ internal static class BodyParser
new WildcardMatcher("application/x-www-form-urlencoded", true)
};

private static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings { DateParseHandling = DateParseHandling.None };

public static bool ShouldParseBody([CanBeNull] string httpMethod, bool allowBodyForAllHttpMethods)
{
if (string.IsNullOrEmpty(httpMethod))
Expand Down Expand Up @@ -147,7 +145,7 @@ public static async Task<BodyData> Parse([NotNull] Stream stream, [CanBeNull] st
{
try
{
data.BodyAsJson = JsonConvert.DeserializeObject(data.BodyAsString, JsonSerializerSettings);
data.BodyAsJson = JsonUtils.DeserializeObject(data.BodyAsString);
data.DetectedBodyType = BodyType.Json;
}
catch
Expand Down
22 changes: 22 additions & 0 deletions src/WireMock.Net/Util/JsonUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,28 @@ public static JToken Parse(string json)
return JsonConvert.DeserializeObject<JToken>(json, JsonSerializerSettings);
}

/// <summary>
/// Deserializes the JSON to a .NET object.
/// Using : DateParseHandling = DateParseHandling.None
/// </summary>
/// <param name="json">A System.String that contains JSON.</param>
/// <returns>The deserialized object from the JSON string.</returns>
public static object DeserializeObject(string json)
{
return JsonConvert.DeserializeObject(json, JsonSerializerSettings);
}

/// <summary>
/// Deserializes the JSON to the specified .NET type.
/// Using : DateParseHandling = DateParseHandling.None
/// </summary>
/// <param name="json">A System.String that contains JSON.</param>
/// <returns>The deserialized object from the JSON string.</returns>
public static T DeserializeObject<T>(string json)
{
return JsonConvert.DeserializeObject<T>(json, JsonSerializerSettings);
}

public static T ParseJTokenToObject<T>(object value)
{
switch (value)
Expand Down

0 comments on commit a8c17ce

Please sign in to comment.