summaryrefslogtreecommitdiff
path: root/java/src/json/ext/Parser.rl
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/json/ext/Parser.rl')
-rw-r--r--java/src/json/ext/Parser.rl135
1 files changed, 18 insertions, 117 deletions
diff --git a/java/src/json/ext/Parser.rl b/java/src/json/ext/Parser.rl
index d43c74f..17ea303 100644
--- a/java/src/json/ext/Parser.rl
+++ b/java/src/json/ext/Parser.rl
@@ -50,10 +50,9 @@ public class Parser extends RubyObject {
private int maxNesting;
private boolean allowNaN;
private boolean symbolizeNames;
- private boolean quirksMode;
private RubyClass objectClass;
private RubyClass arrayClass;
- private RubyHash match_string;
+ private RubyHash matchString;
private static final int DEFAULT_MAX_NESTING = 100;
@@ -121,10 +120,6 @@ public class Parser extends RubyObject {
* <dd>If set to <code>true</code>, returns symbols for the names (keys) in
* a JSON object. Otherwise strings are returned, which is also the default.
*
- * <dt><code>:quirks_mode?</code>
- * <dd>If set to <code>true</code>, if the parse is in quirks_mode, false
- * otherwise.
- *
* <dt><code>:create_additions</code>
* <dd>If set to <code>false</code>, the Parser doesn't create additions
* even if a matching class and <code>create_id</code> was found. This option
@@ -136,9 +131,6 @@ public class Parser extends RubyObject {
* <dt><code>:array_class</code>
* <dd>Defaults to Array.
*
- * <dt><code>:quirks_mode</code>
- * <dd>Enables quirks_mode for parser, that is for example parsing single
- * JSON values instead of documents is possible.
* </dl>
*/
@JRubyMethod(name = "new", required = 1, optional = 1, meta = true)
@@ -161,15 +153,20 @@ public class Parser extends RubyObject {
this.maxNesting = opts.getInt("max_nesting", DEFAULT_MAX_NESTING);
this.allowNaN = opts.getBool("allow_nan", false);
this.symbolizeNames = opts.getBool("symbolize_names", false);
- this.quirksMode = opts.getBool("quirks_mode", false);
this.createId = opts.getString("create_id", getCreateId(context));
this.createAdditions = opts.getBool("create_additions", false);
this.objectClass = opts.getClass("object_class", runtime.getHash());
this.arrayClass = opts.getClass("array_class", runtime.getArray());
- this.match_string = opts.getHash("match_string");
+ this.matchString = opts.getHash("match_string");
+ if(symbolizeNames && createAdditions) {
+ throw runtime.newArgumentError(
+ "options :symbolize_names and :create_additions cannot be " +
+ " used in conjunction"
+ );
+ }
this.vSource = args[0].convertToString();
- if (!quirksMode) this.vSource = convertEncoding(context, vSource);
+ this.vSource = convertEncoding(context, vSource);
return this;
}
@@ -180,33 +177,7 @@ public class Parser extends RubyObject {
* Returns the source string if no conversion is needed.
*/
private RubyString convertEncoding(ThreadContext context, RubyString source) {
- ByteList bl = source.getByteList();
- int len = bl.length();
- if (len < 2) {
- throw Utils.newException(context, Utils.M_PARSER_ERROR,
- "A JSON text must at least contain two octets!");
- }
-
- if (info.encodingsSupported()) {
- RubyEncoding encoding = (RubyEncoding)source.encoding(context);
- if (encoding != info.ascii8bit.get()) {
- return (RubyString)source.encode(context, info.utf8.get());
- }
-
- String sniffedEncoding = sniffByteList(bl);
- if (sniffedEncoding == null) return source; // assume UTF-8
- return reinterpretEncoding(context, source, sniffedEncoding);
- }
-
- String sniffedEncoding = sniffByteList(bl);
- if (sniffedEncoding == null) return source; // assume UTF-8
- Ruby runtime = context.getRuntime();
- return (RubyString)info.jsonModule.get().
- callMethod(context, "iconv",
- new IRubyObject[] {
- runtime.newString("utf-8"),
- runtime.newString(sniffedEncoding),
- source});
+ return (RubyString)source.encode(context, info.utf8.get());
}
/**
@@ -259,17 +230,6 @@ public class Parser extends RubyObject {
return checkAndGetSource().dup();
}
- /**
- * <code>Parser#quirks_mode?()</code>
- *
- * <p>If set to <code>true</code>, if the parse is in quirks_mode, false
- * otherwise.
- */
- @JRubyMethod(name = "quirks_mode?")
- public IRubyObject quirks_mode_p(ThreadContext context) {
- return context.getRuntime().newBoolean(quirksMode);
- }
-
public RubyString checkAndGetSource() {
if (vSource != null) {
return vSource;
@@ -393,7 +353,7 @@ public class Parser extends RubyObject {
}
}
action parse_number {
- if (pe > fpc + 9 - (parser.quirksMode ? 1 : 0) &&
+ if (pe > fpc + 8 &&
absSubSequence(fpc, fpc + 9).equals(JSON_MINUS_INFINITY)) {
if (parser.allowNaN) {
@@ -623,11 +583,11 @@ public class Parser extends RubyObject {
%% write exec;
if (parser.createAdditions) {
- RubyHash match_string = parser.match_string;
- if (match_string != null) {
+ RubyHash matchString = parser.matchString;
+ if (matchString != null) {
final IRubyObject[] memoArray = { result, null };
try {
- match_string.visitAll(new RubyHash.Visitor() {
+ matchString.visitAll(new RubyHash.Visitor() {
@Override
public void visit(IRubyObject pattern, IRubyObject klass) {
if (pattern.callMethod(context, "===", memoArray[0]).isTrue()) {
@@ -648,7 +608,7 @@ public class Parser extends RubyObject {
}
if (cs >= JSON_string_first_final && result != null) {
- if (info.encodingsSupported() && result instanceof RubyString) {
+ if (result instanceof RubyString) {
((RubyString)result).force_encoding(context, info.utf8.get());
}
res.update(result, p + 1);
@@ -835,60 +795,6 @@ public class Parser extends RubyObject {
write data;
- action parse_object {
- currentNesting = 1;
- parseObject(res, fpc, pe);
- if (res.result == null) {
- fhold;
- fbreak;
- } else {
- result = res.result;
- fexec res.p;
- }
- }
-
- action parse_array {
- currentNesting = 1;
- parseArray(res, fpc, pe);
- if (res.result == null) {
- fhold;
- fbreak;
- } else {
- result = res.result;
- fexec res.p;
- }
- }
-
- main := ignore*
- ( begin_object >parse_object
- | begin_array >parse_array )
- ignore*;
- }%%
-
- public IRubyObject parseStrict() {
- int cs = EVIL;
- int p, pe;
- IRubyObject result = null;
- ParserResult res = new ParserResult();
-
- %% write init;
- p = byteList.begin();
- pe = p + byteList.length();
- %% write exec;
-
- if (cs >= JSON_first_final && p == pe) {
- return result;
- } else {
- throw unexpectedToken(p, pe);
- }
- }
-
- %%{
- machine JSON_quirks_mode;
- include JSON_common;
-
- write data;
-
action parse_value {
parseValue(res, fpc, pe);
if (res.result == null) {
@@ -905,7 +811,7 @@ public class Parser extends RubyObject {
ignore*;
}%%
- public IRubyObject parseQuirksMode() {
+ public IRubyObject parseImplemetation() {
int cs = EVIL;
int p, pe;
IRubyObject result = null;
@@ -916,7 +822,7 @@ public class Parser extends RubyObject {
pe = p + byteList.length();
%% write exec;
- if (cs >= JSON_quirks_mode_first_final && p == pe) {
+ if (cs >= JSON_first_final && p == pe) {
return result;
} else {
throw unexpectedToken(p, pe);
@@ -924,12 +830,7 @@ public class Parser extends RubyObject {
}
public IRubyObject parse() {
- if (parser.quirksMode) {
- return parseQuirksMode();
- } else {
- return parseStrict();
- }
-
+ return parseImplemetation();
}
/**