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

免费做网站app下载亿驱动力竞价托管

免费做网站app下载,亿驱动力竞价托管,米兰设计周中国高校设计官网,做网站的创始人文章目录 前言一、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/182819.html

相关文章:

  • 嘉兴seo推广优化seo成功的案例和分析
  • 服务专业的网站建设服务公众号推广费用一般多少
  • 合肥最新通告今天搜索引擎优化的重要性
  • 专业企业网站设计网络公司软文什么意思
  • wordpress首页不显示该分类下文章seo查询工具网站
  • 南京做网站优化价格最近的大新闻
  • 如何使用模板做网站合肥优化营商环境
  • 网站项目整体思路seo常用工具网站
  • 做网站手机验证收费吗网站推广内容
  • 武汉网站建设 乐云seo安徽网站seo公司
  • ext做的网站武汉十大技能培训机构
  • 襄阳市做网站 优帮云成品短视频app下载有哪些软件
  • 网络运营者应当对其收集的用户信息严格保密云南seo公司
  • 免费文字变形logo设计网站seo优化方法
  • 网站关于我们怎么做seo排名快速优化
  • 厦门网上房地产官网查询广州seo外包公司
  • 定制建站网站建设技师培训
  • asp.net网站开发是什么搜索排名优化软件
  • 南海区住房城乡建设和水务局网站网络营销与市场营销的区别
  • 四川营销型网站建设网络营销相关的岗位有哪些
  • 深圳网站设计开发seo快速排名站外流量推广
  • 南宁保洁网站建设平台推广策略都有哪些
  • 深圳有哪些做网站公司好线上培训机构有哪些
  • 做360网站优化快怎么创建网页
  • 顺企网萍乡网站建设网站seo设计方案案例
  • wordpress audio player 下载优化设计六年级下册语文答案
  • 提高网站权重图片搜索引擎
  • 楚雄市网站建设公司产品seo是什么意思
  • 如何做和别人一样的网站网站优化推广平台
  • 独立域名网站seo技术