Class JsonUtil

java.lang.Object
com.powsybl.commons.json.JsonUtil

public final class JsonUtil extends Object
Author:
Mathieu Bague <mathieu.bague at rte-france.com>
  • Method Details

    • createObjectMapper

      public static com.fasterxml.jackson.databind.ObjectMapper createObjectMapper()
    • writeJson

      public static void writeJson(Path jsonFile, Object object, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
    • readJson

      public static <T> T readJson(Path jsonFile, Class<T> clazz, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
    • readJsonAndUpdate

      public static <T> T readJsonAndUpdate(InputStream is, T object, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
    • readJsonAndUpdate

      public static <T> T readJsonAndUpdate(Path jsonFile, T object, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
    • createJsonFactory

      public static com.fasterxml.jackson.core.JsonFactory createJsonFactory()
    • writeJson

      public static void writeJson(Writer writer, Consumer<com.fasterxml.jackson.core.JsonGenerator> consumer)
    • toJson

      public static String toJson(Consumer<com.fasterxml.jackson.core.JsonGenerator> consumer)
    • writeJson

      public static void writeJson(Path file, Consumer<com.fasterxml.jackson.core.JsonGenerator> consumer)
    • parseJson

      public static <T> T parseJson(Path file, Function<com.fasterxml.jackson.core.JsonParser,T> function)
    • parseJson

      public static <T> T parseJson(String json, Function<com.fasterxml.jackson.core.JsonParser,T> function)
    • parseJson

      public static <T> T parseJson(Reader reader, Function<com.fasterxml.jackson.core.JsonParser,T> function)
    • writeOptionalStringField

      public static void writeOptionalStringField(com.fasterxml.jackson.core.JsonGenerator jsonGenerator, String fieldName, String value) throws IOException
      Throws:
      IOException
    • writeOptionalEnumField

      public static void writeOptionalEnumField(com.fasterxml.jackson.core.JsonGenerator jsonGenerator, String fieldName, Enum<?> value) throws IOException
      Throws:
      IOException
    • writeOptionalBooleanField

      public static void writeOptionalBooleanField(com.fasterxml.jackson.core.JsonGenerator jsonGenerator, String fieldName, boolean value, boolean defaultValue) throws IOException
      Throws:
      IOException
    • writeOptionalFloatField

      public static void writeOptionalFloatField(com.fasterxml.jackson.core.JsonGenerator jsonGenerator, String fieldName, float value) throws IOException
      Throws:
      IOException
    • writeOptionalDoubleField

      public static void writeOptionalDoubleField(com.fasterxml.jackson.core.JsonGenerator jsonGenerator, String fieldName, double value) throws IOException
      Throws:
      IOException
    • writeOptionalDoubleField

      public static void writeOptionalDoubleField(com.fasterxml.jackson.core.JsonGenerator jsonGenerator, String fieldName, double value, double defaultValue) throws IOException
      Throws:
      IOException
    • writeOptionalIntegerField

      public static void writeOptionalIntegerField(com.fasterxml.jackson.core.JsonGenerator jsonGenerator, String fieldName, int value) throws IOException
      Throws:
      IOException
    • writeExtensions

      public static <T> Set<String> writeExtensions(Extendable<T> extendable, com.fasterxml.jackson.core.JsonGenerator jsonGenerator, com.fasterxml.jackson.databind.SerializerProvider serializerProvider) throws IOException
      Throws:
      IOException
    • writeExtensions

      public static <T> Set<String> writeExtensions(Extendable<T> extendable, com.fasterxml.jackson.core.JsonGenerator jsonGenerator, com.fasterxml.jackson.databind.SerializerProvider serializerProvider, ExtensionProviders<? extends ExtensionJsonSerializer> supplier) throws IOException
      Throws:
      IOException
    • writeExtensions

      public static <T> Set<String> writeExtensions(Extendable<T> extendable, com.fasterxml.jackson.core.JsonGenerator jsonGenerator, com.fasterxml.jackson.databind.SerializerProvider serializerProvider, JsonUtil.SerializerSupplier supplier) throws IOException
      Throws:
      IOException
    • writeExtensions

      public static <T> Set<String> writeExtensions(Extendable<T> extendable, com.fasterxml.jackson.core.JsonGenerator jsonGenerator, boolean headerWanted, com.fasterxml.jackson.databind.SerializerProvider serializerProvider, ExtensionProviders<? extends ExtensionJsonSerializer> supplier) throws IOException
      Throws:
      IOException
    • writeExtensions

      public static <T> Set<String> writeExtensions(Extendable<T> extendable, com.fasterxml.jackson.core.JsonGenerator jsonGenerator, boolean headerWanted, com.fasterxml.jackson.databind.SerializerProvider serializerProvider, JsonUtil.SerializerSupplier supplier) throws IOException
      Throws:
      IOException
    • updateExtensions

      public static <T extends Extendable> List<Extension<T>> updateExtensions(com.fasterxml.jackson.core.JsonParser parser, com.fasterxml.jackson.databind.DeserializationContext context, T extendable) throws IOException
      Updates the extensions of the provided extendable with possibly partial definition read from JSON.

      Note that in order for this to work correctly, extension providers need to implement ExtensionJsonSerializer.deserializeAndUpdate(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext, E).

      Throws:
      IOException
    • updateExtensions

      public static <T extends Extendable> List<Extension<T>> updateExtensions(com.fasterxml.jackson.core.JsonParser parser, com.fasterxml.jackson.databind.DeserializationContext context, ExtensionProviders<? extends ExtensionJsonSerializer> supplier, T extendable) throws IOException
      Updates the extensions of the provided extendable with possibly partial definition read from JSON.

      Note that in order for this to work correctly, extension providers need to implement ExtensionJsonSerializer.deserializeAndUpdate(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext, E).

      Throws:
      IOException
    • updateExtensions

      public static <T extends Extendable> List<Extension<T>> updateExtensions(com.fasterxml.jackson.core.JsonParser parser, com.fasterxml.jackson.databind.DeserializationContext context, JsonUtil.SerializerSupplier supplier, T extendable) throws IOException
      Throws:
      IOException
    • updateExtensions

      public static <T extends Extendable> List<Extension<T>> updateExtensions(com.fasterxml.jackson.core.JsonParser parser, com.fasterxml.jackson.databind.DeserializationContext context, ExtensionProviders<? extends ExtensionJsonSerializer> supplier, Set<String> extensionsNotFound, T extendable) throws IOException
      Throws:
      IOException
    • updateExtensions

      public static <T extends Extendable> List<Extension<T>> updateExtensions(com.fasterxml.jackson.core.JsonParser parser, com.fasterxml.jackson.databind.DeserializationContext context, JsonUtil.SerializerSupplier supplier, Set<String> extensionsNotFound, T extendable) throws IOException
      Updates the extensions of the provided extendable with possibly partial definition read from JSON.

      Note that in order for this to work correctly, extension providers need to implement ExtensionJsonSerializer.deserializeAndUpdate(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext, E).

      Throws:
      IOException
    • readExtensions

      public static <T extends Extendable> List<Extension<T>> readExtensions(com.fasterxml.jackson.core.JsonParser parser, com.fasterxml.jackson.databind.DeserializationContext context) throws IOException
      Throws:
      IOException
    • readExtensions

      public static <T extends Extendable> List<Extension<T>> readExtensions(com.fasterxml.jackson.core.JsonParser parser, com.fasterxml.jackson.databind.DeserializationContext context, ExtensionProviders<? extends ExtensionJsonSerializer> supplier) throws IOException
      Throws:
      IOException
    • readExtensions

      public static <T extends Extendable> List<Extension<T>> readExtensions(com.fasterxml.jackson.core.JsonParser parser, com.fasterxml.jackson.databind.DeserializationContext context, ExtensionProviders<? extends ExtensionJsonSerializer> supplier, Set<String> extensionsNotFound) throws IOException
      Throws:
      IOException
    • readExtension

      public static <T extends Extendable> Extension<T> readExtension(com.fasterxml.jackson.core.JsonParser parser, com.fasterxml.jackson.databind.DeserializationContext context, ExtensionProviders<? extends ExtensionJsonSerializer> supplier, Set<String> extensionsNotFound) throws IOException
      Throws:
      IOException
    • skip

      public static void skip(com.fasterxml.jackson.core.JsonParser parser) throws IOException
      Skip a part of a JSON document
      Throws:
      IOException
    • assertLessThanOrEqualToReferenceVersion

      public static void assertLessThanOrEqualToReferenceVersion(String contextName, String elementName, String version, String referenceVersion)
    • assertGreaterThanReferenceVersion

      public static void assertGreaterThanReferenceVersion(String contextName, String elementName, String version, String referenceVersion)
    • assertGreaterOrEqualThanReferenceVersion

      public static void assertGreaterOrEqualThanReferenceVersion(String contextName, String elementName, String version, String referenceVersion)
    • assertLessThanReferenceVersion

      public static void assertLessThanReferenceVersion(String contextName, String elementName, String version, String referenceVersion)
    • parseObject

      public static void parseObject(com.fasterxml.jackson.core.JsonParser parser, JsonUtil.FieldHandler fieldHandler)
      Parses an object from the current parser position, using the provided field handler. The parsing will expect the starting position to be START_OBJECT.
    • parsePolymorphicObject

      public static void parsePolymorphicObject(com.fasterxml.jackson.core.JsonParser parser, JsonUtil.FieldHandler fieldHandler)
      Parses an object from the current parser position, using the provided field handler. The parsing will accept the starting position to be either a START_OBJECT or a FIELD_NAME, see contract for JsonDeserializer.deserialize(JsonParser, DeserializationContext).
    • parseObject

      public static void parseObject(com.fasterxml.jackson.core.JsonParser parser, boolean polymorphic, JsonUtil.FieldHandler fieldHandler)
      Parses an object from the current parser position, using the provided field handler. If polymorphic is true, the parsing will accept the starting position to be either a START_OBJECT or a FIELD_NAME, see contract for JsonDeserializer.deserialize(JsonParser, DeserializationContext).
    • parseObjectArray

      public static <T> void parseObjectArray(com.fasterxml.jackson.core.JsonParser parser, Consumer<T> objectAdder, Function<com.fasterxml.jackson.core.JsonParser,T> objectParser)
    • parseIntegerArray

      public static List<Integer> parseIntegerArray(com.fasterxml.jackson.core.JsonParser parser)
    • parseLongArray

      public static List<Long> parseLongArray(com.fasterxml.jackson.core.JsonParser parser)
    • parseFloatArray

      public static List<Float> parseFloatArray(com.fasterxml.jackson.core.JsonParser parser)
    • parseDoubleArray

      public static List<Double> parseDoubleArray(com.fasterxml.jackson.core.JsonParser parser)
    • parseStringArray

      public static List<String> parseStringArray(com.fasterxml.jackson.core.JsonParser parser)
    • setSourceVersion

      public static void setSourceVersion(com.fasterxml.jackson.databind.DatabindContext context, String version, String sourceVersionAttributeKey)
      Saves the provided version into the context (typically a DeserializationContext), for later retrieval.
    • getSourceVersion

      public static String getSourceVersion(com.fasterxml.jackson.databind.DatabindContext context, String sourceVersionAttributeKey)
      Reads the version from the context (typically a DeserializationContext) where it has been previously stored.
    • readValue

      public static <T> T readValue(com.fasterxml.jackson.databind.DeserializationContext context, com.fasterxml.jackson.core.JsonParser parser, Class<?> type)
      Reads a value using the given deserialization context (instead of only using the parser reading method that recreates a context every time). Also handles reading null values.
    • readList

      public static <T> List<T> readList(com.fasterxml.jackson.databind.DeserializationContext context, com.fasterxml.jackson.core.JsonParser parser, Class<?> type)
    • readSet

      public static <T> Set<T> readSet(com.fasterxml.jackson.databind.DeserializationContext context, com.fasterxml.jackson.core.JsonParser parser, Class<?> type)
    • writeOptionalEnum

      public static <T extends Enum> void writeOptionalEnum(com.fasterxml.jackson.core.JsonGenerator jsonGenerator, String field, Optional<T> optional) throws IOException
      Throws:
      IOException
    • writeOptionalDouble

      public static void writeOptionalDouble(com.fasterxml.jackson.core.JsonGenerator jsonGenerator, String field, OptionalDouble optional) throws IOException
      Throws:
      IOException
    • writeOptionalBoolean

      public static void writeOptionalBoolean(com.fasterxml.jackson.core.JsonGenerator jsonGenerator, String field, Optional<Boolean> optional) throws IOException
      Throws:
      IOException