1

In my backend I have a class User which has multiple Trips. Those trips have multiple user. This is a circular reference.

I use this code in my rest service to serialize the user object:

Gson gson = new Gson();
return gson.toJson(user, TripUser.class);

In my android app I do the following:

Gson gson = new Gson();
TripUser Tuser = gson.fromJson(data, TripUser.class);
return Tuser;

What can I do about the circular reference exception? Is there another way to make this work?

Glenn Obyn
  • 11
  • 1

2 Answers2

0

You should try to use flexjson 2.1 This would solve your problem because you can chose which items he should serialize to Json etc. Also this library gives no problems with circular references

Liquid
  • 1,871
  • 1
  • 17
  • 32
-3

I am not fully sure , but u can try like this :

Gson gson = new Gson();
Type type = new TypeToken<TripUser>(){}.getType();
TripUser user = (TripUser) gson.fromJson(data, type);
Anand
  • 2,875
  • 1
  • 18
  • 16