Class TraceToJson


  • public final class TraceToJson
    extends Object
    Utility class for creating a JSON representation of traces. See toJsonString(List) for more information.
    • Method Detail

      • create

        public static TraceToJson create()
        Create a new instance. By default, it has a single custom order defined, which triggers on a trace with type timestamp. It orders the properties in a 'logical/natural' order.

        The default fallback for all other traces is sorting them by property name.

        Returns:
        a default trace serializer instance
      • exclude

        public TraceToJson exclude​(BiPredicate<String,​Object> exclusionFilter)
        Add a filter for excluding properties in the serialized trace output, based on the name or the value of the property.

        For example, if you want to ignore all properties ending on ".data", or all properties of type byte[]:

         
           final TraceToJson traceToJson = TraceToJson.create()
               .exclude((name, value) -> name.endsWith(".data") || value instanceof byte[])
         
         
        Parameters:
        exclusionFilter - the filter to add
        Returns:
        a new TraceToJson based on this, extended with the filter
      • addCustomConversion

        public <T> TraceToJson addCustomConversion​(Class<T> type,
                                                   Function<? super T,​String> conversion)
        Add a custom conversion for elements of a given type. A conversion can convert an element of the given type to a String. Multiple conversions can be added by calling this method repeatedly.

        If an element is matched, the name of the property corresponding to the element will be added as the field name, the conversion of the element will be added as the field value.

        Note: these conversions take precedence before the default implementations, so for example adding a conversion for a Long type will override the existing behaviour.

        Type Parameters:
        T - the required type
        Parameters:
        type - the type class to match with
        conversion - the conversion to apply
        Returns:
        a new TraceToJson based on this, extended with the custom conversion
      • addCustomOrder

        public TraceToJson addCustomOrder​(Predicate<? super nl.minvenj.nfi.flits.api.Trace> predicate,
                                          Comparator<? super String> comparator)
        Add a custom order for the properties of a trace. If a trace is matched, the properties will be sorted using the supplied comparator.

        Note:the custom orders will take precedence before the already supplied orders, and before the by the default ordering rules (see create()).

        Parameters:
        predicate - the predicate to check if the order applies to a certain trace
        comparator - the comparator to use
        Returns:
        a new TraceToJson based on this, extended with the custom order
      • toJsonString

        public String toJsonString​(List<nl.minvenj.nfi.flits.api.Trace> traces)
        Create a JSON string from a list of trace. The traces tree will be traversed and each property name and corresponding value will be added as a field to the JSON object. The properties will be sorted lexicographically by name. The children which will always come after all other properties. They will however also be sorted in the same manner, by type.

        Each type of property has a certain conversion defined by this class. As an example, a Map will be converted to a JSON object, with keys and values as the fields.

        See the implementation for the conversion of other types.

        Parameters:
        traces - the traces to create a JSON representation from
        Returns:
        the traces as a JSON string
        Throws:
        UncheckedIOException - when an I/O error occurs, e.g., during reading of a data stream property or writing to the JSON stream
      • writeToFile

        public void writeToFile​(List<nl.minvenj.nfi.flits.api.Trace> traces,
                                File file)
        Write a JSON representation of a list of Trace to a File, see toJsonString(List) for more information about the conversion to a JSON string.

        If the file does not exists, it will be created first.

        Parameters:
        traces - the traces to create a JSON representation from
        file - the file to write to
        Throws:
        UncheckedIOException - when an I/O error occurs, e.g., during reading of data stream property
      • writeToPath

        public void writeToPath​(List<nl.minvenj.nfi.flits.api.Trace> traces,
                                Path path)
        Write a JSON representation of a list of Trace to a Path, see toJsonString(List) for more information about the conversion to a JSON string.

        If the path does not exists, it will be created first.

        Parameters:
        traces - the traces to create a JSON representation from
        path - the path to write to
        Throws:
        UncheckedIOException - when an I/O error occurs, e.g., during reading of data stream property
      • writeToStream

        public void writeToStream​(List<nl.minvenj.nfi.flits.api.Trace> traces,
                                  OutputStream outputStream)
        Write a JSON representation of a list of trace to an OutputStream, see toJsonString(List) for more information about the conversion to a JSON string.
        Parameters:
        traces - the traces to create a JSON representation from
        outputStream - the output stream to write to
        Throws:
        UncheckedIOException - when an I/O error occurs, e.g., during reading of a data stream property or writing to the output stream
      • writeToGenerator

        public void writeToGenerator​(List<nl.minvenj.nfi.flits.api.Trace> traces,
                                     com.fasterxml.jackson.core.JsonGenerator generator)
        Write a JSON representation of a list of trace to a JsonGenerator, see toJsonString(List) for more information about the conversion to a JSON string. If the list is of size 1 writeToGenerator(Trace, JsonGenerator) will be called.
        Parameters:
        traces - the traces to create a JSON representation from
        generator - the JSON generator to write to
        Throws:
        IllegalArgumentException - when multiple traces are given. Multiple traces are currently not supported.
      • writeTypes

        public void writeTypes​(nl.minvenj.nfi.flits.api.Trace trace,
                               com.fasterxml.jackson.core.JsonGenerator generator)
      • writeProperties

        public void writeProperties​(nl.minvenj.nfi.flits.api.Trace trace,
                                    Set<nl.minvenj.nfi.flits.serialize.Property> properties,
                                    com.fasterxml.jackson.core.JsonGenerator generator)
      • writePreviews

        public void writePreviews​(nl.minvenj.nfi.flits.api.Trace trace,
                                  com.fasterxml.jackson.core.JsonGenerator generator)