(Go: >> BACK << -|- >> HOME <<)

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flexible Serialization #344

Open
wekempf opened this issue Aug 16, 2017 · 0 comments
Open

Flexible Serialization #344

wekempf opened this issue Aug 16, 2017 · 0 comments

Comments

@wekempf
Copy link
wekempf commented Aug 16, 2017

I commented on #304 but figured it would be best to flesh out the ideas and propose them separately. By adding two base attributes and an interface we'd gain full control over how responses are deserialized to .NET types.

The first attribute would control the deserialization process from the beginning. (This code is typed into a browser, so don't expect it to compile.)

[AttributeUsage(AttributeTargets.Class)]
public abstract class TypeSerializationAttribute : Attribute
{
    public virtual object Deserialize(Type resultType, HttpResponseMessage message);
}

When it comes time to deserialize you'd look for an attribute on the type that derives from this. If one exists then deserialization is deffered to it, otherwise you'd continue to do JSON deserialization.

Next up is an attribute that would allow you to "post" serialize a value from the response. An example of this would be a HeaderAttribute to parse out a value from the headers.

[AttributeUsage(AttributeTargets.Property)]
public abstract class ValueSerializationAttribute : Attribute
{
   public virtual object Deserialize(PropertyInfo property, HttpResponseMessage message);
}

Finally the interface from the PR would be your final hook into the deserialization process. Given these three hooks there really shouldn't be any restrictions in what you can do to provide custom serialization, and in a manner that's flexible. Just occurred to me (because I was thinking an interface for deserialization would be simpler than the attribute based approach in some cases), you don't really have to supply the interface based hook at all, since an attribute based approach could give this to you just by using a custom attribute. If that approach were used then a PostProcessAttribute would make it easier, even though technically the TypeSerializationAttribute above would be enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant