JSONWriter写入文件工具类、Fastjson 读取文件并get、set入实体类

文章介绍了使用Fastjson库进行文件操作,包括将List对象序列化到JSON文件和从JSON文件反序列化对象的方法,涉及到对象属性的读取和写入处理。
摘要由CSDN通过智能技术生成
/**
	 * fastjson写入方法
	 * @param list 数据
	 * @param jsonWriter 生成JSONWriter对象
	 * @throws NoSuchMethodException
	 * @throws SecurityException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 * @throws InvocationTargetException
	 */
	public static void writeToFile(List<?> list, JSONWriter jsonWriter) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
			
		for (Object object : list) {
			jsonWriter.startObject();
			Class<? extends Object> model = object.getClass();
			Field[] fields = model.getDeclaredFields();
			for(Field field : fields) {
				//属性名称
				String fieldName = field.getName();
				//get方法执行结果
				Object getMethodResult = invokeGetMethod(fieldName,model,object);
				if(!(getMethodResult instanceof Date)) {
					jsonWriter.writeKey(fieldName);
					jsonWriter.writeObject(getMethodResult);
				} else {
					if(getMethodResult != null) {
						jsonWriter.writeKey(fieldName);
						jsonWriter.writeObject(DateUtils.formatDateTime((Date)getMethodResult));
					}
				}
			}
			jsonWriter.endObject();
		}
	}
/**
	 * fastjson 读取方法
	 * @param object 实体类
	 * @param jsonReader 
     *InputStreamReader inputStreamReader = new InputStreamReader(new                 FileInputStream("D:\\dataBackups\\cangku.dat"),"utf-8");
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
			JSONReader jsonReader = new JSONReader(bufferedReader);
     *
     *
	 * @return
	 * @throws NoSuchMethodException
	 * @throws SecurityException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 * @throws InvocationTargetException
	 * @throws NoSuchFieldException 
	 */
	public static Object readByFile(Object object, JSONReader jsonReader) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException{
		Class<? extends Object> model = object.getClass();
		//开始解析json对象
		jsonReader.startObject();
		while(jsonReader.hasNext()) {
			String key = jsonReader.readString();
			Field field = model.getDeclaredField(key);
			if (field != null) {
				//属性名称
				String fieldName = field.getName();
				//属性类型 
				Class<?> fieldType = field.getType();
				if (field.getName().equals(key)) {
					//执行set方法
					invokeSetMethod(fieldName,fieldType,model,object,jsonReader.readString());
				} else {
					jsonReader.readObject();	//读取对象,保证在添加字段之后不会因为文件中和对象字段对不上出错
				}
			}
		}
		//结束解析对象
        jsonReader.endObject();
		return object;
	}

	/**
	 *	跟据文件路径和文件名生成jsonWriter
	 * @param jsonPath 文件路径
	 * @param fileName 文件名称
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws FileNotFoundException
	 */	
	public static JSONWriter getJsonWrite(String jsonPath, String fileName) throws UnsupportedEncodingException, FileNotFoundException {
		File dir = new File(jsonPath);
		if (!dir.exists())
			dir.mkdirs();
		
		File jsonFile = new File(dir + File.separator + fileName + ".dat");
		OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(jsonFile),"utf-8");
		BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
		JSONWriter jsonWriter = new JSONWriter(bufferedWriter);
		return jsonWriter;
	}
//拼接并执行get方法
	private static Object invokeGetMethod(String fieldName, Class<? extends Object> model, Object object) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		//拼接方法名
		String firstCharUpper = fieldName.substring(0, 1).toUpperCase();
		String invokeMethodName = "get" + firstCharUpper + fieldName.substring(1);
		//获取方法
		Method pendingInvokeMethod = model.getMethod(invokeMethodName);
		//执行方法
		Object invokeResult = pendingInvokeMethod.invoke(object);
		return invokeResult;
	}
	
	//拼接并执行set方法
	private static void invokeSetMethod(String fieldName, Class<?> fieldType, Class<? extends Object> model, Object object,String readString) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		//拼接方法名
		String firstCharUpper = fieldName.substring(0, 1).toUpperCase();
		String invokeMethodName = "set" + firstCharUpper + fieldName.substring(1);
		if(fieldType.isAssignableFrom(String.class)) {
			//获取方法
			Method pendingInvokeMethod = model.getMethod(invokeMethodName,String.class);
			//执行方法
			pendingInvokeMethod.invoke(object, readString);
		} else if (fieldType.isAssignableFrom(Date.class)) {
			//获取方法
			Method pendingInvokeMethod = model.getMethod(invokeMethodName,Date.class);
			pendingInvokeMethod.invoke(object, DateUtils.parseDate(readString));
		}
	}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值