667 words

JSON serialization and __odbHidden members

#1
2015-10-30 10:13

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.

tester15
tester15's picture
Joined on 2015-10-30
User Post #1
#2
2015-10-30 13:55

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
ObjectDB - Fast Object Database for Java (JPA/JDO)
support
support's picture
Joined on 2010-05-03
User Post #2,326
#3
2015-10-30 14:13

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.

tester15
tester15's picture
Joined on 2015-10-30
User Post #2
#4
2015-11-02 09:58

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
ObjectDB - Fast Object Database for Java (JPA/JDO)
support
support's picture
Joined on 2010-05-03
User Post #2,327
#5
2015-11-05 17:04

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.

tester15
tester15's picture
Joined on 2015-10-30
User Post #3
#6
2015-11-09 14:38

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 #47 in version 2.6), so they should be excluded from any serialization without any further action.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
support
support's picture
Joined on 2010-05-03
User Post #2,336
#7
2016-08-20 09:13
--- Webel IT Australia, "The Elements of the Web", Specialists in model-based UML, SysML, Enterprise Java, XML, and Drupal CMS web engineering. Dr Darren Kelly, BSc, PhD, https://www.webel.com.au
webel
webel's picture
Joined on 2011-05-27
User Post #96

Post Reply

Please read carefully the posting instructions - before posting to the ObjectDB website.

  • You may have to disable pop up blocking in order to use the toolbar (e.g. in Chrome).
  • Use ctrl + right click to open the browser context menu in the editing area (e.g. for using a browser spell checker).
  • To insert formatted lines (e.g. Java code, stack trace) - select a style in the toolbar and then insert the text in the new created block.
  • Avoid overflow of published source code examples by breaking long lines.
  • You may mark in paragraph code words (e.g. class names) with the code style (can be applied by ctrl + D).
  • Long stack traces (> 50 lines) and complex source examples (> 100 lines) should be posted as attachments.
Attachments:
Maximum file size: 32 MB
Cancel