114 lines
2.3 KiB
Java
114 lines
2.3 KiB
Java
package cn.palmte.work;
|
|
|
|
import java.util.function.Function;
|
|
|
|
/**
|
|
* @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
|
|
* @since 2.0 2022/12/30 15:38
|
|
*/
|
|
|
|
public class Json implements Result {
|
|
|
|
private Object data;
|
|
private String message;
|
|
private boolean success;
|
|
|
|
public Object getData() {
|
|
return data;
|
|
}
|
|
|
|
public boolean isSuccess() {
|
|
return success;
|
|
}
|
|
|
|
public String getMessage() {
|
|
return message;
|
|
}
|
|
|
|
public Json data(Object data) {
|
|
this.data = data;
|
|
return this;
|
|
}
|
|
|
|
public Json message(String message) {
|
|
this.message = message;
|
|
return this;
|
|
}
|
|
|
|
public Json success(boolean success) {
|
|
this.success = success;
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Apply the common {@link Json} result
|
|
*
|
|
* @param func the {@link Function}
|
|
* @param param parameter
|
|
*/
|
|
public static <T> Json apply(Function<T, Boolean> func, T param) {
|
|
if (func.apply(param)) {
|
|
return Json.ok();
|
|
}
|
|
return Json.failed();
|
|
}
|
|
|
|
public static <T> Json apply(boolean success) {
|
|
if (success) {
|
|
return Json.ok();
|
|
}
|
|
return Json.failed();
|
|
}
|
|
|
|
/**
|
|
* @param success if success
|
|
* @param message the message of the response
|
|
* @param data response data
|
|
*/
|
|
public static Json create(boolean success, String message, Object data) {
|
|
return new Json()
|
|
.data(data)
|
|
.message(message)
|
|
.success(success);
|
|
}
|
|
|
|
public static Json ok() {
|
|
return create(true, "ok", null);
|
|
}
|
|
|
|
public static Json ok(String message, Object data) {
|
|
return create(true, message, data);
|
|
}
|
|
|
|
public static Json ok(Object data) {
|
|
return create(true, "ok", data);
|
|
}
|
|
|
|
public static Json ok(String message) {
|
|
return create(true, message, null);
|
|
}
|
|
|
|
public static Json failed() {
|
|
return create(false, "失败", null);
|
|
}
|
|
|
|
public static Json failed(String message) {
|
|
return create(false, message, null);
|
|
}
|
|
|
|
public static Json failed(String message, Object data) {
|
|
return create(false, message, data);
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return new StringBuilder()//
|
|
.append("{\"message\":\"").append(message)//
|
|
.append("\",\"data\":\"").append(data)//
|
|
.append("\",\"success\":\"").append(success)//
|
|
.append("\"}")//
|
|
.toString();
|
|
}
|
|
}
|
|
|