= JsonUtils
:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Utilities to convert from and to JSON format.

== API

[source,java]
.JsonUtils.java
----
class JsonUtils {
  Optional<Class<?>> getPlatformDefaultJsonProviderForJaxb()     // <.>
  Try<T> tryRead(Class<T> mappedType, String stringUtf8, JsonUtils.JacksonCustomizer... customizers)     // <.>
  Try<T> tryRead(Class<T> mappedType, DataSource source, JsonUtils.JacksonCustomizer... customizers)     // <.>
  Try<List<T>> tryReadAsList(Class<T> elementType, DataSource source, JsonUtils.JacksonCustomizer... customizers)     // <.>
  void write(Object pojo, DataSink sink, JsonUtils.JacksonCustomizer... customizers)     // <.>
  String toStringUtf8(Object pojo, JsonUtils.JacksonCustomizer... customizers)     // <.>
  ObjectMapper indentedOutput(ObjectMapper mapper)     // <.>
  ObjectMapper onlyIncludeNonNull(ObjectMapper mapper)     // <.>
  ObjectMapper jdk8Support(ObjectMapper mapper)     // <.>
  ObjectMapper jaxbAnnotationSupport(ObjectMapper mapper)     // <.>
  ObjectMapper readingJavaTimeSupport(ObjectMapper mapper)     // <.>
  ObjectMapper writingJavaTimeSupport(ObjectMapper mapper)     // <.>
  ObjectMapper readingCanSupport(ObjectMapper mapper)     // <.>
  ObjectMapper writingCanSupport(ObjectMapper mapper)     // <.>
}
----

<.> xref:#getPlatformDefaultJsonProviderForJaxb_[getPlatformDefaultJsonProviderForJaxb()]
+
--
Consumers of the framework may choose to use a different provider.
--
<.> xref:#tryRead_Class_String_JsonUtils_JacksonCustomizer[tryRead(Class, String, JsonUtils_JacksonCustomizer)]
+
--
Tries to deserialize JSON content from given UTF8 encoded _String_ into an instance of given _mappedType_ .
--
<.> xref:#tryRead_Class_DataSource_JsonUtils_JacksonCustomizer[tryRead(Class, DataSource, JsonUtils_JacksonCustomizer)]
+
--
Tries to deserialize JSON content from given xref:refguide:commons:index/io/DataSource.adoc[DataSource] into an instance of given _mappedType_ .
--
<.> xref:#tryReadAsList_Class_DataSource_JsonUtils_JacksonCustomizer[tryReadAsList(Class, DataSource, JsonUtils_JacksonCustomizer)]
+
--
Tries to deserialize JSON content from given xref:refguide:commons:index/io/DataSource.adoc[DataSource] into a _List_ with given _elementType_ .
--
<.> xref:#write_Object_DataSink_JsonUtils_JacksonCustomizer[write(Object, DataSink, JsonUtils_JacksonCustomizer)]
+
--
Writes given _pojo_ to given xref:refguide:commons:index/io/DataSink.adoc[DataSink] .
--
<.> xref:#toStringUtf8_Object_JsonUtils_JacksonCustomizer[toStringUtf8(Object, JsonUtils_JacksonCustomizer)]
+
--
Converts given _pojo_ to an UTF8 encoded _String_ .
--
<.> xref:#indentedOutput_ObjectMapper[indentedOutput(ObjectMapper)]
+
--
enable indentation for the underlying generator
--
<.> xref:#onlyIncludeNonNull_ObjectMapper[onlyIncludeNonNull(ObjectMapper)]
+
--
only properties with non-null values are to be included
--
<.> xref:#jdk8Support_ObjectMapper[jdk8Support(ObjectMapper)]
+
--
add support for JDK 8, e.g. _Optional_
--
<.> xref:#jaxbAnnotationSupport_ObjectMapper[jaxbAnnotationSupport(ObjectMapper)]
+
--
add support for JAXB annotations
--
<.> xref:#readingJavaTimeSupport_ObjectMapper[readingJavaTimeSupport(ObjectMapper)]
+
--
add support for reading java.time (ISO)
--
<.> xref:#writingJavaTimeSupport_ObjectMapper[writingJavaTimeSupport(ObjectMapper)]
+
--
add support for writing java.time (ISO)
--
<.> xref:#readingCanSupport_ObjectMapper[readingCanSupport(ObjectMapper)]
+
--
add support for reading Can
--
<.> xref:#writingCanSupport_ObjectMapper[writingCanSupport(ObjectMapper)]
+
--
add support for writing Can
--

== Members

[#getPlatformDefaultJsonProviderForJaxb_]
=== getPlatformDefaultJsonProviderForJaxb()

Consumers of the framework may choose to use a different provider.

[#tryRead_Class_String_JsonUtils_JacksonCustomizer]
=== tryRead(Class, String, JsonUtils_JacksonCustomizer)

Tries to deserialize JSON content from given UTF8 encoded _String_ into an instance of given _mappedType_ .

[#tryRead_Class_DataSource_JsonUtils_JacksonCustomizer]
=== tryRead(Class, DataSource, JsonUtils_JacksonCustomizer)

Tries to deserialize JSON content from given xref:refguide:commons:index/io/DataSource.adoc[DataSource] into an instance of given _mappedType_ .

[#tryReadAsList_Class_DataSource_JsonUtils_JacksonCustomizer]
=== tryReadAsList(Class, DataSource, JsonUtils_JacksonCustomizer)

Tries to deserialize JSON content from given xref:refguide:commons:index/io/DataSource.adoc[DataSource] into a _List_ with given _elementType_ .

[#write_Object_DataSink_JsonUtils_JacksonCustomizer]
=== write(Object, DataSink, JsonUtils_JacksonCustomizer)

Writes given _pojo_ to given xref:refguide:commons:index/io/DataSink.adoc[DataSink] .

[#toStringUtf8_Object_JsonUtils_JacksonCustomizer]
=== toStringUtf8(Object, JsonUtils_JacksonCustomizer)

Converts given _pojo_ to an UTF8 encoded _String_ .

[#indentedOutput_ObjectMapper]
=== indentedOutput(ObjectMapper)

enable indentation for the underlying generator

[#onlyIncludeNonNull_ObjectMapper]
=== onlyIncludeNonNull(ObjectMapper)

only properties with non-null values are to be included

[#jdk8Support_ObjectMapper]
=== jdk8Support(ObjectMapper)

add support for JDK 8, e.g. _Optional_

[#jaxbAnnotationSupport_ObjectMapper]
=== jaxbAnnotationSupport(ObjectMapper)

add support for JAXB annotations

[#readingJavaTimeSupport_ObjectMapper]
=== readingJavaTimeSupport(ObjectMapper)

add support for reading java.time (ISO)

[#writingJavaTimeSupport_ObjectMapper]
=== writingJavaTimeSupport(ObjectMapper)

add support for writing java.time (ISO)

[#readingCanSupport_ObjectMapper]
=== readingCanSupport(ObjectMapper)

add support for reading Can

[#writingCanSupport_ObjectMapper]
=== writingCanSupport(ObjectMapper)

add support for writing Can
