ToJsv() Extension Method Inconsistency

Hey Mythz - I am not sure if this is expected behavior or not, but I wanted to flag this because it caused me a whole day of confusion until by chance I discovered the issue.

ServiceStack.Text has an extension .ToJsv() which encodes the specified object into Jsv()

We have a utility method for generating URLs which basically just encoded each key value pair into a query string value using JSV.

99% of the time this was working fine, however ONCE in a while, we would notice that when the incoming request came into ServiceStack from that generated URL, string values would be surrounded in double quotes AFTER deserialization.

I tracked this down to when we had URLs that were param values, it was surrounding them with quotes because of the invalid characters which kinda goes with what it says on the JSV page, however, here is where the inconsistency shows up, if you use ToJsv on a string value with invalid characters, it surrounds the whole thing in double quotes, however, if you cast that string to an object and call .ToJsv() on it, this does not happen.

I confirmed this with a unit test just to be 100% sure

Knowing this I am able to continue on and achieve my desired results, however, this seems a little bit odd to me that just casting the string to an object results in a different ToJsv() value and I believe this should at least be documented more clearly if this is the desired behavior.

Thanks Mythz

JSV (like CSV) uses double quotes when the value has a character that it needs escape, in this case it’s the :.

Right but why is it that when I cast the string to an object ToJsv() has a different result?

Can you post the source code text (i.e. instead of a screenshot) of your test so I can run it.

    [Test]
    public void SerializeUri()
    {
        const string url = "https://api.twilio.com/2010-04-01/Accounts/AC4f105e1eb6c4aeced5a569ff20d3f282/Messages/MMb6f82432208ab746e78552d27aad2c9d/Media/MEe9cfcc580ccb0b55bd53d0fff46b43cc";
        var stringEncoded = url.ToJsv();
        var objEncoded = ((object)url).ToJsv();
        Console.WriteLine($$"string:{stringEncoded}\r\nobject:{objEncoded}");
        Assert.AreEqual(stringEncoded, objEncoded);
    }

Pasting screwed up the Console.WriteLine (I think because it uses $ interpolation) but you can just comment that out

Yeah you need to double-quote $$$ to escape it in discourse.

This has been resolved to always encode string values in this commit. This change is available from the latest v5.5.1 that’s now on MyGet.

Thank you as always!

1 Like