Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
io.quarkus.devui.runtime.jsonrpc.json.JsonMapper Maven / Gradle / Ivy
package io.quarkus.devui.runtime.jsonrpc.json;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;
import io.quarkus.dev.console.DeploymentLinker;
public interface JsonMapper {
String toString(Object object, boolean pretty);
T fromString(String json, Class target);
T fromValue(Object json, Class target);
static DeploymentLinker deploymentLinker() {
return new DeploymentLinker<>() {
@Override
public Map createLinkData(JsonMapper object) {
return Map.of("delegate", object,
"toString", (BiFunction) object::toString,
"fromString", (BiFunction, Object>) object::fromString,
"fromValue", (BiFunction, Object>) object::fromValue);
}
@Override
@SuppressWarnings("unchecked")
public JsonMapper createLink(Map linkData) {
Object delegate = linkData.get("delegate");
BiFunction toString = (BiFunction) linkData.get("toString");
BiFunction, Object> fromString = (BiFunction, Object>) linkData
.get("fromString");
BiFunction, Object> fromValue = (BiFunction, Object>) linkData
.get("fromValue");
return new JsonMapper() {
@Override
public String toString() {
return "JsonMapper[delegate=" + delegate + "]";
}
@Override
public String toString(Object object, boolean pretty) {
return toString.apply(object, pretty);
}
@Override
public T fromString(String json, Class target) {
return target.cast(fromString.apply(json, target));
}
@Override
public T fromValue(Object json, Class target) {
if (target.isPrimitive()) {
return (T) json;
} else {
return target.cast(fromValue.apply(json, target));
}
}
};
}
};
}
interface Factory {
/**
* Creates the mapper, delegating to the deployment to configure and implement it.
*
* We can't implement it in the runtime because we don't have a dependency to Jackson in the runtime.
*
* @return A JSON mapper implemented in the deployment module.
*/
JsonMapper create(JsonTypeAdapter, Map> jsonObjectAdapter,
JsonTypeAdapter, List>> jsonArrayAdapter,
JsonTypeAdapter, String> bufferAdapter);
static DeploymentLinker deploymentLinker() {
return new DeploymentLinker<>() {
@Override
public Map createLinkData(Factory object) {
return Map.of(
"delegate", object,
"create",
(Function, Map>) args -> {
var created = object.create(typeAdapterFromLinkData(args.get("jsonObjectAdapter")),
typeAdapterFromLinkData(args.get("jsonArrayAdapter")),
typeAdapterFromLinkData(args.get("bufferAdapter")));
return JsonMapper.deploymentLinker().createLinkData(created);
});
}
@Override
@SuppressWarnings("unchecked")
public Factory createLink(Map linkData) {
Object delegate = linkData.get("delegate");
Function, Map> create = (Function, Map>) linkData
.get("create");
return new Factory() {
@Override
public String toString() {
return "JsonMapper[delegate=" + delegate + "]";
}
@Override
public JsonMapper create(JsonTypeAdapter, Map> jsonObjectAdapter,
JsonTypeAdapter, List>> jsonArrayAdapter,
JsonTypeAdapter, String> bufferAdapter) {
var linkData = create.apply(Map.of("jsonObjectAdapter", typeAdapterToLinkData(jsonObjectAdapter),
"jsonArrayAdapter", typeAdapterToLinkData(jsonArrayAdapter),
"bufferAdapter", typeAdapterToLinkData(bufferAdapter)));
return JsonMapper.deploymentLinker().createLink(linkData);
}
};
}
private Map typeAdapterToLinkData(JsonTypeAdapter, ?> object) {
return Map.of("type", object.type,
"serializer", object.serializer,
"deserializer", object.deserializer);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private JsonTypeAdapter typeAdapterFromLinkData(Object linkData) {
Map map = (Map) linkData;
return new JsonTypeAdapter<>((Class) map.get("type"),
(Function) map.get("serializer"),
(Function) map.get("deserializer"));
}
};
}
}
}