Reputation: 103
I searched, but couldn't find a satisfying answer. I know there are serializers like: DataContractJsonSerializer, JavaScriptSerializer to do that. But these required a weird requirement of specifying 'KnownType', which is not possible in my scenario as object i am serializing belongs to generic library, and actual implementations of interface could come from client. Any suggestions ?
Upvotes: 1
Views: 105
Reputation: 3889
Json.Net
has a method SerializeObject
. This would do the job for you.
It's signature is:
public static string SerializeObject(object value);
usage:
using Newtonsoft.Json;
...
string jsonData = JsonConvert.SerializeObject(obj);
Upvotes: 0
Reputation: 15053
ServiceStack has a JSON serializer which doesn't require specifying the known types:
var json = JsonSerializer.SerializeToString(myObj);
Or using extension methods you can call ToJson on any object:
var json = myObj.ToJson();
Upvotes: 1