Skip to content
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

Composing JSON with objects #129

Closed
kuon opened this issue Dec 18, 2013 · 8 comments
Closed

Composing JSON with objects #129

kuon opened this issue Dec 18, 2013 · 8 comments

Comments

@kuon
Copy link

kuon commented Dec 18, 2013

I am using some PostgreSQL methods to generate some bits of json that I'd like to compose with objects.

Right now I can do something like:

require 'oj'

json = %|
  {
    "some": "json",
    "might": "be large"
  }
|

obj = {
  :rubyobject => "A value",
  :importjson => Oj.load(json, :mode => :compat)
}

Oj.dump(obj, :mode => :compat)

I'd like to do the same, but without decoding and re-encoding the JSON document.

@ohler55
Copy link
Owner

ohler55 commented Dec 19, 2013

Let me make sure I understand correctly. I might have a quick addition that would give this to you.

You would like to build a JSON document and insert another document in the one you are creating. Did I understand correctly?

If so, take a look at the newly added Oj::StringWriter. It allows you to build a JSON document one element at a time. I could extend that to add an existing JSON document as a value. Simple addition if the StringWriter works for you.

@kuon
Copy link
Author

kuon commented Dec 19, 2013

Yes something like this would work:

require 'oj'

writer = Oj::StringWriter.new

writer.push_object
writer.push_value("A value", 'rubyobject')
writer.push_document('{"some":"json fragment"}', 'jsonfragment') # New API
writer.pop

puts writer.to_s

# Should print something like:

{"rubyobject":"A value","jsonfragment":{"some":"json fragment"}} 

@ohler55
Copy link
Owner

ohler55 commented Dec 19, 2013

Cool, I'll let you know when it is added. Will most likely call it push_json(). Try to get it in in the next day or two if not tonight.

@kuon
Copy link
Author

kuon commented Dec 19, 2013

Yes push_json makes sense. Thanks for your time.

@ohler55
Copy link
Owner

ohler55 commented Dec 19, 2013

Try out release 2.5.0.

@kuon
Copy link
Author

kuon commented Dec 20, 2013

I tested release 2.5.1 and it works as expected.

I'm going to use it to compose a large json document (about 100MB out of a PG DB) that is currently decoded and re-encoded, I hope for a speed boost. I'll let you know.

@kuon
Copy link
Author

kuon commented Dec 21, 2013

For info I got a huge performance gain over decode/reencode. Thanks a lot.

@kuon kuon closed this as completed Dec 21, 2013
@ohler55
Copy link
Owner

ohler55 commented Dec 21, 2013

Great, glad it helped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants