I’m trying to write documents into CouchDB in .NET, but my I don’t want to mark all of my Id properties with [JsonProperty(MemberName = "_id")], since I need to serialize them normally (with the member name being Id) to send them out via my own HTTP API.
Basically, I want this:
var serializer = new JsonSerializer();
/* some magic happens */
serializer.Serialize(textWriter, new Thing { Id = "foo", Value = "bar" });
To result in this:
{_id:"Foo",Value:"Bar"}
But without the magic joojoo, it should still be this:
{Id:"Foo",Value:"Bar"}
I’m assuming that this shouldn’t be too hard, but I don’t know my way around the internals of Json.Net well enough to Just Do It™.
First create a ContractResolver:
Then add the magic by attaching it:
And you get: