Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return json result with unicode characters escaped as \u1234

I'm implementing a method that is returning a json result like:

public JsonResult MethodName(Guid key){
    var result = ApiHelper.GetData(key); //Data is stored in db as varchar with åäö
    return Json(new { success = true, data = result },"application/json", Encoding.Unicode, JsonRequestBehavior.AllowGet );
}

The displayed result:

{"success":true,"data":[{"Title":"Here could be characters like åäö","Link":"http://www.url.com/test",...},

But I would like to display it like:

{"success":true,"data":[{"Title":"Here could be characters like \u00e5\u00e4\u00f6","Link":"http:\/\/www.url.com\/test",...},

How can I accomplish this? Can I convert it, parse it or change the responseEncoding in web.config to get it to display unicode characters?

like image 883
Maxelsson Avatar asked Oct 09 '22 01:10

Maxelsson


1 Answers

There is no need for escaping if the client is a web browser, or any other client that handles http correctly, as long as your server correctly tells the client about content type and content encoding, and the encoding you select supports the codepoints in the outgoing data.

If the client does not behave correctly, and it really needs the strings to be escaped like that, you will have to write your own ActionResult class and do the escaping yourself. Inherit from JsonResult to start with, and use reflection to create the JSON document as you like it.

It's a chore!

EDIT: This will get you started

public class MyController : Controller {
    public JsonResult MethodName(Guid key){
        var result = ApiHelper.GetData(key);
        return new EscapedJsonResult(result);
    }
}

public class EscapedJsonResult<T> : JsonResult {
    public EscapedJsonResult(T data) {
        this.Data = data;
        this.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
    }

    public override ExecuteResult(ControllerContext context) {
        var response = context.HttpContext.Response;

        response.ContentType = "application/json";
        response.ContentEncoding = Encoding.UTF8;

        var output = new StreamWriter(response.OutputStream);

        // TODO: Do some reflection magic to go through all properties
        // of the this.Data property and write JSON to the output stream
        // using the StreamWriter

        // You need to handle arrays, objects, possibly dictionaries, numbers,
        // booleans, dates and strings, and possibly some other stuff... All
        // by your self!
    }

    // Finds non-ascii and double quotes
    private static Regex nonAsciiCodepoints =
        new Regex(@"[""]|[^\x20-\x7f]");

    // Call this for encoding string values
    private static string encodeStringValue(string value) {
        return nonAsciiCodepoints.Replace(value, encodeSingleChar);
    }

    // Encodes a single character - gets called by Regex.Replace
    private static string encodeSingleChar(Match match) {
        return "\\u" + char.ConvertToUtf32(match.Value, 0).ToString("x4");
    }
}
like image 100
Anders Marzi Tornblad Avatar answered Oct 13 '22 10:10

Anders Marzi Tornblad