Custom Deserializer string format

I have registered custom deserializer:

JsConfig.Init(new ServiceStack.Text.Config  {   });

JsConfig<BlockPage>.DeSerializeFn = ConfigureSerialization.Deserialize;

But inside the deserialize function the value passed is not a valid JSON string.

I’t’s definitely a valid string when it’s passed into:

var response = await httpClient.GetAsync(requestUri);
var content = await response.Content.ReadAsStringAsync();
var page = content.FromJson<BlockPage>(); //in debugger is valid json string

but inside the Deserialize function it looks like it has some sort of escaping and is not a valid json string.

public static BlockPage Deserialize(string value)
{
    //doesn't work because not a valid json string
    var serial = value.FromJson<BlockPage>(); 

    return serial;
}

What am I missing?

Can you provide the class definition of BlockPage and and an example of the invalid JSON you are seeing?

Seems the issue is the original json string has a field that contains a html escaped property with a large json object in it. The json validates in json lint so it is escaped properly in response from API.

//...
	"content": {
		"rendered": "\n\n{&#8220;headline&#8221;:&#8221;sxsxsxsx&#8221;,&#8221;sales_text&#8221;:&#8221;<p>sxxsxsxs<\\\/p>\\n&#8221;,&#8221;person_details&#8221;:[{&#8220;person_images&#8221;:{&#8220;person_image&#8221;:   etc....

When it gets passed into the deserialize function the value parameter seems to have had another escaping process applied to it and it’s no longer a valid json string. The BlockPage class is irrelevant as the string passed to is not a valid json string so will fail regardless.

Please provide a repro when reporting issues if you want us to be able to look into it.

Here you go: GitHub - GuerrillaCoder/serial-problem

For your use case you will want to be using the JsConfig<T>.RawDeserializeFn override instead. Difference between DeSerializeFn and RawDeserializeFn is that DeSerializeFn assumes that the string will need to be unescaped whilst RawDeserializeFn doesn’t which is what is causing the issue.

1 Like