〖FastJson 2〗
『Enum』
FastJson 中的枚举类说明,主要用在序列化和反序列化的API。
﹝JSONWriter.Feature﹞
serializeFeatures 的属性 JSONWriter.Feature。
序号 | 属性 | 作用 |
---|
1 | FieldBased | 基于字段反序列化,如果不配置默认基于 public 的 field 和 getter 方法序列化。 |
2 | IgnoreNoneSerializable | 忽略没有标记 @JSONField(serialize = false) 的字段。 |
3 | ErrorOnNoneSerializable | 当遇到没有标记 @JSONField(serialize = false) 的字段时抛出异常。 |
4 | BeanToArray | 将 JavaBean 序列化为数组而不是对象。 |
5 | WriteNulls | 写入值为 null 的属性。 |
6 | BrowserCompatible | 产生兼容浏览器的 JSON,首字母小写,不带引号。 |
7 | NullAsDefaultValue | 将 null 值作为默认值写入 JSON。 |
8 | WriteBooleanAsNumber | 将布尔类型(Boolean)序列化为 0 或 1 而不是 true/false。 |
9 | WriteNonStringValueAsString | 将非字符串类型写为字符串类型。 |
10 | WriteClassName | 在序列化时写入对象的类名。 |
11 | NotWriteRootClassName | 不写入顶层对象的类名。 |
12 | NotWriteHashMapArrayListClassName | 不写入 HashMap 和 ArrayList 的类名。 |
13 | NotWriteDefaultValue | 不写入默认值。 |
14 | WriteEnumsUsingName | 将枚举类型序列化为名称而不是值。 |
15 | WriteEnumUsingToString | 将枚举类型序列化为 toString() 方法的返回值。 |
16 | IgnoreErrorGetter | 忽略 getter 方法抛出的异常。 |
17 | PrettyFormat | 以漂亮的格式输出 JSON。 |
18 | ReferenceDetection | 启用引用检测。 |
19 | WriteNameAsSymbol | 写入属性的名称作为符号。 |
20 | WriteBigDecimalAsPlain | 将 BigDecimal 序列化为数字而不是字符串。 |
21 | UseSingleQuotes | 使用单引号而不是双引号。 |
22 | MapSortField | 对 Map 类型按照字段名排序。 |
23 | WriteNullListAsEmpty | 将 null 值的 List 类型属性序列化为空数组。 |
24 | WriteNullStringAsEmpty | 将 null 值的 String 类型属性序列化为 “”。 |
25 | WriteNullNumberAsZero | 将 null 值的 Number 类型属性序列化为 0。 |
26 | WriteNullBooleanAsFalse | 将 null 值的 Boolean 类型属性序列化为 false。 |
27 | NotWriteEmptyArray | 不写入空的数组。 |
28 | WriteNonStringKeyAsString | 将非字符串类型的键序列化为字符串。 |
29 | WritePairAsJavaBean | 将键值对序列化为 JavaBean。 |
30 | OptimizedForAscii | 针对 ASCII 字符优化序列化性能。 |
31 | EscapeNoneAscii | 对于超出 ASCII 范围【即代码点为 128 及以上】的字符使用特定的机制输出。 |
32 | WriteByteArrayAsBase64 | 以 Base64 编码形式序列化 byte 数组。 |
33 | IgnoreNonFieldGetter | 忽略 getter 方法标记为 @JSONField(serialize = false) 的字段。 |
﹝JSONReader.Feature﹞
deserializeFeatures的属性 JSONReader.Feature
序号 | 属性 | 作用 |
---|
1 | FieldBased | 使用基于字段的解析方式。 |
2 | IgnoreNoneSerializable | 忽略没有标记 @JSONField(deserialize = false) 的非反序列化字段。 |
3 | ErrorOnNoneSerializable | 遇到没有标记 @JSONField(deserialize = false) 的字段时抛出异常。 |
4 | SupportArrayToBean | 支持将数组转换为 JavaBean 对象。 |
5 | InitStringFieldAsEmpty | 将字符串类型的属性初始化为空字符串。 |
6 | SupportAutoType | 支持自动类型转换。 |
7 | SupportSmartMatch | 支持智能匹配。 |
8 | UseNativeObject | 使用本地对象进行解析。 |
9 | SupportClassForName | 支持使用类名进行解析。 |
10 | IgnoreSetNullValue | 忽略设置为 null 的属性值。 |
11 | UseDefaultConstructorAsPossible | 尽量使用默认构造函数进行实例化。 |
12 | UseBigDecimalForFloats | 使用 BigDecimal 解析浮点数。 |
13 | UseBigDecimalForDoubles | 使用 BigDecimal 解析双精度浮点数。 |
14 | ErrorOnEnumNotMatch | 当枚举类型不匹配时抛出异常。 |
15 | TrimString | 修剪字符串的前导和尾随空格。 |
16 | ErrorOnNotSupportAutoType | 当不支持自动类型转换时抛出异常。 |
17 | DuplicateKeyValueAsArray | 将重复的键值对解析为数组。 |
18 | AllowUnQuotedFieldNames | 允许不使用引号的字段名。 |
19 | NonStringKeyAsString | 将非字符串类型的键解析为字符串。 |
20 | Base64StringAsByteArray | 将 Base64 编码的字符串解析为字节数组。 |
﹝PropertyNamingStrategy﹞
使用NameFilter.of 方法时需要传枚举类,以下是枚举类的说明。
序号 | 属性 | 作用 |
---|
1 | CamelCase | 『序列化』保持原样。 |
2 | PascalCase | 『序列化』首字母大写。 |
3 | SnakeCase | 『序列化』使用下划线分隔单词,且全部小写。 |
4 | UpperCase | 『序列化』全部大写。 |
5 | UpperCamelCaseWithSpaces | 『序列化』首字母大写,单词之间用空格分隔。 |
6 | UpperCamelCaseWithUnderScores | 『序列化』首字母大写,单词之间用下划线分隔。 |
7 | UpperCamelCaseWithDashes | 『序列化』首字母大写,单词之间用破折号分隔。 |
8 | UpperCamelCaseWithDots | 『序列化』首字母大写,单词之间用点号分隔。 |
9 | KebabCase | 『序列化』全部小写,单词之间用破折号分隔。 |
10 | UpperCaseWithUnderScores | 『序列化』全部大写,单词之间用下划线分隔。 |
11 | UpperCaseWithDashes | 『序列化』全部大写,单词之间用破折号分隔。 |
12 | UpperCaseWithDots | 『序列化』全部大写,单词之间用点号分隔。 |
13 | LowerCase | 『序列化』全部小写。 |
14 | LowerCaseWithUnderScores | 『序列化』全部小写,单词之间用下划线分隔。 |
15 | LowerCaseWithDashes | 『序列化』全部小写,单词之间用破折号分隔。 |
16 | LowerCaseWithDots | 『序列化』全部小写,单词之间用点号分隔。 |
『JavaBean』
包括后面可能会用到一些实体类、实现类、接口、注解,等。可以暂时跳过,后面用到的时候再拿过来看。
﹝Annotate﹞
| package com.ruoyi.project.missionnel.annotate; |
| |
| import java.lang.annotation.ElementType; |
| import java.lang.annotation.Retention; |
| import java.lang.annotation.RetentionPolicy; |
| import java.lang.annotation.Target; |
| |
| @Retention(RetentionPolicy.RUNTIME) |
| @Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER}) |
| public @interface Alias { |
| String value() default ""; |
| Name[] names() default {}; |
| } |
复制
| package com.ruoyi.project.missionnel.annotate; |
| |
| import java.lang.annotation.ElementType; |
| import java.lang.annotation.Retention; |
| import java.lang.annotation.RetentionPolicy; |
| import java.lang.annotation.Target; |
| |
| @Retention(RetentionPolicy.RUNTIME) |
| @Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER}) |
| public @interface Name { |
| |
| String type(); |
| |
| String value(); |
| |
| } |
复制
﹝Bean﹞
| package com.ruoyi.project.missionnel.bean; |
| |
| import com.ruoyi.project.missionnel.inter.JavaBean; |
| import lombok.AllArgsConstructor; |
| import lombok.Data; |
| import lombok.NoArgsConstructor; |
| import lombok.experimental.Accessors; |
| |
| @Data |
| @NoArgsConstructor |
| @AllArgsConstructor |
| @Accessors(chain = true) |
| public class Family implements JavaBean { |
| |
| private String father = "阿柴的爸爸"; |
| |
| private String mother = "阿柴的妈妈"; |
| |
| private String brother = "阿柴的哥哥"; |
| |
| private String sister = "阿柴的姐姐"; |
| |
| } |
复制
| package com.ruoyi.project.missionnel.bean; |
| |
| import com.ruoyi.project.missionnel.annotate.Alias; |
| import com.ruoyi.project.missionnel.annotate.Name; |
| import com.ruoyi.project.missionnel.inter.JavaBean; |
| import lombok.AllArgsConstructor; |
| import lombok.Data; |
| import lombok.NoArgsConstructor; |
| |
| @Data |
| @NoArgsConstructor |
| @AllArgsConstructor |
| public class Message implements JavaBean { |
| |
| @Alias(names = {@Name(type = "Small", value = "message"), @Name(type = "Big", value = "MESSAGE")}) |
| private String msg; |
| |
| } |
复制
| package com.ruoyi.project.missionnel.bean; |
| |
| import com.ruoyi.project.missionnel.inter.JavaBean; |
| import lombok.AllArgsConstructor; |
| import lombok.Data; |
| import lombok.NoArgsConstructor; |
| import lombok.experimental.Accessors; |
| |
| @Data |
| @NoArgsConstructor |
| @AllArgsConstructor |
| @Accessors(chain = true) |
| public class Params<T> implements JavaBean { |
| private T param; |
| } |
复制
| package com.ruoyi.project.missionnel.bean; |
| |
| import com.alibaba.fastjson2.annotation.JSONField; |
| import com.ruoyi.project.missionnel.inter.JavaBean; |
| import com.ruoyi.project.missionnel.support.JsonParser; |
| import lombok.AllArgsConstructor; |
| import lombok.Builder; |
| import lombok.Data; |
| import lombok.NoArgsConstructor; |
| |
| import java.util.Date; |
| |
| import static com.alibaba.fastjson2.JSONReader.Feature.InitStringFieldAsEmpty; |
| import static com.alibaba.fastjson2.JSONWriter.Feature.NotWriteDefaultValue; |
| |
| @Data |
| @Builder |
| @NoArgsConstructor |
| @AllArgsConstructor |
| public class Student implements JavaBean { |
| |
| private static final long serialVersionUID = -3289647584974663707L; |
| |
| @JSONField(name = "userName", label = "ELS", alternateNames = {"myName", "testName"}) |
| private String name; |
| |
| @JSONField(ordinal = 1) |
| private Integer age; |
| |
| @JSONField(ordinal = 2) |
| private String gender; |
| |
| @JSONField(defaultValue = "开朗") |
| private String character; |
| |
| @JSONField(serialize = false) |
| private String job; |
| |
| @JSONField(deserialize = false) |
| private String address; |
| |
| @JSONField(serializeFeatures = {NotWriteDefaultValue}) |
| private String father; |
| |
| @JSONField(deserializeFeatures = {InitStringFieldAsEmpty}) |
| private String mother; |
| |
| @JSONField(format = "yyyy年MM月dd日 HH时mm分ss秒") |
| private Date birthday; |
| |
| @JSONField(deserializeUsing = JsonParser.class) |
| private String sister; |
| |
| @JSONField(serializeUsing = JsonParser.class) |
| private String brother; |
| |
| @JSONField(jsonDirect = true) |
| private String json; |
| |
| @JSONField(unwrapped = true) |
| private Message message; |
| |
| } |
复制
| package com.ruoyi.project.missionnel.bean; |
| |
| import com.alibaba.fastjson2.annotation.JSONField; |
| import com.ruoyi.project.missionnel.inter.JavaBean; |
| import lombok.AllArgsConstructor; |
| import lombok.Data; |
| import lombok.NoArgsConstructor; |
| |
| @Data |
| @NoArgsConstructor |
| @AllArgsConstructor |
| public class User implements JavaBean { |
| |
| @JSONField(name = "哈哈哈") |
| private String name = "阿柴"; |
| |
| private int age = 18; |
| |
| private String sex = "男"; |
| |
| private long height = 180; |
| |
| private long weight = 150; |
| |
| private Family family = new Family(); |
| |
| } |
复制
﹝Filter﹞
| package com.ruoyi.project.missionnel.filter; |
| |
| import com.alibaba.fastjson2.filter.NameFilter; |
| import com.ruoyi.project.missionnel.annotate.Alias; |
| import com.ruoyi.project.missionnel.annotate.Name; |
| import lombok.AllArgsConstructor; |
| import lombok.Data; |
| import lombok.NoArgsConstructor; |
| |
| @Data |
| @NoArgsConstructor |
| @AllArgsConstructor |
| public class AliasFilter implements NameFilter { |
| |
| private String type; |
| |
| @Override |
| public String process(Object javaType, String key, Object value) { |
| try { |
| |
| Alias alias = javaType.getClass().getDeclaredField(key).getDeclaredAnnotation(Alias.class); |
| |
| Name[] names = alias.names(); |
| |
| String defaultName = alias.value(); |
| |
| if (names.length == 0 && defaultName.length() > 0) return defaultName; |
| |
| for (Name name : names) if (type.equals(name.type())) return name.value(); |
| |
| } catch (Exception e) { |
| |
| return key; |
| |
| } |
| |
| return key; |
| } |
| } |
复制
﹝Interface﹞
| package com.ruoyi.project.missionnel.inter; |
| |
| import com.alibaba.fastjson2.JSON; |
| import com.alibaba.fastjson2.JSONObject; |
| |
| public interface JavaBean { |
| |
| |
| |
| |
| |
| default String toJSONString() { |
| return JSON.toJSONString(this); |
| } |
| |
| |
| |
| |
| |
| default JSONObject toJSONObject() { |
| return (JSONObject) JSON.toJSON(this); |
| } |
| |
| } |
复制
﹝Support﹞
| package com.ruoyi.project.missionnel.support; |
| |
| import cn.hutool.core.lang.Console; |
| import com.alibaba.fastjson2.JSONReader; |
| import com.alibaba.fastjson2.JSONWriter; |
| import com.alibaba.fastjson2.reader.ObjectReader; |
| import com.alibaba.fastjson2.writer.ObjectWriter; |
| |
| import java.lang.reflect.Type; |
| |
| @SuppressWarnings("rawtypes") |
| public class JsonParser implements ObjectReader, ObjectWriter { |
| |
| @Override |
| public long getFeatures() { |
| return ObjectReader.super.getFeatures(); |
| } |
| |
| @Override |
| public Object readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) { |
| String str = jsonReader.readString(); |
| Console.error("Read测试文字: {}", str); |
| return str; |
| } |
| |
| |
| @Override |
| public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) { |
| Console.error("Write测试文字: {}", object.toString()); |
| jsonWriter.writeString(object.toString()); |
| } |
| } |
复制
『JSON』
JSON 接口定义了类型转换的API。包括 Object 类转换成 String 【序列化】,和 String 转换成 Object 类【反序列化】。同时重载了很多方法,用来自定义序列化和反序列化。因为是接口,所以里面定义的方法都是 Static 的,可以直接调用。
﹝JSON.toJSONString﹞
可以把 Object 类型的对象进行序列化,同时有很多重载方法,可以通过 JSONWriter.Feature 自定义序列化行为。
| User user = new User(); |
| |
| |
| String jsonString1 = JSON.toJSONString(user); |
| |
| |
| String jsonString2 = JSON.toJSONString(user, JSONWriter.Feature.PrettyFormat); |
| |
| |
| String jsonString3 = JSON.toJSONString(user, new AliasFilter()); |
| |
| System.out.println(jsonString1); |
| System.out.println(jsonString2); |
| System.out.println(jsonString3); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
复制
﹝JSON.parse﹞
将字符串反序列化成对象,返回的对象是 Object ,也就意味着可以强转换成 JSONObject 或者是 JSONArray。这个方法还有多个重载方法,可以通过 JSONWriter.Feature 来进行反序列化。
| String str = "{\"age\":18,\"family\":{\"brother\":\"阿柴的哥哥\",\"father\":\"阿柴的爸爸\",\"mother\":\"阿柴的妈妈\",\"sister\":\"阿柴的姐姐\"},\"height\":180,\"sex\":\"男\",\"weight\":150,\"哈哈哈\":\"阿柴\"}"; |
| |
| Object parse = JSON.parse(str); |
| |
| System.out.println(parse); |
| |
| |
复制
﹝JSON.parseObject﹞
另一种反序列化方式,这种方式处理的方式更加丰富。可以把字符串反序列化成 JSONObject 或者 Java 实体类,如果是携带泛型的实体类,可以用 TypeReference 进行处理。
| String str = "{\"age\":18,\"family\":{\"brother\":\"阿柴的哥哥\",\"father\":\"阿柴的爸爸\",\"mother\":\"阿柴的妈妈\",\"sister\":\"阿柴的姐姐\"},\"height\":180,\"sex\":\"男\",\"weight\":150,\"哈哈哈\":\"阿柴\"}"; |
| String string = "{\"param\":\"泛型参数\"}"; |
| |
| JSONObject jsonObject = JSON.parseObject(str); |
| |
| User user = JSON.parseObject(str, User.class); |
| |
| Params<String> params = JSON.parseObject(string, new TypeReference<Params<String>>() {}); |
| |
| System.out.println(jsonObject); |
| System.out.println(user); |
| System.out.println(params); |
| |
| |
| |
| |
复制
﹝JSON.to﹞
本身是一个万能的转换 API ,但是如果是实体类,转换成 JSONObject 就会抛出异常。但是其他的的 API 会弥补这个不足。
| User user = new User(); |
| |
| String str = JSON.to(String.class, user); |
| |
| JSONObject jsonObject = JSON.to(JSONObject.class, str); |
| |
| User userNew = JSON.to(User.class, str); |
| |
| |
| |
| |
| System.out.println(str); |
| System.out.println(jsonObject); |
| System.out.println(userNew); |
| |
| |
| |
| |
复制
﹝JSON.toJSON﹞
可以将实体类转换成 JSONObject 或者 JSONArray 。但是返回值是 Object 所以需要强转。但是 String 类型的字符串不能转换,会抛出异常。
| String[] array = {"1", "2", "3"}; |
| User user = new User(); |
| |
| JSONObject json = (JSONObject) JSON.toJSON(user); |
| |
| JSONArray jsonArray = (JSONArray) JSON.toJSON(array); |
| |
| System.out.println(json); |
| System.out.println(jsonArray); |
| |
| |
| |
复制
『JSONObject』
﹝Put 及其衍生﹞
put:放入键值对。
| JSONObject json = new JSONObject(); |
| json.put("name", "阿柴"); |
| |
复制
fluentPut:和 put 的作用是一样的,但是可以实现链式编程。
| JSONObject json = new JSONObject().fluentPut("name", "阿柴").fluentPut("age", 18).fluentPut("sex", "男").fluentPut("address", "北京市中南海"); |
| |
复制
putIfAbsent: 将键值对放入 JSONObject 对象,如果放入的Key不存在,将键值对放入,如果Key存在,那么不做替换处理,返回当前Key对应的值。
| JSONObject json = new JSONObject().fluentPut("name", "阿柴").fluentPut("age", 18).fluentPut("sex", "男").fluentPut("address", "北京市中南海"); |
| |
| Object resultName = json.putIfAbsent("name", "黑柴"); |
| Object resultGirlFriend = json.putIfAbsent("girlFriend", "白柴"); |
| |
| System.out.println(json); |
| System.out.println(resultName); |
| System.out.println(resultGirlFriend); |
| |
| |
| |
| |
复制
putAll:将 Map<? extends K, ? extends V> 类型的参数,放入 JSONObject 对象中。JSONObject 继承了 LinkedHashMap 所以也可以传入。
| JSONObject json = new JSONObject().fluentPut("name", "阿柴").fluentPut("age", 18); |
| |
| JSONObject jsonAnother = new JSONObject().fluentPut("sex", "男").fluentPut("address", "北京市中南海"); |
| jsonAnother.putAll(json); |
| |
| System.out.println(json); |
| System.out.println(jsonAnother); |
| |
| |
| |
复制
构造函数:构造函数可以将 Map 类型的对象初始化。
| JSONObject json = new JSONObject().fluentPut("name", "阿柴").fluentPut("age", 18); |
| |
| JSONObject jsonAnother = new JSONObject(json).fluentPut("sex", "男").fluentPut("address", "北京市中南海"); |
| |
| System.out.println(json); |
| System.out.println(jsonAnother); |
| |
| |
| |
复制
﹝Get 及其衍生 💗﹞
get:get可以获取 JSONObject 中存入的值,get有很多衍生的方法。例如 getString 可以直接把获取的值,转换为 String 而不需要手动强转,这一点既安全又方便;其他类型的参数也有相关的API
例如:getDouble、getFloat、getLong、getShort、getByte、getBoolean、getBigInteger、getBigDecimal、getDate、getInstant,这里不再进行展示。
| JSONObject json = new JSONObject(); |
| |
| JSONObject address = new JSONObject(); |
| address.put("country", "中国"); |
| address.put("province", "天津市"); |
| address.put("city", "天津市"); |
| address.put("area", "河西区"); |
| |
| JSONArray cars = new JSONArray(); |
| cars.add("保时捷918"); |
| cars.add("法拉利拉法"); |
| cars.add("迈凯伦P1"); |
| |
| List<String> girlFriends = new ArrayList<>(); |
| girlFriends.add("桃乃木奈香"); |
| girlFriends.add("枫花恋"); |
| girlFriends.add("水野朝阳"); |
| girlFriends.add("筱田优"); |
| girlFriends.add("美乃雀"); |
| girlFriends.add("三上悠亚"); |
| girlFriends.add("葵司"); |
| girlFriends.add("深田咏美"); |
| |
| Family family = new Family(); |
| |
| Params<String> params = new Params<>(); |
| params.setParam("泛型参数"); |
| |
| json.put("name", "阿柴"); |
| json.put("age", 18); |
| json.put("sex", "男"); |
| json.put("nation", "汉族"); |
| json.put("address", address); |
| json.put("girlFriends", girlFriends); |
| json.put("cars", cars); |
| json.put("family", family); |
| json.put("params", params); |
| |
| Object name = json.get("name"); |
| int age = json.getIntValue("age"); |
| String sex = json.getString("sex"); |
| |
| |
| Object nation = json.getOrDefault("nation", "没有数据!"); |
| Object idCard = json.getOrDefault("idCard", "没有数据!"); |
| |
| JSONObject addressJson = json.getJSONObject("address"); |
| JSONArray carsJson = json.getJSONArray("cars"); |
| List<String> girls = json.getList("girlFriends", String.class); |
| Family familyObj = json.getObject("family", Family.class); |
| |
| |
| Params<String> paramsStr = json.getObject("params", new TypeReference<Params<String>>() {}); |
| |
| |
| TypeReference<Params<String>> typeReference = new TypeReference<Params<String>>() {}; |
| Type type = typeReference.getType(); |
| Params<String> paramsType = json.getObject("params", type); |
| |
| System.out.println(name); |
| System.out.println(age); |
| System.out.println(sex); |
| System.out.println(nation); |
| System.out.println(idCard); |
| System.out.println(addressJson); |
| System.out.println(carsJson); |
| System.out.println(girls); |
| System.out.println(familyObj); |
| System.out.println(paramsStr); |
| System.out.println(paramsType); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
复制
﹝containsKey﹞
containsKey:判断当前对象存在已有的Key。
| JSONObject json = new JSONObject(); |
| json.put("name", "阿柴"); |
| json.put("age", "18"); |
| json.put("sex", "男"); |
| |
| boolean name = json.containsKey("name"); |
| System.out.println(name); |
| |
| |
复制
﹝containsValue﹞
containsValue:当前对象是存在已有Value。
| JSONObject json = new JSONObject(); |
| json.put("name", "阿柴"); |
| json.put("age", "18"); |
| json.put("sex", "男"); |
| |
| boolean name = json.containsValue("阿柴"); |
| System.out.println(name); |
复制
﹝nameFilter﹞
nameFilter:将Key的值进行统一变动,我们这里添加前缀和后缀。
| JSONObject json = new JSONObject(); |
| json.put("name", "阿柴"); |
| json.put("age", "18"); |
| json.put("sex", "男"); |
| |
| |
| NameFilter nameFilter = (Object object, String name, Object value) -> "pre_" + name + "_suf"; |
| json.nameFilter(nameFilter); |
| |
| System.out.println(json); |
| |
| |
复制
﹝valueFilter﹞
valueFilter:和 nameFilter 一样,统一对值进行处理。
| JSONObject json = new JSONObject(); |
| json.put("name", "阿柴"); |
| json.put("age", 18); |
| json.put("sex", "男"); |
| json.put("address", "北京市"); |
| |
| ValueFilter valueFilter = (Object object, String name, Object value) -> "A" + value + "B"; |
| json.valueFilter(valueFilter); |
| |
| System.out.println(json); |
| |
| |
复制
﹝eval﹞
eval:可以用来查找某个值,详细用法可以看后面的JSONPath用法,这里我们知道有这个API即可。
| JSONObject json = new JSONObject(); |
| json.put("name", "阿柴"); |
| json.put("age", "18"); |
| json.put("sex", "男"); |
| |
| |
| Object eval = json.eval(JSONPath.of("$.sex")); |
| System.out.println(eval); |
| |
| |
复制
﹝to 💗﹞
to:把 JSONObject 对象转换成任意 Java 对象。
| JSONObject json = new JSONObject(); |
| json.put("msg", "阿柴该还京东白条了。"); |
| json.put("param", "阿柴该还花呗了。"); |
| |
| |
| Message message = json.to(Message.class); |
| |
| |
| Params<String> params = json.to(new TypeReference<Params<String>>() {}); |
| |
| |
| TypeReference<Params<String>> typeReference = new TypeReference<Params<String>>() {}; |
| Type type = typeReference.getType(); |
| Params<String> paramsAnother = json.to(type); |
| |
| |
| String lambda = json.to(item -> item.getString("msg")); |
| |
| System.out.println(message); |
| System.out.println(params); |
| System.out.println(paramsAnother); |
| System.out.println(lambda); |
| |
| |
| |
| |
| |
复制
﹝isValid﹞
isValid:验证 JSON 是否合规。详细内容请查看 JSONSchema 代码。
| String schemaString = "{\"type\": \"object\", \"properties\": {\"name\": {\"type\": \"string\"}}, \"required\": [\"name\"]}"; |
| JSONSchema jsonSchema = JSONSchema.parseSchema(schemaString); |
| |
| JSONObject json = new JSONObject(); |
| json.put("name", "阿柴"); |
| |
| JSONObject jsonAnother = new JSONObject(); |
| jsonAnother.put("age", 18); |
| |
| boolean valid = json.isValid(jsonSchema); |
| boolean validAnother = jsonAnother.isValid(jsonSchema); |
| |
| System.out.println(valid); |
| System.out.println(validAnother); |
| |
| |
| |
复制
﹝toJSONString﹞
toJSONString:将 JSONObject 序列化成字符串,序列化的时候可以使用 JSONWriter.Feature 中的枚举类来进行自定义序列化。
| JSONObject json = new JSONObject(); |
| json.put("name", "阿柴"); |
| json.put("age", 18); |
| json.put("sex", "男"); |
| json.put("address", "北京市"); |
| |
| String jsonString = json.toJSONString(); |
| System.out.println(jsonString); |
| |
| |
复制
﹝toString﹞
toString:将对象序列化,和 toJSONString 的结果是一样的。
| JSONObject json = new JSONObject(); |
| json.put("name", "阿柴"); |
| json.put("age", 18); |
| json.put("sex", "男"); |
| json.put("address", "北京市"); |
| |
| String jsonString = json.toString(); |
| System.out.println(jsonString); |
| |
| |
复制
﹝compute﹞
compute:是从 HashMap 那里继承过来的方法,对特定的 Key 值进行操作,并最终返回的方法。
| JSONObject json = new JSONObject(); |
| json.put("name", "阿柴"); |
| json.put("age", 18); |
| json.put("sex", "男"); |
| json.put("address", "北京市"); |
| |
| Object name = json.compute("name", (key, value) -> "+" + value + "+"); |
| |
| System.out.println(name); |
| |
| |
复制
﹝computeIfAbsent﹞
computeIfAbsent:也是从 HashMap 继承来的方法,当Key值存在的时候返回当前值,如果不存在插入键值对。
| JSONObject json = new JSONObject(); |
| json.put("name", "阿柴"); |
| json.put("age", 18); |
| json.put("sex", "男"); |
| json.put("address", "北京市"); |
| |
| Object name = json.computeIfAbsent("name", (key) -> "阿柴的哥哥"); |
| Object brother = json.computeIfAbsent("brother", (key) -> "阿柴的哥哥"); |
| |
| System.out.println(name); |
| System.out.println(brother); |
| System.out.println(json); |
| |
| |
| |
| |
复制
﹝computeIfPresent﹞
computeIfPresent:如果 key 对应的 value 不存在,则返回 null ,如果存在则返回通过 remappingFunction 重新计算后的值。
| JSONObject json = new JSONObject(); |
| json.put("name", "阿柴"); |
| json.put("age", 18); |
| json.put("sex", "男"); |
| json.put("address", "北京市"); |
| |
| Object name = json.computeIfPresent("name", (key, value) -> value + "+++"); |
| Object brother = json.computeIfPresent("brother", (key, value) -> value + "+++"); |
| |
| System.out.println(name); |
| System.out.println(brother); |
| |
| |
| |
复制
﹝entrySet﹞
entrySet:获取 entrySet 键值对集合。
| JSONObject json = new JSONObject(); |
| json.put("name", "阿柴"); |
| json.put("age", 18); |
| json.put("sex", "男"); |
| json.put("address", "北京市"); |
| |
| |
| Set<Map.Entry<String, Object>> entries = json.entrySet(); |
| entries.forEach((item) -> System.out.println(item.getKey() + "---" + item.getValue())); |
| |
| |
| |
| |
| |
复制
﹝keySet﹞
keySet:获取 Key 的集合。
| JSONObject json = new JSONObject(); |
| json.put("name", "阿柴"); |
| json.put("age", 18); |
| json.put("sex", "男"); |
| json.put("address", "北京市"); |
| |
| Set<String> keySet = json.keySet(); |
| keySet.forEach(System.out::println); |
| |
| |
| |
| |
| |
复制
﹝values﹞
values:获取值得集合。
| JSONObject json = new JSONObject(); |
| json.put("name", "阿柴"); |
| json.put("age", 18); |
| json.put("sex", "男"); |
| json.put("address", "北京市"); |
| |
| Collection<Object> values = json.values(); |
| values.forEach(System.out::println); |
复制
﹝isEmpty﹞
isEmpty:判空操作,很简单了,不进行解释了。
| JSONObject json = new JSONObject(); |
| json.put("name", "阿柴"); |
| json.put("age", 18); |
| json.put("sex", "男"); |
| json.put("address", "北京市"); |
| |
| boolean empty = json.isEmpty(); |
| System.out.println(empty); |
| |
| |
复制
﹝merge﹞
merge:如果不存在将新的值赋值到 key ,如果存在更新 key 对应的 value。
| JSONObject json = new JSONObject(); |
| json.put("name", "阿柴"); |
| json.put("age", 18); |
| json.put("sex", "男"); |
| json.put("address", "北京市"); |
| |
| Object merge = json.merge("name", "黑柴", (key, value) -> value + "Test"); |
| |
| System.out.println(merge); |
| |
| |
复制
﹝remove﹞
remove:移除指定的键值对。
| JSONObject json = new JSONObject(); |
| json.put("name", "阿柴"); |
| json.put("age", 18); |
| json.put("sex", "男"); |
| json.put("address", "北京市"); |
| |
| json.remove("address"); |
| |
| System.out.println(json); |
| |
| |
复制
﹝replace﹞
replace:替换指指定的键值对。
| JSONObject json = new JSONObject(); |
| json.put("name", "阿柴"); |
| json.put("age", 18); |
| json.put("sex", "男"); |
| json.put("address", "北京市"); |
| |
| json.replace("name", "黑柴"); |
| |
| System.out.println(json); |
| |
| |
复制
﹝size﹞
size:获取对象的长度。
| json.put("name", "阿柴"); |
| json.put("age", 18); |
| json.put("sex", "男"); |
| json.put("address", "北京市"); |
| |
| int size = json.size(); |
| |
| System.out.println(size); |
| |
| |
复制
﹝clear﹞
clear:清空当前对象。
| JSONObject json = new JSONObject(); |
| json.put("name", "阿柴"); |
| json.put("age", 18); |
| json.put("sex", "男"); |
| json.put("address", "北京市"); |
| |
| json.clear(); |
| |
| System.out.println(json); |
复制
﹝equals﹞
equals:比较方法,可以和 JSONObject,Map 进行比较。
| JSONObject json = new JSONObject(); |
| json.put("name", "阿柴"); |
| |
| Map<String, String> map = new HashMap<>(); |
| map.put("name", "阿柴"); |
| |
| boolean equals = json.equals(map); |
| |
| System.out.println(equals); |
| |
| |
复制
﹝forEach﹞
forEach:对 JSONObject 对象进行遍历。这里要注意在 forEach 方法中,对 key,value 做更改是不会影响原对象的。
| JSONObject json = new JSONObject(); |
| json.put("name", "阿柴"); |
| json.put("age", 18); |
| json.put("sex", "男"); |
| json.put("address", "北京市"); |
| |
| json.forEach((key, value) -> System.out.println(key + "---" + value)); |
| |
| |
| |
| |
| |
复制
﹝hashCode﹞
hashCode:获取哈希值。
| JSONObject json = new JSONObject(); |
| json.put("name", "阿柴"); |
| json.put("age", 18); |
| json.put("sex", "男"); |
| json.put("address", "北京市"); |
| |
| int hashCode = json.hashCode(); |
| |
| System.out.println(hashCode); |
| |
| |
复制
﹝replaceAll﹞
replaceAll:对所有的值进行替换。
| JSONObject json = new JSONObject(); |
| json.put("name", "阿柴"); |
| json.put("age", 18); |
| json.put("sex", "男"); |
| json.put("address", "北京市"); |
| |
| json.replaceAll((key, value) -> value + "Test"); |
| System.out.println(json); |
| |
| |
复制
﹝parseObject﹞﹝static﹞
将字符串反序列化成 Java 对象。
| JSONObject jsonObject = new JSONObject(); |
| jsonObject.put("msg", "黑柴是阿柴的妹妹!"); |
| |
| Message message = JSONObject.parseObject(jsonObject.toJSONString(), Message.class); |
| |
| System.out.println(message); |
复制
﹝toJSONString﹞﹝static﹞
将对象序列化成字符串。
| JSONObject jsonObject = new JSONObject(); |
| jsonObject.put("msg", "黑柴是阿柴的妹妹!"); |
| |
| String message = JSONObject.toJSONString(jsonObject); |
| |
| System.out.println(message); |
复制
﹝of﹞﹝static﹞
快速生成 JSONObject 对象。
| JSONObject of = JSONObject.of("1", "1", "2", "2", "3", "3"); |
| |
| System.out.println(of); |
复制
﹝parse﹞﹝static﹞
将字符串序列化成 JSONObject 对象。和 parseObject 作用一样,但是可以传入 JSONReader.Feature。
| String str = "{\"1\":\"1\",\"2\":\"2\",\"3\":\"3\"}"; |
| |
| JSONObject parse = JSONObject.parse(str); |
| |
| System.out.println(parse); |
复制
『@JSONField』
@JSONField
是 FastJson 库中的一个注解,用于控制 JSON 序列化和反序列化时的行为。通过在 Java 类的属性上添加 @JSONField
注解,可以指定属性在 JSON 中的名称、顺序、格式化方式等。
@JSONField 的属性
属性 | 作用 |
---|
name | 【序列化】会将原本的属性变成name设置的属性。 |
alternateNames | 【反序列化】会将别名的属性一同反序列化。 |
defaultValue | 【反序列化】如果没有匹配值,会设置默认值。 |
ordinal | 【序列化】指定序列化的顺序。【没有设置该属性的字段默认为0】 |
format | 【序列化】用在Date属性上,自动格式化日期。 |
jsonDirect | 【序列化】针对值为json字符串的字段,,true则序列化, 默认为false,不序列化的话会有转义符存在。 |
label | 【序列化】可以给属性设置标签,这样可以批量处理某一类的属性,不设置则默认序列化。 |
serialize | 【序列化】可以指定字段是否参加序列化,默认为True,当为False的时候不参加序列化。 |
deserialize | 【反序列化】可以指定字段是否参加反序列化,默认为True,当为False的时候不参加反序列化。 |
serializeFeatures | 【序列化】可以制定序列化的特点。 |
deserializeFeatures | 【反序列化】可以制定反序列化的特点。 |
serializeUsing | 【序列化】可以自定义序列化。 |
deserializeUsing | 【反序列化】可以自定义反序列化。 |
unwrapped | 【序列化】若为Ture,相当于把子对象的属性,添加到父对象中,不再显示子对象。默认为false。 |
总体示例
| import com.alibaba.fastjson2.JSON; |
| import com.fasterxml.jackson.core.JsonProcessingException; |
| import com.ruoyi.project.missionnel.bean.Message; |
| import com.ruoyi.project.missionnel.bean.Student; |
| |
| import java.util.Date; |
| |
| public class Main { |
| public static void main(String[] args) throws JsonProcessingException { |
| |
| Student student = Student.builder() |
| .name("阿柴") |
| .age(20) |
| .gender("男") |
| .job("自由工作者") |
| .address("北京市中南海") |
| .brother("黑柴") |
| .sister("白柴") |
| .birthday(new Date()) |
| .json("{\"word\":\"xxx\"}") |
| .message(new Message("获得了录取通知书!")) |
| .build(); |
| |
| String serializeStr = JSON.toJSONString(student); |
| System.out.println(serializeStr); |
| |
| Student studentNew = JSON.parseObject(serializeStr, Student.class); |
| System.out.println(studentNew); |
| |
| } |
| } |
复制
label 属性示例
| import com.alibaba.fastjson2.JSON; |
| import com.alibaba.fastjson2.filter.Labels; |
| import com.fasterxml.jackson.core.JsonProcessingException; |
| import com.ruoyi.project.missionnel.bean.Student; |
| |
| public class Main { |
| public static void main(String[] args) throws JsonProcessingException { |
| |
| Student student = Student.builder() |
| .name("阿柴") |
| .age(20) |
| .build(); |
| |
| String serializeStr = JSON.toJSONString(student, Labels.excludes("ELS")); |
| System.out.println(serializeStr); |
| |
| } |
| } |
复制
alternateNames 属性示例
| import com.alibaba.fastjson2.JSON; |
| import com.fasterxml.jackson.core.JsonProcessingException; |
| import com.ruoyi.project.missionnel.bean.Student; |
| |
| public class Main { |
| public static void main(String[] args) throws JsonProcessingException { |
| |
| String str ="{\"myName\":\"汉族\",\"age\":18}"; |
| |
| Student student = JSON.parseObject(str, Student.class); |
| System.out.println(student); |
| |
| } |
| } |
| |
复制
『NameFilter』
NameFilter是FastJson中的一个接口,用于在序列化和反序列化过程中控制字段的命名。通过实现NameFilter接口,可以自定义字段的命名规则,从而实现特定的需求。
本身在银行工作,对接数据源,不同的数据源序列化的字段是不一样的,所以有了下面的案例。根据不同的Type,序列化成不同的值。
﹝Main﹞
| import com.alibaba.fastjson2.JSON; |
| import com.fasterxml.jackson.core.JsonProcessingException; |
| import com.ruoyi.project.missionnel.annotate.AliasFilter; |
| import com.ruoyi.project.missionnel.bean.Message; |
| |
| public class Main { |
| public static void main(String[] args) throws JsonProcessingException { |
| |
| Message message = new Message("阿柴会吃肉!"); |
| String small = JSON.toJSONString(message, new AliasFilter("Small")); |
| String big = JSON.toJSONString(message, new AliasFilter("Big")); |
| System.out.println(small); |
| System.out.println(big); |
| |
| |
| |
| |
| } |
| } |
复制
﹝Another Method﹞
| import com.alibaba.fastjson2.JSON; |
| import com.alibaba.fastjson2.filter.NameFilter; |
| import com.fasterxml.jackson.core.JsonProcessingException; |
| import com.ruoyi.project.missionnel.bean.User; |
| |
| import static com.alibaba.fastjson2.PropertyNamingStrategy.UpperCase; |
| |
| public class Main { |
| public static void main(String[] args) throws JsonProcessingException { |
| |
| |
| NameFilter camelCase = NameFilter.of(UpperCase); |
| |
| |
| NameFilter filter = (Object object, String name, Object value) -> "test" + name; |
| |
| |
| NameFilter compose = NameFilter.compose(filter, camelCase); |
| |
| User user = new User().setName("阿柴").setAge("18").setSex("男").setHeight("186"); |
| |
| String jsonString = JSON.toJSONString(user, compose); |
| |
| System.out.println(jsonString); |
| |
| |
| |
| } |
| } |
复制
『ValueFilter』
ValueFilter 和 NameFilter的使用方式一样,不再做其他展示。
| import com.alibaba.fastjson2.JSON; |
| import com.alibaba.fastjson2.filter.ValueFilter; |
| import com.ruoyi.project.missionnel.bean.User; |
| |
| public class Main { |
| public static void main(String[] args) { |
| |
| ValueFilter pre = (Object object, String name, Object value) -> "pre_" + value; |
| ValueFilter suf = (Object object, String name, Object value) -> value + "_suf"; |
| |
| |
| ValueFilter filter = ValueFilter.compose(pre, suf); |
| |
| User user = new User().setName("阿柴").setAge("18").setSex("男").setHeight("186"); |
| |
| String jsonString = JSON.toJSONString(user, filter); |
| |
| System.out.println(jsonString); |
| |
| |
| |
| } |
| } |
复制