-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed the way request bodies are converted.
This resolves #4 AddJsonBody was needed when dealing with Contact Properties.
- Loading branch information
1 parent
950d8f0
commit b4f9d94
Showing
9 changed files
with
212 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using Newtonsoft.Json; | ||
using RestSharp.Serializers; | ||
using System.IO; | ||
|
||
namespace MailJet.Client.Converters | ||
{ | ||
public class NewtonsoftJsonSerializer : ISerializer | ||
{ | ||
private Newtonsoft.Json.JsonSerializer serializer; | ||
|
||
public NewtonsoftJsonSerializer(Newtonsoft.Json.JsonSerializer serializer) | ||
{ | ||
this.serializer = serializer; | ||
} | ||
|
||
public string ContentType | ||
{ | ||
get { return "application/json"; } // Probably used for Serialization? | ||
set { } | ||
} | ||
|
||
public string DateFormat { get; set; } | ||
|
||
public string Namespace { get; set; } | ||
|
||
public string RootElement { get; set; } | ||
|
||
public string Serialize(object obj) | ||
{ | ||
using (var stringWriter = new StringWriter()) | ||
{ | ||
using (var jsonTextWriter = new JsonTextWriter(stringWriter)) | ||
{ | ||
serializer.Serialize(jsonTextWriter, obj); | ||
|
||
return stringWriter.ToString(); | ||
} | ||
} | ||
} | ||
|
||
public T Deserialize<T>(RestSharp.IRestResponse response) | ||
{ | ||
var content = response.Content; | ||
|
||
using (var stringReader = new StringReader(content)) | ||
{ | ||
using (var jsonTextReader = new JsonTextReader(stringReader)) | ||
{ | ||
return serializer.Deserialize<T>(jsonTextReader); | ||
} | ||
} | ||
} | ||
|
||
public static NewtonsoftJsonSerializer Default | ||
{ | ||
get | ||
{ | ||
Newtonsoft.Json.JsonSerializer json = new Newtonsoft.Json.JsonSerializer() | ||
{ | ||
NullValueHandling = NullValueHandling.Ignore, | ||
}; | ||
json.Converters.Insert(0, new TKEYVALUELISTConverter()); | ||
NewtonsoftJsonSerializer serializer = new NewtonsoftJsonSerializer(json); | ||
return serializer; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using MailJet.Client.Request; | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace MailJet.Client.Converters | ||
{ | ||
public class TKEYVALUELISTConverter : JsonConverter | ||
{ | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return typeof(List<TKEYVALUELIST>) == objectType; | ||
} | ||
|
||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
var data = (List<TKEYVALUELIST>)value; | ||
|
||
writer.WriteStartObject(); | ||
foreach (TKEYVALUELIST item in data) | ||
{ | ||
writer.WritePropertyName(item.Name); | ||
writer.WriteValue(item.Value); | ||
} | ||
writer.WriteEndObject(); | ||
} | ||
|
||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace MailJet.Client.Request | ||
{ | ||
public struct TKEYVALUELIST | ||
{ | ||
public string Name { get; set; } | ||
public string Value { get; set; } | ||
|
||
public static List<TKEYVALUELIST> FromDictionary(Dictionary<string, string> dictionary) | ||
{ | ||
return dictionary.Select(x => new TKEYVALUELIST { Name = x.Key, Value = x.Value }).ToList(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using MailJet.Client.Request; | ||
using System.Collections.Generic; | ||
|
||
namespace MailJet.Client.Response.Data | ||
{ | ||
public class ContactDataUpdate : DataItem | ||
{ | ||
public long ContactID { get; set; } | ||
public List<TKEYVALUELIST> Data { get; set; } | ||
} | ||
} |