当前位置: 首页 > wzjs >正文

政府网站建设 报价网站ip查询

政府网站建设 报价,网站ip查询,网站开发专业基础课程,seo推广营销网站文章目录 前言一、FastJSON 简介二、引入 FastJSON三、FastJSON 注解详解1. JSONField2. JSONType3. JSONCreator4. JSONPOJOBuilder5. JSONField.SerializeUsing 和 JSONField.DeserializeUsing 四、FastJSON 核心使用知识点1. 序列化和反序列化基本操作2. 配置全局序列化和反…

文章目录

    • 前言
    • 一、FastJSON 简介
    • 二、引入 FastJSON
    • 三、FastJSON 注解详解
      • 1. `@JSONField`
      • 2. `@JSONType`
      • 3. `@JSONCreator`
      • 4. `@JSONPOJOBuilder`
      • 5. `@JSONField.SerializeUsing` 和 `@JSONField.DeserializeUsing`
    • 四、FastJSON 核心使用知识点
      • 1. 序列化和反序列化基本操作
      • 2. 配置全局序列化和反序列化特性
      • 3. 处理复杂对象和集合
      • 4. 安全问题
    • 五、总结

前言

在当今的 Java 开发领域,JSON 作为一种轻量级的数据交换格式,被广泛应用于前后端数据交互、配置文件存储等场景。FastJSON 作为阿里巴巴开源的高性能 JSON 处理库,以其简洁的 API 和出色的性能受到众多开发者的青睐。

一、FastJSON 简介

FastJSON 是阿里巴巴开源的一个高性能的 JSON 处理库,它能够快速地将 Java 对象序列化为 JSON 字符串,也能高效地将 JSON 字符串反序列化为 Java 对象。其特点包括解析速度快、使用简单、支持丰富的特性等。

二、引入 FastJSON

在使用 FastJSON 之前,我们需要在项目中引入它的依赖。如果你使用的是 Maven 项目,可以在 pom.xml 文件中添加以下依赖:

<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>2.0.33</version>
</dependency>

三、FastJSON 注解详解

1. @JSONField

  • 作用:这是 FastJSON 中最常用的注解之一,用于对 Java 对象的字段进行序列化和反序列化的精细控制。
  • 常用属性及示例
    • name:指定 JSON 字段的名称,用于实现 Java 对象字段名与 JSON 字段名的映射。
import com.alibaba.fastjson.annotation.JSONField;public class User {@JSONField(name = "user_name")private String name;private int age;public User(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
import com.alibaba.fastjson.JSON;public class Main {public static void main(String[] args) {User user = new User("John", 25);String json = JSON.toJSONString(user);System.out.println(json); // 输出: {"user_name":"John","age":25}}
}
- **`format`**:用于格式化日期类型的字段,在序列化和反序列化时将日期对象转换为指定格式的字符串。
import com.alibaba.fastjson.annotation.JSONField;
import java.util.Date;public class Event {@JSONField(format = "yyyy-MM-dd")private Date date;public Event(Date date) {this.date = date;}public Date getDate() {return date;}public void setDate(Date date) {this.date = date;}
}
import com.alibaba.fastjson.JSON;
import java.util.Date;public class Main {public static void main(String[] args) {Event event = new Event(new Date());String json = JSON.toJSONString(event);System.out.println(json); // 输出类似: {"date":"2025-03-11"}}
}
- **`serialize` 和 `deserialize`**:`serialize` 用于控制该字段是否参与序列化,`deserialize` 用于控制该字段是否参与反序列化。
import com.alibaba.fastjson.annotation.JSONField;public class Product {private String name;@JSONField(serialize = false)private double price;public Product(String name, double price) {this.name = name;this.price = price;}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;}
}
import com.alibaba.fastjson.JSON;public class Main {public static void main(String[] args) {Product product = new Product("Phone", 999.99);String json = JSON.toJSONString(product);System.out.println(json); // 输出: {"name":"Phone"}}
}
- **`ordinal`**:指定字段在 JSON 中的顺序,值越小越靠前。
import com.alibaba.fastjson.annotation.JSONField;public class Book {@JSONField(ordinal = 2)private String title;@JSONField(ordinal = 1)private String author;public Book(String author, String title) {this.author = author;this.title = title;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}
}
import com.alibaba.fastjson.JSON;public class Main {public static void main(String[] args) {Book book = new Book("J.K. Rowling", "Harry Potter");String json = JSON.toJSONString(book);System.out.println(json); // 输出: {"author":"J.K. Rowling","title":"Harry Potter"}}
}

2. @JSONType

  • 作用:用于类级别,可对整个类的序列化和反序列化行为进行配置。
  • 常用属性及示例
    • orders:指定序列化时字段的顺序。
import com.alibaba.fastjson.annotation.JSONType;@JSONType(orders = {"name", "age"})
public class Person {private String name;private int age;public Person(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
import com.alibaba.fastjson.JSON;public class Main {public static void main(String[] args) {Person person = new Person("John", 25);String json = JSON.toJSONString(person);System.out.println(json); // 输出: {"name":"John","age":25}}
}
- **`ignores`**:指定要忽略的字段。
import com.alibaba.fastjson.annotation.JSONType;@JSONType(ignores = {"password"})
public class UserInfo {private String username;private String password;public UserInfo(String username, String password) {this.username = username;this.password = password;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}
}
import com.alibaba.fastjson.JSON;public class Main {public static void main(String[] args) {UserInfo user = new UserInfo("admin", "secret");String json = JSON.toJSONString(user);System.out.println(json); // 输出: {"username":"admin"}}
}
- **`includes`**:指定要包含的字段。
- **`typeName`**:在处理多态类型的序列化和反序列化时,通过 `typeName` 指定类型信息,用于区分不同的子类。
import com.alibaba.fastjson.annotation.JSONType;@JSONType(typeName = "animal")
public abstract class Animal {private String name;public Animal(String name) {this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}@JSONType(typeName = "dog")
class Dog extends Animal {public Dog(String name) {super(name);}
}
import com.alibaba.fastjson.JSON;public class Main {public static void main(String[] args) {Dog dog = new Dog("Buddy");String json = JSON.toJSONString(dog);System.out.println(json); // 输出包含类型信息的 JSON}
}

3. @JSONCreator

  • 作用:用于指定反序列化时使用的构造函数或工厂方法。
import com.alibaba.fastjson.annotation.JSONCreator;
import com.alibaba.fastjson.annotation.JSONField;public class Point {private int x;private int y;@JSONCreatorpublic Point(@JSONField(name = "x_coord") int x, @JSONField(name = "y_coord") int y) {this.x = x;this.y = y;}public int getX() {return x;}public int getY() {return y;}
}
import com.alibaba.fastjson.JSON;public class Main {public static void main(String[] args) {String json = "{\"x_coord\": 10, \"y_coord\": 20}";Point point = JSON.parseObject(json, Point.class);System.out.println("x: " + point.getX() + ", y: " + point.getY()); // 输出: x: 10, y: 20}
}

4. @JSONPOJOBuilder

  • 作用:用于指定使用构建器模式进行对象的反序列化。当 Java 类使用构建器模式创建对象时,该注解可以帮助 FastJSON 正确地反序列化 JSON 数据。
import com.alibaba.fastjson.annotation.JSONPOJOBuilder;class User {private String name;private int age;private User(Builder builder) {this.name = builder.name;this.age = builder.age;}public String getName() {return name;}public int getAge() {return age;}@JSONPOJOBuilder(buildMethodName = "build", withPrefix = "set")public static class Builder {private String name;private int age;public Builder setName(String name) {this.name = name;return this;}public Builder setAge(int age) {this.age = age;return this;}public User build() {return new User(this);}}
}
import com.alibaba.fastjson.JSON;public class Main {public static void main(String[] args) {String json = "{\"name\":\"John\",\"age\":25}";User user = JSON.parseObject(json, User.class);System.out.println("Name: " + user.getName() + ", Age: " + user.getAge()); }
}

5. @JSONField.SerializeUsing@JSONField.DeserializeUsing

  • 作用:用于自定义序列化和反序列化的逻辑。当 FastJSON 提供的默认序列化和反序列化方式无法满足需求时,可以通过自定义序列化器和反序列化器来实现特定的逻辑。
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;import java.io.IOException;
import java.lang.reflect.Type;class CustomSerializer implements ObjectSerializer {@Overridepublic void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {SerializeWriter out = serializer.out;String value = (String) object;out.writeString("Custom: " + value);}
}public class CustomObject {@JSONField(serializeUsing = CustomSerializer.class)private String data;public CustomObject(String data) {this.data = data;}public String getData() {return data;}public void setData(String data) {this.data = data;}
}
import com.alibaba.fastjson.JSON;public class Main {public static void main(String[] args) {CustomObject obj = new CustomObject("Test");String json = JSON.toJSONString(obj);System.out.println(json); // 输出: {"data":"Custom: Test"}}
}

四、FastJSON 核心使用知识点

1. 序列化和反序列化基本操作

  • 序列化:将 Java 对象转换为 JSON 字符串。
import com.alibaba.fastjson.JSON;public class Main {public static void main(String[] args) {Person person = new Person("Alice", 30);String json = JSON.toJSONString(person);System.out.println(json); }
}
  • 反序列化:将 JSON 字符串转换为 Java 对象。
import com.alibaba.fastjson.JSON;public class Main {public static void main(String[] args) {String json = "{\"name\":\"Alice\",\"age\":30}";Person person = JSON.parseObject(json, Person.class);System.out.println(person.getName()); }
}

2. 配置全局序列化和反序列化特性

FastJSON 提供了 SerializerFeatureFeature 来配置序列化和反序列化的特性。

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;public class Main {public static void main(String[] args) {Person person = new Person("Bob", 22);String json = JSON.toJSONString(person, SerializerFeature.PrettyFormat);System.out.println(json); }
}

3. 处理复杂对象和集合

FastJSON 可以方便地处理复杂的 Java 对象和集合。

import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import java.util.List;public class Main {public static void main(String[] args) {List<Person> personList = new ArrayList<>();personList.add(new Person("Charlie", 28));personList.add(new Person("David", 32));String json = JSON.toJSONString(personList);System.out.println(json); List<Person> parsedList = JSON.parseArray(json, Person.class);for (Person person : parsedList) {System.out.println(person.getName()); }}
}

4. 安全问题

FastJSON 曾存在严重的安全漏洞,在使用时需要注意版本的选择和安全配置。建议使用较新的版本,并开启安全模式,例如:

import com.alibaba.fastjson.parser.ParserConfig;public class Main {public static void main(String[] args) {ParserConfig.getGlobalInstance().setAutoTypeSupport(false);// 其他代码}
}

五、总结

FastJSON 以其丰富的注解和强大的功能,为 Java 开发者提供了便捷高效的 JSON 处理解决方案。通过合理运用各种注解,我们可以实现对 JSON 序列化和反序列化过程的精细控制。同时,在使用过程中要重视安全问题,确保项目的稳定性和安全性。

http://www.dtcms.com/wzjs/146313.html

相关文章:

  • wordpress获取页面正文谷歌seo服务
  • 郑州淘宝网站建设百度售后服务电话
  • 北京网站建设 seo公司哪家好网络推广服务商
  • 沧州市东光建设局 网站发帖平台
  • 静态网站可以做哪些内容网络营销专业学校排名
  • 百度搜索网站打开错误软文推广文章
  • 昆明网站建设哪家google seo实战教程
  • 为什么大家用wordpress建网站营销案例
  • 上海营销网站建设公司外贸营销网站
  • 自己做传奇网站怎么在百度上做推广
  • 网络设计一个月多少钱黑锋网seo
  • 澄迈网站建设成都关键词优化服务
  • 网站充值功能怎么做一诺网络推广公司
  • 369网站建设会计培训班哪个机构比较好
  • 网站标题字数推广竞价托管公司
  • 网站备案网站简介扬中网站制作
  • wordpress文章页添加摘要seo关键词排名优化方法
  • 动态网站如何做登录界面火蝠电商代运营公司
  • 制作网站链接谷歌推广代理公司
  • web应用开发要学什么太原seo全网营销
  • 河北邢台有什么好玩的地方秦洁婷seo博客
  • 工作室装修效果图百度搜索优化关键词排名
  • 张家港电脑网站制作吸引人的软文
  • 21dove谁做的的网站漯河seo公司
  • 做网站用那个浏览器青岛seo软件
  • 网站建设总结产品经理培训哪个机构好
  • 网站建设成品潍坊seo培训
  • 泰安新闻网今日头条链接优化方法
  • 怎么注册个人的网站市场推广计划书
  • 武汉做网站及logo的公司如何模板建站