1

I have an Object array which is a list of argument values a function could take. This could be any complex object.

I am trying to build a json out of the Object array using gson as below:

private JsonArray createArgsJsonArray(Object... argVals) {
    JsonArray argsArray = new JsonArray();
    Arrays.stream(argVals).forEach(arg -> argsArray.add(gson.toJson(arg)));
    return argsArray;
}
  1. This treats all the arg values as String.
  2. It escapes the String args

    "args":["\"STRING\"","1251996697","85"]
    

I prefer the following output:

   "args":["STRING",1251996697,85]

Is there a way to achieve this using gson?

I used org.json, I was able to achieve the desired result, but it does not work for complex objects.

EDIT:

I applied the solution provided by @Michał Ziober, but now how do I get back the object.

Gson gson = new Gson();
Object strObj = "'";
JsonObject fnObj = new JsonObject();
JsonObject fnObj2 = new JsonObject();
fnObj.add("response", gson.toJsonTree(strObj));
fnObj2.addProperty("response", gson.toJson(strObj));

System.out.println(gson.fromJson(fnObj.toString(), 
Object.class)); --> prints {response='}   //Not what I want!
System.out.println(gson.fromJson(fnObj2.toString(), 
Object.class)); --> prints {response="\u0027"}
2
  • I think you are trying to call the toString() method of the object?
    – Kris
    Commented Sep 19, 2019 at 12:58
  • yes, you are right! I print it using toString. Commented Sep 19, 2019 at 13:13

3 Answers 3

3

Use toJsonTree method:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import java.util.Date;

public class GsonApp {

  public static void main(String[] args) {
    GsonApp app = new GsonApp();
    System.out.println(app.createArgsJsonArray("text", 1, 12.2D));
    System.out.println(app.createArgsJsonArray(new Date(), new A(), new String[] {"A", "B"}));
  }

  private Gson gson = new GsonBuilder().create();

  private JsonArray createArgsJsonArray(Object... argVals) {
    JsonArray argsArray = new JsonArray();

    for (Object arg : argVals) {
      argsArray.add(gson.toJsonTree(arg));
    }

    return argsArray;
  }
}

class A {
  private int id = 12;
}

Above code prints:

["text",1,12.2]
["Sep 19, 2019 3:25:20 PM",{"id":12},["A","B"]]
4
  • Thanks for the solution, but now I don't how to get back the object? I have added details in the EDIT section of my question. Commented Sep 20, 2019 at 6:36
  • @user2761431, I'm not sure what do you want to do. You want to create a JSON Array with some different elements and after that you want to deserialise it back to Object[]? Commented Sep 20, 2019 at 7:14
  • Ziober yes, I need to serialize and de-serialize. Commented Sep 20, 2019 at 9:08
  • @user2761431, if you have an object what is the point to serialise and deserialise it back? In your example, you want to serialise apostrophe and what do you expect after deserialisation? Please, create a new question, with working example what you get right now and what do you expect to receive. Commented Sep 20, 2019 at 10:12
1

If you want to end up with a String, just do:

private String createArgsJsonArray(Object... argVals) {
    Gson gson = new Gson();
    return gson.toJson(argVals);
}

If you wish to collect it back and alter just do:

Object[] o = new Gson().fromJson(argValsStr, Object[].class);
1

Try using setPrettyPrinting with DisableHtml escaping.

Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
 JsonParser jp = new JsonParser();
 JsonElement je = jp.parse(jsonArray.toString());
 System.out.println( gson.toJson(je));
1
  • I will not be able to build the JsonArray in the desired format? I have to process it post building the array? Commented Sep 19, 2019 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.