0

I have an JSF application that stores/retrieves data to/from Oracle DB. Very simple application. I used Gson (from google) API to convert String type data to JSON format into a table in DB.

When I inspected stored data from the table, I expected "{" and "}" beginning and end but I didn't see it instead data was wrapped with double quotes. Is this correct?

Here is an example.

  1. From UI (.xhtml) there is an inputText field to capture data. The data is part (property) of JPA entity model and this model is persisted to DB.

    (foo = foo)
    
    gson.toJson(variable_that_holds_above_value);
    
  2. After querying data from the DB, data stored as ... The column that contains below data is clob type.

    "(foo \u003d foo)"
    

"=" is encoded as "\u003d" b/c I didn't use disableHtmlEscaping().

[update]

I realized that gson.toJson() does what it should but what I wanted/expected was "{"keyName":"foo=foo"}. I naively thought simply providing a string to toJson() will do that for me.

I decided to use JsonObject instead. Reading this How to convert a String to JsonObject using gson library helped me.

JsonObject jb = new JsonObject();
jb.addProperty("keyName","foo=foo");
DaeYoung
  • 1,161
  • 6
  • 27
  • 59
  • [JSON](http://www.json.org/) *objects* do require braces, although simply a single value such as a string is still considered valid JSON such as `"foo"`. Just a note most of the time JSON comes into a program as a string such as `\" { "foo": "bar" } \"` where it needs to be parsed from a string into an object. – Spencer Wieczorek Jul 29 '17 at 17:56
  • I realized that that I have to use JsonObject in order to store data as "{"foo" : "bar"}. Thank you Majora320 and Spencer Wieczorek for your advice. – DaeYoung Jul 29 '17 at 19:34

0 Answers0