tada / pljava

PL/Java is a free add-on module that brings Java™ Stored Procedures, Triggers, Functions, Aggregates, Operators, Types, etc., to the PostgreSQL™ backend.
http://tada.github.io/pljava/
Other
238 stars 77 forks source link

JSONB type mapping #447

Closed udesaiitrs closed 11 months ago

udesaiitrs commented 11 months ago

Hi, looking through the documentation, we do not see JSONB as a supported type. Is that correct or is the Wiki out of date? If it is not supported, what would be the best way to implement a function that takes JSONB types as an argument? Would we just use a raw BYTEA type?

Thanks!

jcflack commented 11 months ago

It's correct that there is no current support. What you would want to do would depend on exactly what your function wants to do with its JSONB arguments.

You might be stuck giving the arguments String type. That will require PostgreSQL to serialize the JSONB into JSON string form when calling the function, and then, adding insult to injury, if you wanted to act on the string as something structured, you would end up using one of the available Java JSON libraries to parse it again.

For XML, in contrast, an argument can be given the Java SQLXML type, which then allows structured manipulation with Java's (multiple!) batteries-included XML APIs.

Java has not adopted a batteries-included JSON API (though there are some quite widely used third-party libraries), so there is no equally obvious choice of a native Java type to which a JSON or JSONB argument should be mapped. Often, you would want to select your Java JSON library of choice, and then be able to supply an adapter mapping the PostgreSQL type onto that. That is getting into the territory of PR #399 and is not in any currently released PL/Java version.

PL/Java currently allows the pg_node_tree type to be received as a Java SQLXML argument, using a simple mapping of the node-tree syntax to XML, to which all the Java capabilities like XSLT, XPath, and so on can be applied. As far as a mapping for JSON that would not require external libraries, I am partial to implementing that same approach, using the standard XML representation of JSON defined by w3 XML Query. But that isn't implemented yet.

udesaiitrs commented 11 months ago

Okay understood, thanks for the detailed explanation @jcflack! We are constrained to JSON unfortunately for now. I will test the performance with what you suggested. Thanks again!