Class JsonUtil


  • public final class JsonUtil
    extends Object
    Author:
    Mathieu Bague
    • Method Detail

      • 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
      • readExtensions

        public static <T extends ExtendableList<Extension<T>> readExtensions​(com.fasterxml.jackson.core.JsonParser parser,
                                                                               com.fasterxml.jackson.databind.DeserializationContext context)
                                                                        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)
      • 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.