json_pure 2.0.4 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -52,7 +52,8 @@ public class Parser extends RubyObject {
52
52
  private boolean symbolizeNames;
53
53
  private RubyClass objectClass;
54
54
  private RubyClass arrayClass;
55
- private RubyHash matchString;
55
+ private RubyClass decimalClass;
56
+ private RubyHash match_string;
56
57
 
57
58
  private static final int DEFAULT_MAX_NESTING = 100;
58
59
 
@@ -131,6 +132,10 @@ public class Parser extends RubyObject {
131
132
  * <dt><code>:array_class</code>
132
133
  * <dd>Defaults to Array.
133
134
  *
135
+ * <dt><code>:decimal_class</code>
136
+ * <dd>Specifies which class to use instead of the default (Float) when
137
+ * parsing decimal numbers. This class must accept a single string argument
138
+ * in its constructor.
134
139
  * </dl>
135
140
  */
136
141
  @JRubyMethod(name = "new", required = 1, optional = 1, meta = true)
@@ -157,7 +162,8 @@ public class Parser extends RubyObject {
157
162
  this.createAdditions = opts.getBool("create_additions", false);
158
163
  this.objectClass = opts.getClass("object_class", runtime.getHash());
159
164
  this.arrayClass = opts.getClass("array_class", runtime.getArray());
160
- this.matchString = opts.getHash("match_string");
165
+ this.decimalClass = opts.getClass("decimal_class", null);
166
+ this.match_string = opts.getHash("match_string");
161
167
 
162
168
  if(symbolizeNames && createAdditions) {
163
169
  throw runtime.newArgumentError(
@@ -489,13 +495,13 @@ public class Parser extends RubyObject {
489
495
 
490
496
  return p;
491
497
  }
492
-
498
+
493
499
  RubyInteger createInteger(int p, int new_p) {
494
500
  Ruby runtime = getRuntime();
495
501
  ByteList num = absSubSequence(p, new_p);
496
502
  return bytesToInum(runtime, num);
497
503
  }
498
-
504
+
499
505
  RubyInteger bytesToInum(Ruby runtime, ByteList num) {
500
506
  return runtime.is1_9() ?
501
507
  ConvertBytes.byteListToInum19(runtime, num, 10, true) :
@@ -525,7 +531,9 @@ public class Parser extends RubyObject {
525
531
  res.update(null, p);
526
532
  return;
527
533
  }
528
- RubyFloat number = createFloat(p, new_p);
534
+ IRubyObject number = parser.decimalClass == null ?
535
+ createFloat(p, new_p) : createCustomDecimal(p, new_p);
536
+
529
537
  res.update(number, new_p + 1);
530
538
  return;
531
539
  }
@@ -540,16 +548,23 @@ public class Parser extends RubyObject {
540
548
  if (cs < JSON_float_first_final) {
541
549
  return -1;
542
550
  }
543
-
551
+
544
552
  return p;
545
553
  }
546
-
554
+
547
555
  RubyFloat createFloat(int p, int new_p) {
548
556
  Ruby runtime = getRuntime();
549
557
  ByteList num = absSubSequence(p, new_p);
550
558
  return RubyFloat.newFloat(runtime, dc.parse(num, true, runtime.is1_9()));
551
559
  }
552
560
 
561
+ IRubyObject createCustomDecimal(int p, int new_p) {
562
+ Ruby runtime = getRuntime();
563
+ ByteList num = absSubSequence(p, new_p);
564
+ IRubyObject numString = runtime.newString(num.toString());
565
+ return parser.decimalClass.callMethod(context, "new", numString);
566
+ }
567
+
553
568
  %%{
554
569
  machine JSON_string;
555
570
  include JSON_common;
@@ -616,7 +631,7 @@ public class Parser extends RubyObject {
616
631
  }
617
632
  }
618
633
 
619
- if (cs >= JSON_string_first_final && result != null) {
634
+ if (cs >= JSON_string_first_final && result != null) {
620
635
  if (result instanceof RubyString) {
621
636
  ((RubyString)result).force_encoding(context, info.utf8.get());
622
637
  }
@@ -734,7 +749,7 @@ public class Parser extends RubyObject {
734
749
  fhold;
735
750
  fbreak;
736
751
  }
737
-
752
+
738
753
  pair = ignore* begin_name >parse_name ignore* name_separator
739
754
  ignore* begin_value >parse_value;
740
755
  next_pair = ignore* value_separator pair;
Binary file
@@ -1,14 +1,14 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: json_pure 2.0.4 ruby lib
2
+ # stub: json_pure 2.1.0 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "json_pure".freeze
6
- s.version = "2.0.4"
6
+ s.version = "2.1.0"
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib".freeze]
10
10
  s.authors = ["Florian Frank".freeze]
11
- s.date = "2017-04-10"
11
+ s.date = "2017-04-18"
12
12
  s.description = "This is a JSON implementation in pure Ruby.".freeze
13
13
  s.email = "flori@ping.de".freeze
14
14
  s.extra_rdoc_files = ["README.md".freeze]
@@ -70,6 +70,9 @@ module JSON
70
70
  # option defaults to false.
71
71
  # * *object_class*: Defaults to Hash
72
72
  # * *array_class*: Defaults to Array
73
+ # * *decimal_class*: Specifies which class to use instead of the default
74
+ # (Float) when parsing decimal numbers. This class must accept a single
75
+ # string argument in its constructor.
73
76
  def initialize(source, opts = {})
74
77
  opts ||= {}
75
78
  source = convert_encoding source
@@ -94,6 +97,7 @@ module JSON
94
97
  @create_id = @create_additions ? JSON.create_id : nil
95
98
  @object_class = opts[:object_class] || Hash
96
99
  @array_class = opts[:array_class] || Array
100
+ @decimal_class = opts[:decimal_class]
97
101
  @match_string = opts[:match_string]
98
102
  end
99
103
 
@@ -193,7 +197,7 @@ module JSON
193
197
  def parse_value
194
198
  case
195
199
  when scan(FLOAT)
196
- Float(self[1])
200
+ @decimal_class && @decimal_class.new(self[1]) || Float(self[1])
197
201
  when scan(INTEGER)
198
202
  Integer(self[1])
199
203
  when scan(TRUE)
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: false
2
2
  module JSON
3
3
  # JSON version
4
- VERSION = '2.0.4'
4
+ VERSION = '2.1.0'
5
5
  VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
6
6
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
7
7
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
@@ -79,8 +79,8 @@ class JSONEncodingTest < Test::Unit::TestCase
79
79
  json = '["\ud840\udc01"]'
80
80
  assert_equal json, generate(utf8, :ascii_only => true)
81
81
  assert_equal utf8, parse(json)
82
- assert_raises(JSON::ParserError) { parse('"\u"') }
83
- assert_raises(JSON::ParserError) { parse('"\ud800"') }
82
+ assert_raise(JSON::ParserError) { parse('"\u"') }
83
+ assert_raise(JSON::ParserError) { parse('"\ud800"') }
84
84
  end
85
85
 
86
86
  def test_chars
@@ -108,6 +108,11 @@ class JSONParserTest < Test::Unit::TestCase
108
108
  assert_equal -1.0/0, parse('-Infinity', :allow_nan => true)
109
109
  end
110
110
 
111
+ def test_parse_bigdecimals
112
+ assert_equal(BigDecimal, JSON.parse('{"foo": 9.01234567890123456789}', decimal_class: BigDecimal)["foo"].class)
113
+ assert_equal(BigDecimal.new("0.901234567890123456789E1"),JSON.parse('{"foo": 9.01234567890123456789}', decimal_class: BigDecimal)["foo"] )
114
+ end
115
+
111
116
  if Array.method_defined?(:permutation)
112
117
  def test_parse_more_complex_arrays
113
118
  a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_pure
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.4
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Frank
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-11 00:00:00.000000000 Z
11
+ date: 2017-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake