Java for beginners
406 subscribers
49 photos
29 links
Learn Java daily with bite-sized lessons, practical code examples, output previews, and fun quizzes.
Download Telegram
Here's your Telegram post for Day 17!

---

🎉 Day 17 is here, and we're tackling a crucial skill for modern Java development: **JSON Data Handling!** 🚀

JSON (JavaScript Object Notation) is everywhere – from web APIs to configuration files. Today, you'll learn how to seamlessly convert Java objects into JSON strings (serialization) and vice-versa (deserialization) using powerful libraries like **Jackson** or **Gson**. It's easier than you think and opens up a world of possibilities for your applications!

Let's dive into an example using Jackson. 🛠️

```java
// Product.java
// This is our simple Java POJO (Plain Old Java Object)
public class Product {
private int id;
private String name;
private double price;
private boolean available;

// Default constructor for deserialization
public Product() {}

public Product(int id, String name, double price, boolean available) {
this.id = id;
this.name = name;
this.price = price;
this.available = available;
}

// Getters and Setters (required by Jackson for serialization/deserialization)
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public double getPrice() { return price; }
public void setPrice(double price) { this.price = price; }
public boolean isAvailable() { return available; }
public void setAvailable(boolean available) { this.available = available; }

@Override
public String toString() {
return "Product [id=" + id + ", name=" + name + ", price=" + price + ", available=" + available + "]";
}
}

// MainJsonHandler.java
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;

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

try {
// --- 1. Serialize Java Object to JSON String ---
Product laptop = new Product(101, "SuperLaptop", 1200.00, true);
String jsonOutput = objectMapper.writeValueAsString(laptop);
System.out.println("Serialized Product to JSON:n" + jsonOutput);

// --- 2. Deserialize JSON String to Java Object ---
String jsonInput = "{"id":202,"name":"GamingMouse","price":75.50,"available":false}";
Product mouse = objectMapper.readValue(jsonInput, Product.class);
System.out.println("nDeserialized JSON to Product:n" + mouse);

} catch (IOException e) {
e.printStackTrace();
}
}
}
```

**Note:** To run this code, you'll need the Jackson dependency. For Maven, add this to your `pom.xml`:
```xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version> <!-- Use the latest version -->
</dependency>
```

Output example:
```
Serialized Product to JSON:
{"id":101,"name":"SuperLaptop","price":1200.0,"available":true}

Deserialized JSON to Product:
Product [id=202, name=GamingMouse, price=75.5, available=false]
```

### Your Challenge! 🚀
Modify the `MainJsonHandler` to:
1. Create a `List<Product>` with a few different product objects.
2. Serialize this `List<Product>` into a JSON array string.
3. Deserialize that JSON array string back into another `List<Product>`.
Can you do it? Show us your solution! 👇

Happy coding, and see you tomorrow! #JavaLearning #JSON #Jackson #Serialization #Programming

---