dewski/json_builder

handling escape characters

Closed this issue · 6 comments

How to parse text coming like this -
""

\r\n\tStrong knowledge of GAAP and Basel standards for valuing snakes.

\r\n""

I am facing these two issues while using
json (1.6.5, 1.5.3)
json_builder (3.1.4)
json_pure (1.5.3)

Here is my sample app that works with your first issue:

app/controllers/jobs_controller.rb

class JobsController < ApplicationController
  respond_to :json

  def index
    @jobs = Job.all
    # additional example @jobs = []
  end
end

app/views/jobs/index.json.json_builder

results @jobs do |job|
  description job.body
end

Responses:

{ "results": [{ "description": "test" }] }

// And for the empty array test
{ "results": [] }

Could you provide the full backtrace to your error and the source of your JobsController for me?

As for issue 2, I don't understand your question, could you rephrase it? More specifically how you intend to use that string. You are able to just put that as the value in a JSON key/value pair.

Issue 2 :

In database column description i am saving text like this :

<p>
Find new snake-handlers and make sure they stay happy and challenged.</p>

This is showing like this on screen when used job.description :
""

\r\n\tFind new snake-handlers and make sure they stay happy and challenged.

\r\n""

Per your back trace, you are using the API::V1::JobsController, but in the code you posted it's just JobsController. Did you modify it before pasting it here? No issue with that, just may help with finding the issue since I am not sure how that controller would render app/views/jobs/index.json.json_builder when you are using the API::V1 namespace.

Additionally...

  • Could you provide the version of both Ruby and Rails you are using?
  • What is returned in rails console when you type Job.all and Job.count?

If none of that makes any difference, could you create a sample application and post that in your profile?

Thanks!

Regarding issue #2, I am also unable to reproduce this:

{
  "results": [
    {
      "title": "Rails guy",
      "description": "<p>\nFind new snake-handlers and make sure they stay happy and challenged.</p>"
    }
  ]
}

Going to close this due to lack of example support.