0

I have an object that I convert to json in order to send it to a web service. I added a method to the class that returns a json string

public string ToJson()
{
    return new JavaScriptSerializer().Serialize(this).ToLower();
}

The HTTPClient.PutAsync() method takes a StringContent object, which I create like so:

var content = new StringContent(object.ToJson(), Encoding.UTF8, "application/json");

I can call PutAsync() and everything works fine.


I recently found a different serialisation method that uses [DataContract], [DataMember] , DataContractJsonSerializer and a Stream to serialize an object. I would like to use this method instead, because it gives more control of the result with the attributes, but requires a lot more boilerplate code (writing to stream, repositioning, reading, etc.).

Given that I used the Data attributes to specify how my object should be serialized, how can I specify that the DataContractJsonSerializer should be used to serialize it? Preferably, I can simply pass the object to StringContent without an explicit method call, like so:

var content = new StringContent(object, Encoding.UTF8, "application/json");

Similar to how ToString() is implicitly called in certain situations, I'd like to know if there's anything that understands that if I specify the content type to be "application/json", that the passed object should be serialized into json.

dbc
  • 104,963
  • 20
  • 228
  • 340
null
  • 5,207
  • 1
  • 19
  • 35

1 Answers1

1

You could try to adapt the ToJson method so that it uses the DataContractJsonSerializer instead:

public string ToJson()
{
    var serializer = new DataContractJsonSerializer(this.GetType());
    using (var stream = new MemoryStream())
    {
        serializer.WriteObject(stream, this);
        return Encoding.UTF8.GetString(stream.ToArray());
    }
}

By the way, have you considered using the Newtonsoft.Json library? It also provides you with lots of control over the serialization process with the [JsonProperty] attributes and custom converters:

public string ToJson()
{
    return JsonConvert.SerializeObject(this);
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • But neither of this will be called implicitly, right? I'd like to feed objects into the HTTPClient directly, without the need to call a custom method that does the serialisation. Yes, I did consider using a third party library for this and decided that it's pointless in my situation and that the built in ways are more than sufficient. – null Jan 04 '16 at 13:43
  • The closest you could get is using the [`HttpClient.PostAsJsonAsync`](https://msdn.microsoft.com/en-us/library/hh944521(v=vs.118).aspx) extension method which allows you to directly pass some instance but under the hood this will use the Newtonsoft.Json library for serializing the object and there's not much you could do about it. – Darin Dimitrov Jan 04 '16 at 13:44
  • That's disappointing. Thanks for your help converting to the contract serializer though. – null Jan 04 '16 at 13:52