-
-
Save patridge/349bcee5e8938dff91a7 to your computer and use it in GitHub Desktop.
Issue with Refit JsonConverter
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
using System; | |
using System.Globalization; | |
using System.Threading.Tasks; | |
using Android.App; | |
using Android.Widget; | |
using Android.OS; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Converters; | |
using Refit; | |
namespace RefitJsonConverterIssue | |
{ | |
public interface ISomeApiService | |
{ | |
[Get ("/test/something1/")] | |
Task<SomeResult> GetParamAttribConverterResult ([JsonConverter (typeof(SomeDateTimeConverter))]DateTime someDate); | |
[Get ("/test/something2/")] | |
Task<SomeResult> GetJsonConvertDefaultResult (DateTime someDate); | |
} | |
[Activity (Label = "RefitJsonConverterIssue", MainLauncher = true, Icon = "@drawable/icon")] | |
public class MainActivity : Activity | |
{ | |
int count = 1; | |
protected override void OnCreate (Bundle bundle) | |
{ | |
base.OnCreate (bundle); | |
JsonConvert.DefaultSettings = | |
() => new JsonSerializerSettings () { | |
Converters = { | |
new SomeDateTimeConverter () | |
} | |
}; | |
var testDate = new DateTime (2014, 11, 7, 16, 32, 54); | |
string testDateJson = JsonConvert.SerializeObject (testDate); | |
System.Diagnostics.Debug.Assert (testDateJson == "2014-11-07 16:32:54"); | |
// Set our view from the "main" layout resource | |
SetContentView (Resource.Layout.Main); | |
// Get our button from the layout resource, | |
// and attach an event to it | |
Button button = FindViewById<Button> (Resource.Id.myButton); | |
var service = RestService.For<ISomeApiService> ("https://yourapihere-com-{yourrunscopebucketid}.runscope.net/"); | |
button.Click += delegate { | |
Task.Run (async () => { | |
// NOTE: these all fail since the API doesn't exist. Done just to see outbound URLs in Runscope. | |
try { | |
var paramAttributeResult = await service.GetParamAttribConverterResult (testDate); | |
} catch (Exception ex) { | |
Console.WriteLine ("expected fail 1"); | |
} | |
try { | |
var defaultConverterResult = await service.GetJsonConvertDefaultResult (testDate); | |
} catch (Exception ex) { | |
Console.WriteLine ("expected fail 1"); | |
} | |
RunOnUiThread (() => { | |
button.Text = string.Format ("{0} clicks!", count++); | |
}); | |
}); | |
}; | |
} | |
} | |
public class SomeResult | |
{ | |
public string SomeString { get; set; } | |
public DateTime SomeDate { get; set; } | |
} | |
public class SomeDateTimeConverter : DateTimeConverterBase | |
{ | |
const string dateTimeFormat = "yyyy-MM-yy HH:mm:ss"; | |
public override void WriteJson (Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) | |
{ | |
string output; | |
if (value is DateTime) { | |
output = ((DateTime)value).ToString (dateTimeFormat); | |
} else { | |
throw new Exception ("Expected date object value."); | |
} | |
writer.WriteValue (output); | |
} | |
public override object ReadJson (Newtonsoft.Json.JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) | |
{ | |
if (reader.TokenType != JsonToken.String) { | |
throw new Exception (string.Format ("Unexpected token parsing date. Expected string, got {0}.", reader.TokenType)); | |
} | |
DateTime date; | |
bool success = DateTime.TryParseExact ((string)reader.Value, dateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out date); | |
if (!success) { | |
throw new Exception ("Invalid date format."); | |
} | |
return date; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment