Class JsonWritingUtils

java.lang.Object
net.logstash.logback.composite.JsonWritingUtils

public class JsonWritingUtils extends Object
Utilities for writing JSON

Note: This class is for internal use only and subject to backward incompatible change at any time.

  • Constructor Details

    • JsonWritingUtils

      public JsonWritingUtils()
  • Method Details

    • writeMapEntries

      public static void writeMapEntries(JsonGenerator generator, Map<?,?> map) throws IOException, JsonMappingException
      Writes entries of the map as fields.
      Parameters:
      generator - the JsonGenerator to produce JSON content
      map - map whose entries are written as JSON field/values
      Throws:
      IOException - if an I/O error occurs
      JsonMappingException - when problem to convert map values of type Object into JSON
    • writeMapStringFields

      public static void writeMapStringFields(JsonGenerator generator, String fieldName, Map<String,String> map) throws IOException, JsonMappingException
      Writes a map as String fields to the generator if and only if the fieldName and values are not null.
      Parameters:
      generator - the JsonGenerator to produce JSON content
      fieldName - name of the JSON property to write the map content under
      map - map whose entries are written as JSON field/values
      Throws:
      IOException - if an I/O error occurs
      JsonMappingException - when problem to convert map values of type Object into JSON
    • writeMapStringFields

      public static void writeMapStringFields(JsonGenerator generator, String fieldName, Map<String,String> map, boolean lowerCaseKeys) throws IOException, JsonMappingException
      Writes a map as String fields to the generator if and only if the fieldName and values are not null.
      Parameters:
      generator - the JsonGenerator to produce JSON content
      fieldName - name of the JSON property to write the map content under
      map - map whose entries are written as JSON field/values
      lowerCaseKeys - when true, the map keys will be written in lower case.
      Throws:
      IOException - if an I/O error occurs
      JsonMappingException - when problem to convert map values of type Object into JSON
    • writeStringArrayField

      public static void writeStringArrayField(JsonGenerator generator, String fieldName, String[] fieldValues) throws IOException
      Writes an array of strings to the generator if and only if the fieldName and values are not null.
      Parameters:
      generator - the JsonGenerator to produce JSON content
      fieldName - the field name
      fieldValues - the field values
      Throws:
      IOException - if an I/O error occurs
    • writeStringField

      public static void writeStringField(JsonGenerator generator, String fieldName, String fieldValue) throws IOException
      Writes the field to the generator if and only if the fieldName and fieldValue are not null.
      Parameters:
      generator - the JsonGenerator to produce JSON content
      fieldName - the field name
      fieldValue - the field value
      Throws:
      IOException - if an I/O error occurs
    • writeNumberField

      public static void writeNumberField(JsonGenerator generator, String fieldName, int fieldValue) throws IOException
      Writes the field to the generator if and only if the fieldName is not null.
      Parameters:
      generator - the JsonGenerator to produce JSON content
      fieldName - the field name
      fieldValue - the field value
      Throws:
      IOException - if an I/O error occurs
    • writeNumberField

      public static void writeNumberField(JsonGenerator generator, String fieldName, long fieldValue) throws IOException
      Writes the field to the generator if and only if the fieldName is not null.
      Parameters:
      generator - the JsonGenerator to produce JSON content
      fieldName - the field name
      fieldValue - the field value
      Throws:
      IOException - if an I/O error occurs
    • shouldWriteField

      public static boolean shouldWriteField(String fieldName)
      Indicates whether the given field name must be written or not. A field should be written if its name is not null and not ignored.
      Parameters:
      fieldName - the field name
      Returns:
      true if the field should be written, false otherwise
      See Also: