This works very well for overriding the serialization of byte arrays, and is fine for now, however I would like the ability to specify certain properties to be serialized to hex whilst others are their default Base64.
Is there a function in which the DTO Property is passed to SerializeFn which I can use to check for an attribute on serialization?
So the caching was the issue, by doing the following:
JsConfig<byte[]>.SerializeFn = Encoders.Hex.GetString;
JsConfig<byte[]>.DeSerializeFn = Encoders.Hex.GetBytes;
var appHost = new SelfHostingServices(container);
It sets up the JsConfig before the AppHost is instantiated and caches the Request deserializers.
You can ignore my request Working fine now!
==============
Hi mythz,
So your method does work for Serializing DTO’s, however I’m having a problem with the following route:
[Route("/test/{Bytes}")]
public class TestRequest
{
public byte[] Bytes { get; set; }
}
When I call this with a GET request, using Base64 it works fine, but when I call it with Hex it does not call the custom function.
However, if you do the following:
JsConfig<byte[]>.SerializeFn = Encoders.Hex.GetString;
JsConfig<byte[]>.DeSerializeFn = Encoders.Hex.GetBytes;
var fn = JsvReader.GetParseFn(typeof (byte[]));
var result = fn.Invoke("10ED0F8F7");
It does parse it correctly!
I’ve tracked this deserialization of the DTO to the JsvReader via the StringMapTypeDeserializer, it appears it should ask JsConfig for the DeSerializeFn the same as my example above, but for some reason it returns System.ConvertFromBase64.
Would you mind having a look at this?
Edit: I have tried calling JsvReader.GetParseFn with the serializers correctly set up before any routes come in to stop it caching the wrong method.