From bdb2b910a5ebd92471de13d7e90d54408f6b093e Mon Sep 17 00:00:00 2001 From: Florian Frank Date: Wed, 27 Jul 2011 02:14:54 +0200 Subject: started quirks mode for generator --- java/src/json/ext/GeneratorState.java | 46 ++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 11 deletions(-) (limited to 'java/src/json/ext/GeneratorState.java') diff --git a/java/src/json/ext/GeneratorState.java b/java/src/json/ext/GeneratorState.java index f04eda2..a53ff12 100644 --- a/java/src/json/ext/GeneratorState.java +++ b/java/src/json/ext/GeneratorState.java @@ -1,6 +1,6 @@ /* * This code is copyrighted work by Daniel Luz . - * + * * Distributed under the Ruby and GPLv2 licenses; see COPYING and GPL files * for details. */ @@ -24,10 +24,10 @@ import org.jruby.util.ByteList; /** * The JSON::Ext::Generator::State class. - * + * *

This class is used to create State instances, that are use to hold data * while generating a JSON text from a a Ruby data structure. - * + * * @author mernen */ public class GeneratorState extends RubyObject { @@ -76,6 +76,11 @@ public class GeneratorState extends RubyObject { */ private boolean asciiOnly = DEFAULT_ASCII_ONLY; static final boolean DEFAULT_ASCII_ONLY = false; + /** + * XXX + */ + private boolean quirksMode = DEFAULT_QUIRKS_MODE; + static final boolean DEFAULT_QUIRKS_MODE = false; /** * The current depth (inside a #to_json call) @@ -94,7 +99,7 @@ public class GeneratorState extends RubyObject { /** * State.from_state(opts) - * + * *

Creates a State object from opts, which ought to be * {@link RubyHash Hash} to create a new State instance * configured by opts, something else to create an @@ -136,11 +141,11 @@ public class GeneratorState extends RubyObject { /** * State#initialize(opts = {}) - * + * * Instantiates a new State object, configured by opts. - * + * * opts can have the following keys: - * + * *

*
:indent *
a {@link RubyString String} used to indent levels (default: "") @@ -151,7 +156,7 @@ public class GeneratorState extends RubyObject { *
a String that is put before a ":" pair delimiter * (default: "") *
:object_nl - *
a String that is put at the end of a JSON object (default: "") + *
a String that is put at the end of a JSON object (default: "") *
:array_nl *
a String that is put at the end of a JSON array (default: "") *
:allow_nan @@ -181,6 +186,7 @@ public class GeneratorState extends RubyObject { this.maxNesting = orig.maxNesting; this.allowNaN = orig.allowNaN; this.asciiOnly = orig.asciiOnly; + this.quirksMode = orig.quirksMode; this.depth = orig.depth; return this; } @@ -191,7 +197,7 @@ public class GeneratorState extends RubyObject { @JRubyMethod public IRubyObject generate(ThreadContext context, IRubyObject obj) { RubyString result = Generator.generateJson(context, obj, this); - if (!objectOrArrayLiteral(result)) { + if (!quirksMode && !objectOrArrayLiteral(result)) { throw Utils.newException(context, Utils.M_GENERATOR_ERROR, "only generation of JSON objects or arrays allowed"); } @@ -364,6 +370,22 @@ public class GeneratorState extends RubyObject { return context.getRuntime().newBoolean(asciiOnly); } + @JRubyMethod(name="quirks_mode") + public RubyBoolean quirks_mode_get(ThreadContext context) { + return context.getRuntime().newBoolean(quirksMode); + } + + @JRubyMethod(name="quirks_mode=") + public IRubyObject quirks_mode_set(IRubyObject quirks_mode) { + quirksMode = quirks_mode.isTrue(); + return quirks_mode.getRuntime().newBoolean(quirksMode); + } + + @JRubyMethod(name="quirks_mode?") + public RubyBoolean quirks_mode_p(ThreadContext context) { + return context.getRuntime().newBoolean(quirksMode); + } + public int getDepth() { return depth; } @@ -390,7 +412,7 @@ public class GeneratorState extends RubyObject { /** * State#configure(opts) - * + * *

Configures this State instance with the {@link RubyHash Hash} * opts, and returns itself. * @param vOpts The options hash @@ -418,6 +440,7 @@ public class GeneratorState extends RubyObject { maxNesting = opts.getInt("max_nesting", DEFAULT_MAX_NESTING); allowNaN = opts.getBool("allow_nan", DEFAULT_ALLOW_NAN); asciiOnly = opts.getBool("ascii_only", DEFAULT_ASCII_ONLY); + quirksMode = opts.getBool("quirks_mode", DEFAULT_QUIRKS_MODE); depth = opts.getInt("depth", 0); @@ -426,7 +449,7 @@ public class GeneratorState extends RubyObject { /** * State#to_h() - * + * *

Returns the configuration instance variables as a hash, that can be * passed to the configure method. * @return @@ -443,6 +466,7 @@ public class GeneratorState extends RubyObject { 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("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)); result.op_aset(context, runtime.newSymbol("depth"), depth_get(context)); return result; -- cgit v1.2.1