oj 3.11.5 → 3.11.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 964a9272c141179cf6f61317e89bc8dab43184b0d143355dc2212b89f123ccad
4
- data.tar.gz: 53a3b3cd773a87953cec71a649f491682b022c13d56ebcc4eb5653bc9cbb560e
3
+ metadata.gz: bec8df6a55ce34bd8b34ffb5eaa161d4bad478cc6a3276b48fb7348c80f5b819
4
+ data.tar.gz: e0c588f13d959ed54c4b70f0773796a2c8b0a34650b5196b8ddd09ddd0ab8ea0
5
5
  SHA512:
6
- metadata.gz: 947dad8a5cfc4d5adb3efbd40218439c984ae6084ef304f6b3012e26e047438a03b6f149b7c224982fc8fd0a699b56fa0e6b9a174e8b00d6567dd911f601f227
7
- data.tar.gz: 536b05addb483b631883ccfb9269c6cd93b3e988ea9845021be3e2383a708974ff4e63a9d64a1a76bd8a9934969c42aa16495eccd75e88b7cf4704f619e7b693
6
+ metadata.gz: 99166b4812d71dda9d90b209765772c5d730b51820a57277588b34bd16f84251490f28abdc212c604e8ea90941fb311f0e86060fe021354d8936bf96f40ed07a
7
+ data.tar.gz: 194812a60a88b0a705ab21180bb61961f841571c85a8fa58823d14d9539be772f0fefc812c864ccfcc18bf928ddc83815c92fa039d52c79da1b348442c70a394
data/README.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # [![{}j](http://www.ohler.com/dev/images/oj_comet_64.svg)](http://www.ohler.com/oj) gem
2
2
 
3
- [![Build Status](https://img.shields.io/travis/ohler55/oj/master.svg?logo=travis)](http://travis-ci.org/ohler55/oj?branch=master) ![Gem](https://img.shields.io/gem/v/oj.svg) ![Gem](https://img.shields.io/gem/dt/oj.svg) [![SemVer compatibility](https://api.dependabot.com/badges/compatibility_score?dependency-name=oj&package-manager=bundler&version-scheme=semver)](https://dependabot.com/compatibility-score.html?dependency-name=oj&package-manager=bundler&version-scheme=semver) [![TideLift](https://tidelift.com/badges/github/ohler55/oj)](https://tidelift.com/subscription/pkg/rubygems-oj?utm_source=rubygems-oj&utm_medium=referral&utm_campaign=readme)
3
+ [![Build Status](https://img.shields.io/github/workflow/status/ohler55/oj/CI?logo=github)](https://github.com/ohler55/oj/actions/workflows/CI.yml)
4
+ ![Gem](https://img.shields.io/gem/v/oj.svg)
5
+ ![Gem](https://img.shields.io/gem/dt/oj.svg)
6
+ [![SemVer compatibility](https://api.dependabot.com/badges/compatibility_score?dependency-name=oj&package-manager=bundler&version-scheme=semver)](https://dependabot.com/compatibility-score.html?dependency-name=oj&package-manager=bundler&version-scheme=semver)
7
+ [![TideLift](https://tidelift.com/badges/github/ohler55/oj)](https://tidelift.com/subscription/pkg/rubygems-oj?utm_source=rubygems-oj&utm_medium=referral&utm_campaign=readme)
4
8
 
5
9
  A *fast* JSON parser and Object marshaller as a Ruby gem.
6
10
 
data/ext/oj/fast.c CHANGED
@@ -80,22 +80,6 @@ static Leaf get_doc_leaf(Doc doc, const char *path);
80
80
  static Leaf get_leaf(Leaf *stack, Leaf *lp, const char *path);
81
81
  static void each_value(Doc doc, Leaf leaf);
82
82
 
83
- static void doc_init(Doc doc);
84
- static void doc_free(Doc doc);
85
- static VALUE doc_open(VALUE clas, VALUE str);
86
- static VALUE doc_open_file(VALUE clas, VALUE filename);
87
- static VALUE doc_where(VALUE self);
88
- static VALUE doc_local_key(VALUE self);
89
- static VALUE doc_home(VALUE self);
90
- static VALUE doc_type(int argc, VALUE *argv, VALUE self);
91
- static VALUE doc_fetch(int argc, VALUE *argv, VALUE self);
92
- static VALUE doc_each_leaf(int argc, VALUE *argv, VALUE self);
93
- static VALUE doc_move(VALUE self, VALUE str);
94
- static VALUE doc_each_child(int argc, VALUE *argv, VALUE self);
95
- static VALUE doc_each_value(int argc, VALUE *argv, VALUE self);
96
- static VALUE doc_dump(int argc, VALUE *argv, VALUE self);
97
- static VALUE doc_size(VALUE self);
98
-
99
83
  VALUE oj_doc_class = Qundef;
100
84
 
101
85
  // This is only for CentOS 5.4 with Ruby 1.9.3-p0.
@@ -899,12 +883,14 @@ static Leaf get_leaf(Leaf *stack, Leaf *lp, const char *path) {
899
883
  } else {
900
884
  return 0;
901
885
  }
902
- } else if (COL_VAL == leaf->value_type && 0 != leaf->elements) {
886
+ } else if (NULL == leaf->elements) {
887
+ leaf = NULL;
888
+ } else if (COL_VAL == leaf->value_type) {
903
889
  Leaf first = leaf->elements->next;
904
890
  Leaf e = first;
905
891
  int type = leaf->rtype;
906
892
 
907
- leaf = 0;
893
+ leaf = NULL;
908
894
  if (T_ARRAY == type) {
909
895
  int cnt = 0;
910
896
 
@@ -929,6 +915,7 @@ static Leaf get_leaf(Leaf *stack, Leaf *lp, const char *path) {
929
915
  const char *slash = next_slash(path);
930
916
  int klen;
931
917
 
918
+ leaf = NULL;
932
919
  if (0 == slash) {
933
920
  klen = (int)strlen(key);
934
921
  path += klen;
@@ -1217,7 +1204,7 @@ static char *append_key(char *p, const char *key) {
1217
1204
  * @see Oj::Doc.open
1218
1205
  */
1219
1206
 
1220
- /* @overload where?() => String
1207
+ /* @overload where() => String
1221
1208
  *
1222
1209
  * Returns a String that describes the absolute path to the current location
1223
1210
  * in the JSON document.
@@ -1259,6 +1246,25 @@ static VALUE doc_where(VALUE self) {
1259
1246
  }
1260
1247
  }
1261
1248
 
1249
+ /* @overload where?() => String
1250
+ * @deprecated
1251
+ * Returns a String that describes the absolute path to the current location
1252
+ * in the JSON document.
1253
+ */
1254
+ static VALUE doc_where_q(VALUE self) {
1255
+ return doc_where(self);
1256
+ }
1257
+
1258
+ /* @overload path() => String
1259
+ *
1260
+ * Returns a String that describes the absolute path to the current location
1261
+ * in the JSON document.
1262
+ */
1263
+ static VALUE doc_path(VALUE self) {
1264
+ return doc_where(self);
1265
+ }
1266
+
1267
+
1262
1268
  /* @overload local_key() => String, Fixnum, nil
1263
1269
  *
1264
1270
  * Returns the final key to the current location.
@@ -1340,14 +1346,14 @@ static VALUE doc_type(int argc, VALUE *argv, VALUE self) {
1340
1346
  return type;
1341
1347
  }
1342
1348
 
1343
- /* @overload fetch(path=nil) => nil, true, false, Fixnum, Float, String, Array,
1349
+ /* @overload fetch(path=nil,default=nil) => nil, true, false, Fixnum, Float, String, Array,
1344
1350
  * Hash
1345
1351
  *
1346
1352
  * Returns the value at the location identified by the path or the current
1347
1353
  * location if the path is nil or not provided. This method will create and
1348
1354
  * return an Array or Hash if that is the type of Object at the location
1349
1355
  * specified. This is more expensive than navigating to the leaves of the JSON
1350
- * document.
1356
+ * document. If a default is provided that is used if no value if found.
1351
1357
  * @param [String] path path to the location to get the type of if provided
1352
1358
  * @example
1353
1359
  * Oj::Doc.open('[1,2]') { |doc| doc.fetch() } #=> [1, 2]
@@ -1373,6 +1379,28 @@ static VALUE doc_fetch(int argc, VALUE *argv, VALUE self) {
1373
1379
  return val;
1374
1380
  }
1375
1381
 
1382
+ /* @overload exists?(path) => true, false
1383
+ *
1384
+ * Returns true if the value at the location identified by the path exists.
1385
+ * @param [String] path path to the location
1386
+ * @example
1387
+ * Oj::Doc.open('[1,2]') { |doc| doc.exists('/1') } #=> true
1388
+ * Oj::Doc.open('[1,2]') { |doc| doc.exists('/3') } #=> false
1389
+ */
1390
+ static VALUE doc_exists(VALUE self, VALUE str) {
1391
+ Doc doc;
1392
+ Leaf leaf;
1393
+
1394
+ doc = self_doc(self);
1395
+ Check_Type(str, T_STRING);
1396
+ if (0 != (leaf = get_doc_leaf(doc, StringValuePtr(str)))) {
1397
+ if (NULL != leaf) {
1398
+ return Qtrue;
1399
+ }
1400
+ }
1401
+ return Qfalse;
1402
+ }
1403
+
1376
1404
  /* @overload each_leaf(path=nil) => nil
1377
1405
  *
1378
1406
  * Yields to the provided block for each leaf node with the identified
@@ -1695,11 +1723,14 @@ void oj_init_doc() {
1695
1723
  rb_define_singleton_method(oj_doc_class, "open", doc_open, 1);
1696
1724
  rb_define_singleton_method(oj_doc_class, "open_file", doc_open_file, 1);
1697
1725
  rb_define_singleton_method(oj_doc_class, "parse", doc_open, 1);
1698
- rb_define_method(oj_doc_class, "where?", doc_where, 0);
1726
+ rb_define_method(oj_doc_class, "where?", doc_where_q, 0);
1727
+ rb_define_method(oj_doc_class, "where", doc_where, 0);
1728
+ rb_define_method(oj_doc_class, "path", doc_path, 0);
1699
1729
  rb_define_method(oj_doc_class, "local_key", doc_local_key, 0);
1700
1730
  rb_define_method(oj_doc_class, "home", doc_home, 0);
1701
1731
  rb_define_method(oj_doc_class, "type", doc_type, -1);
1702
1732
  rb_define_method(oj_doc_class, "fetch", doc_fetch, -1);
1733
+ rb_define_method(oj_doc_class, "exists?", doc_exists, 1);
1703
1734
  rb_define_method(oj_doc_class, "each_leaf", doc_each_leaf, -1);
1704
1735
  rb_define_method(oj_doc_class, "move", doc_move, 1);
1705
1736
  rb_define_method(oj_doc_class, "each_child", doc_each_child, -1);
data/lib/oj/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
 
2
2
  module Oj
3
3
  # Current version of the module.
4
- VERSION = '3.11.5'
4
+ VERSION = '3.11.6'
5
5
  end
data/test/test_fast.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
- # encoding: UTF-8
2
+ # encoding: utf-8
3
3
 
4
4
  $: << File.dirname(__FILE__)
5
5
 
@@ -279,10 +279,26 @@ class DocTest < Minitest::Test
279
279
  ['/array/1', {'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}],
280
280
  ['/array', [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}]],
281
281
  ['/', {'array' => [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}], 'boolean' => true}],
282
+ ['/nothing', nil],
283
+ ['/array/10', nil],
282
284
  ].each do |path,val|
283
- assert_equal(val, doc.fetch(path))
285
+ if val.nil?
286
+ assert_nil(doc.fetch(path))
287
+ else
288
+ assert_equal(val, doc.fetch(path))
289
+ end
284
290
  end
285
291
  end
292
+ # verify empty hash and arrays return nil when a member is requested
293
+ Oj::Doc.open('{}') do |doc|
294
+ assert_nil(doc.fetch('/x'))
295
+ assert_nil(doc.fetch('/0'))
296
+ end
297
+ Oj::Doc.open('[]') do |doc|
298
+ assert_nil(doc.fetch('/x'))
299
+ assert_nil(doc.fetch('/0'))
300
+ end
301
+
286
302
  end
287
303
 
288
304
  def test_move_fetch_path
@@ -297,6 +313,20 @@ class DocTest < Minitest::Test
297
313
  end
298
314
  end
299
315
 
316
+ def test_exisits
317
+ Oj::Doc.open(@json1) do |doc|
318
+ [['/array/1', true],
319
+ ['/array/1', true],
320
+ ['/array/1/hash', true],
321
+ ['/array/1/dash', false],
322
+ ['/array/3', false],
323
+ ['/nothing', false],
324
+ ].each do |path,val|
325
+ assert_equal(val, doc.exists?(path))
326
+ end
327
+ end
328
+ end
329
+
300
330
  def test_home
301
331
  Oj::Doc.open(@json1) do |doc|
302
332
  doc.move('/array/1/num')
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oj
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.11.5
4
+ version: 3.11.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Ohler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-15 00:00:00.000000000 Z
11
+ date: 2021-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler