Class JsonOutputFormatter

  • All Implemented Interfaces:
    OutputFormatter<de.iip_ecosphere.platform.support.function.IOConsumer<com.fasterxml.jackson.core.JsonGenerator>>

    @MachineFormatter
    public class JsonOutputFormatter
    extends java.lang.Object
    implements OutputFormatter<de.iip_ecosphere.platform.support.function.IOConsumer<com.fasterxml.jackson.core.JsonGenerator>>
    JSON output formatter (preliminary).
    Author:
    Holger Eichelberger, SSE
    • Field Detail

      • objectMapper

        private com.fasterxml.jackson.databind.ObjectMapper objectMapper
      • writer

        private java.io.StringWriter writer
      • gen

        private com.fasterxml.jackson.core.JsonGenerator gen
      • parentName

        private java.lang.String parentName
    • Constructor Detail

      • JsonOutputFormatter

        public JsonOutputFormatter()
    • Method Detail

      • cleanNesting

        private void cleanNesting​(java.lang.String parent)
                           throws java.io.IOException
        Cleans up the nesting.
        Parameters:
        parent - the target parent path
        Throws:
        java.io.IOException - when writing data fails
      • initialize

        private void initialize()
                         throws java.io.IOException
        Initializes the JSON writing.
        Throws:
        java.io.IOException - if initialization fails
      • add

        public void add​(java.lang.String name,
                        de.iip_ecosphere.platform.support.function.IOConsumer<com.fasterxml.jackson.core.JsonGenerator> func)
                 throws java.io.IOException
        Description copied from interface: OutputFormatter
        Adds information to one chunk of output. Hierarchical names separated by OutputFormatter.SEPARATOR can be used, but nested values for the same parent (object) field must be named in sequence.
        Specified by:
        add in interface OutputFormatter<de.iip_ecosphere.platform.support.function.IOConsumer<com.fasterxml.jackson.core.JsonGenerator>>
        Parameters:
        name - optional data name field (may be null for none)
        func - the data to be added
        Throws:
        java.io.IOException - if adding the data fails for some reason
      • startArrayStructure

        public void startArrayStructure​(java.lang.String name)
                                 throws java.io.IOException
        Description copied from interface: OutputFormatter
        Starts an array structure. Following OutputFormatter.add(String, Object) calls will add elements to the array. Must be closed with OutputFormatter.endStructure()
        Specified by:
        startArrayStructure in interface OutputFormatter<de.iip_ecosphere.platform.support.function.IOConsumer<com.fasterxml.jackson.core.JsonGenerator>>
        Parameters:
        name - optional data name field holding the array (may be null for none)
        Throws:
        java.io.IOException - if starting this structure fails
      • startObjectStructure

        public void startObjectStructure​(java.lang.String name)
                                  throws java.io.IOException
        Description copied from interface: OutputFormatter
        Starts an object structure. Following OutputFormatter.add(String, Object) calls will add elements to the object. Must be closed with OutputFormatter.endStructure()
        Specified by:
        startObjectStructure in interface OutputFormatter<de.iip_ecosphere.platform.support.function.IOConsumer<com.fasterxml.jackson.core.JsonGenerator>>
        Parameters:
        name - optional data name field holding the array (may be null for none)
        Throws:
        java.io.IOException - if starting this structure fails
      • endStructure

        public void endStructure()
                          throws java.io.IOException
        Description copied from interface: OutputFormatter
        Ends a structure started before.
        Specified by:
        endStructure in interface OutputFormatter<de.iip_ecosphere.platform.support.function.IOConsumer<com.fasterxml.jackson.core.JsonGenerator>>
        Throws:
        java.io.IOException - if ending the actual structure fails
      • chunkCompleted

        public byte[] chunkCompleted()
                              throws java.io.IOException
        Description copied from interface: OutputFormatter
        Completes a chunk of output data.
        Specified by:
        chunkCompleted in interface OutputFormatter<de.iip_ecosphere.platform.support.function.IOConsumer<com.fasterxml.jackson.core.JsonGenerator>>
        Returns:
        the chunk
        Throws:
        java.io.IOException - if creating the chunk fails for some reason