save object in db as-is or as json? - java

I have an object that its content I want to save in mysql. What I'm doing now is convert it to JSON and write it as text. If I need to read it I decode the json and create the same object. But I saw that I can save the object to mysql db as Object and read it as object, then cast it to my class. I do not need to have the ability to search in the json.
lets say this is my object:
class bus extends car{
private int seats;
private Person driver;
public bus(Person driver){
super();
this.seats = 50;
this.driver = driver;
}
}
So what is the preferred way(time, coding, memory usage etc..)?
And what is the correct way to save this kind of object in mysql db?

Rather than saving it as JSON you could use xstream and serialize your class to xml, which can then be saved as 'clob' to the database. Once you deserialize using xstream it will give back the object so you don't have to do manual conversion. xstream also supports serializing to json as well.
http://x-stream.github.io/
http://x-stream.github.io/json-tutorial.html

If you do not need options provided by this format than consider to use standard way - binary format based on serialization
There are benefits
Very less space consuming
No need third party libs
Out of box solution, because of java
Consider to use special ORM tools for this reason. It will dramatically simplify your task and save your time.
There are ORM tools
1. Hibernate
2. MyBatis
3. Eclipse link
4. Spring jdbc
Using one of them let you concentrate on a business points rather than on a low level concepts

Related

Best way to convert Cassandra ResultSet to java objects in OSS driver

We are in the process of migrating from Cassandra DSE to OSS driver and the pain point is converting the result set into a list of Java objects. I don't want to use Mapper annotation, like detailed here
So what is the most efficient way to convert ResultSet into list of java objects? Any suggestion highly appreciated
If you don't want to use Mapper then just need to write converting function that will take Row, extract all necessary columns, and return POJO constructed from teh extracted data... I don't see other ways, except writing an adhoc version of object mapper that would use the reflection to match POJO's field names with the column names

Java toObject() for Strings that are printed from toString()

The component I'm working on is changing its datastore from mongo -> mysql. In mongo, you can fit a pojo (with other pojo fields) into a document, not in mysql.
For a particular Object, I'd like to be able to toString() it and store that in a mysql TEXT column.
When I retrieve it, I'd like to be able to MyClass.staticToObject(String stringifiedObj) to get a MyClass instance. Is this possible?
I know I can write a custom toString() and parser but it seems a little tedious/prone to errors.
Is there a faster way?
Use any JSON library Guava, Jackson f.i. Serialize it to JSON -> store to DB -> retrieve -> deserialize. I think it's pretty common use case for NOSQL DBs. And for you needs you could add 2 static methods to pojo toJson/fromJson.

Java deserialization of old Object

I'm having a problem when serializing and deserializing my objects in my project. I'm writing the object to a name.dat file.
However whenever i make a change in the Name class i can nolonger deserialize it, since it's two different objects.
Is there any way around this?
Your best options are:
Don't change your classes :-)
Throw away any serialized objects each time you change your classes.
Don't use Java object serialization.
Given that 1) and 2) are probably out of the question, option 3) should be given serious consideration. There a variety of alternatives to Java serialization, depending on the nature of the data you are persisting. These include:
Using Java properties files
Storing the data in a classical database (using SQL and the JDBC API)
Using an object-relational database mapping such as Hibernate
Using XML or JSON and a "binding" technology so that you can serialize / deserialize POJOs.
Finally, it is possible to implement class versioning using Java object serialization. However, it is tricky. And if you are continually changing the classes, then it is not going to be pleasant. Start by reading Versioning of Serializable Objects.

How do I retrieve blob (containing serialized java object) from DB and convert them to xml?

I have some Java objects stored in an Oracle database. I wish to know the structure and content of the objects. So, I want to retrieve the blob and convert them to xml/any other displayable form.
Is this possible? If yes, how?
I would extract the binary object from the database, create back the java object in memory (so you can also ensure the data is valid) and after that I would use a library like Protostuff to quickly serialize the object in XML.
The advantage of using Protostuff is that you don't need anything but the java object. The object "schema" is calculated at runtime if needed.
Consider also that Protostuff supports a lot of different formats, like JSON, Protobuffer, YAML, etc...

Create Object vs Reading Json

I'm working on an Android app. The app gets the data as JSON string (name of universities and student lists) and manipulate the app according to the data.
What will be a better approach?
Create a new Object and parse the JSON string into it, and work with the object, or
Keep the JSON string, and just use JSONObject whenever I need to grab information from the string
Or any other way?
If I'm using the new Object, how can I transfer (or share) the object with other activities in the app?
I know that for string we can use putextra().
Use objects.
I would suggest to use Jackson library,
be cause it is very fast and easy to ingrate.
You can find code examples here :
http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/
P.S. : Jackson is not the only library for this approach > Jackson Vs. Gson
I almost always parse the JsonObject into a specific object E.g. class University.
One benefit of doing this, is you can put it nicely into collections. E.g. HashMaps, Set or just straight List. When dealing with it as a JsonObject you won't be able to as easily work with it.
You can do things like sort it if you Object inherits Comparable, and can define equals/toString/hashCode.
There are a number of other benefits, but I think you'll find holding that data in a real data structure will give you the most benefit
I would recommend parsing the string (or using a library to do this for you!) and filling an object. This way, your model object can control the data and how it is shared with other objects, and use internal data structures to optimize usage. If you stuck with the JSON string you'd end up parsing it multiple times to pull out data.
However you decide to share the object/string across activities shouldn't affect your decision for how to model the data. You'll likely end up passing it across activities in any case.
I suggest that you use objects too.
You can use Gson library to do any conversion between json string and objects. It is very, very easy to use. http://code.google.com/p/google-gson/
To transfer the data between other activities you can make your object implement the Serializable interface, this way you can use the .putExtra() and pass it forward.

Categories