Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancements #1

Closed
wants to merge 3 commits into from
Closed

Enhancements #1

wants to merge 3 commits into from

Conversation

matthias-oe
Copy link

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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:[email protected]]
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.

@doberkofler doberkofler closed this Oct 4, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants