This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Machine Learning with R: Expert techniques for predictive modeling and have Brett Lantz on-line!
See this thread for details.
Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

How to serialize JavaBeans to byte array without Serializable?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys. How's everybody doing? Hope y'all doing fine. I'm fairly new to Java and I've got a bit of a problem here. Is it possible to serialize JavaBeans to byte array without implementing Serializable? I was given a task to write a class that serializes and deserializes JavaBeans without using JSON, Protobuf, Java serialization API, and other serializations. I'm obliged to implement this interface instead:



Could you guys give me a piece of advice on how this can be done?
Thanks for your time.
 
Saloon Keeper
Posts: 5811
146
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

You could use java.beans.XMLEncoder and XMLDecoder for encoding/decoding, and convert to and from byte[] using java.util.Base64.
 
Rancher
Posts: 4275
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why the restrictions?
 
Brad Wood
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:Welcome to the Ranch.

You could use java.beans.XMLEncoder and XMLDecoder for encoding/decoding, and convert to and from byte[] using java.util.Base64.


Thanks! I'm gonna try it.

Dave Tolls wrote:Why the restrictions?


I was applying for a job and they gave me this assignment. Guess depending on how well I'll tackle this problem, they'll be deciding whether to give me a job or not.
 
Marshal
Posts: 65821
250
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Moving question to the general forum as too difficult for “beginning”.
 
Saloon Keeper
Posts: 21137
134
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:Welcome to the Ranch.

You could use java.beans.XMLEncoder and XMLDecoder for encoding/decoding, and convert to and from byte[] using java.util.Base64.



Could. Although I suspect that that, too would run afoul of the "other serializations" constraint.

The Base64 methods are on the right track, though. Or, alternatively, converting the bytes to hexadecimal strings.

Since this is a bean serializer, not only the property values must be written out, however. You also need to include the name of the class and its property names paired with the values.

Being as how I'm a jerk. I'd probably write them out in Amiga IFF format. Which is actually quite suited for this sort of thing, antique though it is.

Or, if you want to be a REAL pain in the fundament - serialize as text in LISP expressions.
 
Brad Wood
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys. It's me again. Thank you so much for your replies. I really appreciate it. Looks like I've managed to come up with a solution. Here's a link to GitHub: https://github.com/Ashmor00/JavaBeansSerialization2

I'd be really grateful for your feedback guys.
 
Dave Tolls
Rancher
Posts: 4275
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't XMLEncoder just another form of serialisation?
 
Rancher
Posts: 108
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:Isn't XMLEncoder just another form of serialisation?



That's what I thought as well. I assumed this question was about using reflection and writing the functionality from scratch.
 
Bartender
Posts: 2372
102
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Brad Wood wrote:...I was applying for a job and they gave me this assignment. Guess depending on how well I'll tackle this problem, they'll be deciding whether to give me a job or not.

Isn't this cheating ? They have given you an assignment to judge how you are going to solve a problem and you are going to use Tim's idea to solve it.
 
Dave Tolls
Rancher
Posts: 4275
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My solution would be "why are you asking me to write this by hand when there are pre-existing solutions out there, including a number that are part of the JDK".
 
Campbell Ritchie
Marshal
Posts: 65821
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer to that question is of course,

Because you are applying for our job.

 
Dave Tolls
Rancher
Posts: 4275
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To which the reply is "if my job involves working for people who think that reusing existing libraries is anathema, bye bye".


But then I do have that luxury I suppose.
 
Campbell Ritchie
Marshal
Posts: 65821
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Careful, or I shall have to move you to MD
 
salvin francis
Bartender
Posts: 2372
102
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:My solution would be "why are you asking me to write this by hand when there are pre-existing solutions out there, including a number that are part of the JDK".


Sometimes, the interviewers are interested to know how a candidate would approach a problem to solve it. e.g. What if there was no Maps in java, how would you create one ?
A candidate is well aware of the complete requirements. He/she has probably used Maps, and knows how it behaves. Now he/she has to come up with a solution to implement all those behaviors. Typically these "What if" questions are verbal ones and not assignments to be completed.

On the other hand, specific to this question, maybe a company is aware about the security issues with serializing and de-serializing an object and is asking the candidate to come up with ideas of his own ?
 
Dave Tolls
Rancher
Posts: 4275
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then provide a proper problem, not one whose solution is to use the existing frameworks.

I will say I can't think of an instance where I have actually been asked such a question.  It's always been a quick coding test with no daft restrictions (even way back when) and/or the form of "how would you go about solving this issue".  The issue never having been some pre-existing thing like a Map.

Strikes me as, frankly, incredibly lazy on the part of the interviewer to ask such a question.  Also, not too helpful for them.
I'm never going to write a Map.
 
salvin francis
Bartender
Posts: 2372
102
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hypothetical questions are quite common now-a-days. I was once asked in a programming interview : "What if we want to move Mount Everest to a different location. What would be your strategy for this?" The discussion that followed involved stuff like the assumption that the mountain was a roughly the shape of a cone. Volume of a cone requires the radius and height. The number of trucks required to carry this payload, etc...

It all depends on what a job's requirements are. If they want an innovative programmer, you can expect something in these lines. No question is too silly or lazy.
 
Tim Holloway
Saloon Keeper
Posts: 21137
134
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:Hypothetical questions are quite common now-a-days. I was once asked in a programming interview : "What if we want to move Mount Everest to a different location. What would be your strategy for this?" The discussion that followed involved stuff like the assumption that the mountain was a roughly the shape of a cone. Volume of a cone requires the radius and height. The number of trucks required to carry this payload, etc...

It all depends on what a job's requirements are. If they want an innovative programmer, you can expect something in these lines. No question is too silly or lazy.



It beats "If you were a tree..."
 
salvin francis
Bartender
Posts: 2372
102
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:"If you were a tree..."


I am Groot
 
Tim Holloway
Saloon Keeper
Posts: 21137
134
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would be a manchineel tree. Planted in a big tub in the middle of HR and gently misted so that light droplets would land on all who go by.
 
Sheriff
Posts: 24654
58
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:I was once asked in a programming interview : "What if we want to move Mount Everest to a different location. What would be your strategy for this?" The discussion that followed involved stuff like the assumption that the mountain was a roughly the shape of a cone. Volume of a cone requires the radius and height. The number of trucks required to carry this payload, etc...



And nothing about gaining the approval of people who like Mount Everest just where it is, including those whose livelihoods depend on it. Sigh.
 
Campbell Ritchie
Marshal
Posts: 65821
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:. . . move Mount Everest to a different location. What would be your strategy for this? . . .

It is usually easier to redefine lat and long; you can move it thousands of miles in a few seconds.
 
Brad Wood
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys. It's me again. So they told me today that I shouldn't have used XMLEncoder/XMLDecoder. It wasn't allowed. Dave Tolls was right. But they gave me some time to come up with another solution. Looks like I have to use reflection for this task. The problem is that I'm fairly new to Java and don't have experience with dealing with reflection whatsoever. So could you guys give me a piece of advice on this? How can this be done? Should I use the ClassLoader? I'd really be grateful for you help. guys.
 
Tim Holloway
Saloon Keeper
Posts: 21137
134
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The basic reflection methods are in java.lang.Class. If they're feeling generous, the apache BeanUtils make the process more civilized. BeanUtils is used, for example, in many Enterprise Java systems as the engine that handles Expression Language (EL) expressions in things like JSPs and JavaServer Faces View template rendering.
 
Paul Clapham
Sheriff
Posts: 24654
58
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

salvin francis wrote:. . . move Mount Everest to a different location. What would be your strategy for this? . . .

It is usually easier to redefine lat and long; you can move it thousands of miles in a few seconds.



Exactly. A little bit of GPS-spoofing and you're done. No trucks required. No environmental impact surveys either.
 
Tim Moores
Saloon Keeper
Posts: 5811
146
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While it's good to know what reflaction can do, and being able to use it in principle, that it's used here as an aptitude test is a red flag, IMO. There are many libraries that can be used in such circumstances (NeanUtils, which Tim mentioned, and possibly JXPath or Digester etc., depending on what the use case is) that coding it yourself seems a waste of time. A good developer should know that :-)
 
Campbell Ritchie
Marshal
Posts: 65821
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . . No environmental impact surveys either.

You can have as many impact surveys as you like, secure in the knowledge that they will go to the wrong place
 
Tim Holloway
Saloon Keeper
Posts: 21137
134
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Paul Clapham wrote:. . . . No environmental impact surveys either.

You can have as many impact surveys as you like, secure in the knowledge that they will go to the wrong place



Die Hard, anyone?  
 
salvin francis
Bartender
Posts: 2372
102
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Brad Wood wrote:...Looks like I have to use reflection for this task. The problem is that I'm fairly new to Java and don't have experience with dealing with reflection whatsoever.


This raises another red flag to me.
 
I love a good mentalist. And so does this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!