summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Frank <flori@ping.de>2012-08-31 16:20:42 +0200
committerFlorian Frank <flori@ping.de>2012-09-05 19:58:25 +0200
commit1ecb66c0ff55ad29acc70de99749f13330e6a697 (patch)
tree6862da707319104a70f54069bb8e9acc0cee2d94
parent48d4d9f0fc6127bbde816f6950589ff667be66c4 (diff)
downloadjson-1ecb66c0ff55ad29acc70de99749f13330e6a697.tar.gz
Implement replace_nan stuff for jruby as well
-rw-r--r--ext/json/ext/generator/generator.c2
-rw-r--r--java/src/json/ext/Generator.java10
-rw-r--r--java/src/json/ext/GeneratorState.java26
-rw-r--r--java/src/json/ext/OptionsReader.java9
-rw-r--r--json.gemspec2
-rw-r--r--json_pure.gemspec2
-rw-r--r--lib/json/pure/generator.rb4
-rwxr-xr-xtests/test_json_generate.rb22
8 files changed, 55 insertions, 22 deletions
diff --git a/ext/json/ext/generator/generator.c b/ext/json/ext/generator/generator.c
index 59784c0..214e125 100644
--- a/ext/json/ext/generator/generator.c
+++ b/ext/json/ext/generator/generator.c
@@ -1356,6 +1356,8 @@ void Init_generator()
rb_define_method(cState, "max_nesting=", cState_max_nesting_set, 1);
rb_define_method(cState, "check_circular?", cState_check_circular_p, 0);
rb_define_method(cState, "allow_nan?", cState_allow_nan_p, 0);
+ //rb_define_method(cState, "replace_nan", cState_replace_nan_get, 0);
+ //rb_define_method(cState, "replace_nan=", cState_replace_nan_set, 0);
rb_define_method(cState, "replace_nan?", cState_replace_nan_p, 0);
rb_define_method(cState, "ascii_only?", cState_ascii_only_p, 0);
rb_define_method(cState, "quirks_mode?", cState_quirks_mode_p, 0);
diff --git a/java/src/json/ext/Generator.java b/java/src/json/ext/Generator.java
index 17a4757..284c90f 100644
--- a/java/src/json/ext/Generator.java
+++ b/java/src/json/ext/Generator.java
@@ -239,8 +239,14 @@ public final class Generator {
if (Double.isInfinite(value) || Double.isNaN(value)) {
if (!session.getState().allowNaN()) {
- if (session.getState().replaceNaN()) {
- buffer.append("null".getBytes());
+ IRubyObject replaceNaN = session.getState().replaceNaN();
+ if (replaceNaN.isTrue()) {
+ if (replaceNaN.respondsTo("call")) {
+ buffer.append(replaceNaN.callMethod(
+ session.getContext(), "call", object).toString().getBytes());
+ } else {
+ buffer.append("null".getBytes());
+ }
return;
} else {
throw Utils.newException(session.getContext(),
diff --git a/java/src/json/ext/GeneratorState.java b/java/src/json/ext/GeneratorState.java
index ec3f5b0..880c396 100644
--- a/java/src/json/ext/GeneratorState.java
+++ b/java/src/json/ext/GeneratorState.java
@@ -74,8 +74,7 @@ public class GeneratorState extends RubyObject {
/**
* XXX
*/
- private boolean replaceNaN = DEFAULT_REPLACE_NAN;
- static final boolean DEFAULT_REPLACE_NAN = false;
+ private IRubyObject replaceNaN;
/**
* If set to <code>true</code> all JSON documents generated do not contain
* any other characters than ASCII characters.
@@ -108,6 +107,7 @@ public class GeneratorState extends RubyObject {
public GeneratorState(Ruby runtime, RubyClass metaClass) {
super(runtime, metaClass);
+ replaceNaN = runtime.getNil();
}
/**
@@ -122,7 +122,7 @@ public class GeneratorState extends RubyObject {
* ({@link RubyClass} <code>State</code>)
* @param opts The object to use as a base for the new <code>State</code>
* @param block The block passed to the method
- * @return A <code>GeneratorState</code> as determined above
+ * @return A <code>Generat34786869orState</code> as determined above
*/
@JRubyMethod(meta=true)
public static IRubyObject from_state(ThreadContext context,
@@ -384,15 +384,27 @@ public class GeneratorState extends RubyObject {
return context.getRuntime().newBoolean(allowNaN);
}
- public boolean replaceNaN() {
+ @JRubyMethod(name="replace_nan=")
+ public IRubyObject replace_nan_set(IRubyObject replace_nan) {
+ replaceNaN = replace_nan;
+ return replaceNaN;
+ }
+
+ public IRubyObject replaceNaN() {
+ return replaceNaN;
+ }
+
+ @JRubyMethod(name="replace_nan")
+ public IRubyObject replace_nan_get(ThreadContext context) {
return replaceNaN;
}
@JRubyMethod(name="replace_nan?")
public RubyBoolean replace_nan_p(ThreadContext context) {
- return context.getRuntime().newBoolean(replaceNaN);
+ return context.getRuntime().newBoolean(!replaceNaN.isNil());
}
+
public boolean asciiOnly() {
return asciiOnly;
}
@@ -483,7 +495,7 @@ public class GeneratorState extends RubyObject {
maxNesting = opts.getInt("max_nesting", DEFAULT_MAX_NESTING);
allowNaN = opts.getBool("allow_nan", DEFAULT_ALLOW_NAN);
- replaceNaN = opts.getBool("replace_nan", DEFAULT_REPLACE_NAN);
+ replaceNaN = opts.get("replace_nan", context.getRuntime().getFalse());
asciiOnly = opts.getBool("ascii_only", DEFAULT_ASCII_ONLY);
quirksMode = opts.getBool("quirks_mode", DEFAULT_QUIRKS_MODE);
bufferInitialLength = opts.getInt("buffer_initial_length", DEFAULT_BUFFER_INITIAL_LENGTH);
@@ -511,7 +523,7 @@ public class GeneratorState extends RubyObject {
result.op_aset(context, runtime.newSymbol("object_nl"), object_nl_get(context));
result.op_aset(context, runtime.newSymbol("array_nl"), array_nl_get(context));
result.op_aset(context, runtime.newSymbol("allow_nan"), allow_nan_p(context));
- result.op_aset(context, runtime.newSymbol("replace_nan"), replace_nan_p(context));
+ result.op_aset(context, runtime.newSymbol("replace_nan"), replace_nan_get(context));
result.op_aset(context, runtime.newSymbol("ascii_only"), ascii_only_p(context));
result.op_aset(context, runtime.newSymbol("quirks_mode"), quirks_mode_p(context));
result.op_aset(context, runtime.newSymbol("max_nesting"), max_nesting_get(context));
diff --git a/java/src/json/ext/OptionsReader.java b/java/src/json/ext/OptionsReader.java
index 6835735..f39d58f 100644
--- a/java/src/json/ext/OptionsReader.java
+++ b/java/src/json/ext/OptionsReader.java
@@ -50,6 +50,15 @@ final class OptionsReader {
return opts == null ? null : opts.fastARef(runtime.newSymbol(key));
}
+ IRubyObject get(String key, IRubyObject defaultValue) {
+ if (opts == null) {
+ return defaultValue;
+ } else {
+ IRubyObject value = opts.fastARef(runtime.newSymbol(key));
+ return value.isNil() ? defaultValue : value;
+ }
+ }
+
boolean getBool(String key, boolean defaultValue) {
IRubyObject value = get(key);
return value == null ? defaultValue : value.isTrue();
diff --git a/json.gemspec b/json.gemspec
index b00fc5b..b12c230 100644
--- a/json.gemspec
+++ b/json.gemspec
@@ -6,7 +6,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Florian Frank"]
- s.date = "2012-08-19"
+ s.date = "2012-08-31"
s.description = "This is a JSON implementation as a Ruby extension in C."
s.email = "flori@ping.de"
s.extensions = ["ext/json/ext/generator/extconf.rb", "ext/json/ext/parser/extconf.rb"]
diff --git a/json_pure.gemspec b/json_pure.gemspec
index 6b25120..0728a2e 100644
--- a/json_pure.gemspec
+++ b/json_pure.gemspec
@@ -6,7 +6,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Florian Frank"]
- s.date = "2012-08-19"
+ s.date = "2012-08-31"
s.description = "This is a JSON implementation in pure Ruby."
s.email = "flori@ping.de"
s.extra_rdoc_files = ["README.rdoc"]
diff --git a/lib/json/pure/generator.rb b/lib/json/pure/generator.rb
index a0fec6a..fdc9b74 100644
--- a/lib/json/pure/generator.rb
+++ b/lib/json/pure/generator.rb
@@ -208,11 +208,15 @@ module JSON
@allow_nan
end
+ attr_writer :allow_nan # XXX
+
# XXX
def replace_nan?
!!@replace_nan
end
+ attr_accessor :replace_nan
+
# Returns true, if only ASCII characters should be generated. Otherwise
# returns false.
def ascii_only?
diff --git a/tests/test_json_generate.rb b/tests/test_json_generate.rb
index 184a849..7e61a84 100755
--- a/tests/test_json_generate.rb
+++ b/tests/test_json_generate.rb
@@ -253,17 +253,17 @@ EOT
end
def test_json_infinite_float
- #assert_raise(JSON::GeneratorError) { JSON.generate([ JSON::NaN ]) }
- #assert_raise(JSON::GeneratorError) { JSON.generate([ JSON::Infinity ]) }
- #assert_raise(JSON::GeneratorError) { JSON.generate([ JSON::MinusInfinity ]) }
- #assert_equal '[NaN]', JSON.generate([ JSON::NaN ], :allow_nan => true)
- #assert_equal '[Infinity]', JSON.generate([ JSON::Infinity ], :allow_nan => true)
- #assert_equal '[-Infinity]', JSON.generate([ JSON::MinusInfinity ], :allow_nan => true)
- #assert_equal '[null]', JSON.generate([ JSON::NaN ], :replace_nan => true)
- #assert_equal '[null]', JSON.generate([ JSON::Infinity ], :replace_nan => true)
- #assert_equal '[null]', JSON.generate([ JSON::MinusInfinity ], :replace_nan => true)
+ assert_raise(JSON::GeneratorError) { JSON.generate([ JSON::NaN ]) }
+ assert_raise(JSON::GeneratorError) { JSON.generate([ JSON::Infinity ]) }
+ assert_raise(JSON::GeneratorError) { JSON.generate([ JSON::MinusInfinity ]) }
+ assert_equal '[NaN]', JSON.generate([ JSON::NaN ], :allow_nan => true)
+ assert_equal '[Infinity]', JSON.generate([ JSON::Infinity ], :allow_nan => true)
+ assert_equal '[-Infinity]', JSON.generate([ JSON::MinusInfinity ], :allow_nan => true)
+ assert_equal '[null]', JSON.generate([ JSON::NaN ], :replace_nan => true)
+ assert_equal '[null]', JSON.generate([ JSON::Infinity ], :replace_nan => true)
+ assert_equal '[null]', JSON.generate([ JSON::MinusInfinity ], :replace_nan => true)
assert_equal '["NaN"]', JSON.generate([ JSON::NaN ], :replace_nan => lambda { |x| x.to_s.inspect })
- #assert_equal '["Infinity"]', JSON.generate([ JSON::Infinity ], :replace_nan => lambda { |x| x.to_s.inspect })
- #assert_equal '["-Infinity"]', JSON.generate([ JSON::MinusInfinity ], :replace_nan => lambda { |x| x.to_s.inspect })
+ assert_equal '["Infinity"]', JSON.generate([ JSON::Infinity ], :replace_nan => lambda { |x| x.to_s.inspect })
+ assert_equal '["-Infinity"]', JSON.generate([ JSON::MinusInfinity ], :replace_nan => lambda { |x| x.to_s.inspect })
end
end