Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize object using GSON

How can I serialize and deserialize this object with gson to json:

public class test{

@Expose
public List< Pair<Double,Double> > list;

@Expose
public int alpha;
}

I've tried this:

Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
String str = gson.toJson(testInstance,test.class);

where testInstance is an instance of class test, but it's not working because of Pair structure in List.

like image 436
Krivers Avatar asked Mar 24 '26 02:03

Krivers


1 Answers

You have configured Gson object using excludeFieldsWithoutExposeAnnotation method. From documentation:

Configures Gson to exclude all fields from consideration for serialization or deserialization that do not have the Expose annotation.

Could you change Pair class? If yes, you have to add @Expose annotation to each property which you want serialize to JSON. If no, you can create similar class in your program and add annotations.

like image 127
Michał Ziober Avatar answered Mar 25 '26 14:03

Michał Ziober



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!