Skip to content

Commit

Permalink
[MOD] QT3TS: JSON, alignments with latest spec
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianGruen committed Oct 21, 2015
1 parent 683b677 commit 96dd092
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 28 deletions.
20 changes: 11 additions & 9 deletions basex-core/src/main/java/org/basex/io/parse/json/JsonParser.java
Expand Up @@ -274,20 +274,16 @@ private byte[] string() throws QueryIOException {
while(pos < length) {
final int p = pos;
int ch = consume();
// string is closed..
if(ch == '"') {
// unpaired surrogate?
if(high != 0) add(high, pos - 7, p);
skipWs();
return tb.toArray();
}

// escape sequence
if(ch == '\\') {
if(escape) {
if(high != 0) {
tb.add(high);
high = 0;
}
}

ch = consume();
switch(ch) {
case '\\':
Expand Down Expand Up @@ -329,12 +325,18 @@ private byte[] string() throws QueryIOException {
}

if(high != 0) {
if(ch >= 0xDC00 && ch <= 0xDFFF) ch = (high - 0xD800 << 10) + ch - 0xDC00 + 0x10000;
else add(high, p, pos);
if(ch >= 0xDC00 && ch <= 0xDFFF) {
// compute resulting codepoint
ch = (high - 0xD800 << 10) + ch - 0xDC00 + 0x10000;
} else if(escape) {
// add invalid high surrogate, treat low surrogate as next character
add(high, p, pos);
}
high = 0;
}

if(ch >= 0xD800 && ch <= 0xDBFF) {
// remember high surrogate
high = (char) ch;
} else {
add(ch, p, pos);
Expand Down
7 changes: 3 additions & 4 deletions basex-core/src/main/java/org/basex/query/QueryError.java
Expand Up @@ -566,6 +566,7 @@ public enum QueryError {
ONEORMORE(FORG, 4, "One or more values expected."),
/** FORG0005. */
EXACTLYONE(FORG, 5, "Exactly one value expected."),

/** FORG0006. */
EXPTYPE_X_X_X(FORG, 6, "% expected, % found: %."),
/** FORG0006. */
Expand Down Expand Up @@ -609,8 +610,6 @@ public enum QueryError {
/** FORG0006. */
ERRFORMAT_X_X(FORG, 6, "%: %."),
/** FORG0006. */
INVALIDOPT_X(FORG, 6, "%"),
/** FORG0006. */
INVALIDOPTION_X(FORG, 6, "Unknown option '%'."),

/** FORG0008. */
Expand Down Expand Up @@ -971,7 +970,6 @@ public enum QueryError {
STRQNM_X_X(XPTY, 4, "String or QName expected, % found: %."),
/** XPTY0004. */
CPIWRONG_X_X(XPTY, 4, "String or NCName expected, % found: %."),

/** XPTY0004. */
INVCAST_X_X(XPTY, 4, "Cannot cast % to %."),
/** XPTY0004. */
Expand All @@ -986,7 +984,6 @@ public enum QueryError {
CALCTYPE_X_X_X(XPTY, 4, "% not defined for % and %."),
/** XPTY0004. */
INVFUNCITEM_X(XPTY, 4, "Function expected, % found: %."),

/** XPTY0004. */
CMPTYPE_X(XPTY, 4, "Items of type % cannot be compared."),
/** XPTY0004. */
Expand All @@ -1003,6 +1000,8 @@ public enum QueryError {
CITYPES_X_X(XPTY, 4, "Incompatible types in context value declarations: % vs. %."),
/** XPTY0004. */
LOOKUP_X(XPTY, 4, "Input of lookup operator is not a map or array: %."),
/** XPTY0004. */
INVALIDOPT_X(XPTY, 4, "%"),

/** XPTY0018. */
EVALNODESVALS(XPTY, 18, "Path yields both nodes and atomic values."),
Expand Down
20 changes: 5 additions & 15 deletions basex-core/src/main/java/org/basex/query/func/fn/FnParseJson.java
Expand Up @@ -44,11 +44,7 @@ final Item parse(final byte[] json, final boolean xml, final QueryContext qc,
final JsonParserOptions opts = new JsonParserOptions();
if(exprs.length > 1) {
final Map options = toMap(exprs[1], qc);
try {
new FuncOptions(null, info).acceptUnknown().parse(options, opts);
} catch(final QueryException ex) {
throw JSON_OPT_X.get(ii, ex.getLocalizedMessage());
}
new FuncOptions(null, info).acceptUnknown().parse(options, opts);
}

final boolean esc = opts.get(JsonParserOptions.ESCAPE);
Expand All @@ -57,12 +53,10 @@ final Item parse(final byte[] json, final boolean xml, final QueryContext qc,
if(fb == null) {
fallback = null;
} else {
try {
fallback = STRFUNC.cast(fb, qc, sc, ii);
} catch(final QueryException ex) {
throw JSON_OPT_X.get(ii, ex.getLocalizedMessage());
}
fallback = STRFUNC.cast(fb, qc, sc, ii);
}
if(esc && fallback != null) throw JSON_OPT_X.get(ii,
"Escaping cannot be combined with a fallback function.");

try {
opts.set(JsonOptions.FORMAT, xml ? JsonFormat.BASIC : JsonFormat.MAP);
Expand All @@ -79,11 +73,7 @@ public String convert(final String string) {
});
return conv.convert(json, null);
} catch(final QueryRTException ex) {
final QueryException qe = ex.getCause();
final QueryError err = qe.error();
if(err != INVPROMOTE_X_X && err != INVPROMOTE_X_X_X) throw qe;
Util.debug(ex);
throw JSON_OPT_X.get(ii, qe.getLocalizedMessage());
throw ex.getCause();
} catch(final QueryIOException ex) {
Util.debug(ex);
final QueryException qe = ex.getCause(info);
Expand Down

0 comments on commit 96dd092

Please sign in to comment.