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

The following examples show how to use com.fasterxml.jackson.core.JsonGenerator#writeNull() . 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: SplitSerDe.java    From aws-athena-query-federation with Apache License 2.0 6 votes vote down vote up
@Override
protected void doSerialize(Split split, JsonGenerator jgen, SerializerProvider provider)
        throws IOException
{
    jgen.writeFieldName(SPILL_LOCATION_FIELD);
    spillLocationSerializer.serialize(split.getSpillLocation(), jgen, provider);

    jgen.writeFieldName(ENCRYPTION_KEY_FIELD);
    if (split.getEncryptionKey() != null) {
        encryptionKeySerializer.serialize(split.getEncryptionKey(), jgen, provider);
    }
    else {
        jgen.writeNull();
    }

    jgen.writeObjectFieldStart(PROPERTIES_FIELD);
    for (Map.Entry<String, String> entry : split.getProperties().entrySet()) {
        jgen.writeFieldName(entry.getKey());
        jgen.writeString(entry.getValue());
    }
    jgen.writeEndObject();
}
 
Example 2
Source File: StringArraySerializer.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void serializeContents(String[] value, JsonGenerator gen, SerializerProvider provider)
    throws IOException
{
    final int len = value.length;
    if (len == 0) {
        return;
    }
    if (_elementSerializer != null) {
        serializeContentsSlow(value, gen, provider, _elementSerializer);
        return;
    }
    for (int i = 0; i < len; ++i) {
        String str = value[i];
        if (str == null) {
            gen.writeNull();
        } else {
            gen.writeString(value[i]);
        }
    }
}
 
Example 3
Source File: ContextualStoredAsJsonSerializer.java    From Rosetta with Apache License 2.0 5 votes vote down vote up
protected void serializeAsString(T value, JsonGenerator gen, SerializerProvider provider) throws IOException {
  String res = serializeToString(value, getMapper(gen), provider);
  if ("null".equals(res)) {
    gen.writeNull();
  } else {
    gen.writeString(res);
  }
}
 
Example 4
Source File: Bean64Table.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute valueStringArray serialization
 */
public static byte[] serializeValueStringArray(String[] 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;
      String 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.writeString(item);
        }
      }
      jacksonSerializer.writeEndArray();
    }
    jacksonSerializer.writeEndObject();
    jacksonSerializer.flush();
    return stream.toByteArray();
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 5
Source File: BeanBeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute value2 serialization
 */
public static byte[] serializeValue2(BeanInner[] 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;
      BeanInner 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 {
          beanInnerBindMap.serializeOnJackson(item, jacksonSerializer);
        }
      }
      jacksonSerializer.writeEndArray();
    }
    jacksonSerializer.writeEndObject();
    jacksonSerializer.flush();
    return stream.toByteArray();
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 6
Source File: LoggerSettingsBindMap.java    From kripton with Apache License 2.0 5 votes vote down vote up
@Override
public int serializeOnJackson(LoggerSettings object, JsonGenerator jacksonSerializer) throws
    Exception {
  jacksonSerializer.writeStartObject();
  int fieldCount=0;

  // Serialized Field:

  // field appenders (mapped with "appenders")
  if (object.appenders!=null)  {
    fieldCount++;
    int n=object.appenders.size();
    LoggerAppenderSettings item;
    // write wrapper tag
    jacksonSerializer.writeFieldName("appenders");
    jacksonSerializer.writeStartArray();
    for (int i=0; i<n; i++) {
      item=object.appenders.get(i);
      if (item==null) {
        jacksonSerializer.writeNull();
      } else {
        loggerAppenderSettingsBindMap.serializeOnJackson(item, jacksonSerializer);
      }
    }
    jacksonSerializer.writeEndArray();
  }

  // field level (mapped with "level")
  if (object.level!=null)  {
    fieldCount++;
    jacksonSerializer.writeStringField("level", object.level.toString());
  }

  jacksonSerializer.writeEndObject();
  return fieldCount;
}
 
Example 7
Source File: JsonSerializer.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
protected void primitiveValue(final JsonGenerator jgen, final EdmTypeInfo typeInfo, final Object value)
    throws IOException, EdmPrimitiveTypeException {

  final EdmPrimitiveTypeKind kind = typeInfo == null ?
      EdmTypeInfo.determineTypeKind(value) :
      typeInfo.getPrimitiveTypeKind();

  if (value == null) {
    jgen.writeNull();
  } else if (kind == EdmPrimitiveTypeKind.Boolean) {
    jgen.writeBoolean((Boolean) value);
  } else if (kind == null) {
    if (serverMode) {
      throw new EdmPrimitiveTypeException("The primitive type could not be determined.");
    } else {
      jgen.writeString(value.toString()); // This might not be valid OData.
    }
  } else {
    // TODO: add facets
    final String serialized = EdmPrimitiveTypeFactory.getInstance(kind)
        .valueToString(value, null, null, Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null);

    if (isIEEE754Compatible && (kind == EdmPrimitiveTypeKind.Int64 || kind == EdmPrimitiveTypeKind.Decimal)
        || !NUMBER_TYPES.contains(kind)) {
      jgen.writeString(serialized);
    } else {
      jgen.writeNumber(serialized);
    }
  }
}
 
Example 8
Source File: Bean64Table.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute valueSetString serialization
 */
public static byte[] serializeValueSetString(Set<String> 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("element");
      jacksonSerializer.writeStartArray();
      for (String item: value) {
        if (item==null) {
          jacksonSerializer.writeNull();
        } else {
          jacksonSerializer.writeString(item);
        }
      }
      jacksonSerializer.writeEndArray();
    }
    jacksonSerializer.writeEndObject();
    jacksonSerializer.flush();
    return stream.toByteArray();
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 9
Source File: NullableSerializer.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(Nullable<Object> value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
	if (value.isPresent()) {
		Object object = value.get();
		if (object == null) {
			gen.writeNull();
		} else {
			gen.writeObject(object);
		}
	}
}
 
Example 10
Source File: Bean64Table.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute valueLongList serialization
 */
public static byte[] serializeValueLongList(LinkedList<Long> 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();
      Long 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: FloatBeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute value2 serialization
 */
public static byte[] serializeValue2(Float[] 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;
      Float 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 12
Source File: BindAppPreferences.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute stringList serialization
 */
protected String serializeStringList(List<String> 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();
      String item;
      // write wrapper tag
      jacksonSerializer.writeFieldName("stringList");
      jacksonSerializer.writeStartArray();
      for (int i=0; i<n; i++) {
        item=value.get(i);
        if (item==null) {
          jacksonSerializer.writeNull();
        } else {
          jacksonSerializer.writeString(item);
        }
      }
      jacksonSerializer.writeEndArray();
    }
    jacksonSerializer.writeEndObject();
    jacksonSerializer.flush();
    return stream.toString();
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 13
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 {
        double value = DOUBLE.getDouble(block, position);
        jsonGenerator.writeNumber(value);
    }
}
 
Example 14
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 {
        float value = intBitsToFloat(toIntExact(REAL.getLong(block, position)));
        jsonGenerator.writeNumber(value);
    }
}
 
Example 15
Source File: TimeValueSerializer.java    From FROST-Server with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void serialize(TimeValue value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
    if (value.isEmpty()) {
        gen.writeNull();
    } else {
        gen.writeString(value.asISO8601());
    }
}
 
Example 16
Source File: Bean64ATable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute valueSetString serialization
 */
public static byte[] serializeValueSetString(Set<String> 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("element");
      jacksonSerializer.writeStartArray();
      for (String item: value) {
        if (item==null) {
          jacksonSerializer.writeNull();
        } else {
          jacksonSerializer.writeString(item);
        }
      }
      jacksonSerializer.writeEndArray();
    }
    jacksonSerializer.writeEndObject();
    jacksonSerializer.flush();
    return stream.toByteArray();
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 17
Source File: CharDaoImpl.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for param serializer2 serialization
 */
private byte[] serializer2(List<Short> 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.size();
      Short 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 18
Source File: Bean01BindMap.java    From kripton with Apache License 2.0 4 votes vote down vote up
@Override
public int serializeOnJackson(Bean01 object, JsonGenerator jacksonSerializer) throws Exception {
  jacksonSerializer.writeStartObject();
  int fieldCount=0;

  // Serialized Field:

  // field id (mapped with "id")
  if (object.getId()!=null)  {
    fieldCount++;
    jacksonSerializer.writeNumberField("id", object.getId());
  }

  // field temp (mapped with "temp")
  if (object.temp!=null)  {
    fieldCount++;
    int n=object.temp.size();
    String item;
    // write wrapper tag
    jacksonSerializer.writeFieldName("temp");
    jacksonSerializer.writeStartArray();
    for (int i=0; i<n; i++) {
      item=object.temp.get(i);
      if (item==null) {
        jacksonSerializer.writeNull();
      } else {
        jacksonSerializer.writeString(item);
      }
    }
    jacksonSerializer.writeEndArray();
  }

  // field text (mapped with "text")
  if (object.getText()!=null)  {
    fieldCount++;
    jacksonSerializer.writeStringField("text", object.getText());
  }

  jacksonSerializer.writeEndObject();
  return fieldCount;
}
 
Example 19
Source File: StoneSerializers.java    From dropbox-sdk-java with MIT License 4 votes vote down vote up
@Override
public void serialize(Void value, JsonGenerator g) throws IOException, JsonGenerationException {
    g.writeNull();
}
 
Example 20
Source File: JsonMessageConverter.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
private static void handleValue(Object value, IFieldStructure structure, JsonGenerator generator) throws IOException {
    generator.writeFieldName("value");

    if(value == null) {
        generator.writeNull();
        return;
    }

    if(value instanceof List<?>) {
        generator.writeStartArray();

        for(Object element : (List<?>)value) {
            if(element == null) {
                continue;
            }

            generator.writeStartObject();
            generator.writeStringField("type", getValueClass(element, structure));
            handleValue(element, structure, generator);
            generator.writeEndObject();
        }

        generator.writeEndArray();
    } else if(value instanceof IMessage) {
        convert((IMessage)value, structure, generator);
    } else if(value instanceof LocalDate || value instanceof LocalTime || value instanceof LocalDateTime) {
        generator.writeString(formatTemporal((TemporalAccessor)value));
    } else if(value instanceof Character) {
        generator.writeString(value.toString());
    } else {
        generator.writeObject(value);
    }

    if(structure != null && structure.isEnum()) {
        String alias = null;

        for(IAttributeStructure attribute : structure.getValues().values()) {
            if(attribute.getCastValue().equals(value)) {
                alias = attribute.getName();
                break;
            }
        }

        if(alias != null) {
            generator.writeStringField("alias", alias);
        }
    }
}