Wednesday, September 29, 2010

~~Java Object to Json and Vice Versa Using gson~~

A recent piece of work I completed took about three days of dreading. I wasn't sure how reflection in java could help me out.

Thanks to the connected world we live. All I had to get is a package called "gson" put out by google code community.

As I said, it was about three days of dreading and when the time came to finish it, I sat down and coded for exactly few minutes.

I had to send a json string built out of a list of custom object and to do it I wrote code similar to below:

//you can fill up the list with your custom objects
List<Object> customObjectList = new ArrayList<Object>();
//used gson-1.4.jar(freely available, no license needed)
Gson gson = new Gson();
//just one line as below, feed the list of your object and that's it
String internalOrderJSON = gson.toJson(objectList);

//it is just a simple string with json structure, still a string
//so it can be sent just like string to a server as in below
response.setContentType("text/plain");
response.getWriter().write(internalOrderJSON);

Ok..so the above was the easy part. Very plain and simple. But you know the world we live in, we give crap to somebody and lets say they put up with it, well the bad news is they will crap you back :)

Similarly, now whosoever I handed the json to, sent it back to me on the server side to convert it back to objects, to my persistant object and then persist it!!!

Oh well this is the part that I had dreaded originally.

But life was glorious again when I searched to find if java reflection was supported by the gson stack and Yes!! it was.

Ok, so implementation wise here is how you can do it.

//lets say the origin of the json string that you have is a http call
String jsonStringReceived = request.getParameter("someName");

//again get a reference to the Gson API.
Gson gson = new Gson();
//this one line is the magic, you tell Gson what type using reflection at run time
Type collectionType = new TypeToken<List<Object>>() {}.getType();
//and the following is not almost same as before
List<Object> yourCustomObjectList = gson.fromJson(jsonStringReceived, collectionType);
//your code to use the list of your object can go here


So, to summarize: all you need is a jar called gson-1.4.jar (at the time of this writing, gson-1.4.jar did the trick for me)

In addition to importing java's "reflection" package in your java class, you will need to import some other specific gson packages, but the following should be all you need if you plan to use the above technique:

import java.lang.reflect.Type;
import com.google.gson.reflect.TypeToken;
import com.google.gson.Gson;
get a copy of gson-1.4.jar for the above imports.

Thanks for stopping by and Happy Coding!
~Nirmal


Build a better mousetrap, and the world will beat a path to your door...
~Emerson

Friday, April 23, 2010

grep: global regular expression print

Some of the most useful unix tools and commands for daily use in a day's life of a system admin...(more to be added, keep checking)

printenv --prints all system and user created env. vairables.

cd ~directory_name -takes you directly to that directory

id --display system id number

tail --display last 10 lines. if tail -20 then display last 20 lines

grep string fileName --display lines with the "string" in the fale called "fileName"

cd ~ --go to home directory

diff {file1} {file2} -- show the differences in file1 and file2

sdiff {file1} {file2} --show files side by side

wc {file} --cound words in a file

df --system disk capacity

du --system disk usage

\\10.5.109.104\c$ to remote access unshared

ps ux --display processes owned by the current user.

kill -9 process_id --will kill the process, -9 here ensures execution.

du -sc * | sort -n | tail -- list size of all the names and sizes of all files and directories, sort in increasing size order, print the last few lines

uname -aspi --enlist the osx kernel etc

grep -r -i somethingtosearchfor ./ --recursively search all files and directories for the string.

find . -exec grep "boo" {} \; --search for string "boo" in every directory below the current directory

tail -n8 fileName | grep "boo" --performs a grep on the last 8 lines of file called "a_file"

find | grep "hello" --will print out the file that find returns that contain the text "hello"

find . -exec grep "boo" {} \; --search for the string "boo" in every directory below the current directory.

grep -c "boo" fileName --will print an integer indicating the number of lines that matched the string "boo"

grep -l "boo" * --searching through multiple files for the same string

grep -x "boo" a_file --searching for eXact matches only.

grep "e$" a_file --search in all lines containg words having the letter "e" as the last letter