diff --git a/xiuzhen-common/pom.xml b/xiuzhen-common/pom.xml index 63a5dc7f85476cacaba7720fcda1cc4436516ef8..9bc415df3acae06470b0b2e28c6def325f739412 100644 --- a/xiuzhen-common/pom.xml +++ b/xiuzhen-common/pom.xml @@ -33,6 +33,13 @@ compile + + + com.alibaba + fastjson + 2.0.7 + + com.far diff --git a/xiuzhen-common/src/main/java/com/far/game/xiuzhen/common/utils/json/FileFactory.java b/xiuzhen-common/src/main/java/com/far/game/xiuzhen/common/utils/json/FileFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..b91c99189f6dce51974ac10ee37cdefed32cdc92 --- /dev/null +++ b/xiuzhen-common/src/main/java/com/far/game/xiuzhen/common/utils/json/FileFactory.java @@ -0,0 +1,19 @@ +package com.far.game.xiuzhen.common.utils.json; + +import java.util.Map; + +public class FileFactory { + + private Map fileWrapperMap; + + public FileFactory() { + } + + public FileFactory(Map fileWrapperMap) { + this.fileWrapperMap = fileWrapperMap; + } + + public FileWrapper getFileWrapper(String dirName) { + return fileWrapperMap.get(dirName); + } +} diff --git a/xiuzhen-common/src/main/java/com/far/game/xiuzhen/common/utils/json/FileWrapper.java b/xiuzhen-common/src/main/java/com/far/game/xiuzhen/common/utils/json/FileWrapper.java new file mode 100644 index 0000000000000000000000000000000000000000..cfd9ed7b174dadc0767de4db18b043b685ecf0a2 --- /dev/null +++ b/xiuzhen-common/src/main/java/com/far/game/xiuzhen/common/utils/json/FileWrapper.java @@ -0,0 +1,43 @@ +package com.far.game.xiuzhen.common.utils.json; + +import com.far.tools.interf.func.NewCallble; + + +public class FileWrapper { + + private PropData propData; + + public FileWrapper() {} + + public FileWrapper(PropData propData) { + this.propData = propData; + } + + public PropData getPropData() { + return propData; + } + + /** + * 指定json文件名,生成对应类 + * @param fileName 类名 + * @param newCallble 函数接口 + * @return Java映射对象 + */ + public T getObj(String fileName, NewCallble newCallble) { + T module = newCallble.newCall(); + cn.hutool.core.bean.BeanUtil.copyProperties(propData.getKeyMap().get(fileName), module); + return module; + } + + /** + * 生成对应类,json文件名要求与类名保持一致 + * @param newCallble 函数接口 + * @return java映射对象 + */ + public T getObj(NewCallble newCallble) { + T module = newCallble.newCall(); + String name = module.getClass().getTypeName(); + cn.hutool.core.bean.BeanUtil.copyProperties(propData.getKeyMap().get(name.substring(name.lastIndexOf(".") + 1)+".json"), module); + return module; + } +} diff --git a/xiuzhen-common/src/main/java/com/far/game/xiuzhen/common/utils/json/PropData.java b/xiuzhen-common/src/main/java/com/far/game/xiuzhen/common/utils/json/PropData.java new file mode 100644 index 0000000000000000000000000000000000000000..f3ae6a233a67d5174b04da3bf310d9983ab97960 --- /dev/null +++ b/xiuzhen-common/src/main/java/com/far/game/xiuzhen/common/utils/json/PropData.java @@ -0,0 +1,34 @@ +package com.far.game.xiuzhen.common.utils.json; + +import com.alibaba.fastjson.JSONObject; + +import java.util.List; +import java.util.Map; + +public class PropData { + + private List keys; + + private Map keyObj; + + public PropData(List keys, Map keyObj) { + this.keys = keys; + this.keyObj = keyObj; + } + + public List getKeys() { + return keys; + } + + public void setKeys(List keys) { + this.keys = keys; + } + + public Map getKeyMap() { + return keyObj; + } + + public void setKeyObj(Map keyObj) { + this.keyObj = keyObj; + } +} diff --git a/xiuzhen-common/src/main/java/com/far/game/xiuzhen/common/utils/json/PropDataUtil.java b/xiuzhen-common/src/main/java/com/far/game/xiuzhen/common/utils/json/PropDataUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..2b8139f25f03b413cf605d9459e790b32ee58c14 --- /dev/null +++ b/xiuzhen-common/src/main/java/com/far/game/xiuzhen/common/utils/json/PropDataUtil.java @@ -0,0 +1,112 @@ +package com.far.game.xiuzhen.common.utils.json; + +import com.alibaba.fastjson.JSONObject; +import com.far.game.xiuzhen.common.utils.json.test.Staff; +import com.far.game.xiuzhen.common.utils.json.test.Student; +import com.far.game.xiuzhen.common.utils.json.test.Teacher; + +import java.io.InputStream; +import java.util.*; + +/** + * json文件-->JSONObject转换工具 + */ +public class PropDataUtil { + + public PropDataUtil(){} + + /** + * 初始化单个目录 + * @param dirName 目录名 + * @return 文件包装器 + */ + public static FileWrapper init(String dirName) { + return new FileWrapper(PropDataUtil.TransformJsonPropData(dirName)); + } + + /** + * 初始化多个目录 + * @param dirNames 多个目录 + * @return 文件工厂对象 + */ + public static FileFactory init(String... dirNames) { + Map fileWrapperMap = new HashMap<>(); + for (String dirName : dirNames) { + fileWrapperMap.put(dirName,new FileWrapper(PropDataUtil.TransformJsonPropData(dirName))); + } + return new FileFactory(fileWrapperMap); + } + + /** + * 通过目录名,获取PropData对象 + * @param dirName 目录名 + * @return PropData,包含List keys,Map keyObj; + */ + private static PropData TransformJsonPropData(String dirName){ + + if (dirName.isEmpty()) { + throw new RuntimeException("目录名为空~~~"); + } + InputStream dir = Thread.currentThread().getContextClassLoader().getResourceAsStream(dirName); + + if (dir == null) { + throw new RuntimeException(dirName + "下没有json文件"); + } + + List jsonFileNameList = new ArrayList<>(); + + try(Scanner scanner = new Scanner(dir)) { + while (scanner.hasNextLine()) { + jsonFileNameList.add(scanner.nextLine()); + } + } catch (Exception e) { + e.printStackTrace(); + } + + if (jsonFileNameList.isEmpty()) { + throw new RuntimeException(dirName + "下没有json文件"); + } + + Map jsonObjectMap = new HashMap<>(); + + for (String jsonFileName : jsonFileNameList) { + InputStream jsonFile = Thread.currentThread().getContextClassLoader().getResourceAsStream(dirName +"/"+ jsonFileName); + if (jsonFile != null) { + try(Scanner scanner = new Scanner(jsonFile)) { + while (scanner.hasNextLine()) { + JSONObject jsonObject = JSONObject.parseObject(scanner.nextLine()); + jsonObjectMap.put(jsonFileName,jsonObject); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + } + return new PropData(jsonFileNameList, jsonObjectMap); + } + + + public static void main(String[] args) { + + FileFactory fileFactory = PropDataUtil.init("school","company"); + + FileWrapper companyWrapper = fileFactory.getFileWrapper("company"); + Staff staff = companyWrapper.getObj(Staff::new); + System.out.println(staff); + + FileWrapper schoolWrapper = fileFactory.getFileWrapper("school"); + Student student = schoolWrapper.getObj(Student::new); + System.out.println(student); + + Teacher teacher = schoolWrapper.getObj(Teacher::new); + System.out.println(teacher); + + System.out.println(); + System.out.println(); + PropData schoolPropData = schoolWrapper.getPropData(); + System.out.println(schoolPropData.getKeys()); + System.out.println(schoolPropData.getKeyMap()); + + } +} diff --git a/xiuzhen-common/src/main/java/com/far/game/xiuzhen/common/utils/json/test/Staff.java b/xiuzhen-common/src/main/java/com/far/game/xiuzhen/common/utils/json/test/Staff.java new file mode 100644 index 0000000000000000000000000000000000000000..2a64549da965b5e7a04537ede31056edba9cb9c0 --- /dev/null +++ b/xiuzhen-common/src/main/java/com/far/game/xiuzhen/common/utils/json/test/Staff.java @@ -0,0 +1,10 @@ +package com.far.game.xiuzhen.common.utils.json.test; + +import lombok.Data; + +@Data +public class Staff { + private String name; + + private String position; +} diff --git a/xiuzhen-common/src/main/java/com/far/game/xiuzhen/common/utils/json/test/Student.java b/xiuzhen-common/src/main/java/com/far/game/xiuzhen/common/utils/json/test/Student.java new file mode 100644 index 0000000000000000000000000000000000000000..199595ee3653969ecf773a364e91c88ea96259aa --- /dev/null +++ b/xiuzhen-common/src/main/java/com/far/game/xiuzhen/common/utils/json/test/Student.java @@ -0,0 +1,11 @@ +package com.far.game.xiuzhen.common.utils.json.test; + +import lombok.Data; + +@Data +public class Student { + private String name; + + private String age; + +} diff --git a/xiuzhen-common/src/main/java/com/far/game/xiuzhen/common/utils/json/test/Teacher.java b/xiuzhen-common/src/main/java/com/far/game/xiuzhen/common/utils/json/test/Teacher.java new file mode 100644 index 0000000000000000000000000000000000000000..2d1e4f3c7b78538460386d15c1e0bfe78ba151f1 --- /dev/null +++ b/xiuzhen-common/src/main/java/com/far/game/xiuzhen/common/utils/json/test/Teacher.java @@ -0,0 +1,12 @@ +package com.far.game.xiuzhen.common.utils.json.test; + +import lombok.Data; + +@Data +public class Teacher { + private String name; + + private String age; + + private String phone; +} diff --git a/xiuzhen-common/src/main/resources/company/Staff.json b/xiuzhen-common/src/main/resources/company/Staff.json new file mode 100644 index 0000000000000000000000000000000000000000..8437d0f28911678a22ff4dfddcca8787f4699d42 --- /dev/null +++ b/xiuzhen-common/src/main/resources/company/Staff.json @@ -0,0 +1 @@ +{"name":"王五","position": "员工"} diff --git a/xiuzhen-common/src/main/resources/school/Student.json b/xiuzhen-common/src/main/resources/school/Student.json new file mode 100644 index 0000000000000000000000000000000000000000..29c0cfada5a109ccde4860acdf87ebe57871ac44 --- /dev/null +++ b/xiuzhen-common/src/main/resources/school/Student.json @@ -0,0 +1 @@ +{"name":"lisi","age": 20} diff --git a/xiuzhen-common/src/main/resources/school/Teacher.json b/xiuzhen-common/src/main/resources/school/Teacher.json new file mode 100644 index 0000000000000000000000000000000000000000..d5dd76fc0d0ed5742150240d1e309eedb484e63d --- /dev/null +++ b/xiuzhen-common/src/main/resources/school/Teacher.json @@ -0,0 +1 @@ +{"name":"zhangsan","age": 23,"phone": "15720984436"}