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

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> jsonObjectAdapter, JsonTypeAdapter> jsonArrayAdapter, JsonTypeAdapter 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> jsonObjectAdapter, JsonTypeAdapter> jsonArrayAdapter, JsonTypeAdapter 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")); } }; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy