Example usage for com.fasterxml.jackson.core JsonGenerator writeObjectFieldStart

List of usage examples for com.fasterxml.jackson.core JsonGenerator writeObjectFieldStart

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonGenerator writeObjectFieldStart.

Prototype

public final void writeObjectFieldStart(String fieldName) throws IOException, JsonGenerationException 

Source Link

Document

Convenience method for outputting a field entry ("member") (that will contain a JSON Object value), and the START_OBJECT marker.

Usage

From source file:org.apache.flink.benchmark.Runner.java

public static void main(String[] args) throws Exception {
    final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    env.getConfig().enableObjectReuse();
    env.getConfig().disableSysoutLogging();

    ParameterTool parameters = ParameterTool.fromArgs(args);

    if (!(parameters.has("p") && parameters.has("types") && parameters.has("algorithms"))) {
        printUsage();/*  w ww .  j a  va  2  s.co m*/
        System.exit(-1);
    }

    int parallelism = parameters.getInt("p");
    env.setParallelism(parallelism);

    Set<IdType> types = new HashSet<>();

    if (parameters.get("types").equals("all")) {
        types.add(IdType.INT);
        types.add(IdType.LONG);
        types.add(IdType.STRING);
    } else {
        for (String type : parameters.get("types").split(",")) {
            if (type.toLowerCase().equals("int")) {
                types.add(IdType.INT);
            } else if (type.toLowerCase().equals("long")) {
                types.add(IdType.LONG);
            } else if (type.toLowerCase().equals("string")) {
                types.add(IdType.STRING);
            } else {
                printUsage();
                throw new RuntimeException("Unknown type: " + type);
            }
        }
    }

    Queue<RunnerWithScore> queue = new PriorityQueue<>();

    if (parameters.get("algorithms").equals("all")) {
        for (Map.Entry<String, Class> entry : AVAILABLE_ALGORITHMS.entrySet()) {
            for (IdType type : types) {
                AlgorithmRunner runner = (AlgorithmRunner) entry.getValue().newInstance();
                runner.initialize(type, SAMPLES, parallelism);
                runner.warmup(env);
                queue.add(new RunnerWithScore(runner, 1.0));
            }
        }
    } else {
        for (String algorithm : parameters.get("algorithms").split(",")) {
            double ratio = 1.0;
            if (algorithm.contains("=")) {
                String[] split = algorithm.split("=");
                algorithm = split[0];
                ratio = Double.parseDouble(split[1]);
            }

            if (AVAILABLE_ALGORITHMS.containsKey(algorithm.toLowerCase())) {
                Class clazz = AVAILABLE_ALGORITHMS.get(algorithm.toLowerCase());

                for (IdType type : types) {
                    AlgorithmRunner runner = (AlgorithmRunner) clazz.newInstance();
                    runner.initialize(type, SAMPLES, parallelism);
                    runner.warmup(env);
                    queue.add(new RunnerWithScore(runner, ratio));
                }
            } else {
                printUsage();
                throw new RuntimeException("Unknown algorithm: " + algorithm);
            }
        }
    }

    JsonFactory factory = new JsonFactory();

    while (queue.size() > 0) {
        RunnerWithScore current = queue.poll();
        AlgorithmRunner runner = current.getRunner();

        StringWriter writer = new StringWriter();
        JsonGenerator gen = factory.createGenerator(writer);
        gen.writeStartObject();
        gen.writeStringField("algorithm", runner.getClass().getSimpleName());

        boolean running = true;

        while (running) {
            try {
                runner.run(env, gen);
                running = false;
            } catch (ProgramInvocationException e) {
                // only suppress job cancellations
                if (!(e.getCause() instanceof JobCancellationException)) {
                    throw e;
                }
            }
        }

        JobExecutionResult result = env.getLastJobExecutionResult();

        long runtime_ms = result.getNetRuntime();
        gen.writeNumberField("runtime_ms", runtime_ms);
        current.credit(runtime_ms);

        if (!runner.finished()) {
            queue.add(current);
        }

        gen.writeObjectFieldStart("accumulators");
        for (Map.Entry<String, Object> accumulatorResult : result.getAllAccumulatorResults().entrySet()) {
            gen.writeStringField(accumulatorResult.getKey(), accumulatorResult.getValue().toString());
        }
        gen.writeEndObject();

        gen.writeEndObject();
        gen.close();
        System.out.println(writer.toString());
    }
}

From source file:com.google.openrtb.json.AbstractOpenRtbJsonWriter.java

private static boolean openExt(JsonGenerator gen, boolean openExt) throws IOException {
    if (!openExt) {
        gen.writeObjectFieldStart("ext");
    }/*from  ww w .j  av a 2 s .  c o m*/
    return true;
}

From source file:com.netflix.hystrix.contrib.sample.stream.HystrixUtilizationJsonStream.java

private static void writeCommandUtilizationJson(JsonGenerator json, HystrixCommandKey key,
        HystrixCommandUtilization utilization) throws IOException {
    json.writeObjectFieldStart(key.name());
    json.writeNumberField("activeCount", utilization.getConcurrentCommandCount());
    json.writeEndObject();/*  w ww.  j a  v a  2s  . c  om*/
}

From source file:com.netflix.hystrix.serial.SerialHystrixUtilization.java

private static void serializeUtilization(HystrixUtilization utilization, JsonGenerator json) {
    try {//from   w  w w. j a v  a2 s  .c o  m
        json.writeStartObject();
        json.writeStringField("type", "HystrixUtilization");
        json.writeObjectFieldStart("commands");
        for (Map.Entry<HystrixCommandKey, HystrixCommandUtilization> entry : utilization
                .getCommandUtilizationMap().entrySet()) {
            final HystrixCommandKey key = entry.getKey();
            final HystrixCommandUtilization commandUtilization = entry.getValue();
            writeCommandUtilizationJson(json, key, commandUtilization);

        }
        json.writeEndObject();

        json.writeObjectFieldStart("threadpools");
        for (Map.Entry<HystrixThreadPoolKey, HystrixThreadPoolUtilization> entry : utilization
                .getThreadPoolUtilizationMap().entrySet()) {
            final HystrixThreadPoolKey threadPoolKey = entry.getKey();
            final HystrixThreadPoolUtilization threadPoolUtilization = entry.getValue();
            writeThreadPoolUtilizationJson(json, threadPoolKey, threadPoolUtilization);
        }
        json.writeEndObject();
        json.writeEndObject();
        json.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.netflix.hystrix.contrib.sample.stream.HystrixUtilizationJsonStream.java

private static void writeThreadPoolUtilizationJson(JsonGenerator json, HystrixThreadPoolKey threadPoolKey,
        HystrixThreadPoolUtilization utilization) throws IOException {
    json.writeObjectFieldStart(threadPoolKey.name());
    json.writeNumberField("activeCount", utilization.getCurrentActiveCount());
    json.writeNumberField("queueSize", utilization.getCurrentQueueSize());
    json.writeNumberField("corePoolSize", utilization.getCurrentCorePoolSize());
    json.writeNumberField("poolSize", utilization.getCurrentPoolSize());
    json.writeEndObject();//from   w  ww  .  jav a  2  s.  c om
}

From source file:net.logstash.logback.composite.JsonWritingUtils.java

/**
 * Writes a map as String fields to the generator if and only if the fieldName and values are not null.
 *///from w ww  . j  a v  a2s  .  c o m
public static void writeMapStringFields(JsonGenerator generator, String fieldName, Map<String, String> map)
        throws IOException, JsonMappingException {
    if (shouldWriteField(fieldName) && map != null && !map.isEmpty()) {
        generator.writeObjectFieldStart(fieldName);
        for (Map.Entry<String, String> entry : map.entrySet()) {
            writeStringField(generator, entry.getKey(), entry.getValue());
        }
        generator.writeEndObject();
    }
}

From source file:com.netflix.hystrix.contrib.sample.stream.HystrixUtilizationJsonStream.java

protected static String convertToJson(HystrixUtilization utilization) throws IOException {
    StringWriter jsonString = new StringWriter();
    JsonGenerator json = jsonFactory.createGenerator(jsonString);

    json.writeStartObject();/*from  w ww . j  a  v  a 2 s  . c o m*/
    json.writeStringField("type", "HystrixUtilization");
    json.writeObjectFieldStart("commands");
    for (Map.Entry<HystrixCommandKey, HystrixCommandUtilization> entry : utilization.getCommandUtilizationMap()
            .entrySet()) {
        final HystrixCommandKey key = entry.getKey();
        final HystrixCommandUtilization commandUtilization = entry.getValue();
        writeCommandUtilizationJson(json, key, commandUtilization);

    }
    json.writeEndObject();

    json.writeObjectFieldStart("threadpools");
    for (Map.Entry<HystrixThreadPoolKey, HystrixThreadPoolUtilization> entry : utilization
            .getThreadPoolUtilizationMap().entrySet()) {
        final HystrixThreadPoolKey threadPoolKey = entry.getKey();
        final HystrixThreadPoolUtilization threadPoolUtilization = entry.getValue();
        writeThreadPoolUtilizationJson(json, threadPoolKey, threadPoolUtilization);
    }
    json.writeEndObject();
    json.writeEndObject();
    json.close();

    return jsonString.getBuffer().toString();
}

From source file:com.netflix.hystrix.serial.SerialHystrixConfiguration.java

private static void serializeConfiguration(HystrixConfiguration config, JsonGenerator json) {
    try {// w  w  w  .j av a 2s.c om
        json.writeStartObject();
        json.writeStringField("type", "HystrixConfig");
        json.writeObjectFieldStart("commands");
        for (Map.Entry<HystrixCommandKey, HystrixCommandConfiguration> entry : config.getCommandConfig()
                .entrySet()) {
            final HystrixCommandKey key = entry.getKey();
            final HystrixCommandConfiguration commandConfig = entry.getValue();
            writeCommandConfigJson(json, key, commandConfig);

        }
        json.writeEndObject();

        json.writeObjectFieldStart("threadpools");
        for (Map.Entry<HystrixThreadPoolKey, HystrixThreadPoolConfiguration> entry : config
                .getThreadPoolConfig().entrySet()) {
            final HystrixThreadPoolKey threadPoolKey = entry.getKey();
            final HystrixThreadPoolConfiguration threadPoolConfig = entry.getValue();
            writeThreadPoolConfigJson(json, threadPoolKey, threadPoolConfig);
        }
        json.writeEndObject();

        json.writeObjectFieldStart("collapsers");
        for (Map.Entry<HystrixCollapserKey, HystrixCollapserConfiguration> entry : config.getCollapserConfig()
                .entrySet()) {
            final HystrixCollapserKey collapserKey = entry.getKey();
            final HystrixCollapserConfiguration collapserConfig = entry.getValue();
            writeCollapserConfigJson(json, collapserKey, collapserConfig);
        }
        json.writeEndObject();
        json.writeEndObject();
        json.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}

From source file:com.cedarsoft.couchdb.io.CouchDocSerializer.java

private static <T> void serializeInlineAttachments(@Nonnull CouchDoc<T> doc, @Nonnull JsonGenerator generator)
        throws IOException {
    if (!doc.hasInlineAttachments()) {
        return;/*from  ww w  .j  a v  a2  s .  c  o m*/
    }

    generator.writeObjectFieldStart(PROPERTY_ATTACHMENTS);

    for (CouchDoc.Attachment attachment : doc.getAttachments()) {
        if (!attachment.isInline()) {
            throw new IllegalStateException("Cannot serialize non-inline attachments: " + attachment);
        }
        generator.writeObjectFieldStart(attachment.getId().asString());

        generator.writeStringField(PROPERTY_CONTENT_TYPE, attachment.getContentType().toString());
        generator.writeStringField(PROPERTY_DATA, new String(Base64.encode(attachment.getData())));

        generator.writeEndObject();
    }

    generator.writeEndObject();
}

From source file:com.netflix.hystrix.contrib.sample.stream.HystrixConfigurationJsonStream.java

private static void writeCollapserConfigJson(JsonGenerator json, HystrixCollapserKey collapserKey,
        HystrixCollapserConfiguration collapserConfig) throws IOException {
    json.writeObjectFieldStart(collapserKey.name());
    json.writeNumberField("maxRequestsInBatch", collapserConfig.getMaxRequestsInBatch());
    json.writeNumberField("timerDelayInMilliseconds", collapserConfig.getTimerDelayInMilliseconds());
    json.writeBooleanField("requestCacheEnabled", collapserConfig.isRequestCacheEnabled());
    json.writeObjectFieldStart("metrics");
    HystrixCollapserConfiguration.CollapserMetricsConfig metricsConfig = collapserConfig
            .getCollapserMetricsConfig();
    json.writeNumberField("percentileBucketSizeInMilliseconds",
            metricsConfig.getRollingPercentileBucketSizeInMilliseconds());
    json.writeNumberField("percentileBucketCount", metricsConfig.getRollingCounterNumberOfBuckets());
    json.writeBooleanField("percentileEnabled", metricsConfig.isRollingPercentileEnabled());
    json.writeNumberField("counterBucketSizeInMilliseconds",
            metricsConfig.getRollingCounterBucketSizeInMilliseconds());
    json.writeNumberField("counterBucketCount", metricsConfig.getRollingCounterNumberOfBuckets());
    json.writeEndObject();/*from   w ww.j  a va2 s.  c  o  m*/
    json.writeEndObject();
}