You can do this using a custom converter, but depending on how complex your objects are, this can become quite complicated. For example, one that can handle any object with string and int values, as well as IEnumerables, would look something like this:
public class CaseConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return true;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value.GetType() == typeof(string) || value.GetType() == typeof(int))
{
writer.WriteValue(value.ToString().ToLower());
}
else
{
var enumerable = value as System.Collections.IEnumerable;
if (enumerable != null)
{
writer.WriteStartArray();
foreach (var item in enumerable)
{
serializer.Serialize(writer, item);
}
writer.WriteEndArray();
}
else
{
writer.WriteStartObject();
PropertyInfo[] properties = value.GetType().GetProperties();
foreach (PropertyInfo pi in properties)
{
writer.WritePropertyName(pi.Name.ToLower());
serializer.Serialize(writer, pi.GetValue(value));
}
writer.WriteEndObject();
}
}
}
If you know what the object type you are serializing is, you can write your converter to be specific to that type instead of using reflection.
When calling the custom converter, you just add it to the serializer settings.
var client = new DocumentClient(new Uri(""), "", serializerSettings: new JsonSerializerSettings
{
Converters = new List<JsonConverter>
{
new CaseConverter()
}
});