How to output JSON in Rails without escaping back slashes

Viewed 3752

I need to output some JSON for a customer in a somewhat unusual format. My app is written with Rails 5.

Desired JSON:

{
  "key": "\/Date(0000000000000)\/"
}

The timestamp value needs to have a \/ at both the start and end of the string. As far as I can tell, this seems to be a format commonly used in .NET services. I'm stuck trying to get the slashes to output correctly.

I reduced the problem to a vanilla Rails 5 application with a single controller action. All the permutations of escapes I can think of have failed so far.

def index
  render json: {
    a: '\/Date(0000000000000)\/',
    b: "\/Date(0000000000000)\/",
    c: '\\/Date(0000000000000)\\/',
    d: "\\/Date(0000000000000)\\/"
  }
end

Which outputs the following:

{
    "a": "\\/Date(0000000000000)\\/",
    "b": "/Date(0000000000000)/",
    "c": "\\/Date(0000000000000)\\/",
    "d": "\\/Date(0000000000000)\\/"
}

For the sake of discussion, assume that the format cannot be changed since it is controlled by a third party.

I have uploaded a test app to Github to demonstrate the problem. https://github.com/gregawoods/test_app_ignore_me

3 Answers
Related