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

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

Introduction

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

Prototype

public abstract void writeNumber(String encodedValue)
        throws IOException, JsonGenerationException, UnsupportedOperationException;

Source Link

Document

Write method that can be used for custom numeric types that can not be (easily?) converted to "standard" Java number types.

Usage

From source file:org.example.testcases.BasicTypeArraysSerializer.java

private void writeArray_double(JsonGenerator jg, double[] array) throws IOException {
    jg.writeStartArray();/*from  w w w .ja v  a 2  s .  c o  m*/
    for (double val : array) {
        jg.writeNumber(val);
    }
    jg.writeEndArray();
}

From source file:org.dbrain.data.jackson.serializers.JsonLongSerializer.java

@Override
public void serialize(Number value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
    if (value != null) {
        long longValue = value.longValue();
        if (longValue >= MIN_VALUE && longValue <= MAX_VALUE) {
            jgen.writeNumber(longValue);
        } else {//from ww w .  j  a  v a 2 s.  co  m
            jgen.writeString(value.toString());
        }
    } else {
        jgen.writeNull();
    }
}

From source file:com.googlecode.wickedcharts.highcharts.jackson.MinorTickIntervalSerializer.java

@Override
public void serialize(final MinorTickInterval value, final JsonGenerator jgen,
        final SerializerProvider provider) throws IOException, JsonProcessingException {
    if (value.getAuto()) {
        jgen.writeString("auto");
    } else if (value.getInterval() != null) {
        jgen.writeNumber(value.getInterval().doubleValue());
    } else if (value.isNull()) {
        jgen.writeNull();/* ww w.  ja va2 s .c  om*/
    } else {
        throw new IllegalStateException("Invalid state of TickInterval: " + value);
    }

}

From source file:com.cedarsoft.serialization.jackson.NumberSerializer.java

@Override
public void serialize(@Nonnull JsonGenerator serializeTo, @Nonnull Number object,
        @Nonnull Version formatVersion) throws IOException, VersionException, JsonProcessingException {
    if (isIntegerNumber(object)) {
        serializeTo.writeNumber(object.longValue());
    } else {//from  w  w  w . j av  a  2  s.c  o m
        serializeTo.writeNumber(object.doubleValue());
    }
}

From source file:ijfx.service.overlay.io.OverlaySerializer.java

private void writeDoubleArray(JsonGenerator jg, Double[] array) throws IOException {
    jg.writeStartArray();/*from w  w  w. j a  v  a2s . c  om*/
    for (Double n : array) {
        jg.writeNumber(n);
    }
    jg.writeEndArray();
}

From source file:org.killbill.billing.plugin.meter.timeline.consumer.TimelineChunkDecoded.java

@JsonValue
@Override/*www .  j a  v  a2 s .c om*/
public String toString() {
    try {
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        final JsonGenerator generator = objectMapper.getJsonFactory().createJsonGenerator(out);
        generator.writeStartObject();

        generator.writeFieldName("metric");
        generator.writeNumber(chunk.getMetricId());

        generator.writeFieldName("decodedSamples");
        generator.writeString(getDecodedSamples());

        generator.writeEndObject();
        generator.close();
        return out.toString();
    } catch (IOException e) {
        log.error("IOException in toString()", e);
    }

    return null;
}

From source file:com.googlecode.wickedcharts.highcharts.jackson.PixelOrPercentSerializer.java

@Override
public void serialize(final PixelOrPercent value, final JsonGenerator jgen, final SerializerProvider provider)
        throws IOException, JsonProcessingException {

    switch (value.getUnit()) {
    case PERCENT:
        jgen.writeString(value.getValue() + "%");
        break;//from ww  w .j ava  2 s  .  c  o m
    case PIXELS:
        jgen.writeNumber(value.getValue());
        break;
    default:
        throw new RuntimeException("Invalid Unit: " + value.getUnit());
    }
}

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

private static void convertExecutionToJson(JsonGenerator json,
        HystrixRequestEvents.ExecutionSignature executionSignature, List<Integer> latencies)
        throws IOException {
    json.writeStartObject();/*from  w ww  .j  a v  a2 s  .  c  o m*/
    json.writeStringField("name", executionSignature.getCommandName());
    json.writeArrayFieldStart("events");
    ExecutionResult.EventCounts eventCounts = executionSignature.getEventCounts();
    for (HystrixEventType eventType : HystrixEventType.values()) {
        if (!eventType.equals(HystrixEventType.COLLAPSED)) {
            if (eventCounts.contains(eventType)) {
                int eventCount = eventCounts.getCount(eventType);
                if (eventCount > 1) {
                    json.writeStartObject();
                    json.writeStringField("name", eventType.name());
                    json.writeNumberField("count", eventCount);
                    json.writeEndObject();
                } else {
                    json.writeString(eventType.name());
                }
            }
        }
    }
    json.writeEndArray();
    json.writeArrayFieldStart("latencies");
    for (int latency : latencies) {
        json.writeNumber(latency);
    }
    json.writeEndArray();
    if (executionSignature.getCachedCount() > 0) {
        json.writeNumberField("cached", executionSignature.getCachedCount());
    }
    if (executionSignature.getEventCounts().contains(HystrixEventType.COLLAPSED)) {
        json.writeObjectFieldStart("collapsed");
        json.writeStringField("name", executionSignature.getCollapserKey().name());
        json.writeNumberField("count", executionSignature.getCollapserBatchSize());
        json.writeEndObject();
    }
    json.writeEndObject();
}

From source file:com.netflix.hystrix.contrib.requests.stream.HystrixRequestEventsJsonStream.java

private static void convertExecutionToJson(JsonGenerator json,
        HystrixRequestEvents.ExecutionSignature executionSignature, List<Integer> latencies)
        throws IOException {
    json.writeStartObject();/*from w  w  w  .  j a v a  2 s  .com*/
    json.writeStringField("name", executionSignature.getCommandName());
    json.writeArrayFieldStart("events");
    ExecutionResult.EventCounts eventCounts = executionSignature.getEventCounts();
    for (HystrixEventType eventType : HystrixEventType.values()) {
        if (eventType != HystrixEventType.COLLAPSED) {
            if (eventCounts.contains(eventType)) {
                int eventCount = eventCounts.getCount(eventType);
                if (eventCount > 1) {
                    json.writeStartObject();
                    json.writeStringField("name", eventType.name());
                    json.writeNumberField("count", eventCount);
                    json.writeEndObject();
                } else {
                    json.writeString(eventType.name());
                }
            }
        }
    }
    json.writeEndArray();
    json.writeArrayFieldStart("latencies");
    for (int latency : latencies) {
        json.writeNumber(latency);
    }
    json.writeEndArray();
    if (executionSignature.getCachedCount() > 0) {
        json.writeNumberField("cached", executionSignature.getCachedCount());
    }
    if (executionSignature.getEventCounts().contains(HystrixEventType.COLLAPSED)) {
        json.writeObjectFieldStart("collapsed");
        json.writeStringField("name", executionSignature.getCollapserKey().name());
        json.writeNumberField("count", executionSignature.getCollapserBatchSize());
        json.writeEndObject();
    }
    json.writeEndObject();
}

From source file:de.terrestris.shogun.serializer.LeanBaseModelSetSerializer.java

@Override
public void serialize(Set set, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonProcessingException {
    jgen.writeStartArray();//from   w w  w .ja v  a  2  s.  c  o m
    for (Iterator iterator = set.iterator(); iterator.hasNext();) {
        BaseModelInterface object = (BaseModelInterface) iterator.next();
        jgen.writeNumber(object.getId());
    }
    jgen.writeEndArray();
}