Search in sources :

Example 66 with GsonBuilder

use of com.google.gson.GsonBuilder in project streamsx.health by IBMStreams.

the class JsonToModelConverter method initGson.

private void initGson() {
    GsonBuilder builder = new GsonBuilder();
    gson = builder.create();
}
Also used : GsonBuilder(com.google.gson.GsonBuilder)

Example 67 with GsonBuilder

use of com.google.gson.GsonBuilder in project streamsx.health by IBMStreams.

the class ModelToJsonConverter method initGson.

private void initGson() {
    GsonBuilder builder = new GsonBuilder();
    gson = builder.create();
}
Also used : GsonBuilder(com.google.gson.GsonBuilder)

Example 68 with GsonBuilder

use of com.google.gson.GsonBuilder in project streamsx.health by IBMStreams.

the class ModelToJsonConverter method initGson.

private void initGson() {
    GsonBuilder builder = new GsonBuilder();
    gson = builder.create();
}
Also used : GsonBuilder(com.google.gson.GsonBuilder)

Example 69 with GsonBuilder

use of com.google.gson.GsonBuilder in project CorfuDB by CorfuDB.

the class QCLayout method main.

public static String[] main(String[] args) {
    if (args != null && args.length > 0 && args[0].contentEquals("reboot")) {
        LayoutServer ls = CorfuServer.getLayoutServer();
        if (ls != null) {
            ls.shutdown();
            CorfuServer.addLayoutServer();
            return replyOk();
        } else {
            return replyErr("No active layout server");
        }
    }
    // Parse the options given, using docopt.
    Map<String, Object> opts = new Docopt(USAGE).withVersion(GitRepositoryState.getRepositoryState().describe).parse(args);
    // Configure base options
    // configureBase(opts);
    // Parse host address and port
    String addressport = (String) opts.get("<address>:<port>");
    String host = addressport.split(":")[0];
    Integer port = Integer.parseInt(addressport.split(":")[1]);
    String qapp = (String) opts.get("<qapp>");
    String addressportPrefix = "";
    if (qapp != null) {
        addressportPrefix = qapp;
    }
    NettyClientRouter router;
    if ((router = routers.get(addressportPrefix + addressport)) == null) {
        // Create a client router and get layout.
        log.trace("Creating router for {} ++ {}:{}", addressportPrefix, port);
        router = new NettyClientRouter(host, port);
        router.addClient(new BaseClient()).addClient(new LayoutClient()).start();
        routers.putIfAbsent(addressportPrefix + addressport, router);
    }
    router = routers.get(addressportPrefix + addressport);
    Long epoch = 0L;
    if (opts.get("--epoch") != null) {
        epoch = Long.parseLong((String) opts.get("--epoch"));
        log.trace("Specify router's epoch as " + epoch);
        router.setEpoch(epoch);
    } else {
        try {
            Layout l = router.getClient(LayoutClient.class).getLayout().get();
            if (l != null) {
                log.trace("Set router's epoch to " + l.getEpoch());
                router.setEpoch(l.getEpoch());
            } else {
                log.trace("Cannot set router's epoch");
            }
        } catch (Exception e) {
            return replyErr("ERROR Exception getting initial epoch " + e.getCause());
        }
    }
    if ((Boolean) opts.get("getClientID")) {
        String clientID = router.getClientID().toString();
        return replyOk(clientID);
    } else if ((Boolean) opts.get("query")) {
        try {
            Layout l = router.getClient(LayoutClient.class).getLayout().get();
            Gson gs = new GsonBuilder().setPrettyPrinting().create();
            return replyOk("layout: " + gs.toJson(l));
        } catch (ExecutionException ex) {
            if (ex.getCause().getClass() == WrongEpochException.class) {
                WrongEpochException we = (WrongEpochException) ex.getCause();
                return replyErr("Exception during query", ex.getCause().toString(), "correctEpoch: " + we.getCorrectEpoch(), "stack: " + ExceptionUtils.getStackTrace(ex));
            } else {
                return replyErr("Exception during query", ex.getCause().toString(), "stack: " + ExceptionUtils.getStackTrace(ex));
            }
        } catch (Exception e) {
            return replyErr("ERROR Exception getting layout" + e);
        }
    } else if ((Boolean) opts.get("bootstrap")) {
        Layout l = getLayout(opts);
        log.debug("Bootstrapping with layout {}", l);
        try {
            if (router.getClient(LayoutClient.class).bootstrapLayout(l).get()) {
                router.getClient(ManagementClient.class).bootstrapManagement(l).get();
                return replyOk();
            } else {
                return replyErr("NACK");
            }
        } catch (ExecutionException ex) {
            return replyErr("Exception bootstrapping layout", ex.getCause().toString());
        } catch (Exception e) {
            return replyErr("Exception bootstrapping layout", e.toString());
        }
    } else if ((Boolean) opts.get("set_epoch")) {
        log.debug("Set epoch with new epoch={}", epoch);
        try {
            CorfuRuntime rt;
            if ((rt = setEpochRTs.get(addressport)) == null) {
                log.trace("Creating CorfuRuntime for set_epoch for {} ", addressport);
                rt = new CorfuRuntime().addLayoutServer(addressport);
                setEpochRTs.putIfAbsent(addressport, rt);
            }
            rt = setEpochRTs.get(addressport);
            // Construct a layout that contains just enough to allow .moveServersToEpoch()
            // to send SET_EPOCH to our desired endpoint.
            List<String> ls = new ArrayList(1);
            ls.add(addressport);
            List<String> none1 = new ArrayList(0);
            List<Layout.LayoutSegment> none2 = new ArrayList(0);
            Layout tmpLayout = new Layout(ls, none1, none2, epoch);
            tmpLayout.setRuntime(rt);
            tmpLayout.moveServersToEpoch();
            return replyOk();
        } catch (WrongEpochException we) {
            return replyErr("Exception during set_epoch", we.getCause() == null ? "WrongEpochException" : we.getCause().toString(), "correctEpoch: " + we.getCorrectEpoch(), "stack: " + ExceptionUtils.getStackTrace(we));
        } catch (Exception e) {
            return replyErr("Exception during set_epoch", e.toString(), ExceptionUtils.getStackTrace(e));
        }
    } else if ((Boolean) opts.get("prepare")) {
        long rank = Long.parseLong((String) opts.get("--rank"));
        log.debug("Prepare with new rank={}", rank);
        try {
            LayoutPrepareResponse r = router.getClient(LayoutClient.class).prepare(epoch, rank).get();
            Layout r_layout = r.getLayout();
            if (r_layout == null) {
                return replyOk("ignored: ignored");
            } else {
                return replyOk("layout: " + r_layout.asJSONString());
            }
        } catch (ExecutionException ex) {
            if (ex.getCause().getClass() == OutrankedException.class) {
                OutrankedException oe = (OutrankedException) ex.getCause();
                return replyErr("Exception during prepare", ex.getCause().toString(), "newRank: " + Long.toString(oe.getNewRank()), "layout: " + (oe.getLayout() == null ? "" : oe.getLayout().asJSONString()));
            } else if (ex.getCause().getClass() == WrongEpochException.class) {
                WrongEpochException we = (WrongEpochException) ex.getCause();
                return replyErr("Exception during prepare", ex.getCause().toString(), "correctEpoch: " + we.getCorrectEpoch(), "stack: " + ExceptionUtils.getStackTrace(ex));
            } else {
                return replyErr("Exception during prepare", ex.getCause().toString(), "stack: " + ExceptionUtils.getStackTrace(ex));
            }
        } catch (Exception e) {
            return replyErr("Exception during prepare", e.toString(), ExceptionUtils.getStackTrace(e));
        }
    } else if ((Boolean) opts.get("propose")) {
        long rank = Long.parseLong((String) opts.get("--rank"));
        Layout l = getLayout(opts);
        log.debug("Propose with new rank={}, layout={}", rank, l);
        try {
            if (router.getClient(LayoutClient.class).propose(l.getEpoch(), rank, l).get()) {
                return replyOk();
            } else {
                return replyErr("NACK");
            }
        } catch (ExecutionException ex) {
            if (ex.getCause().getClass() == OutrankedException.class) {
                OutrankedException oe = (OutrankedException) ex.getCause();
                return replyErr("Exception during propose", ex.getCause().toString(), "newRank: " + Long.toString(oe.getNewRank()), "stack: " + ExceptionUtils.getStackTrace(ex));
            } else if (ex.getCause().getClass() == WrongEpochException.class) {
                WrongEpochException we = (WrongEpochException) ex.getCause();
                return replyErr("Exception during propose", ex.getCause().toString(), "correctEpoch: " + we.getCorrectEpoch(), "stack: " + ExceptionUtils.getStackTrace(ex));
            } else {
                return replyErr("Exception during propose", ex.getCause().toString(), "stack: " + ExceptionUtils.getStackTrace(ex));
            }
        } catch (Exception e) {
            return replyErr("Exception during propose", e.toString(), "stack: " + ExceptionUtils.getStackTrace(e));
        }
    } else if ((Boolean) opts.get("committed")) {
        long rank = Long.parseLong((String) opts.get("--rank"));
        Layout l = getLayout(opts);
        log.debug("Propose with new rank={}", rank);
        try {
            if (router.getClient(LayoutClient.class).committed(l.getEpoch(), l).get()) {
                return replyOk();
            } else {
                return replyErr("NACK");
            }
        } catch (ExecutionException ex) {
            if (ex.getCause().getClass() == WrongEpochException.class) {
                WrongEpochException we = (WrongEpochException) ex.getCause();
                return replyErr("Exception during commit", ex.getCause().toString(), "correctEpoch: " + we.getCorrectEpoch(), "stack: " + ExceptionUtils.getStackTrace(ex));
            } else {
                return replyErr("Exception during commit", ex.getCause().toString(), "stack: " + ExceptionUtils.getStackTrace(ex));
            }
        } catch (Exception e) {
            return replyErr("Exception during commit", e.toString(), "stack: " + ExceptionUtils.getStackTrace(e));
        }
    }
    return replyErr("Hush, compiler.");
}
Also used : NettyClientRouter(org.corfudb.runtime.clients.NettyClientRouter) GsonBuilder(com.google.gson.GsonBuilder) LayoutServer(org.corfudb.infrastructure.LayoutServer) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) BaseClient(org.corfudb.runtime.clients.BaseClient) LayoutClient(org.corfudb.runtime.clients.LayoutClient) OutrankedException(org.corfudb.runtime.exceptions.OutrankedException) ExecutionException(java.util.concurrent.ExecutionException) WrongEpochException(org.corfudb.runtime.exceptions.WrongEpochException) OutrankedException(org.corfudb.runtime.exceptions.OutrankedException) Docopt(org.docopt.Docopt) Layout(org.corfudb.runtime.view.Layout) LayoutPrepareResponse(org.corfudb.protocols.wireprotocol.LayoutPrepareResponse) WrongEpochException(org.corfudb.runtime.exceptions.WrongEpochException) CorfuRuntime(org.corfudb.runtime.CorfuRuntime) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException)

Example 70 with GsonBuilder

use of com.google.gson.GsonBuilder in project tika by apache.

the class JsonMetadataBase method prettyInit.

static Gson prettyInit() {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeHierarchyAdapter(Metadata.class, new SortedJsonMetadataSerializer());
    builder.registerTypeHierarchyAdapter(Metadata.class, new JsonMetadataDeserializer());
    builder.setPrettyPrinting();
    return builder.create();
}
Also used : GsonBuilder(com.google.gson.GsonBuilder)

Aggregations

GsonBuilder (com.google.gson.GsonBuilder)1067 Gson (com.google.gson.Gson)803 IOException (java.io.IOException)185 Test (org.junit.Test)141 ArrayList (java.util.ArrayList)101 JsonObject (com.google.gson.JsonObject)90 File (java.io.File)80 JsonElement (com.google.gson.JsonElement)78 HashMap (java.util.HashMap)67 List (java.util.List)62 Map (java.util.Map)59 Retrofit (retrofit2.Retrofit)56 Type (java.lang.reflect.Type)52 FileNotFoundException (java.io.FileNotFoundException)42 TypeToken (com.google.gson.reflect.TypeToken)40 ResponseBody (okhttp3.ResponseBody)39 FileOutputStream (java.io.FileOutputStream)38 Call (retrofit2.Call)38 JsonSyntaxException (com.google.gson.JsonSyntaxException)37 JsonParser (com.google.gson.JsonParser)36