Java内容重新整理删除过期的东西

This commit is contained in:
estom
2025-09-14 03:49:42 -04:00
parent 9b8524ff80
commit 885b795e45
413 changed files with 643 additions and 1340 deletions

View File

@@ -0,0 +1,45 @@
package cn.aofeng.demo.json.gson;
import com.google.gson.Gson;
/**
* 数组的反序列化。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class ArrayDeserialize {
public <T> T deserialize(String json, Class<T> claz) {
Gson gson = new Gson();
return gson.fromJson(json, claz);
}
public static void main(String[] args) {
ArrayDeserialize ad = new ArrayDeserialize();
// 整型数组
String intArrJson = "[9,7,5]";
int[] intArr = ad.deserialize(intArrJson, int[].class);
System.out.println("---------- 整型数组 ----------");
for (int i : intArr) {
System.out.println(i);
}
// 字符串数组
String strArrJson = "[\"张三\",\"李四\",\"王五\"]";
String[] strArr = ad.deserialize(strArrJson, String[].class);
System.out.println("---------- 字符串数组 ----------");
for (String str : strArr) {
System.out.println(str);
}
// 对象数组
String objArrJson = "[{\"name\":\"小明\",\"age\":10},{\"name\":\"马丽\",\"age\":9}]";
Person[] objArr = ad.deserialize(objArrJson, Person[].class);
System.out.println("---------- 对象数组 ----------");
for (Person person : objArr) {
System.out.println(person);
}
}
}

View File

@@ -0,0 +1,41 @@
package cn.aofeng.demo.json.gson;
import com.google.gson.Gson;
/**
* 数组的序列化。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class ArraySerialize {
public void serialize(Object[] arr) {
Gson gson = new Gson();
System.out.println( gson.toJson(arr) );
}
public static void main(String[] args) {
ArraySerialize as = new ArraySerialize();
// 整型对象数组
Integer[] intArr = new Integer[3];
intArr[0] = 9;
intArr[1] = 7;
intArr[2] = 5;
as.serialize(intArr);
// 字符串数组
String[] names = new String[3];
names[0] = "张三";
names[1] = "李四";
names[2] = "王五";
as.serialize(names);
// 对象数组
Person[] persons = new Person[2];
persons[0] = new Person("小明", 10);
persons[1] = new Person("马丽", 9);
as.serialize(persons);
}
}

View File

@@ -0,0 +1,55 @@
package cn.aofeng.demo.json.gson;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
/**
* 集合的反序列化。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class CollectionDeserialize {
public <T> T deserialize(String json, Type type) {
Gson gson = new Gson();
return gson.fromJson(json, type);
}
public static void main(String[] args) {
CollectionDeserialize cd = new CollectionDeserialize();
//整型List
String intListJson = "[9,8,0]";
List<Integer> intList = cd.deserialize( intListJson,
new TypeToken<List<Integer>>(){}.getType() );
System.out.println("---------- 整型List ----------");
for (Integer obj : intList) {
System.out.println(obj);
}
// 字符串Set
String strSetJson = "[\"Best\",\"World\",\"Hello\"]";
Set<String> strSet = cd.deserialize( strSetJson,
new TypeToken<Set<String>>(){}.getType() );
System.out.println("---------- 字符串Set ----------");
for (String str : strSet) {
System.out.println(str);
}
// Map
String objMapJson = "{\"xiaomin\":{\"name\":\"小明\",\"age\":21},\"marry\":{\"name\":\"马丽\",\"age\":20}}";
Map<String, Person> objMap = cd.deserialize( objMapJson,
new TypeToken<Map<String, Person>>(){}.getType() );
System.out.println("---------- Map ----------");
for (Entry<String, Person> entry : objMap.entrySet()) {
System.out.println(entry);
}
}
}

View File

@@ -0,0 +1,54 @@
package cn.aofeng.demo.json.gson;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.google.gson.Gson;
/**
* 集合的序列化。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class CollectionsSerialize {
public void serialize(Collection<?> c) {
Gson gson = new Gson();
System.out.println( gson.toJson(c) );
}
public void serialize(Map<?, ?> map) {
Gson gson = new Gson();
System.out.println( gson.toJson(map) );
}
public static void main(String[] args) {
CollectionsSerialize cs = new CollectionsSerialize();
// 整型List
List<Integer> intList = new ArrayList<Integer>();
intList.add(9);
intList.add(8);
intList.add(0);
cs.serialize(intList);
// 字符串Set
Set<String> strSet = new HashSet<String>();
strSet.add("Hello");
strSet.add("World");
strSet.add("Best");
cs.serialize(strSet);
// Map
Map<String, Person> objMap = new HashMap<String, Person>();
objMap.put("marry", new Person("马丽", 20));
objMap.put("xiaomin", new Person("小明", 21));
cs.serialize(objMap);
}
}

View File

@@ -0,0 +1,46 @@
package cn.aofeng.demo.json.gson;
import java.lang.reflect.Type;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
/**
* 自定义反序列化。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class CustomDeserialize {
public static void main(String[] args) {
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Person.class, new PersonDeserializer());
Gson gson = builder.create();
String json = "{\"PersonName\":\"aofeng\",\"PersonAge\":32}";
Person obj = gson.fromJson(json, Person.class);
System.out.println(obj); // 输出结果Person [name=aofeng, age=32]
}
public static class PersonDeserializer implements JsonDeserializer<Person> {
@Override
public Person deserialize(JsonElement jsonEle, Type type,
JsonDeserializationContext context)
throws JsonParseException {
JsonObject jo = jsonEle.getAsJsonObject();
String name = jo.get("PersonName").getAsString();
int age = jo.get("PersonAge").getAsInt();
Person obj = new Person(name, age);
return obj;
}
} // end of PersonDeserializer
}

View File

@@ -0,0 +1,42 @@
package cn.aofeng.demo.json.gson;
import java.lang.reflect.Type;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
/**
* 自定义序列化。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class CustomSerialize {
public static void main(String[] args) {
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Person.class, new PersonSerializer());
Gson gson = builder.create();
Person obj = new Person("aofeng", 32);
System.out.println( gson.toJson(obj) ); // 输出结果:{"PersonName":"aofeng","PersonAge":32}
}
public static class PersonSerializer implements JsonSerializer<Person> {
@Override
public JsonElement serialize(Person obj, Type type,
JsonSerializationContext context) {
JsonObject jo = new JsonObject();
jo.addProperty("PersonName", obj.getName());
jo.addProperty("PersonAge", obj.getAge());
return jo;
}
} // end of PersonSerializer
}

View File

@@ -0,0 +1,50 @@
package cn.aofeng.demo.json.gson;
/**
* 简单的Java对象。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class Person {
private String name;
private int age;
public Person() {
// nothing
}
@SuppressWarnings("unused")
private void reset() {
name = null;
age = 0;
}
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;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}

View File

@@ -0,0 +1,39 @@
package cn.aofeng.demo.json.gson;
import com.google.gson.Gson;
/**
* Java简单对象的序列化与反序列化。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class SimpleObjectSerialize {
/**
* 序列化将Java对象转换成JSON字符串。
*/
public void serialize(Person person) {
Gson gson = new Gson();
System.out.println( gson.toJson(person) );
}
/**
* 反序列化将JSON字符串转换成Java对象。
*/
public void deserialize(String json) {
Gson gson = new Gson();
Person person = gson.fromJson(json, Person.class);
System.out.println( person );
}
public static void main(String[] args) {
SimpleObjectSerialize ss = new SimpleObjectSerialize();
Person person = new Person("NieYong", 33);
ss.serialize(person);
String json = " {\"name\":\"AoFeng\",\"age\":32}";
ss.deserialize(json);
}
}