当前位置: 首页 > 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/117559.html

相关文章:

  • 免费网站服务网站开发流程有哪几个阶段
  • 软件代做网站在哪找活aso优化贴吧
  • 酒店网站怎么制作百度统计数据
  • 门户网站开发视频教学做网站好的网站建设公司
  • 网站功能设计整合营销策划方案
  • 黄岛网站建设多少钱营销咨询顾问
  • 网站开发环境怎么写长沙网站托管优化
  • 建设英文网站的请示天津百度快速优化排名
  • wordpress页面的添加海南seo快速排名优化多少钱
  • jsp动态网站开发实训心得北京网站优化策略
  • 商务网站建设的一般流程是什么意思整站seo技术
  • 今科网站建设公司梅州网络推广
  • wordpress ftp 设置windows优化大师的特点
  • php学校网站源码百度推广app怎么收费
  • h5页面可以跳转到小程序吗重庆网站seo建设哪家好
  • 深圳 seo 外贸网站建设 多语种百度搜索风云榜小说
  • 咸阳网站开发google play下载安装
  • 湛江建设工程交易中心网站百度站长工具seo查询
  • https网站建设花费百度关键词搜索优化
  • 顺义建站公司搜狗网址
  • 做爰全过程免费费网站网络seo啥意思
  • 建设商城网站费用如何把网站推广
  • 山西网站的公司谷歌账号
  • 记事本做网站代码重庆seo网站建设
  • 新网做网站怎么上传怎么优化推广自己的网站
  • 西安定制网站网络营销服务商
  • 网站上线之前做测试吗互联网运营
  • 互联网营销师培训基地seo网站关键词优化费用
  • wordpress cdn 回源量搜索引擎营销优化的方法
  • 光明随心订网站怎么做百度实名认证