Skip to content

Commit fba8c95

Browse files
authored
Use json.net (#96)
* Use json.net * clean up * update changelog * use newtonsoft in main library * serialize enums * ability to create interfaces * no more jsonnet serializer * clean up * tidY * sln * remove thing
1 parent 1d596f1 commit fba8c95

17 files changed

+76
-4560
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# Next
44

55
* Bug fix for memory leak with ad-hoc publishing
6+
* Move to netstandard 1.6
7+
* Chinchilla and Chinchilla.Api now takes a dependency on Newtonsoft.Json
68

79
# 0.3.12
810

src/Chinchilla.Api/Chinchilla.Api.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
</PropertyGroup>
2525

2626
<ItemGroup>
27-
<PackageReference Include="System.Runtime.Serialization.Primitives" Version="4.3.0" />
27+
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
2828
</ItemGroup>
2929

3030
</Project>

src/Chinchilla.Api/RabbitAdmin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public async Task<bool> CreateAsync(VirtualHost virtualHost)
5555
{"name", virtualHost.Name}
5656
});
5757

58-
return response.StatusCode == HttpStatusCode.NoContent;
58+
return response.StatusCode == HttpStatusCode.Created;
5959
}
6060

6161
public Task<bool> CreateAsync(VirtualHost virtualHost, Queue queue)

src/Chinchilla.Api/RabbitHttpClient.cs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using Chinchilla.Api.Extensions;
77
using System;
88
using System.IO;
9+
using Newtonsoft.Json;
10+
using Newtonsoft.Json.Serialization;
911

1012
namespace Chinchilla.Api
1113
{
@@ -19,13 +21,20 @@ public class RabbitHttpClient
1921

2022
private readonly HttpClient client;
2123

24+
private readonly JsonSerializer serializer;
25+
2226
public RabbitHttpClient(string root, string username, string password)
2327
{
2428
this.root = root;
2529
this.username = username;
2630
this.password = password;
2731

2832
client = new HttpClient();
33+
34+
serializer = new JsonSerializer
35+
{
36+
ContractResolver = LowercasePropertyContractResolver.Instance
37+
};
2938
}
3039

3140
public Task<HttpResponseMessage> Execute(
@@ -89,12 +98,12 @@ private async Task<T> DeserializeResponse<T>(HttpResponseMessage response)
8998
{
9099
var content = await response.Content.ReadAsStreamAsync();
91100

92-
using (var sr = new StreamReader(content))
101+
using (var streamReader = new StreamReader(content))
93102
{
94-
var body = await sr.ReadToEndAsync();
95-
96-
SimpleJson.CurrentJsonSerializerStrategy = new RabbitJsonSerializerStrategy();
97-
return SimpleJson.DeserializeObject<T>(body);
103+
using (var jsonTextReader = new JsonTextReader(streamReader))
104+
{
105+
return serializer.Deserialize<T>(jsonTextReader);
106+
}
98107
}
99108
}
100109

@@ -115,7 +124,12 @@ private HttpRequestMessage CreateRequest(
115124

116125
if (body != null)
117126
{
118-
var serializedBody = SimpleJson.SerializeObject(body, new RabbitJsonSerializerStrategy());
127+
var serializedBody = JsonConvert.SerializeObject(body,
128+
new JsonSerializerSettings
129+
{
130+
ContractResolver = LowercasePropertyContractResolver.Instance
131+
});
132+
119133
request.Content = new StringContent(serializedBody, Encoding.UTF8, "application/json");
120134
}
121135
else if (method != HttpMethod.Get)
@@ -150,5 +164,15 @@ private string BuildAuthHeader(string user, string password)
150164
authInfo = Convert.ToBase64String(Encoding.UTF8.GetBytes(authInfo));
151165
return "Basic " + authInfo;
152166
}
167+
168+
public class LowercasePropertyContractResolver : DefaultContractResolver
169+
{
170+
public static readonly LowercasePropertyContractResolver Instance = new LowercasePropertyContractResolver();
171+
172+
protected override string ResolvePropertyName(string propertyName)
173+
{
174+
return propertyName.ToLowerInvariant();
175+
}
176+
}
153177
}
154178
}

src/Chinchilla.Api/RabbitJsonSerializerStrategy.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)