Skip to content

Commit

Permalink
[MOD] XQuery: Some JSON/CSV formats will become deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianGruen committed Feb 3, 2025
1 parent 3081416 commit 36b6566
Show file tree
Hide file tree
Showing 19 changed files with 46 additions and 36 deletions.
12 changes: 6 additions & 6 deletions basex-core/src/main/java/org/basex/build/csv/CsvOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public class CsvOptions extends Options {

/** CSV formats. */
public enum CsvFormat {
/** Default. */ DIRECT,
/** Attributes. */ ATTRIBUTES,
/** XQuery. */ XQUERY,
/** parse-csv. */ W3,
/** csv-to-arrays. */ W3_ARRAYS,
/** csv-to-xml. */ W3_XML;
/** Default. */ DIRECT,
/** Attributes. */ ATTRIBUTES,
/** fn:parse-csv. */ W3,
/** fn:csv-to-arrays. */ W3_ARRAYS,
/** fn:csv-to-xml. */ W3_XML,
/** XQuery (deprecated; use {@link #W3}). */ XQUERY;

@Override
public String toString() {
Expand Down
10 changes: 6 additions & 4 deletions basex-core/src/main/java/org/basex/build/json/JsonOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ public class JsonOptions extends Options {

/** JSON formats. */
public enum JsonFormat {
/** Direct. */ DIRECT,
/** Direct. */ DIRECT,
/** Attributes. */ ATTRIBUTES,
/** JsonML. */ JSONML,
/** Basic. */ BASIC,
/** XQuery. */ XQUERY;
/** JsonML. */ JSONML,
/** fn:parse-json. */ W3,
/** fn:json-to-xml. */ W3_XML,
/** XQuery (deprecated; use {@link #W3}). */ XQUERY,
/** Basic (deprecated; use {@link #W3_XML}). */ BASIC;

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ final class DialogJsonParser extends DialogParser {
encoding = encoding(dialog, jopts.get(JsonParserOptions.ENCODING));

final String[] formats = Arrays.stream(new JsonFormat[] {
JsonFormat.DIRECT, JsonFormat.ATTRIBUTES, JsonFormat.JSONML, JsonFormat.BASIC
JsonFormat.DIRECT, JsonFormat.ATTRIBUTES, JsonFormat.JSONML, JsonFormat.W3_XML
}).map(JsonFormat::toString).toArray(String[]::new);
format = new BaseXCombo(dialog, formats);
format.setSelectedItem(jopts.get(JsonOptions.FORMAT));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public abstract class CsvConverter extends Job {
*/
public static CsvConverter get(final CsvParserOptions copts) throws QueryException {
switch(copts.get(CsvOptions.FORMAT)) {
case XQUERY: return new CsvXQueryConverter(copts);
case XQUERY: return new CsvXQueryConverter(copts); // deprecated
case W3: return new CsvW3Converter(copts);
case W3_ARRAYS: return new CsvW3ArraysConverter(copts);
case W3_XML: return new CsvW3XmlConverter(copts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ public static JsonConverter get(final JsonParserOptions jopts) throws QueryExcep
switch(jopts.get(JsonOptions.FORMAT)) {
case JSONML: return new JsonMLConverter(jopts);
case ATTRIBUTES: return new JsonAttsConverter(jopts);
case XQUERY: return new JsonXQueryConverter(jopts);
case BASIC: return new JsonBasicConverter(jopts);
case XQUERY: //deprecated
case W3: return new JsonW3Converter(jopts);
case BASIC: //deprecated
case W3_XML: return new JsonW3XmlConverter(jopts);
default: return new JsonDirectConverter(jopts);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public JsonParser(final String input, final JsonParserOptions opts, final JsonCo
liberal = opts.get(JsonParserOptions.LIBERAL);
escape = opts.get(JsonParserOptions.ESCAPE);
final JsonDuplicates dupl = opts.get(JsonParserOptions.DUPLICATES);
duplicates = dupl != null ? dupl : opts.get(JsonOptions.FORMAT) == JsonFormat.BASIC ?
final JsonFormat jf = opts.get(JsonOptions.FORMAT);
duplicates = dupl != null ? dupl : jf == JsonFormat.W3_XML || jf == JsonFormat.BASIC ?
JsonDuplicates.RETAIN : JsonDuplicates.USE_FIRST;
this.conv = conv;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* @author BaseX Team, BSD License
* @author Leo Woerteler
*/
public final class JsonXQueryConverter extends JsonConverter {
public final class JsonW3Converter extends JsonConverter {
/** Stack for intermediate values. */
private final Stack<Value> stack = new Stack<>();
/** Stack for intermediate arrays. */
Expand All @@ -47,7 +47,7 @@ public final class JsonXQueryConverter extends JsonConverter {
* @param opts JSON options
* @throws QueryException query exception
*/
JsonXQueryConverter(final JsonParserOptions opts) throws QueryException {
JsonW3Converter(final JsonParserOptions opts) throws QueryException {
super(opts);
final JsonDuplicates dupl = jopts.get(JsonParserOptions.DUPLICATES);
if(dupl == JsonDuplicates.RETAIN) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @author BaseX Team, BSD License
* @author Christian Gruen
*/
public final class JsonBasicConverter extends JsonXmlConverter {
public final class JsonW3XmlConverter extends JsonXmlConverter {
/** Escape characters. */
private final boolean escape;

Expand All @@ -22,7 +22,7 @@ public final class JsonBasicConverter extends JsonXmlConverter {
* @param opts JSON options
* @throws QueryException query exception
*/
JsonBasicConverter(final JsonParserOptions opts) throws QueryException {
JsonW3XmlConverter(final JsonParserOptions opts) throws QueryException {
super(opts);
escape = jopts.get(JsonParserOptions.ESCAPE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public abstract class CsvSerializer extends StandardSerializer {
public static Serializer get(final OutputStream os, final SerializerOptions so)
throws IOException {
switch(so.get(SerializerOptions.CSV).get(CsvOptions.FORMAT)) {
case XQUERY: return new CsvXQuerySerializer(os, so);
case XQUERY: return new CsvXQuerySerializer(os, so); // deprecated
case W3: return new CsvW3Serializer(os, so);
case W3_ARRAYS: return new CsvW3ArraysSerializer(os, so);
case W3_XML: return new CsvW3XmlSerializer(os, so);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ public void serialize(final Item item) throws IOException {
row(m.get(CsvConverter.COLUMNS), tl);
}
// print rows
if(!m.contains(CsvConverter.ROWS)) throw CSV_SERIALIZE_X.getIO("Map has no 'rows' key");
for(final Item record : m.get(CsvConverter.ROWS)) row(((XQArray) record).iterable(), tl);
if(!m.contains(CsvConverter.ROWS))
throw CSV_SERIALIZE_X.getIO("Map has no 'rows' key");
for(final Item record : m.get(CsvConverter.ROWS)) {
row(((XQArray) record).iterable(), tl);
}
} catch(final QueryException ex) {
throw new QueryIOException(ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.basex.util.list.*;

/**
* This class serializes map data as CSV. The input must conform to the XQuery CSV representation.
* This class serializes a map as CSV. The input must conform to the XQuery CSV representation.
*
* @author BaseX Team, BSD License
* @author Christian Gruen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ public static Serializer get(final OutputStream os, final SerializerOptions so)
throws IOException {
switch(so.get(SerializerOptions.JSON).get(JsonOptions.FORMAT)) {
case JSONML: return new JsonMLSerializer(os, so);
case BASIC: return new JsonBasicSerializer(os, so);
default: return new JsonNodeSerializer(os, so);
case BASIC: // deprecated
case W3_XML: return new JsonBasicSerializer(os, so);
default: return new JsonNodeSerializer(os, so);
}
}

Expand Down
4 changes: 2 additions & 2 deletions basex-core/src/main/java/org/basex/query/QueryContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ public void addLocks() {
* The specified type is interpreted as follows:
* <ul>
* <li> If {@code "json"} is specified, the value is converted according to the rules
* specified in {@link JsonXQueryConverter}.</li>
* specified in {@link JsonW3Converter}.</li>
* <li> Otherwise, the type is cast to the specified XDM type.</li>
* </ul>
* @param name name of variable; context value if empty string or {@code null}
Expand Down Expand Up @@ -716,7 +716,7 @@ private Value cast(final Object value, final String type) throws QueryException
// convert JSON input
if(type.equalsIgnoreCase(MainParser.JSON.name())) {
final JsonParserOptions jp = new JsonParserOptions();
jp.set(JsonOptions.FORMAT, JsonFormat.XQUERY);
jp.set(JsonOptions.FORMAT, JsonFormat.W3);
return JsonConverter.get(jp).convert(object.toString(), "");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
public final class FnJsonDoc extends ParseJson {
@Override
public Value value(final QueryContext qc) throws QueryException {
return doc(qc, JsonFormat.XQUERY);
return doc(qc, JsonFormat.W3);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
public final class FnJsonToXml extends ParseJson {
@Override
public Value value(final QueryContext qc) throws QueryException {
return parse(qc, JsonFormat.BASIC);
return parse(qc, JsonFormat.W3_XML);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
public final class FnParseJson extends ParseJson {
@Override
public Value value(final QueryContext qc) throws QueryException {
return parse(qc, JsonFormat.XQUERY);
return parse(qc, JsonFormat.W3);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public Item item(final QueryContext qc, final InputInfo ii) throws QueryExceptio
final JsonSerialOptions options = toOptions(arg(1), new JsonSerialOptions(), qc);
if(node == null) return Empty.VALUE;

options.set(JsonOptions.FORMAT, JsonFormat.BASIC);
options.set(JsonOptions.FORMAT, JsonFormat.W3_XML);
final Boolean indent = options.get(JsonSerialOptions.INDENT);
// no indentation specified: adopt module indentation
if(indent == null) options.set(JsonSerialOptions.INDENT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ private Value parse(final QueryContext qc, final JsonFormat format, final byte[]

final JsonConverter converter = JsonConverter.get(options);
final JsonFormat jf = options.get(JsonOptions.FORMAT);
if(options.get(JsonParserOptions.VALIDATE) != null && jf != JsonFormat.BASIC) {
if(options.get(JsonParserOptions.VALIDATE) != null && jf != JsonFormat.W3_XML &&
jf != JsonFormat.BASIC) {
throw INVALIDOPTION_X.get(info, Options.unknown(JsonParserOptions.VALIDATE));
}

Expand All @@ -90,7 +91,7 @@ private Value parse(final QueryContext qc, final JsonFormat format, final byte[]
converter.numberParser(s -> np.invoke(qc, info, Atm.get(s)).item(qc, info));
}
final Value nll = options.get(JsonParserOptions.NULL);
if(nll != Empty.VALUE && jf != JsonFormat.XQUERY) {
if(nll != Empty.VALUE && jf != JsonFormat.W3 && jf != JsonFormat.XQUERY) {
throw INVALIDOPTION_X.get(info, Options.unknown(JsonParserOptions.NULL));
}
converter.nullValue(nll);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ public final class JsonSerializerTest extends SandboxTest {
}

/**
* Tests for the 'xquery' serialization format.
* Tests for the 'w3' serialization format.
*/
@Test public void xquery() {
final JsonFormat format = JsonFormat.XQUERY;
@Test public void w3() {
final JsonFormat format = JsonFormat.W3;

// objects
serialize("map { }", "{}", format);
Expand Down

0 comments on commit 36b6566

Please sign in to comment.