Search in sources :

Example 1 with JSON

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

the class AbstractResult method format0.

/**
 * @param value The value to be formatted
 * @param visual Whether the formatted output is to be consumed visually
 *            (HTML, TEXT) or by a machine (CSV, JSON, XML)
 */
private static final String format0(Object value, boolean changed, boolean visual) {
    // [#2741] TODO: This logic will be externalised in new SPI
    String formatted = changed && visual ? "*" : "";
    if (value == null) {
        formatted += visual ? "{null}" : null;
    } else if (value.getClass() == byte[].class) {
        formatted += DatatypeConverter.printBase64Binary((byte[]) value);
    } else if (value.getClass().isArray()) {
        // [#6545] Nested arrays are handled recursively
        formatted += Arrays.stream((Object[]) value).map(f -> format0(f, false, visual)).collect(joining(", ", "[", "]"));
    } else if (value instanceof EnumType) {
        EnumType e = (EnumType) value;
        formatted += e.getLiteral();
    } else if (value instanceof List) {
        List<?> l = (List<?>) value;
        formatted += l.stream().map(f -> format0(f, false, visual)).collect(joining(", ", "[", "]"));
    } else if (value instanceof Record) {
        Record r = (Record) value;
        formatted += Arrays.stream(r.intoArray()).map(f -> format0(f, false, visual)).collect(joining(", ", "(", ")"));
    } else // [#6080] Support formatting of nested ROWs
    if (value instanceof Param) {
        formatted += format0(((Param<?>) value).getValue(), false, visual);
    } else // [#5238] Oracle DATE is really a TIMESTAMP(0)...
    if (value instanceof Date) {
        Date d = (Date) value;
        String date = value.toString();
        if (Date.valueOf(date).equals(value))
            formatted += date;
        else
            formatted += new Timestamp(d.getTime());
    } else {
        formatted += value.toString();
    }
    return formatted;
}
Also used : Arrays(java.util.Arrays) Row(org.jooq.Row) Display(org.jooq.ChartFormat.Display) Table(org.jooq.Table) DatatypeConverter(jakarta.xml.bind.DatatypeConverter) VALUE_ELEMENTS_WITH_FIELD_ATTRIBUTE(org.jooq.XMLFormat.RecordFormat.VALUE_ELEMENTS_WITH_FIELD_ATTRIBUTE) IOException(org.jooq.exception.IOException) Document(org.w3c.dom.Document) StringUtils.leftPad(org.jooq.tools.StringUtils.leftPad) DSLContext(org.jooq.DSLContext) AttributesImpl(org.xml.sax.helpers.AttributesImpl) StringUtils.rightPad(org.jooq.tools.StringUtils.rightPad) DSL.name(org.jooq.impl.DSL.name) Timestamp(java.sql.Timestamp) SettingsTools.renderLocale(org.jooq.conf.SettingsTools.renderLocale) Constants(org.jooq.Constants) Field(org.jooq.Field) TableRecord(org.jooq.TableRecord) Math.min(java.lang.Math.min) ChartFormat(org.jooq.ChartFormat) Result(org.jooq.Result) Cursor(org.jooq.Cursor) Collectors.joining(java.util.stream.Collectors.joining) DocumentFragment(org.w3c.dom.DocumentFragment) List(java.util.List) TableField(org.jooq.TableField) COLUMN_NAME_ELEMENTS(org.jooq.XMLFormat.RecordFormat.COLUMN_NAME_ELEMENTS) SAXException(org.xml.sax.SAXException) Writer(java.io.Writer) Math.max(java.lang.Math.max) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) JSONValue(org.jooq.tools.json.JSONValue) XML(org.jooq.XML) EnumType(org.jooq.EnumType) Deque(java.util.Deque) FormattingProvider(org.jooq.FormattingProvider) ArrayList(java.util.ArrayList) JSONFormat(org.jooq.JSONFormat) DSL.insertInto(org.jooq.impl.DSL.insertInto) Schema(org.jooq.Schema) Attributes(org.xml.sax.Attributes) CSVFormat(org.jooq.CSVFormat) ContentHandler(org.xml.sax.ContentHandler) Record(org.jooq.Record) InputSource(org.xml.sax.InputSource) XMLFormat(org.jooq.XMLFormat) NodeList(org.w3c.dom.NodeList) Iterator(java.util.Iterator) JSON(org.jooq.JSON) TXTFormat(org.jooq.TXTFormat) Formattable(org.jooq.Formattable) StringUtils(org.jooq.tools.StringUtils) Date(java.sql.Date) Param(org.jooq.Param) DefaultHandler(org.xml.sax.helpers.DefaultHandler) Configuration(org.jooq.Configuration) StringUtils.abbreviate(org.jooq.tools.StringUtils.abbreviate) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) TreeMap(java.util.TreeMap) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) JSONB(org.jooq.JSONB) ArrayDeque(java.util.ArrayDeque) DSL.table(org.jooq.impl.DSL.table) Collections(java.util.Collections) EnumType(org.jooq.EnumType) Param(org.jooq.Param) List(java.util.List) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) TableRecord(org.jooq.TableRecord) Record(org.jooq.Record) Timestamp(java.sql.Timestamp) Date(java.sql.Date)

Example 2 with JSON

use of org.jooq.JSON 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)2 JSONB (org.jooq.JSONB)2 DatatypeConverter (jakarta.xml.bind.DatatypeConverter)1 StringReader (java.io.StringReader)1 Writer (java.io.Writer)1 Math.max (java.lang.Math.max)1 Math.min (java.lang.Math.min)1 Date (java.sql.Date)1 Timestamp (java.sql.Timestamp)1 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Deque (java.util.Deque)1 Iterator (java.util.Iterator)1 List (java.util.List)1 TreeMap (java.util.TreeMap)1 Collectors.joining (java.util.stream.Collectors.joining)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1