JAVAPRO_IR Telegram 4569
✳️ کار با JSON در جاوا — آشنایی با Jackson و Gson

در برنامه‌نویسی مدرن، JSON یکی از پرکاربردترین فرمت‌ها برای ذخیره و تبادل داده‌ها است. در جاوا، دو کتابخانه‌ی بسیار محبوب برای کار با JSON وجود دارد: Jackson و Gson. هر دو امکان تبدیل (Serialization) و برعکس‌تبدیل (Deserialization) اشیاء جاوا به JSON را فراهم می‌کنند.


🔹 ۱. کتابخانه‌ی Jackson
درواقع Jackson یکی از قدرتمندترین ابزارهای کار با JSON در جاواست که توسط FasterXML توسعه داده شده و در بسیاری از فریمورک‌ها (مثل Spring Boot) به‌صورت پیش‌فرض استفاده می‌شود.

📘 نمونه‌ی کد:

import com.fasterxml.jackson.databind.ObjectMapper;

class User {
    public String name;
    public int age;
}

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

        // شیء جاوا به JSON
        User user = new User();
        user.name = "Saleh";
        user.age = 25;
        String json = mapper.writeValueAsString(user);
        System.out.println("JSON: " + json);

        // JSON به شیء جاوا
        User newUser = mapper.readValue(json, User.class);
        System.out.println("Name: " + newUser.name);
    }
}


📍 خروجی:


JSON: {"name":"Saleh","age":25}
Name: Saleh


📘 ویژگی‌های مهم Jackson:

* پشتیبانی از Annotationها مثل @JsonProperty و @JsonIgnore
* پشتیبانی از Map، List، و Genericها
* کارایی بالا در پردازش داده‌های حجیم


🔹 ۲. کتابخانه‌ی Gson
کتابخانه‌ی Gson محصول شرکت Google است و به‌دلیل سادگی و حجم کم، در پروژه‌های سبک‌تر بسیار محبوب است.

📘 نمونه‌ی کد:

import com.google.gson.Gson;

class User {
    String name;
    int age;
}

public class GsonExample {
    public static void main(String[] args) {
        Gson gson = new Gson();

        // شیء جاوا به JSON
        User user = new User();
        user.name = "Saleh";
        user.age = 25;
        String json = gson.toJson(user);
        System.out.println("JSON: " + json);

        // JSON به شیء جاوا
        User newUser = gson.fromJson(json, User.class);
        System.out.println("Name: " + newUser.name);
    }
}


📍 خروجی مشابه Jackson است:


JSON: {"name":"Saleh","age":25}
Name: Saleh


📘 ویژگی‌های مهم Gson:

* پشتیبانی از انواع Collectionها
* امکان کنترل دقیق Serialization با Annotationهایی مانند @Expose
* سادگی در استفاده و پیاده‌سازی


🧠 جمع‌بندی:
اگر در پروژه‌های بزرگ و سازمانی کار می‌کنید یا از فریمورک‌هایی مثل Spring Boot استفاده می‌کنید، کتابخانه‌ی Jackson انتخاب بهتری است چون سریع‌تر، انعطاف‌پذیرتر و سازگارتر با سیستم‌های بزرگ است.
اما اگر پروژه‌ی شما سبک‌تر است یا در محیط‌هایی مثل اندروید کار می‌کنید، کتابخانه‌ی Gson به‌دلیل سادگی و سبک بودن، گزینه‌ی ایده‌آلی محسوب می‌شود.

#کاربرـحرفهـای


🆔 @javapro_ir
🆔 @group_javapro
👍61



tgoop.com/javapro_ir/4569
Create:
Last Update:

✳️ کار با JSON در جاوا — آشنایی با Jackson و Gson

در برنامه‌نویسی مدرن، JSON یکی از پرکاربردترین فرمت‌ها برای ذخیره و تبادل داده‌ها است. در جاوا، دو کتابخانه‌ی بسیار محبوب برای کار با JSON وجود دارد: Jackson و Gson. هر دو امکان تبدیل (Serialization) و برعکس‌تبدیل (Deserialization) اشیاء جاوا به JSON را فراهم می‌کنند.


🔹 ۱. کتابخانه‌ی Jackson
درواقع Jackson یکی از قدرتمندترین ابزارهای کار با JSON در جاواست که توسط FasterXML توسعه داده شده و در بسیاری از فریمورک‌ها (مثل Spring Boot) به‌صورت پیش‌فرض استفاده می‌شود.

📘 نمونه‌ی کد:

import com.fasterxml.jackson.databind.ObjectMapper;

class User {
    public String name;
    public int age;
}

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

        // شیء جاوا به JSON
        User user = new User();
        user.name = "Saleh";
        user.age = 25;
        String json = mapper.writeValueAsString(user);
        System.out.println("JSON: " + json);

        // JSON به شیء جاوا
        User newUser = mapper.readValue(json, User.class);
        System.out.println("Name: " + newUser.name);
    }
}


📍 خروجی:


JSON: {"name":"Saleh","age":25}
Name: Saleh


📘 ویژگی‌های مهم Jackson:

* پشتیبانی از Annotationها مثل @JsonProperty و @JsonIgnore
* پشتیبانی از Map، List، و Genericها
* کارایی بالا در پردازش داده‌های حجیم


🔹 ۲. کتابخانه‌ی Gson
کتابخانه‌ی Gson محصول شرکت Google است و به‌دلیل سادگی و حجم کم، در پروژه‌های سبک‌تر بسیار محبوب است.

📘 نمونه‌ی کد:

import com.google.gson.Gson;

class User {
    String name;
    int age;
}

public class GsonExample {
    public static void main(String[] args) {
        Gson gson = new Gson();

        // شیء جاوا به JSON
        User user = new User();
        user.name = "Saleh";
        user.age = 25;
        String json = gson.toJson(user);
        System.out.println("JSON: " + json);

        // JSON به شیء جاوا
        User newUser = gson.fromJson(json, User.class);
        System.out.println("Name: " + newUser.name);
    }
}


📍 خروجی مشابه Jackson است:


JSON: {"name":"Saleh","age":25}
Name: Saleh


📘 ویژگی‌های مهم Gson:

* پشتیبانی از انواع Collectionها
* امکان کنترل دقیق Serialization با Annotationهایی مانند @Expose
* سادگی در استفاده و پیاده‌سازی


🧠 جمع‌بندی:
اگر در پروژه‌های بزرگ و سازمانی کار می‌کنید یا از فریمورک‌هایی مثل Spring Boot استفاده می‌کنید، کتابخانه‌ی Jackson انتخاب بهتری است چون سریع‌تر، انعطاف‌پذیرتر و سازگارتر با سیستم‌های بزرگ است.
اما اگر پروژه‌ی شما سبک‌تر است یا در محیط‌هایی مثل اندروید کار می‌کنید، کتابخانه‌ی Gson به‌دلیل سادگی و سبک بودن، گزینه‌ی ایده‌آلی محسوب می‌شود.

#کاربرـحرفهـای


🆔 @javapro_ir
🆔 @group_javapro

BY برنامه نویسی جاوا | Java


Share with your friend now:
tgoop.com/javapro_ir/4569

View MORE
Open in Telegram


Telegram News

Date: |

During the meeting with TSE Minister Edson Fachin, Perekopsky also mentioned the TSE channel on the platform as one of the firm's key success stories. Launched as part of the company's commitments to tackle the spread of fake news in Brazil, the verified channel has attracted more than 184,000 members in less than a month. Telegram message that reads: "Bear Market Screaming Therapy Group. You are only allowed to send screaming voice notes. Everything else = BAN. Text pics, videos, stickers, gif = BAN. Anything other than screaming = BAN. You think you are smart = BAN. Matt Hussey, editorial director at NEAR Protocol also responded to this news with “#meIRL”. Just as you search “Bear Market Screaming” in Telegram, you will see a Pepe frog yelling as the group’s featured image. Telegram is a leading cloud-based instant messages platform. It became popular in recent years for its privacy, speed, voice and video quality, and other unmatched features over its main competitor Whatsapp. Commenting about the court's concerns about the spread of false information related to the elections, Minister Fachin noted Brazil is "facing circumstances that could put Brazil's democracy at risk." During the meeting, the information technology secretary at the TSE, Julio Valente, put forward a list of requests the court believes will disinformation.
from us


Telegram برنامه نویسی جاوا | Java
FROM American