.
INTELLIJ PROJECT - Working with JSON and Text File
https://www.jetbrains.com/help/idea/developing-node-js-applications.html#ws_node_quick_start
Create A New Project |
Click New Project |
Name = prjMvnJsonDataFile Archetype = org.apache.maven.archetypes:maven-archetype-quickstart Click Create. |
Note: Maven Quickstart Archetype maven-archetype-quickstart is an archetype which generates a sample Maven project: https://maven.apache.org/archetypes/maven-archetype-quickstart/ |
Project creation done. |
Add Dependency |
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.6</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.6</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.6</version> </dependency> |
Edit Class |
package org.example; // Java Program to Illustrate Setting Up of Jackson by // parsing Jackson library json files and // generating the same // Importing required classes import java.io.*; import com.fasterxml.jackson.core.JsonGenerationException; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.*; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; // Main class class App { // Main driver method public static void main(String[] args) { // Creating an employee object with it's attributes // set Employee employee = getEmployee(); ObjectMapper mapper = new ObjectMapper(); // Try block to check for exceptions try { // Serializes emp object to a file employee.json String filePath="c:/ZTIJ//employee.json"; mapper.writeValue( new File( filePath), employee); // Deserializes emp object in json string format String empJson = mapper.writeValueAsString(employee); System.out.println( "The employee object in json format:" + empJson); System.out.println( "Updating the dept of emp object"); // Update deptName attribute of emp object employee.setDeptName("Devops"); System.out.println( "Deserializing updated emp json "); // Reading from updated json and deserializes it // to emp object Employee updatedEmp = mapper.readValue( mapper.writeValueAsString(employee), Employee.class); // Print and display the updated employee object System.out.println("Updated emp object is " + updatedEmp.toString()); } // Catch block to handle exceptions // Catch block 1 // Handling JsonGenerationException catch (JsonGenerationException e) { // Display the exception along with line number // using printStackTrace() method e.printStackTrace(); } // Catch block 2 // Handling JsonmappingException catch (JsonMappingException e) { // Display the exception along with line number // using printStackTrace() method e.printStackTrace(); } // Catch block 3 // handling generic I/O exceptions catch (IOException e) { // Display the exception along with line number // using printStackTrace() method e.printStackTrace(); } } // Method 2 // To get the employees private static Employee getEmployee() { // Creating an object of Employee class Employee emp = new Employee(); emp.setId("E010890"); emp.setName("James"); emp.setDeptName("DBMS"); emp.setRating(5); emp.setSalary(1000000.00); // Returning the employee return emp; } } // Class 2 // Helper class class Employee { // Member variables of this class private String id; private String name; private String deptName; private double salary; private int rating; // Member methods of this class public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDeptName() { return deptName; } public void setDeptName(String deptName) { // This keyword refers to current instance this.deptName = deptName; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public int getRating() { return rating; } public void setRating(int rating) { this.rating = rating; } @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", deptName=" + deptName + ", salary=" + salary + ", rating=" + rating + "]"; } } |
source: https://www.geeksforgeeks.org/how-to-setup-jackson-in-java-application/ |
Create an artifact configuration for the JAR |
Build the JAR artifact |
.
No comments:
Post a Comment