JSON serialization and __odbHidden members

#1

Hi..

Building a REST API (Jersey-based one) I noticed that every object I serialize to JSON has a clone field with "__odbHidden" suffix, e.g.there's  "id" and "id__odbHidden" is also (unwantenly) present with the same value.

In general, we can annotate fields to include/exclude them from JSON serialization, but since these fields are not declared in my classes, I have no idea how to tell marshaller to avoid them.

Probably this is more of a general rather than ObjectDB question, I am quite new to the world of REST/JSON, but maybe some one already came through this problem and managed to solve it? serialization seems like a common taks to me so chances are the solution already exists.

Thanks.

#2

The ObjectDB Enhancer adds members to enhanced entity classes, but most of the additions are methods rather then fields and the few added fields are excluded from standard serialization automatically.

In order to provide support in excluding these fields (maybe automatically) from JSON serialization it would be helpful to see a minimal simple test case (using the specified format if possible) that demonstrates this with a specific JSON serialization library.

ObjectDB Support
#3

Hi, first of all thanks for your fast reply.

Yes, probably these are methods. I must admit I have no idea how could they be added to already-declared types in run-time (never seen such approche before).

Here's my class:

public class User implements Serializable {
private Long id;
private LoginInfo loginInfo;
private UserSettings userSettings;
private Activity regActivity ,
      lastActivity;

private Profile profile;
private List<Comment> comments = new ArrayList<>();

public User(){
  
} // here the usual getters/setters (auto-generated)

The LoginInfo class contains just 2 fields (String eMail & password with relevant getters/setters)

RESTful service class:

@Path("/user")
public class rs1 {
    @SuppressWarnings("unused")
    @Context    private UriInfo context;
    @Context HttpServletRequest request;
    @Inject private DAO<User> userDao;
    public rs1() {
    }

// ....

@PUT
    @Consumes("application/json")
    @Produces("application/json")
    @Path("/reguser")
    public Response regUser(LoginInfo loginInfo, @QueryParam("screenX") Integer x,@QueryParam("screenY") Integer y,
      @QueryParam("u_id") String u_id){
     User user = new User();
     user.setLoginInfo(loginInfo);

// setting some other params for user (nothing special)

userDao.persist(user);

return Response.status(200).entity(user).build();

The request is of PUT method with appropriate content-type header of application/json and returns json data, but it has all these "hidden" entrys:

{
   "comments":
   [
   ],
   "comments__odbHidden":
   [
   ],
   "id": 48,
   "id__odbHidden": 48,
   "lastActivity":
   {
       "IP": "127.0.0.1",
       "date": "2015-10-30T17:25:55.359+03:00",
       "ua": "Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/20100101 Firefox/15.0",
       "url": "user/reguser"
   },
   "lastActivity__odbHidden":
   {
       "IP": "127.0.0.1",
       "date": "2015-10-30T17:25:55.359+03:00",
       "ua": "Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/20100101 Firefox/15.0",
       "url": "user/reguser"
   },
   "loginInfo":
   {
       "eMail": "foo@bar.fb",
       "password": "somepass"
   },
   "loginInfo__odbHidden":
   {
       "eMail": "foo@bar.fb",
       "password": "somepass"
   },
   "profile":
   {
   },
   "profile__odbHidden":
   {
   },
   "regActivity":
   {
       "IP": "127.0.0.1",
       "date": "2015-10-30T17:25:55.359+03:00",
       "ua": "Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/20100101 Firefox/15.0",
       "url": "user/reguser"
   }, // and so on

So the problem is clear I guess.

Must add that I am using the Glassfish server 4. I didn't somehow configure JAX-RS implementation thus using built-in jersey one.

The basic idea of excluding elements, as I know and as google suggest, is annotating needed element with @Xmltransient, but here it doesn't work for an obviouse reason.

 

This seems even more odd since when I previously used gson to serialize my entities and everything was fine, but I wanted to delegate serialization to the built-in mechanism.

#4

We may support as an option adding an XmlTransient annotation automatically to these added members.

Will it solve the situation that you are currently facing?

ObjectDB Support
#5

Yes, it is likely to solve the problem. Whenever you add this option, let me know so I can try and see if it shall really work.

#6

Please try build 2.6.4_03, which includes a different (and hopefully better) solution.

Enhancer generated methods do not have get/set prefix now (as was also until fixing issue #325 in version 2.6), so they should be excluded from any serialization without any further action.

ObjectDB Support
#7

Reply