Search in sources :

Example 1 with Formattable

use of org.jooq.Formattable in project jOOQ by jOOQ.

the class AbstractResult method formatXMLRecord.

static final void formatXMLRecord(Writer writer, XMLFormat format, int recordLevel, Record record, AbstractRow<?> fields) throws java.io.IOException {
    String newline = format.newline();
    writer.append("<record");
    if (format.xmlns()) {
        format = format.xmlns(false);
        writer.append(" xmlns=\"" + Constants.NS_EXPORT + "\"");
    }
    writer.append(">");
    int size = fields.size();
    for (int index = 0; index < size; index++) {
        Object value = record.get(index);
        writer.append(newline).append(format.indentString(recordLevel + 1));
        String tag = format.recordFormat() == COLUMN_NAME_ELEMENTS ? escapeXML(fields.field(index).getName()) : "value";
        writer.append("<" + tag);
        if (format.recordFormat() == VALUE_ELEMENTS_WITH_FIELD_ATTRIBUTE) {
            writer.append(" field=\"");
            writer.append(escapeXML(fields.field(index).getName()));
            writer.append("\"");
        }
        if (value == null) {
            writer.append("/>");
        } else {
            writer.append(">");
            if (value instanceof Formattable) {
                Formattable f = (Formattable) value;
                writer.append(newline).append(format.indentString(recordLevel + 2));
                int previous = format.globalIndent();
                f.formatXML(writer, format.globalIndent(format.globalIndent() + format.indent() * (recordLevel + 2)));
                format.globalIndent(previous);
                writer.append(newline).append(format.indentString(recordLevel + 1));
            } else if (value instanceof XML && !format.quoteNested())
                writer.append(((XML) value).data());
            else
                writer.append(escapeXML(format0(value, false, false)));
            writer.append("</" + tag + ">");
        }
    }
    writer.append(newline).append(format.indentString(recordLevel)).append("</record>");
}
Also used : XML(org.jooq.XML) Formattable(org.jooq.Formattable)

Example 2 with Formattable

use of org.jooq.Formattable in project jOOQ by jOOQ.

the class AbstractResult method formatJSON0.

private static final void formatJSON0(Object value, Writer writer, JSONFormat format) throws java.io.IOException {
    // [#2741] TODO: This logic will be externalised in new SPI
    if (value instanceof byte[]) {
        byte[] a = (byte[]) value;
        JSONValue.writeJSONString(DatatypeConverter.printBase64Binary(a), writer);
    } else // [#6563] Arrays can be serialised natively in JSON
    if (value instanceof Object[]) {
        Object[] array = (Object[]) value;
        writer.append('[');
        for (int i = 0; i < array.length; i++) {
            if (i > 0)
                writer.append(',');
            formatJSON0(array[i], writer, format);
        }
        writer.append(']');
    } else // [#7782] Nested records should generate nested JSON data structures
    if (value instanceof Formattable) {
        Formattable f = (Formattable) value;
        f.formatJSON(writer, format);
    } else if (value instanceof JSON && !format.quoteNested()) {
        writer.write(((JSON) value).data());
    } else if (value instanceof JSONB && !format.quoteNested()) {
        writer.write(((JSONB) value).data());
    } else {
        JSONValue.writeJSONString(value, writer);
    }
}
Also used : JSONB(org.jooq.JSONB) JSON(org.jooq.JSON) Formattable(org.jooq.Formattable)

Aggregations

Formattable (org.jooq.Formattable)2 JSON (org.jooq.JSON)1 JSONB (org.jooq.JSONB)1 XML (org.jooq.XML)1