doberkofler / PLSQL-JSON

The JSON encode/decode library for Oracle PL/SQL
MIT License
47 stars 15 forks source link

Enhancements #1

Closed matthias-oe closed 10 years ago

matthias-oe commented 10 years ago

we needed the ability to work with JSON-arrays generated and consumed by google-gson (A Java library to convert JSON to Java objects and vice-versa) in stored procedures, so we decided to enhance the json_array type. As we handle small objects we added the "to_string" method to the json_array and json_object to encapsulate the CLOB-handling, but we are not sure about "the right" way to avoid invalid return values.

doberkofler commented 10 years ago

Dear @matthias-oe!

Thank you for your pull request!

I have used your suggestion and added an API for parsing json strings representing an array and released the code in 0.2.0. It is now possible to construct a json_array and a json_value from a string.

I'm still investigating your pull request with the new to_string method for small objects but it currently seems as if there would be no actual performance improvement. Could you please elaborate on your motivation for adding the to_string method?

matthias-oe commented 10 years ago

Dear @doberkofler

thanks you for considering my request. The new parsing API looks great!

Let me try to explain my ambition to introduce the to_string method: We call our PLSQL-Procedures from Java using plain string IN / OUT or RETURN parameters representing JSON. So we have to produce JSON as varchar2 in the Java-interface procedures. Using the to_string method avoids the same code-fragment to convert from clob to varchar2 in every stored procedure which uses the PLSQL-JSON library. Perhaps I didn’t recognize an implicit type conversion that makes the to_string method obsolete, but I just want to get around the bulky clob.

Here is a small example:

CREATE OR REPLACE FUNCTION Test RETURN VARCHAR2 IS ajson JSON_Array := JSON_Array(); ojson JSON_Object; BEGIN ojson := JSON_Object(); ojson.put ('key', 'menu_horizontal'); ojson.put ('value', 'Horizontal Menu'); ojson.put ('selected', 'true'); ajson.append (ojson);

ojson := JSON_Object(); ojson.put ('key', 'menu_vertikal'); ojson.put ('value', 'Vertikal Menu'); ojson.put ('selected', 'false'); ajson.append (ojson);

RETURN ajson.to_string; END;

doberkofler commented 10 years ago

Dear @matthias-oe

I would generally recommend also using the CLOB type in Java to make sure that there is no overflow. The Oracle JVM does support CLOB as a legal datatype mapping to oracle.sql.CLOB and this would make sure that you never again have to care for an overflow.

Alternatively you should be able to simply return the CLOB in your example and ORacle would automatically covert it to a varchar2:

CREATE OR REPLACE FUNCTION return_a_clob RETURN CLOB IS
    lob CLOB;
BEGIN
    lob := 'this is a clob';
    RETURN lob;
END return_a_clob;
/
CREATE OR REPLACE FUNCTION return_a_varchar2 RETURN VARCHAR2 IS
BEGIN
    RETURN return_a_clob;
END return_a_varchar2;
/
BEGIN
    dbms_output.put_line(return_a_varchar2());
END;
/
matthias-oe commented 10 years ago

Dear doberkofler,

we are using Java in Web-Environments specified by our customers and we want to avoid proprietary datatypes...

Actually the to_clob method seems to be a bit unhandy: aLob CLOB := empty_clob();

dbms_lob.createtemporary(aLob, TRUE); ???.to_clob(aLob);

I tried to hide this in the to_string method.

Anyway, thanks for your great PLSQL-JSON library!

Yours sincerely Matthias

Von: Dieter Oberkofler [mailto:notifications@github.com] Gesendet: Dienstag, 24. Juni 2014 08:34 An: doberkofler/PLSQL-JSON Cc: Matthias Oestreicher Betreff: Re: [PLSQL-JSON] Enhancements (#1)

Dear @matthias-oe I would generally recommend also using the CLOB type in Java to make sure that there is no overflow. The Oracle JVM does support CLOB as a legal datatype mapping to oracle.sql.CLOB and this would make sure that you never again have to care for an overflow. Alternatively you should be able to simply return the CLOB in your example and ORacle would automatically covert it to a varchar2: CREATE OR REPLACE FUNCTION return_a_clob RETURN CLOB IS lob CLOB; BEGIN lob := 'this is a clob'; RETURN lob; END return_a_clob; / CREATE OR REPLACE FUNCTION return_a_varchar2 RETURN VARCHAR2 IS BEGIN RETURN return_a_clob; END return_a_varchar2; / BEGIN dbms_output.put_line(return_a_varchar2()); END; / — Reply to this email directly or view it on GitHub.