using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Threading.Tasks;
using RestSharp.Deserializers;
using RestSharp.Extensions;
using RestSharp.Serializers;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using Coinbase.Serialization;
public static class RestSharpExtensions
static T DeserializeAnonymousType<T>(this IDeserializer deserializer, IRestResponse response, T anonymousTypeObject)
if (deserializer == null)
throw new ArgumentNullException();
return deserializer.Deserialize<T>(response);
public Double Amount { get; set; }
public string Id { get; set; }
public string Name { get; set; }
public bool Primary { get; set; }
public string Type { get; set; }
public Balance Balance { get; set; }
public DateTime CreatedAt { get; set; }
public static IRestClient MockRestClient<T>(HttpStatusCode httpStatusCode, string json)
var data = JsonConvert.DeserializeObject<T>(json);
var response = new Mock<IRestResponse<T>>();
response.Setup(_ => _.StatusCode).Returns(httpStatusCode);
response.Setup(_ => _.Data).Returns(data);
var mockIRestClient = new Mock<IRestClient>();
.Setup(x => x.Execute<T>(It.IsAny<IRestRequest>()))
.ReturnsAsync(response.Object);
return mockIRestClient.Object;
public static void Test()
var response = new RestResponse
ContentType = "application/json",
var JsonSettings = new JsonSerializerSettings
ContractResolver = new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy() },
var jsonDeserializer = new JsonNetDeseralizer(JsonSettings);
""id"":""gje99c1e-7081-5784-9a1e-4p302b92312b"",
""amount"":""0.97845347"",
""created_at"":""2017-10-01T16:50:51Z"",
namespace Coinbase.Serialization
public class JsonNetDeseralizer : IDeserializer
private readonly JsonSerializerSettings settings;
public JsonNetDeseralizer(JsonSerializerSettings settings)
this.settings = settings;
public T Deserialize<T>(IRestResponse response)
return JsonConvert.DeserializeObject<T>(response.Content, settings);
public string RootElement { get; set; }
public string Namespace { get; set; }
public string DateFormat { get; set; }
public static void Main()
Console.WriteLine("Environment version: " + Environment.Version);
Console.WriteLine("RestSharp version: " + typeof(RestResponse).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");