Class TraceToJson
- java.lang.Object
-
- nl.minvenj.nfi.flits.serialize.TraceToJson
-
public final class TraceToJson extends Object
Utility class for creating a JSON representation oftraces. SeetoJsonString(List)for more information.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> TraceToJsonaddCustomConversion(Class<T> type, Function<? super T,String> conversion)Add a custom conversion for elements of a given type.TraceToJsonaddCustomOrder(Predicate<? super nl.minvenj.nfi.flits.api.Trace> predicate, Comparator<? super String> comparator)Add a custom order for the properties of atrace.static TraceToJsoncreate()Create a new instance.TraceToJsonexclude(BiPredicate<String,Object> exclusionFilter)Add a filter for excluding properties in the serializedtraceoutput, based on the name or the value of the property.StringtoJsonString(List<nl.minvenj.nfi.flits.api.Trace> traces)Create a JSON string from a list oftrace.voidwritePreviews(nl.minvenj.nfi.flits.api.Trace trace, com.fasterxml.jackson.core.JsonGenerator generator)voidwriteProperties(nl.minvenj.nfi.flits.api.Trace trace, Set<nl.minvenj.nfi.flits.serialize.Property> properties, com.fasterxml.jackson.core.JsonGenerator generator)voidwriteToFile(List<nl.minvenj.nfi.flits.api.Trace> traces, File file)Write a JSON representation of a list ofTraceto aFile, seetoJsonString(List)for more information about the conversion to a JSON string.voidwriteToGenerator(List<nl.minvenj.nfi.flits.api.Trace> traces, com.fasterxml.jackson.core.JsonGenerator generator)Write a JSON representation of a list oftraceto aJsonGenerator, seetoJsonString(List)for more information about the conversion to a JSON string.voidwriteToPath(List<nl.minvenj.nfi.flits.api.Trace> traces, Path path)Write a JSON representation of a list ofTraceto aPath, seetoJsonString(List)for more information about the conversion to a JSON string.voidwriteToStream(List<nl.minvenj.nfi.flits.api.Trace> traces, OutputStream outputStream)Write a JSON representation of a list oftraceto anOutputStream, seetoJsonString(List)for more information about the conversion to a JSON string.voidwriteTypes(nl.minvenj.nfi.flits.api.Trace trace, com.fasterxml.jackson.core.JsonGenerator generator)
-
-
-
Method Detail
-
create
public static TraceToJson create()
Create a new instance. By default, it has a singlecustom orderdefined, which triggers on atracewith typetimestamp. 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 serializedtraceoutput, 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 typebyte[]: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 aString. 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
Longtype will override the existing behaviour.- Type Parameters:
T- the required type- Parameters:
type- the type class to match withconversion- 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 atrace. 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 tracecomparator- 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 oftrace. 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
Mapwill 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 ofTraceto aFile, seetoJsonString(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 fromfile- 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 ofTraceto aPath, seetoJsonString(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 frompath- 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 oftraceto anOutputStream, seetoJsonString(List)for more information about the conversion to a JSON string.- Parameters:
traces- the traces to create a JSON representation fromoutputStream- 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 oftraceto aJsonGenerator, seetoJsonString(List)for more information about the conversion to a JSON string. If the list is of size 1writeToGenerator(Trace, JsonGenerator)will be called.- Parameters:
traces- the traces to create a JSON representation fromgenerator- 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)
-
-