All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.tio.utils.json.JsonAdapter Maven / Gradle / Ivy

There is a newer version: 1.0.8
Show newest version
/*
 * Copyright (c) 2019-2029, Dreamlu 卢春梦 ([email protected] & dreamlu.net).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.tio.utils.json;

import java.util.List;

/**
 * json 适配器
 *
 * @author L.cm
 */
public interface JsonAdapter {

	/**
	 * 转换成 json 字符串
	 *
	 * @param object object
	 * @return json 字符串
	 */
	String toJsonString(Object object);

	/**
	 * 转换成 json byte array
	 *
	 * @param object object
	 * @return json 字符串
	 */
	default byte[] toJsonBytes(Object object) {
		String jsonString = toJsonString(object);
		return jsonString == null ? null : jsonString.getBytes();
	}

	/**
	 * 将 json 字符串转成对象
	 *
	 * @param json  json
	 * @param clazz Class
	 * @param    泛型
	 * @return 对象
	 */
	 T readValue(String json, Class clazz);

	/**
	 * 将 json byte array 转成对象
	 *
	 * @param json  json
	 * @param clazz Class
	 * @param    泛型
	 * @return 对象
	 */
	default  T readValue(byte[] json, Class clazz) {
		return readValue(new String(json), clazz);
	}

	/**
	 * 将 json 字符串转成对象
	 *
	 * @param json  json
	 * @param clazz Class
	 * @param    泛型
	 * @return 对象
	 */
	 List readList(String json, Class clazz);

	/**
	 * 将 json byte array 转成对象
	 *
	 * @param json  json
	 * @param clazz Class
	 * @param    泛型
	 * @return 对象
	 */
	default  List readList(byte[] json, Class clazz) {
		return readList(new String(json), clazz);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy