dewski / json_builder

Rails provides an excellent XML Builder by default to build RSS and ATOM feeds, but nothing to help you build complex and custom JSON data structures. JSON Builder is here to help.
http://garrettbjerkhoel.com/json_builder/
MIT License
243 stars 52 forks source link

handling escape characters #30

Closed srinatha closed 12 years ago

srinatha commented 12 years ago

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)

dewski commented 12 years ago

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

_app/controllers/jobscontroller.rb

class JobsController < ApplicationController
  respond_to :json

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

_app/views/jobs/index.json.jsonbuilder

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?

dewski commented 12 years ago

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.

srinatha commented 12 years ago

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\""

dewski commented 12 years ago

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...

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

Thanks!

dewski commented 12 years ago

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>"
    }
  ]
}
dewski commented 12 years ago

Going to close this due to lack of example support.