Java Code Examples for com.fasterxml.jackson.core.JsonGenerator#writeNumber()

The following examples show how to use com.fasterxml.jackson.core.JsonGenerator#writeNumber() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IntOrString.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(IntOrString value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
    if (value != null) {
        if (value.getKind() == null) {
            Integer intValue = value.getIntVal();
            if (intValue != null) {
                jgen.writeNumber(intValue);
            } else {
                String stringValue = value.getStrVal();
                if (stringValue != null) {
                    jgen.writeString(stringValue);
                } else {
                    jgen.writeNull();
                }
            }
        } else if (value.getKind() == 0) {
            jgen.writeNumber(value.getIntVal());
        } else if (value.getKind() == 1) {
            jgen.writeString(value.getStrVal());
        } else {
            jgen.writeNull();
        }
    } else {
        jgen.writeNull();
    }
}
 
Example 2
Source File: NumberSerializer.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void serialize(Number value, JsonGenerator g, SerializerProvider provider) throws IOException
{
    // should mostly come in as one of these two:
    if (value instanceof BigDecimal) {
        g.writeNumber((BigDecimal) value);
    } else if (value instanceof BigInteger) {
        g.writeNumber((BigInteger) value);
        
    // These should not occur, as more specific methods should have been called; but
    // just in case let's cover all bases:
    } else if (value instanceof Long) {
        g.writeNumber(value.longValue());
    } else if (value instanceof Double) {
        g.writeNumber(value.doubleValue());
    } else if (value instanceof Float) {
        g.writeNumber(value.floatValue());
    } else if (value instanceof Integer || value instanceof Byte || value instanceof Short) {
        g.writeNumber(value.intValue()); // doesn't need to be cast to smaller numbers
    } else {
        // We'll have to use fallback "untyped" number write method
        g.writeNumber(value.toString());
    }
}
 
Example 3
Source File: Bean84ATable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute columnArrayChar serialization
 */
public static byte[] serializeColumnArrayChar(Character[] value) {
  if (value==null) {
    return null;
  }
  KriptonJsonContext context=KriptonBinder.jsonBind();
  try (KriptonByteArrayOutputStream stream=new KriptonByteArrayOutputStream(); JacksonWrapperSerializer wrapper=context.createSerializer(stream)) {
    JsonGenerator jacksonSerializer=wrapper.jacksonGenerator;
    jacksonSerializer.writeStartObject();
    int fieldCount=0;
    if (value!=null)  {
      fieldCount++;
      int n=value.length;
      Character item;
      // write wrapper tag
      jacksonSerializer.writeFieldName("element");
      jacksonSerializer.writeStartArray();
      for (int i=0; i<n; i++) {
        item=value[i];
        if (item==null) {
          jacksonSerializer.writeNull();
        } else {
          jacksonSerializer.writeNumber(item);
        }
      }
      jacksonSerializer.writeEndArray();
    }
    jacksonSerializer.writeEndObject();
    jacksonSerializer.flush();
    return stream.toByteArray();
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 4
Source File: StateOptions.java    From java-sdk with MIT License 5 votes vote down vote up
@Override
public void serialize(
    Duration duration,
    JsonGenerator jsonGenerator,
    SerializerProvider serializerProvider) throws IOException {
  jsonGenerator.writeNumber(duration.toMillis());
}
 
Example 5
Source File: OptimizedBooleanSerializer.java    From java-master with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(Boolean aBoolean, JsonGenerator jsonGenerator,
                      SerializerProvider serializerProvider)
        throws IOException {

    if (aBoolean) {
        jsonGenerator.writeNumber(1);
    } else {
        jsonGenerator.writeNumber(0);
    }
}
 
Example 6
Source File: ZonedDateTimeSerializer.java    From fusionauth-jwt with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ZonedDateTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
  if (value == null) {
    jgen.writeNull();
  } else {
    jgen.writeNumber(value.toEpochSecond());
  }
}
 
Example 7
Source File: RoundingFloatSerializer.java    From act-platform with ISC License 5 votes vote down vote up
@Override
public void serialize(Float value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
  gen.writeNumber(BigDecimal.valueOf(value)
          .setScale(DECIMAL_POINTS, RoundingMode.HALF_UP)
          .floatValue()
  );
}
 
Example 8
Source File: LongIterableSerializer.java    From jackson-datatypes-collections with Apache License 2.0 5 votes vote down vote up
@Override
protected void serializeContents(LongIterable value, JsonGenerator gen) throws IOException {
    LongIterator iterator = value.longIterator();
    while (iterator.hasNext()) {
        gen.writeNumber(iterator.next());
    }
}
 
Example 9
Source File: IntegerBeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute value serialization
 */
public static byte[] serializeValue(List<Integer> value) {
  if (value==null) {
    return null;
  }
  KriptonJsonContext context=KriptonBinder.jsonBind();
  try (KriptonByteArrayOutputStream stream=new KriptonByteArrayOutputStream(); JacksonWrapperSerializer wrapper=context.createSerializer(stream)) {
    JsonGenerator jacksonSerializer=wrapper.jacksonGenerator;
    jacksonSerializer.writeStartObject();
    int fieldCount=0;
    if (value!=null)  {
      fieldCount++;
      int n=value.size();
      Integer item;
      // write wrapper tag
      jacksonSerializer.writeFieldName("element");
      jacksonSerializer.writeStartArray();
      for (int i=0; i<n; i++) {
        item=value.get(i);
        if (item==null) {
          jacksonSerializer.writeNull();
        } else {
          jacksonSerializer.writeNumber(item);
        }
      }
      jacksonSerializer.writeEndArray();
    }
    jacksonSerializer.writeEndObject();
    jacksonSerializer.flush();
    return stream.toByteArray();
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 10
Source File: ByteBeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute value serialization
 */
public static byte[] serializeValue(List<Byte> value) {
  if (value==null) {
    return null;
  }
  KriptonJsonContext context=KriptonBinder.jsonBind();
  try (KriptonByteArrayOutputStream stream=new KriptonByteArrayOutputStream(); JacksonWrapperSerializer wrapper=context.createSerializer(stream)) {
    JsonGenerator jacksonSerializer=wrapper.jacksonGenerator;
    jacksonSerializer.writeStartObject();
    int fieldCount=0;
    if (value!=null)  {
      fieldCount++;
      int n=value.size();
      Byte item;
      // write wrapper tag
      jacksonSerializer.writeFieldName("element");
      jacksonSerializer.writeStartArray();
      for (int i=0; i<n; i++) {
        item=value.get(i);
        if (item==null) {
          jacksonSerializer.writeNull();
        } else {
          jacksonSerializer.writeNumber(item);
        }
      }
      jacksonSerializer.writeEndArray();
    }
    jacksonSerializer.writeEndObject();
    jacksonSerializer.flush();
    return stream.toByteArray();
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 11
Source File: LocalDateSerializer.java    From jackson-modules-java8 with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(LocalDate date, JsonGenerator g, SerializerProvider provider) throws IOException
{
    if (useTimestamp(provider)) {
        if (_shape == JsonFormat.Shape.NUMBER_INT) {
            g.writeNumber(date.toEpochDay());
        } else {
            g.writeStartArray();
            _serializeAsArrayContents(date, g, provider);
            g.writeEndArray();
        }
    } else {
        g.writeString((_formatter == null) ? date.toString() : date.format(_formatter));
    }
}
 
Example 12
Source File: JsonUtil.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public void writeJsonValue(JsonGenerator jsonGenerator, Block block, int position, ConnectorSession session)
        throws IOException
{
    if (block.isNull(position)) {
        jsonGenerator.writeNull();
    }
    else {
        long value = type.getLong(block, position);
        jsonGenerator.writeNumber(value);
    }
}
 
Example 13
Source File: BindBeanSharedPreferences.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute valueByteSet serialization
 */
protected String serializeValueByteSet(Set<Byte> value) {
  if (value==null) {
    return null;
  }
  KriptonJsonContext context=KriptonBinder.jsonBind();
  try (KriptonByteArrayOutputStream stream=new KriptonByteArrayOutputStream(); JacksonWrapperSerializer wrapper=context.createSerializer(stream)) {
    JsonGenerator jacksonSerializer=wrapper.jacksonGenerator;
    jacksonSerializer.writeStartObject();
    int fieldCount=0;
    if (value!=null)  {
      fieldCount++;
      // write wrapper tag
      jacksonSerializer.writeFieldName("valueByteSet");
      jacksonSerializer.writeStartArray();
      for (Byte item: value) {
        if (item==null) {
          jacksonSerializer.writeNull();
        } else {
          jacksonSerializer.writeNumber(item);
        }
      }
      jacksonSerializer.writeEndArray();
    }
    jacksonSerializer.writeEndObject();
    jacksonSerializer.flush();
    return stream.toString();
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 14
Source File: CharDaoImpl.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for param serializer1 serialization
 */
private byte[] serializer1(char[] value) {
  if (value==null) {
    return null;
  }
  KriptonJsonContext context=KriptonBinder.jsonBind();
  try (KriptonByteArrayOutputStream stream=new KriptonByteArrayOutputStream(); JacksonWrapperSerializer wrapper=context.createSerializer(stream)) {
    JsonGenerator jacksonSerializer=wrapper.jacksonGenerator;
    int fieldCount=0;
    jacksonSerializer.writeStartObject();
    if (value!=null)  {
      int n=value.length;
      char item;
      // write wrapper tag
      jacksonSerializer.writeFieldName("element");
      jacksonSerializer.writeStartArray();
      for (int i=0; i<n; i++) {
        item=value[i];
        jacksonSerializer.writeNumber(item);
      }
      jacksonSerializer.writeEndArray();
    }
    jacksonSerializer.writeEndObject();
    jacksonSerializer.flush();
    return stream.toByteArray();
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 15
Source File: GenderSerializer.java    From weixin-sdk with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(Gender gender, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
    jsonGenerator.writeNumber(gender.getCode());
}
 
Example 16
Source File: RestExecutor.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @param tok Token to process.
 * @param p Parser.
 * @param gen Generator.
 */
private void writeToken(JsonToken tok, JsonParser p, JsonGenerator gen) throws IOException {
    switch (tok) {
        case FIELD_NAME:
            gen.writeFieldName(p.getText());
            break;

        case START_ARRAY:
            gen.writeStartArray();
            break;

        case END_ARRAY:
            gen.writeEndArray();
            break;

        case START_OBJECT:
            gen.writeStartObject();
            break;

        case END_OBJECT:
            gen.writeEndObject();
            break;

        case VALUE_NUMBER_INT:
            gen.writeNumber(p.getBigIntegerValue());
            break;

        case VALUE_NUMBER_FLOAT:
            gen.writeNumber(p.getDecimalValue());
            break;

        case VALUE_TRUE:
            gen.writeBoolean(true);
            break;

        case VALUE_FALSE:
            gen.writeBoolean(false);
            break;

        case VALUE_NULL:
            gen.writeNull();
            break;

        default:
            gen.writeString(p.getText());
    }
}
 
Example 17
Source File: ObjectMapperFactory.java    From kafka-connect-splunk with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
  long time = date.getTime();
  BigDecimal value = BigDecimal.valueOf(time, 3);
  jsonGenerator.writeNumber(value);
}
 
Example 18
Source File: JacksonAutoConfiguration.java    From open-cloud with MIT License 4 votes vote down vote up
@Override
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
    jgen.writeNumber(0);
}
 
Example 19
Source File: DateToLongSerializer.java    From x7 with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(Date date, JsonGenerator jsonGenerator,
                      SerializerProvider serializerProvider) throws IOException {
    jsonGenerator.writeNumber(date.getTime() );
}
 
Example 20
Source File: EthJsonModule.java    From incubator-tuweni with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(Instant value, JsonGenerator gen, SerializerProvider provider) throws IOException {
  gen.writeNumber(value.toEpochMilli());
}