Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

ready for 1.1.0 release

  • Loading branch information...
commit 12ea8e6359e624160952b3894d7f412497dd19c1 1 parent 4023697
Peter Ohler authored
View
2  README.md
@@ -30,6 +30,8 @@ A fast JSON parser and Object marshaller as a Ruby gem.
- Oj can now mimic JSON. With some expections calling JSON.mimic_JSON will allow all JSON calls to use OJ instead of JSON. This gives a speedup of more than 2x on parsing and 5x for generating over the JSON::Ext module.
+ - Oj::Doc now allows a document to be left open and then closed with the Oj::Doc.close() class.
+
## <a name="description">Description</a>
Optimized JSON (Oj), as the name implies was written to provide speed
View
37 ext/oj/fast.c
@@ -1073,14 +1073,20 @@ each_value(Doc doc, Leaf leaf) {
/* call-seq: open(json) { |doc| ... } => Object
*
- * Parses a JSON document String and then yields to the provided block with an
- * instance of the Oj::Doc as the single yield parameter.
+ * Parses a JSON document String and then yields to the provided block if one
+ * is given with an instance of the Oj::Doc as the single yield parameter. If
+ * a block is not given then an Oj::Doc instance is returned and must be
+ * closed with a call to the #close() method when no longer needed.
*
* @param [String] json JSON document string
* @yieldparam [Oj::Doc] doc parsed JSON document
* @yieldreturn [Object] returns the result of the yield as the result of the method call
* @example
* Oj::Doc.open('[1,2,3]') { |doc| doc.size() } #=> 4
+ * # or as an alternative
+ * doc = Oj::Doc.open('[1,2,3]')
+ * doc.size() #=> 4
+ * doc.close()
*/
static VALUE
doc_open(VALUE clas, VALUE str) {
@@ -1108,8 +1114,10 @@ doc_open(VALUE clas, VALUE str) {
/* call-seq: open_file(filename) { |doc| ... } => Object
*
- * Parses a JSON document from a file and then yields to the provided block
- * with an instance of the Oj::Doc as the single yield parameter.
+ * Parses a JSON document from a file and then yields to the provided block if
+ * one is given with an instance of the Oj::Doc as the single yield
+ * parameter. If a block is not given then an Oj::Doc instance is returned and
+ * must be closed with a call to the #close() method when no longer needed.
*
* @param [String] filename name of file that contains a JSON document
* @yieldparam [Oj::Doc] doc parsed JSON document
@@ -1117,6 +1125,10 @@ doc_open(VALUE clas, VALUE str) {
* @example
* File.open('array.json', 'w') { |f| f.write('[1,2,3]') }
* Oj::Doc.open_file(filename) { |doc| doc.size() } #=> 4
+ * # or as an alternative
+ * doc = Oj::Doc.open_file(filename)
+ * doc.size() #=> 4
+ * doc.close()
*/
static VALUE
doc_open_file(VALUE clas, VALUE filename) {
@@ -1148,7 +1160,7 @@ doc_open_file(VALUE clas, VALUE filename) {
}
fclose(f);
json[len] = '\0';
- obj = parse_json(clas, json, 1, allocate); // TBD check given
+ obj = parse_json(clas, json, given, allocate);
if (given && allocate) {
xfree(json);
}
@@ -1538,7 +1550,14 @@ doc_size(VALUE self) {
return ULONG2NUM(((Doc)DATA_PTR(self))->size);
}
-/* TBD
+/* call-seq: close() => nil
+ *
+ * Closes an open document. No further calls to the document will be valid
+ * after closing.
+ * @example
+ * doc = Oj::Doc.open('[1,2,3]')
+ * doc.size() #=> 4
+ * doc.close()
*/
static VALUE
doc_close(VALUE self) {
@@ -1592,8 +1611,10 @@ doc_close(VALUE self) {
* end
* #=> 2
*
- * # Now try again using a path to Oj::Doc.fetch() directly.
- * Oj::Doc.open(json) { |doc| doc.fetch('/2/three') } #=> 3
+ * # Now try again using a path to Oj::Doc.fetch() directly and not using a block.
+ * doc = Oj::Doc.open(json)
+ * doc.fetch('/2/three') #=> 3
+ * doc.close()
*/
void
oj_init_doc() {
View
12 notes
@@ -4,12 +4,6 @@
^c^s show subtree
- next
- - implement Oj::Doc.new or open without a block given
- - document
- - close
- - open for new use
- - readme
- - sample
- write JSON compatible extension
- attributes - ignore for now
@@ -88,9 +82,3 @@
- dump
- support stream as arg
- always dump to stream/file if possible (check performance)
-
- - example
- - contacts
- - pull apart input file with multiple contacts and write individual files
- - phone book get only name and phone
-
View
19 test/perf_fast.rb
@@ -18,6 +18,7 @@
$gets = 0
$fetch = false
$write = false
+$read = false
opts = OptionParser.new
opts.on("-v", "verbose") { $verbose = true }
@@ -26,6 +27,7 @@
opts.on("-g", "--gets [Int]", Integer, "number of gets") { |i| $gets = i }
opts.on("-f", "fetch") { $fetch = true }
opts.on("-w", "write") { $write = true }
+opts.on("-r", "read") { $read = true }
opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
files = opts.parse(ARGV)
@@ -139,6 +141,23 @@ def capture_error(tag, orig, load_key, dump_key, &blk)
end
end
+if $read
+ puts '-' * 80
+ puts "JSON read from file Performance"
+ Oj::Doc.open($json) { |doc| doc.dump(nil, 'oj.json') }
+ File.open('yajl.json', 'w') { |f| Yajl::Encoder.encode($obj, f) }
+ JSON.generator = JSON::Ext::Generator
+ File.open('json_ext.json', 'w') { |f| f.write(JSON.fast_generate($obj)) }
+ Oj::Doc.open($json) do |doc|
+ perf = Perf.new()
+ perf.add('Oj::Doc', 'open_file') { ::Oj::Doc.open_file('oj.json') }
+ perf.add('Yajl', 'decode') { Yajl::decoder.decode(File.read('yajl.json')) }
+ perf.add('JSON::Ext', '') { JSON.parse(File.read('json_ext.json')) }
+ perf.before('JSON::Ext') { JSON.parser = JSON::Ext::Parser }
+ perf.run($iter)
+ end
+end
+
unless $failed.empty?
puts "The following packages were not included for the reason listed"
$failed.each { |tag,msg| puts "***** #{tag}: #{msg}" }
View
19 test/test_fast.rb
@@ -328,6 +328,25 @@ def test_open_close
end
end
+ def test_file_open_close
+ filename = 'open_file_test.json'
+ File.open(filename, 'w') { |f| f.write('{"a":[1,2,3]}') }
+ doc = Oj::Doc.open_file(filename)
+ assert_equal(Oj::Doc, doc.class)
+ assert_equal(5, doc.size)
+ assert_equal('/', doc.where?)
+ doc.move('a/1')
+ doc.home()
+ assert_equal(2, doc.fetch('/a/2'))
+ assert_equal(2, doc.fetch('a/2'))
+ doc.close()
+ begin
+ doc.home()
+ rescue Exception => e
+ assert(true)
+ end
+ end
+
def test_dump
Oj::Doc.open('[1,[2,3]]') do |doc|
assert_equal('[1,[2,3]]', doc.dump())
Please sign in to comment.
Something went wrong with that request. Please try again.