Advertisement
hunkyhari

SO27466772

Dec 14th, 2014
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. /**
  2. *
  3. */
  4. package com.shreesoft.SO27466772;
  5.  
  6. import com.google.gson.Gson;
  7.  
  8. /**
  9. * @author Srihari.Sahu
  10. *
  11. */
  12. public class SO27466772 {
  13.  
  14. /**
  15. * @param args
  16. */
  17. public static void main(String[] args) {
  18. Server s = new Server();
  19. Gson gson = new Gson();
  20. String response = s.spitOutput();
  21. System.out.println("Client Received unparsed :: "+response);
  22. final Person p= gson.fromJson(response, Person.class);
  23. System.out.println("Client Received parsed :: "+p.toString());
  24. }
  25.  
  26. }
  27.  
  28. /**
  29. *
  30. */
  31. package com.shreesoft.SO27466772;
  32.  
  33. /**
  34. * @author Srihari.Sahu
  35. *
  36. */
  37. public class Person {
  38. private String name;
  39. private int age;
  40. /**
  41. * @return the name
  42. */
  43. public String getName() {
  44. return name;
  45. }
  46. /**
  47. * @param name the name to set
  48. */
  49. public void setName(String name) {
  50. this.name = name;
  51. }
  52. /**
  53. * @return the age
  54. */
  55. public int getAge() {
  56. return age;
  57. }
  58. /**
  59. * @param age the age to set
  60. */
  61. public void setAge(int age) {
  62. this.age = age;
  63. }
  64. /* (non-Javadoc)
  65. * @see java.lang.Object#hashCode()
  66. */
  67. @Override
  68. public int hashCode() {
  69. final int prime = 31;
  70. int result = 1;
  71. result = prime * result + age;
  72. result = prime * result + ((name == null) ? 0 : name.hashCode());
  73. return result;
  74. }
  75. /* (non-Javadoc)
  76. * @see java.lang.Object#equals(java.lang.Object)
  77. */
  78. @Override
  79. public boolean equals(Object obj) {
  80. if (this == obj)
  81. return true;
  82. if (obj == null)
  83. return false;
  84. if (getClass() != obj.getClass())
  85. return false;
  86. Person other = (Person) obj;
  87. if (age != other.age)
  88. return false;
  89. if (name == null) {
  90. if (other.name != null)
  91. return false;
  92. } else if (!name.equals(other.name))
  93. return false;
  94. return true;
  95. }
  96. /* (non-Javadoc)
  97. * @see java.lang.Object#toString()
  98. */
  99. @Override
  100. public String toString() {
  101. return "Person [name=" + name + ", age=" + age + "]";
  102. }
  103. }
  104.  
  105. /**
  106. *
  107. */
  108. package com.shreesoft.SO27466772;
  109.  
  110. import com.google.gson.Gson;
  111.  
  112. /**
  113. * @author Srihari.Sahu
  114. *
  115. */
  116. public class Server {
  117. private Person p = null;
  118. Gson gson = new Gson();
  119. public Server(){
  120. p = new Person();
  121. p.setName("Renold");
  122. p.setAge(26);
  123. }
  124. public String spitOutput(){
  125. String retValue = gson.toJson(p);
  126. System.out.println("Server Sending JSON :: "+retValue);
  127. return retValue;
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement