Thursday, June 30, 2011

Something to crow about: Gson Json code generator

Gee Whiz--this is a real time save. Gson is a google project to serialize and deserialize json data. Good enough. The problem is that to do this you really want to drop the deserialized output into a java object for further processing.

There are some generalized object containers out there, but these didn't really do what I was looking for. Guess what I wanted was something lite and easy--no go.

So, here's the whizbang part. This site takes your json, such as:



{
    "dvr_capacity": "1",
    "camera_capacity": "null",
    "camera": [
        {
            "name": "MyCamera1",
            "ip": "asdfasd",
            "port": "80",
            "model": "IQEYE",
            "username": "",
            "password": ""
        },
        {
            "name": "MyCamera2",
            "ip": "adadfasdfa",
            "port": "80",
            "model": "IQEYE",
            "username": "",
            "password": ""
        },
        {
            "name": "MyCamera3",
            "ip": "adfasdfad",
            "port": "80",
            "model": "MOBOTIX",
            "username": "",
            "password": ""
        },
        {
            "name": "MyCamera4",
            "ip": "asdfadfasdfa",
            "port": "80",
            "model": "IQEYE",
            "username": "",
            "password": ""
        },
        {
            "name": "MyCamera5",
            "ip": "asdfasdfad",
            "port": "80",
            "model": "AXIS",
            "username": "",
            "password": ""
        },
        {
            "name": "MyCamera6",
            "ip": "adasdzcxzcsdf",
            "port": "80",
            "model": "MOBOTIX",
            "username": "",
            "password": ""
        },
        {
            "name": "MyCamera7",
            "ip": "adfasdfasdfa",
            "port": "80",
            "model": "MOBOTIX",
            "username": "",
            "password": ""
        },
        {
            "name": "MyCamera8",
            "ip": "qweqreqwerqwel",
            "port": "80",
            "model": "MOBOTIX",
            "username": "",
            "password": ""
        },
        {
            "name": "MyCamera9",
            "ip": "asdfasdfasdf",
            "port": "80",
            "model": "IQEYE",
            "username": "",
            "password": ""
        }
    ]
}

and automatically converts this into the respective Gson compatible java object. COOL.

And saves me a load of typing and typo checking. Now that's a thumbs up!


public class Camera{
private String ip;
private String model;
private String name;
private String password;
private String port;
private String username;

public String getIp(){
return this.ip;
}

public void setIp(String ip){
this.ip = ip;
}

public String getModel(){
return this.model;
}

public void setModel(String model){
this.model = model;
}

public String getName(){
return this.name;
}

public void setName(String name){
this.name = name;
}

public String getPassword(){
return this.password;
}

public void setPassword(String password){
this.password = password;
}

public String getPort(){
return this.port;
}

public void setPort(String port){
this.port = port;
}

public String getUsername(){
return this.username;
}

public void setUsername(String username){
this.username = username;
}
}

1 comment:

  1. Full Example:

    https://github.com/SpotterRF/json-examples/tree/master/java/jackson

    ReplyDelete