FormattingProvider(格式化提供器)
适用于 ✅ 开源版 ✅ 专业版 ✅ 企业版
前几节讨论的大多数导出格式都有一个相关的格式化配置对象,该对象可以在每次格式化调用时传递,包括
-
org.jooq.ChartFormat
用于格式化 图表导出 -
org.jooq.CSVFormat
用于格式化 CSV 导出 -
org.jooq.JSONFormat
用于格式化 JSON 导出 -
org.jooq.TXTFormat
用于格式化 文本导出 -
org.jooq.XMLFormat
用于格式化 XML 导出
例如,当您想要指定 JSON 记录布局,以及删除标题信息时,您可以编写如下代码
System.out.println("Using the object layout"); System.out.println(result.formatJSON(new JSONFormat().header(false).recordFormat(JSONFormat.RecordFormat.OBJECT))); System.out.println("Using the array layout"); System.out.println(result.formatJSON(new JSONFormat().header(false).recordFormat(JSONFormat.RecordFormat.ARRAY)));
输出如下(另请参阅 JSON 导出 了解详细信息)
Using the object layout [{"col1": "string1", "col2": 1}, {"col1": "string2", "col2": 2}] Using the array layout [["string1", 1],["string2", 2]]
除了将此 JSONFormat
对象传递给每次格式化调用之外,您还可以将 org.jooq.FormattingProvider
SPI 注册到您的 Configuration 中,以便指定应默认应用的格式(在没有显式格式化配置对象的情况下)。
例如,为了关闭 JSON 标头并在所有地方指定数组布局,请执行以下操作
JSONFormat format = new JSONFormat().header(false).recordFormat(JSONFormat.RecordFormat.ARRAY); Configuration configuration = create.configuration().derive(FormattingProvider // This specifies the format to be used for Record.formatJSON() calls .onJsonFormatForRecords(() -> format) // This specifies the format to be used for Result.formatJSON() calls .onJsonFormatForResults(() -> format)); System.out.println( configuration.dsl() .select(BOOK.ID, BOOK.TITLE) .from(BOOK) .fetch() .formatJSON() // No need to pass the format here anymore );
反馈
您对此页面有任何反馈吗? 我们很乐意倾听!