ofxHttp server and json

Hi,

I have a project using ofxHttp as a server. It works fine for receiving simple string messages but when I try to send over a json string, somewhere along the line, the escaped characters get messed up. For example:

I send:

string test = "{ \"hello\" : \"world\" }"; //(this is in c#)

And in my OF app I receive this:

"{ \\hello\\ : \\world\\ }" //(this is in OF)

@bakercp any ideas?

EDIT: I can get round this issue by doing:

ofStringReplace(args, "\\", "\"");

But would still like to know why it’s happening if anyone knows.

Hi @csw – this probably has to do with the way that your c# client is converting/escaping symbols internally (like "), etc before sending. Without code to test, I’m really not sure …

What C# code are you using to send the string?

Is this any help:

via http://stackoverflow.com/a/18104891

public static string EscapeStringValue(string value)
{
    const char BACK_SLASH = '\\';
    const char SLASH = '/';
    const char DBL_QUOTE = '"';

    var output = new StringBuilder(value.Length);
    foreach (char c in value)
    {
        switch (c)
        {
            case SLASH:
                output.AppendFormat("{0}{1}", BACK_SLASH, SLASH);
                break;

            case BACK_SLASH:
                output.AppendFormat("{0}{0}", BACK_SLASH);
                break;

            case DBL_QUOTE:
                output.AppendFormat("{0}{1}",BACK_SLASH,DBL_QUOTE);
                break;

            default:
                output.Append(c);
                break;
        }
    }

    return output.ToString();
}

Thanks @bakercp it turns out I was stripping all of the double quotes somewhere deep down in the code! All working as it should be now!

1 Like