0% found this document useful (0 votes)
42 views7 pages

Webservices4PM 150320211

Uploaded by

Suresh Kamble
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views7 pages

Webservices4PM 150320211

Uploaded by

Suresh Kamble
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

Date : 15/03/2021

Webservices# 4:30PM
Mr. RAGHU
------------------------------------
email: javabyraghu@gmail.com
fb: https://www.facebook.com/groups/thejavatemple
JSON - JACKSON

*) Nested JSON | Inner JSON


(A--<>B)
{

key: {

}
}
------------------[jackson-databind Annotations]-------------------
a. JSON Ignore: [ @JsonIgnore ]
To avoid any variable(property) using in JSON-JACKSON programming
we should ignore such variable.

Sensitive information like CVV, Password, Secure Code, otp..etc


Should never be shared with any other App.

b. JSON Property [ @JsonProperty ]


JSON keys are taken from variable names.
So, provide alias names for variables JSON keys only

Ex: (before aliasing )


{"uid":10,"uname":"SAMPLE","upwd":"ABC123","urole":"ADMIN"}

Ex: ( After aliasing )


{"user-id":10,"user-name":"SAMPLE","user-authority":"ADMIN"}

============code====================
1. Model
package in.nit.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

public class User {


@JsonProperty("user-id")
private int uid;
@JsonProperty("user-name")
private String uname;
@JsonIgnore
private String upwd;
@JsonProperty("user-authority")
private String urole;

public User() {
super();
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
public String getUrole() {
return urole;
}
public void setUrole(String urole) {
this.urole = urole;
}

2. Test class
package in.nit.test;

import com.fasterxml.jackson.databind.ObjectMapper;

import in.nit.model.User;

public class Test {

public static void main(String[] args) {


try {
User usr = new User();
usr.setUid(10);
usr.setUname("SAMPLE");
usr.setUpwd("ABC123");
usr.setUrole("ADMIN");

ObjectMapper om = new ObjectMapper();


String json = om.writeValueAsString(usr);
System.out.println(json);

} catch (Exception e) {
e.printStackTrace();
}
}
}

3. pom.xml
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.2</version>
</dependency>
</dependencies>
-----------------------------------------------------------------
*) JSON to File:-
ObjectMapper(C) has provided method name:
writeValue(file,object):void

1st param is filename + location (java.io.File)


2nd param is object which need to be converted to JSON

*) Test class:
package in.nit.test;

import java.io.File;

import com.fasterxml.jackson.databind.ObjectMapper;

import in.nit.model.User;

public class Test {

public static void main(String[] args) {


try {
User usr = new User();
usr.setUid(999);
usr.setUname("SAM");
usr.setUpwd("NEWXYZ");
usr.setUrole("EMPLOYEE");

ObjectMapper om = new ObjectMapper();


//String json = om.writeValueAsString(usr);
//System.out.println(json);
om.writeValue(new File("F:/mylogs/users.json"), usr);
System.out.println("DONE");
} catch (Exception e) {
e.printStackTrace();
}
}
}
=================================================================
JSON to Object

read___() methods are used to convert JSON String to Object Format

readValue(String json, Class<T> clz):T Object

=> Here we need to pass JSON as String and class which you need
its object that conatins JSON data.

--Reflection --
Class<Employee> cls = Employee.class;
Class<Employee> cls = Class.forName("in.nit.model.Employee");
---------------

*) Note: {} is also a valid JSON, it indicates creating object


using default const, but no data is set.
======code==========
1. Model
package in.nit.model;

public class Employee {

private int eid;


private String ename;
private double esal;

public Employee() {
super();
}
public int getEid() {
return eid;
}
public void setEid(int eid) {
this.eid = eid;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public double getEsal() {
return esal;
}
public void setEsal(double esal) {
this.esal = esal;
}
@Override
public String toString() {
return "Employee [eid=" + eid + ", ename=" + ename + ", esal=" + esal +
"]";
}

2. Test class
package in.nit.test;

import com.fasterxml.jackson.databind.ObjectMapper;

import in.nit.model.Employee;

public class Test {

public static void main(String[] args) {


try {
//String json="{\"eid\":10,\"ename\":\"A\",\"esal\":200.0}";
String json="{}"; //valid JSON

ObjectMapper om = new ObjectMapper();


Employee emp = om.readValue(json, Employee.class);

System.out.println(emp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
=======Ex#2 Code============================
1. Models
package in.nit.model;

public class Address {

private String hno;


private String loc;
public Address() {
super();
}
public String getHno() {
return hno;
}
public void setHno(String hno) {
this.hno = hno;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
@Override
public String toString() {
return "Address [hno=" + hno + ", loc=" + loc + "]";
}

}
--
package in.nit.model;

//ctrl+shift+O (imports)
import java.util.List;
import java.util.Map;

public class Employee {

private int eid;


private String ename;
private double esal;
private List<String> prjs; //set,array
private Map<String,Double> vers;
private Address addr;//HAS-A

public Employee() {
super();
}
public int getEid() {
return eid;
}
public void setEid(int eid) {
this.eid = eid;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public double getEsal() {
return esal;
}
public void setEsal(double esal) {
this.esal = esal;
}
public List<String> getPrjs() {
return prjs;
}
public void setPrjs(List<String> prjs) {
this.prjs = prjs;
}
public Map<String, Double> getVers() {
return vers;
}
public void setVers(Map<String, Double> vers) {
this.vers = vers;
}
public Address getAddr() {
return addr;
}
public void setAddr(Address addr) {
this.addr = addr;
}
@Override
public String toString() {
return "Employee [eid=" + eid + ", ename=" + ename + ", esal=" + esal +
", prjs=" + prjs + ", vers=" + vers
+ ", addr=" + addr + "]";
}

2. Test class
package in.nit.test;

import java.io.File;

import com.fasterxml.jackson.databind.ObjectMapper;

import in.nit.model.Employee;

public class Test {

public static void main(String[] args) {


try {
//String json="{\"eid\":10,\"ename\":\"A\",\"esal\":200.0}";
//String json="{}"; //valid JSON

ObjectMapper om = new ObjectMapper();


//Employee emp = om.readValue(json, Employee.class);
Employee emp = om.readValue(new File("F:/mylogs/emps.json"),
Employee.class);
System.out.println(emp);
} catch (Exception e) {
e.printStackTrace();
}
}
}

3. JSON file : emps.json


{"eid":50203,"ename":"SYED","esal":20.30, "prjs":["P1","P2","P3"],"vers":
{"M1":3.3,"M2":4.4},"addr":{"hno":"5-A/1","loc":"HYD"}}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy