0% found this document useful (0 votes)
16 views

Object Mapper

Uploaded by

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

Object Mapper

Uploaded by

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

When working with API automation testing in Java using Rest Assured, the Jackson library's

ObjectMapper is a powerful tool for serializing Java objects into JSON and deserializing JSON into Java
objects. Below are explanations and examples illustrating different use cases of ObjectMapper in API
testing scenarios.
1. Basic Serialization
Serialize a simple Java object to a JSON string.
import com.fasterxml.jackson.databind.ObjectMapper;

public class SerializationExample {


public static void main(String[] args) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();

User user = new User("Smridhi", 25);


String jsonString = objectMapper.writeValueAsString(user);

System.out.println(jsonString);
}
}

class User {
private String name;
private int age;

public User(String name, int age) {


this.name = name;
this.age = age;
}

// Getters and setters


}
2. Basic Deserialization
Deserialize a JSON string into a Java object.
import com.fasterxml.jackson.databind.ObjectMapper;

public class DeserializationExample {


public static void main(String[] args) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();

String jsonString = "{\"name\":\"Smridhi\",\"age\":25}";


User user = objectMapper.readValue(jsonString, User.class);

System.out.println("Name: " + user.getName() + ", Age: " + user.getAge());


}
}

class User {
private String name;
private int age;

// Getters and setters


}
3. Serialization with Nested Objects
Serialize an object that contains nested objects.
import com.fasterxml.jackson.databind.ObjectMapper;

public class NestedSerializationExample {


public static void main(String[] args) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();

Address address = new Address("123 Main St", "New York", "NY");


User user = new User("Smridhi", 25, address);
String jsonString = objectMapper.writeValueAsString(user);
System.out.println(jsonString);
}
}

class User {
private String name;
private int age;
private Address address;

public User(String name, int age, Address address) {


this.name = name;
this.age = age;
this.address = address;
}

// Getters and setters


}

class Address {
private String street;
private String city;
private String state;

public Address(String street, String city, String state) {


this.street = street;
this.city = city;
this.state = state;
}
// Getters and setters
}
4. Deserialization with Nested Objects
Deserialize a JSON string with nested objects.
import com.fasterxml.jackson.databind.ObjectMapper;

public class NestedDeserializationExample {


public static void main(String[] args) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();

String jsonString = "{\"name\":\"Smridhi\",\"age\":25,\"address\":{\"street\":\"123 Main


St\",\"city\":\"New York\",\"state\":\"NY\"}}";
User user = objectMapper.readValue(jsonString, User.class);

System.out.println("Name: " + user.getName() + ", City: " + user.getAddress().getCity());


}
}

5. Handling Collections
Serialize and deserialize a collection of objects.
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Arrays;
import java.util.List;

public class CollectionExample {


public static void main(String[] args) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
List<User> users = Arrays.asList(
new User("Smridhi", 25),
new User("John", 30)
);

// Serialize
String jsonString = objectMapper.writeValueAsString(users);
System.out.println(jsonString);

// Deserialize
List<User> userList = objectMapper.readValue(jsonString, new TypeReference<List<User>>(){});
userList.forEach(user -> System.out.println("Name: " + user.getName()));
}
}

6. Custom JSON Property Names


Use annotations to map JSON property names to Java fields.
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;

public class CustomPropertyExample {


public static void main(String[] args) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();

String jsonString = "{\"user_name\":\"Smridhi\",\"user_age\":25}";


User user = objectMapper.readValue(jsonString, User.class);

System.out.println("Name: " + user.getName() + ", Age: " + user.getAge());


}
}

class User {
@JsonProperty("user_name")
private String name;

@JsonProperty("user_age")
private int age;

// Getters and setters


}
7. Ignoring Unknown Properties
Configure ObjectMapper to ignore unknown properties in JSON.
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class IgnoreUnknownPropertiesExample {


public static void main(String[] args) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

String jsonString = "{\"name\":\"Smridhi\",\"age\":25,\"extraField\":\"value\"}";


User user = objectMapper.readValue(jsonString, User.class);

System.out.println("Name: " + user.getName() + ", Age: " + user.getAge());


}
}

8. Pretty Printing JSON


Serialize a Java object to a pretty-printed JSON string.
import com.fasterxml.jackson.databind.ObjectMapper;

public class PrettyPrintExample {


public static void main(String[] args) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();

User user = new User("Smridhi", 25);


String jsonString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(user);

System.out.println(jsonString);
}
}

9. Reading JSON from a File


Read and deserialize JSON data from a file.
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;

public class FileReadExample {


public static void main(String[] args) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();

User user = objectMapper.readValue(new File("user.json"), User.class);

System.out.println("Name: " + user.getName() + ", Age: " + user.getAge());


}
}

10. Writing JSON to a File


Serialize a Java object and write the JSON data to a file.
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;

public class FileWriteExample {


public static void main(String[] args) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();

User user = new User("Smridhi", 25);


objectMapper.writeValue(new File("user.json"), user);

System.out.println("JSON written to file.");


}
}

You might also like