Glimpse/Glimpse — pull request #457 by CGijbels (2013-07-14 19:49:43+00:00)
Title: Improve the serialization process so that custom application objects can be serialized conditionally

Triggered by issue #386, there seems to be a need to improve the serialization process so that custom application objects can be serialized and made available (if wanted) on the client. The current trend was to remove serialization of custom application objects, since there are objects that just can't be serialized and might end up breaking Glimpse all together.

Therefore this pull request will be used for discussion about the feature and the actual implementation, with the goal to merge it into master when completed.

As a starting point [comment from the issue above](https://github.com/Glimpse/Glimpse/issues/386#issuecomment-20309818) will be used:

We could try to solve all these serialization issues (opt-in, opt-out) by using `ISerializationConverter` types so that you should be safe calling `ISerializer.Serialize` anywhere in the code, which could result in a serialization pipeline as follows:

- The serializer will try to serialize the instance, but will first look for a `ISerializationConverter` matching the instance's type
- The first possible match is a `SerializationDeniedSerializationConverter` which will always be the first in the list, allowing users to add types they don't want to allow to be serialized, even though they could (for security reasons for instance)
- Then the next possible match could be found in a list of explicitly added `ISerializationConverter` instances by the user, so that they are allowed to do some custom formatting not provided by options of the catch-all converter
- Then the next possible match could be found in a list added by Glimpse for Glimpse known types
- Then if no previous `ISerializationConverter` instances can handle the given `Type`, the Glimpse `CatchAllSerializationConverter` kicks in, which will indicate whether it can handle the serialization. Most likely, as it is a catch-all, it is supposed to say `true` but we could add a white-list check over here. If the given type is member of the white-list, then we return `false` and let the serializer do its thing.

If the catch-all would be invoked then the output will be determined like it is more or less already available inside the `SessionModelConverter`:
- Checks if the type is Serializable
- Checks if the type has a ToString method
- Any other check Glimpse might come up with in the future like support for `ISerializable`
- Last resort: return "Instance can not be serialized"

The `JsonNetSerializationConverterAdapter` might need to be adapted so that the `CanConvert` method is not only checking a list of `SupportedTypes` like it does now:

```CSharp
return Converter.SupportedTypes.Any(type => type.IsAssignableFrom(objectType));
```

but the check is delegated to the Converter as in

```CSharp
return Converter.CanConvert(objectType);
```

That way the implementation of such a converter can be any kind of logic, like the one needed by such a catch-all, but by default it will do the check that exists today, because in the end we need a boolean indicating whether or not to call `Converter.Convert(value)`