How can I return json from my WCF rest service (.NET 4), using Json.Net, without it being a string, wrapped in quotes?

I finally figured out a solution to this. It’s not what I would have preferred (which would be to return the specific object type, and somehow instruct WCF to use a Json.Net serializer, instead of the DataContractJsonSerializer), but it is working great, and it’s simple and clear. Extending my contrived example using this new solution: … Read more

Ignore a property during xml serialization but not during deserialization

This is the solution outlined by Manoj: If you want to suppress serialization of a specific property Foo, but still be able to deserialize it, you can add a method public bool ShouldSerializeFoo() that always returns false. Example: public class Circle2 { public double Diameter { get; set; } public double Radius { get { … Read more

NewtonSoft.Json Serialize and Deserialize class with property of type IEnumerable

You don’t need to use JsonConverterAttribute, just keep your model clean and use CustomCreationConverter instead, the code is simpler: public class SampleConverter : CustomCreationConverter<ISample> { public override ISample Create(Type objectType) { return new Sample(); } } Then: var sz = JsonConvert.SerializeObject( sampleGroupInstance ); JsonConvert.DeserializeObject<SampleGroup>( sz, new SampleConverter()); Documentation: Deserialize with CustomCreationConverter

how come BinaryFormatter can serialize an Action but Json.net cannot

The reason that BinaryFormatter is (sometimes) able to round-trip an Action<T> is that such delegates are marked as [Serializable] and implement ISerializable. However, just because the delegate itself is marked as serializable doesn’t mean that its members can be serialized successfully. In testing, I was able to serialize the following delegate: Action<int> a1 = (a) … Read more

SignalR Typenamehandling

This can be done by taking advantage of the fact that your types and the SignalR types are in different assemblies. The idea is to create a JsonConverter that applies to all types from your assemblies. When a type from one of your assemblies is first encountered in the object graph (possibly as the root … Read more

tech