-
Notifications
You must be signed in to change notification settings - Fork 428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: rendering raw JSON #204
Comments
You could implement this feature like this: # config/initializers/jbuilder_raw.rb
require "jbuilder"
class Jbuilder
def set_raw!(attribute_name, json_string)
set! attribute_name, MultiJson.load(json_string)
end
end And in your views use like this: json.set_raw! :foo, bar |
Thanks for the reply, however this is essentially just moving what I was already doing down to the Jbuilder layer I'm trying to avoid having to deserialize the JSON string entirely. Since Jbuilder just serializes it back into JSON after it's passed in as a Ruby hash. Ideally the The conversion: JSON string -> Ruby Hash -> JSON String seems redundant to me, and slows performance if the JSON String is large/complex. |
Jbuilder is doing |
Ah okay thanks @rwz! Wanted to see how feasible a Out of curiosity, how does the |
As far as I know, partial injection of JSON is not yet implemented anywhere. |
I see, thanks again @rwz! I might try to use the cache mechanism instead of a raw json string then. Sounds like there's still some serialization/deserialization that takes place, but performance might be better. |
It would be useful to support partial injection of JSON in some case. For example, extremely high performance are required. require 'oj'
class CompiledJson
def initialize(s); @s = s; end
def to_json(*args); @s; end
def to_s; @s; end
undef_method :as_json
end
Oj.default_options = {:mode => :compat, use_to_json: true}
result = Jbuilder.new do |json|
json.a CompiledJson.new('{x: "y"}')
end
puts result.target!
# => {"a": {x: "y"}} |
@shaokun it's already there and works since forever. class CompiledJson
def initialize(s); @s = s; end
def to_json(*); @s; end
end
Jbuilder.encode do |json|
json.thing CompiledJson.new("foobar")
end # => "{\"thing\":foobar}" |
@rwz The example you given doesn't work in a newly created rails 4.1.4 project. But it seems to work in rails 4.0.x. It returns # => "{"thing":{"s":"foobar"}}" I am using oj and I followed the instruction here: ohler55/oj#140 I am also wondering it's possible to provide some API in jbuilder to support this cached json string? Something similar to the cache! API. It's difficult when I think about it, because the jbuilder is based on hash and array merging, so given an string, we will need to manually specify a key for it. |
Well, that is actually because active support changed the way it handles json since 4.1. And because it defines So, here's the code I ended up with having a very similar problem: https://gist.github.com/rwz/14c0d8a5187b69922bb4 |
Thank you @rwz that was really helpful. I wonder if support for this could be added to jbuilder though. |
Any chance jbuilder will support raw JSON in the near future? My use case is that I'm doing a postgres database query using the aggregate function 'json_agg' which conveniently returns the needed raw json. It would be great if I could just inject it into my jbuilder templates. I guess for now I'll go with the gist @RWS provided, thanks! |
So this might be too unusual a use case, but I have some pre-rendered JSON stored in memory already and I'd like to include it in my Jbuilder templates without needing to deserialize it first. Seems silly to deserialize JSON just to pass it to Jbuilder for serialization again.
What I'm doing now:
Ideally I'd like to do something like:
Is this silly?
The text was updated successfully, but these errors were encountered: