Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- */
- package com.shreesoft.SO27466772;
- import com.google.gson.Gson;
- /**
- * @author Srihari.Sahu
- *
- */
- public class SO27466772 {
- /**
- * @param args
- */
- public static void main(String[] args) {
- Server s = new Server();
- Gson gson = new Gson();
- String response = s.spitOutput();
- System.out.println("Client Received unparsed :: "+response);
- final Person p= gson.fromJson(response, Person.class);
- System.out.println("Client Received parsed :: "+p.toString());
- }
- }
- /**
- *
- */
- package com.shreesoft.SO27466772;
- /**
- * @author Srihari.Sahu
- *
- */
- public class Person {
- private String name;
- private int age;
- /**
- * @return the name
- */
- public String getName() {
- return name;
- }
- /**
- * @param name the name to set
- */
- public void setName(String name) {
- this.name = name;
- }
- /**
- * @return the age
- */
- public int getAge() {
- return age;
- }
- /**
- * @param age the age to set
- */
- public void setAge(int age) {
- this.age = age;
- }
- /* (non-Javadoc)
- * @see java.lang.Object#hashCode()
- */
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + age;
- result = prime * result + ((name == null) ? 0 : name.hashCode());
- return result;
- }
- /* (non-Javadoc)
- * @see java.lang.Object#equals(java.lang.Object)
- */
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- Person other = (Person) obj;
- if (age != other.age)
- return false;
- if (name == null) {
- if (other.name != null)
- return false;
- } else if (!name.equals(other.name))
- return false;
- return true;
- }
- /* (non-Javadoc)
- * @see java.lang.Object#toString()
- */
- @Override
- public String toString() {
- return "Person [name=" + name + ", age=" + age + "]";
- }
- }
- /**
- *
- */
- package com.shreesoft.SO27466772;
- import com.google.gson.Gson;
- /**
- * @author Srihari.Sahu
- *
- */
- public class Server {
- private Person p = null;
- Gson gson = new Gson();
- public Server(){
- p = new Person();
- p.setName("Renold");
- p.setAge(26);
- }
- public String spitOutput(){
- String retValue = gson.toJson(p);
- System.out.println("Server Sending JSON :: "+retValue);
- return retValue;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement