Skip to content

Instantly share code, notes, and snippets.

@crunchie84
Last active August 29, 2015 13:57
Show Gist options
  • Save crunchie84/9664353 to your computer and use it in GitHub Desktop.
Save crunchie84/9664353 to your computer and use it in GitHub Desktop.
custom uri typeconvert to string for ElasticSearch indexing
var elasticSearchConnectionSettings = new ConnectionSettings(new Uri("http://127.0.0.1:9200")
.SetDefaultIndex("my-index")
.SetTimeout(5 * 1000)
.AddContractJsonConverters(
(t) => t == typeof(Uri) ? new UriJsonConverter() : null
);
namespace Domain.TypeConverters
{
using System;
using Newtonsoft.Json;
/// <summary>
/// Converter for converting Uri to String and vica versa
/// </summary>
/// <remarks>
/// Code originated from http://stackoverflow.com/a/8087049/106909
/// </remarks>
public sealed class UriJsonConverter : JsonConverter
{
/// <summary>
/// Determines whether this instance can convert the specified object type.
/// </summary>
/// <param name="objectType"></param>
/// <returns></returns>
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Uri);
}
/// <summary>
/// Reads the JSON representation of the object.
/// </summary>
/// <param name="reader"></param>
/// <param name="objectType"></param>
/// <param name="existingValue"></param>
/// <param name="serializer"></param>
/// <returns></returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.String)
return new Uri((string)reader.Value);
if (reader.TokenType == JsonToken.Null)
return null;
throw new InvalidOperationException("Unhandled case for UriConverter. Check to see if this converter has been applied to the wrong serialization type.");
}
/// <summary>
/// Writes the JSON representation of the object.
/// </summary>
/// <param name="writer"></param>
/// <param name="value"></param>
/// <param name="serializer"></param>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (null == value)
{
writer.WriteNull();
return;
}
var uriValue = value as Uri;
if (uriValue != null)
{
writer.WriteValue(uriValue.OriginalString);
return;
}
throw new InvalidOperationException("Unhandled case for UriConverter. Check to see if this converter has been applied to the wrong serialization type.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment