From 3d35439fb50d2f26679917da7b70df3972a1dcb9 Mon Sep 17 00:00:00 2001 From: catalin-burcea Date: Tue, 26 Nov 2019 12:28:18 +0200 Subject: [PATCH] [BAEL-19675] - Move articles out of jackson part 2 --- jackson-2/README.md | 3 - jackson-2/pom.xml | 14 -- .../JacksonSerializationIgnoreUnitTest.java | 85 ----------- jackson-conversions-2/README.md | 14 ++ jackson-conversions-2/pom.xml | 67 +++++++++ .../jackson/csv/JsonCsvConverter.java | 2 - .../com/baeldung/jackson/csv}/OrderLine.java | 2 +- .../jackson/csv}/OrderLineForCsv.java | 2 +- .../jackson}/dynamicobject/Product.java | 2 +- .../dynamicobject/ProductJsonNode.java | 2 +- .../jackson}/dynamicobject/ProductMap.java | 2 +- .../jackson/multiplefields}/Weather.java | 2 +- .../jackson/unknownproperties/MyDto.java | 54 +++++++ .../unknownproperties}/MyDtoIgnoreType.java | 2 +- .../MyDtoIgnoreUnknown.java | 2 +- .../com/baeldung/jackson/xmlToJson/Color.java | 0 .../baeldung/jackson/xmlToJson/Flower.java | 0 .../com/baeldung/jackson/yaml}/Order.java | 2 +- .../com/baeldung/jackson/yaml/OrderLine.java | 49 ++++++ .../src/main/resources/csv}/orderLines.csv | 0 .../src/main/resources/csv}/orderLines.json | 0 .../com/baeldung/jackson/csv/CsvUnitTest.java | 24 +-- .../DynamicObjectDeserializationUnitTest.java | 2 +- .../IgnoreNullFieldsUnitTest.java | 52 +++++++ .../jackson/ignorenullfields/MyDto.java | 54 +++++++ .../ignorenullfields/MyDtoIgnoreNull.java | 51 +++++++ ...apMultipleFieldsToSingleFieldUnitTest.java | 5 +- .../streaming/StreamingAPIUnitTest.java | 2 +- .../UnknownPropertiesUnitTest.java | 80 ++++++++++ .../jackson/xmlToJson/XmlToJsonUnitTest.java | 0 .../baeldung/jackson/yaml/YamlUnitTest.java | 8 +- .../resources/csv}/expectedCsvFromJson.csv | 0 .../csv}/expectedFormattedCsvFromJson.csv | 0 .../resources/csv}/expectedJsonFromCsv.json | 0 .../deserialize-dynamic-object/embedded.json | 0 .../deserialize-dynamic-object/flat.json | 0 .../src/test/resources/yaml}/orderInput.yaml | 0 jackson-conversions/README.md | 16 ++ jackson-conversions/pom.xml | 49 ++++++ .../jackson/date/CustomDateDeserializer.java | 0 .../jackson/date/CustomDateSerializer.java | 0 .../date/CustomDateTimeSerializer.java | 0 .../date/CustomLocalDateTimeSerializer.java | 0 .../java/com/baeldung/jackson/date/Event.java | 0 .../jackson/date/EventWithFormat.java | 0 .../jackson/date/EventWithJodaTime.java | 0 .../jackson/date/EventWithLocalDateTime.java | 0 .../jackson/date/EventWithSerializer.java | 0 .../jackson/enums/deserialization}/City.java | 2 +- .../enums/deserialization}/Distance.java | 2 +- .../customdeserializer/City.java | 2 +- .../CustomEnumDeserializer.java | 2 +- .../customdeserializer/Distance.java | 2 +- .../deserialization}/jsoncreator/City.java | 2 +- .../jsoncreator/Distance.java | 2 +- .../deserialization}/jsonproperty/City.java | 2 +- .../jsonproperty/Distance.java | 2 +- .../deserialization}/jsonvalue/City.java | 2 +- .../deserialization}/jsonvalue/Distance.java | 2 +- .../enums/serialization}/Distance.java | 4 +- .../serialization/DistanceSerializer.java | 3 +- .../enums}/withEnum/DistanceEnumSimple.java | 2 +- .../withEnum/DistanceEnumWithJsonFormat.java | 2 +- .../withEnum/DistanceEnumWithValue.java | 2 +- .../enums}/withEnum/MyDtoWithEnumCustom.java | 4 +- .../withEnum/MyDtoWithEnumJsonFormat.java | 2 +- .../com/baeldung/jackson/field/MyDto.java | 0 .../jackson/field/MyDtoAccessLevel.java | 0 .../jackson/field/MyDtoWithGetter.java | 0 .../jackson/field/MyDtoWithSetter.java | 0 .../baeldung/jackson}/immutable/Employee.java | 2 +- .../baeldung/jackson}/immutable/Person.java | 2 +- .../baeldung/jackson/map}/ClassWithAMap.java | 3 +- .../com/baeldung/jackson/map}/MyPair.java | 2 +- .../jackson/map}/MyPairDeserializer.java | 3 +- .../jackson/map}/MyPairSerializer.java | 3 +- .../com/baeldung/jackson/mapnull/MyDto.java | 54 +++++++ .../mapnull}/MyDtoNullKeySerializer.java | 2 +- .../baeldung/jackson/tocollection/MyDto.java | 54 +++++++ .../com/baeldung/jackson/xml/Address.java | 33 +++++ .../java/com/baeldung/jackson/xml/Person.java | 47 ++++++ .../jackson/date}/JacksonDateUnitTest.java | 2 +- .../DefaultEnumDeserializationUnitTest.java | 3 +- .../EnumCustomDeserializationUnitTest.java | 3 +- ...serializationUsingJsonCreatorUnitTest.java | 3 +- ...erializationUsingJsonPropertyUnitTest.java | 3 +- ...DeserializationUsingJsonValueUnitTest.java | 3 +- .../JacksonEnumSerializationUnitTest.java | 3 +- .../JacksonSerializationEnumsUnitTest.java | 14 +- .../jackson/field}/JacksonFieldUnitTest.java | 6 +- ...mmutableObjectDeserializationUnitTest.java | 2 +- .../map}/JacksonMapDeserializeUnitTest.java | 4 +- .../map}/JacksonMapSerializeUnitTest.java | 3 +- .../mapnull/JacksonMapNullUnitTest.java | 109 ++++++++++++++ ...serializeWithNestedPropertiesUnitTest.java | 140 +++++++++--------- .../com/baeldung/jackson}/nested/Product.java | 108 +++++++------- .../jackson}/nested/ProductDeserializer.java | 80 +++++----- ...ksonCollectionDeserializationUnitTest.java | 4 +- .../tojsonnode/StringToJsonNodeUnitTest.java | 51 +++++++ .../xml/XMLSerializeDeserializeUnitTest.java | 2 - .../src/test/resources/simple_bean.xml | 0 jackson-simple/README.md | 3 - jackson/README.md | 18 +-- .../IgnoranceAnnotationStructure.java | 0 .../IgnoranceMixinOrIntrospection.java | 0 .../SubTypeConstructorStructure.java | 0 .../SubTypeConversionStructure.java | 0 .../TypeInfoAnnotatedStructure.java | 0 .../inheritance/TypeInfoStructure.java | 0 .../objectmapper/CustomCarDeserializer.java | 0 .../objectmapper/CustomCarSerializer.java | 0 .../jackson/objectmapper/dto/Car.java | 0 .../jackson/objectmapper/dto/Request.java | 0 .../JavaReadWriteJsonExampleUnitTest.java | 1 + ...izationDeserializationFeatureUnitTest.java | 1 + .../test/JacksonDeserializationUnitTest.java | 86 ----------- .../baeldung/jackson/test/UnitTestSuite.java | 5 +- .../src/test/resources/json_car.json | 0 pom.xml | 4 + 119 files changed, 1087 insertions(+), 469 deletions(-) create mode 100644 jackson-conversions-2/README.md create mode 100644 jackson-conversions-2/pom.xml rename {jackson-2 => jackson-conversions-2}/src/main/java/com/baeldung/jackson/csv/JsonCsvConverter.java (95%) rename {jackson-2/src/main/java/com/baeldung/jackson/entities => jackson-conversions-2/src/main/java/com/baeldung/jackson/csv}/OrderLine.java (96%) rename {jackson-2/src/main/java/com/baeldung/jackson/mixin => jackson-conversions-2/src/main/java/com/baeldung/jackson/csv}/OrderLineForCsv.java (92%) rename {jackson/src/main/java/com/baeldung/jackson/deserialization => jackson-conversions-2/src/main/java/com/baeldung/jackson}/dynamicobject/Product.java (92%) rename {jackson/src/main/java/com/baeldung/jackson/deserialization => jackson-conversions-2/src/main/java/com/baeldung/jackson}/dynamicobject/ProductJsonNode.java (90%) rename {jackson/src/main/java/com/baeldung/jackson/deserialization => jackson-conversions-2/src/main/java/com/baeldung/jackson}/dynamicobject/ProductMap.java (91%) rename {jackson-2/src/main/java/com/baeldung/jackson/entities => jackson-conversions-2/src/main/java/com/baeldung/jackson/multiplefields}/Weather.java (94%) create mode 100644 jackson-conversions-2/src/main/java/com/baeldung/jackson/unknownproperties/MyDto.java rename {jackson/src/test/java/com/baeldung/jackson/dtos/ignore => jackson-conversions-2/src/main/java/com/baeldung/jackson/unknownproperties}/MyDtoIgnoreType.java (95%) rename {jackson/src/test/java/com/baeldung/jackson/dtos/ignore => jackson-conversions-2/src/main/java/com/baeldung/jackson/unknownproperties}/MyDtoIgnoreUnknown.java (95%) rename {jackson => jackson-conversions-2}/src/main/java/com/baeldung/jackson/xmlToJson/Color.java (100%) rename {jackson => jackson-conversions-2}/src/main/java/com/baeldung/jackson/xmlToJson/Flower.java (100%) rename {jackson-2/src/main/java/com/baeldung/jackson/entities => jackson-conversions-2/src/main/java/com/baeldung/jackson/yaml}/Order.java (97%) create mode 100644 jackson-conversions-2/src/main/java/com/baeldung/jackson/yaml/OrderLine.java rename {jackson-2/src/main/resources => jackson-conversions-2/src/main/resources/csv}/orderLines.csv (100%) rename {jackson-2/src/main/resources => jackson-conversions-2/src/main/resources/csv}/orderLines.json (100%) rename {jackson-2 => jackson-conversions-2}/src/test/java/com/baeldung/jackson/csv/CsvUnitTest.java (61%) rename {jackson/src/test/java/com/baeldung/jackson/deserialization => jackson-conversions-2/src/test/java/com/baeldung/jackson}/dynamicobject/DynamicObjectDeserializationUnitTest.java (97%) create mode 100644 jackson-conversions-2/src/test/java/com/baeldung/jackson/ignorenullfields/IgnoreNullFieldsUnitTest.java create mode 100644 jackson-conversions-2/src/test/java/com/baeldung/jackson/ignorenullfields/MyDto.java create mode 100644 jackson-conversions-2/src/test/java/com/baeldung/jackson/ignorenullfields/MyDtoIgnoreNull.java rename jackson-2/src/test/java/com/baeldung/jackson/deserialization/jsonalias/JsonAliasUnitTest.java => jackson-conversions-2/src/test/java/com/baeldung/jackson/multiplefields/MapMultipleFieldsToSingleFieldUnitTest.java (88%) rename jackson/src/test/java/com/baeldung/jackson/streaming/JacksonStreamingAPIUnitTest.java => jackson-conversions-2/src/test/java/com/baeldung/jackson/streaming/StreamingAPIUnitTest.java (98%) create mode 100644 jackson-conversions-2/src/test/java/com/baeldung/jackson/unknownproperties/UnknownPropertiesUnitTest.java rename {jackson => jackson-conversions-2}/src/test/java/com/baeldung/jackson/xmlToJson/XmlToJsonUnitTest.java (100%) rename {jackson-2 => jackson-conversions-2}/src/test/java/com/baeldung/jackson/yaml/YamlUnitTest.java (86%) rename {jackson-2/src/test/resources => jackson-conversions-2/src/test/resources/csv}/expectedCsvFromJson.csv (100%) rename {jackson-2/src/test/resources => jackson-conversions-2/src/test/resources/csv}/expectedFormattedCsvFromJson.csv (100%) rename {jackson-2/src/test/resources => jackson-conversions-2/src/test/resources/csv}/expectedJsonFromCsv.json (100%) rename {jackson => jackson-conversions-2}/src/test/resources/deserialize-dynamic-object/embedded.json (100%) rename {jackson => jackson-conversions-2}/src/test/resources/deserialize-dynamic-object/flat.json (100%) rename {jackson-2/src/main/resources => jackson-conversions-2/src/test/resources/yaml}/orderInput.yaml (100%) create mode 100644 jackson-conversions/README.md create mode 100644 jackson-conversions/pom.xml rename {jackson/src/test => jackson-conversions/src/main}/java/com/baeldung/jackson/date/CustomDateDeserializer.java (100%) rename {jackson/src/test => jackson-conversions/src/main}/java/com/baeldung/jackson/date/CustomDateSerializer.java (100%) rename {jackson/src/test => jackson-conversions/src/main}/java/com/baeldung/jackson/date/CustomDateTimeSerializer.java (100%) rename {jackson/src/test => jackson-conversions/src/main}/java/com/baeldung/jackson/date/CustomLocalDateTimeSerializer.java (100%) rename {jackson/src/test => jackson-conversions/src/main}/java/com/baeldung/jackson/date/Event.java (100%) rename {jackson/src/test => jackson-conversions/src/main}/java/com/baeldung/jackson/date/EventWithFormat.java (100%) rename {jackson/src/test => jackson-conversions/src/main}/java/com/baeldung/jackson/date/EventWithJodaTime.java (100%) rename {jackson/src/test => jackson-conversions/src/main}/java/com/baeldung/jackson/date/EventWithLocalDateTime.java (100%) rename {jackson/src/test => jackson-conversions/src/main}/java/com/baeldung/jackson/date/EventWithSerializer.java (100%) rename {jackson/src/main/java/com/baeldung/jackson/deserialization/enums => jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization}/City.java (80%) rename {jackson/src/main/java/com/baeldung/jackson/deserialization/enums => jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization}/Distance.java (92%) rename {jackson/src/main/java/com/baeldung/jackson/deserialization/enums => jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization}/customdeserializer/City.java (77%) rename {jackson/src/main/java/com/baeldung/jackson/deserialization/enums => jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization}/customdeserializer/CustomEnumDeserializer.java (94%) rename {jackson/src/main/java/com/baeldung/jackson/deserialization/enums => jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization}/customdeserializer/Distance.java (92%) rename {jackson/src/main/java/com/baeldung/jackson/deserialization/enums => jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization}/jsoncreator/City.java (77%) rename {jackson/src/main/java/com/baeldung/jackson/deserialization/enums => jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization}/jsoncreator/Distance.java (94%) rename {jackson/src/main/java/com/baeldung/jackson/deserialization/enums => jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization}/jsonproperty/City.java (77%) rename {jackson/src/main/java/com/baeldung/jackson/deserialization/enums => jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization}/jsonproperty/Distance.java (93%) rename {jackson/src/main/java/com/baeldung/jackson/deserialization/enums => jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization}/jsonvalue/City.java (78%) rename {jackson/src/main/java/com/baeldung/jackson/deserialization/enums => jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization}/jsonvalue/Distance.java (91%) rename {jackson/src/main/java/com/baeldung/jackson/enums => jackson-conversions/src/main/java/com/baeldung/jackson/enums/serialization}/Distance.java (91%) rename {jackson/src/main/java/com/baeldung/jackson => jackson-conversions/src/main/java/com/baeldung/jackson/enums}/serialization/DistanceSerializer.java (92%) rename {jackson/src/test/java/com/baeldung/jackson/dtos => jackson-conversions/src/main/java/com/baeldung/jackson/enums}/withEnum/DistanceEnumSimple.java (92%) rename {jackson/src/test/java/com/baeldung/jackson/dtos => jackson-conversions/src/main/java/com/baeldung/jackson/enums}/withEnum/DistanceEnumWithJsonFormat.java (93%) rename {jackson/src/test/java/com/baeldung/jackson/dtos => jackson-conversions/src/main/java/com/baeldung/jackson/enums}/withEnum/DistanceEnumWithValue.java (93%) rename {jackson/src/test/java/com/baeldung/jackson/dtos => jackson-conversions/src/main/java/com/baeldung/jackson/enums}/withEnum/MyDtoWithEnumCustom.java (91%) rename {jackson/src/test/java/com/baeldung/jackson/dtos => jackson-conversions/src/main/java/com/baeldung/jackson/enums}/withEnum/MyDtoWithEnumJsonFormat.java (96%) rename {jackson/src/test => jackson-conversions/src/main}/java/com/baeldung/jackson/field/MyDto.java (100%) rename {jackson/src/test => jackson-conversions/src/main}/java/com/baeldung/jackson/field/MyDtoAccessLevel.java (100%) rename {jackson/src/test => jackson-conversions/src/main}/java/com/baeldung/jackson/field/MyDtoWithGetter.java (100%) rename {jackson/src/test => jackson-conversions/src/main}/java/com/baeldung/jackson/field/MyDtoWithSetter.java (100%) rename {jackson/src/main/java/com/baeldung/jackson/deserialization => jackson-conversions/src/main/java/com/baeldung/jackson}/immutable/Employee.java (89%) rename {jackson/src/main/java/com/baeldung/jackson/deserialization => jackson-conversions/src/main/java/com/baeldung/jackson}/immutable/Person.java (93%) rename {jackson/src/main/java/com/baeldung/jackson/entities => jackson-conversions/src/main/java/com/baeldung/jackson/map}/ClassWithAMap.java (83%) rename {jackson/src/main/java/com/baeldung/jackson/entities => jackson-conversions/src/main/java/com/baeldung/jackson/map}/MyPair.java (97%) rename {jackson/src/main/java/com/baeldung/jackson/serialization => jackson-conversions/src/main/java/com/baeldung/jackson/map}/MyPairDeserializer.java (83%) rename {jackson/src/main/java/com/baeldung/jackson/serialization => jackson-conversions/src/main/java/com/baeldung/jackson/map}/MyPairSerializer.java (89%) create mode 100644 jackson-conversions/src/main/java/com/baeldung/jackson/mapnull/MyDto.java rename {jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos => jackson-conversions/src/main/java/com/baeldung/jackson/mapnull}/MyDtoNullKeySerializer.java (94%) create mode 100644 jackson-conversions/src/main/java/com/baeldung/jackson/tocollection/MyDto.java create mode 100644 jackson-conversions/src/main/java/com/baeldung/jackson/xml/Address.java create mode 100644 jackson-conversions/src/main/java/com/baeldung/jackson/xml/Person.java rename {jackson/src/test/java/com/baeldung/jackson/test => jackson-conversions/src/test/java/com/baeldung/jackson/date}/JacksonDateUnitTest.java (99%) rename {jackson/src/test/java/com/baeldung/jackson/deserialization/enums => jackson-conversions/src/test/java/com/baeldung/jackson/enums/deserialization}/DefaultEnumDeserializationUnitTest.java (91%) rename {jackson/src/test/java/com/baeldung/jackson/deserialization/enums => jackson-conversions/src/test/java/com/baeldung/jackson/enums/deserialization}/customdeserializer/EnumCustomDeserializationUnitTest.java (90%) rename {jackson/src/test/java/com/baeldung/jackson/deserialization/enums => jackson-conversions/src/test/java/com/baeldung/jackson/enums/deserialization}/jsoncreator/EnumDeserializationUsingJsonCreatorUnitTest.java (90%) rename {jackson/src/test/java/com/baeldung/jackson/deserialization/enums => jackson-conversions/src/test/java/com/baeldung/jackson/enums/deserialization}/jsonproperty/EnumDeserializationUsingJsonPropertyUnitTest.java (90%) rename {jackson/src/test/java/com/baeldung/jackson/deserialization/enums => jackson-conversions/src/test/java/com/baeldung/jackson/enums/deserialization}/jsonvalue/EnumDeserializationUsingJsonValueUnitTest.java (90%) rename {jackson/src/test/java/com/baeldung/jackson/enums => jackson-conversions/src/test/java/com/baeldung/jackson/enums/serialization}/JacksonEnumSerializationUnitTest.java (92%) rename {jackson/src/test/java/com/baeldung/jackson/test => jackson-conversions/src/test/java/com/baeldung/jackson/enums/serialization}/JacksonSerializationEnumsUnitTest.java (85%) rename {jackson/src/test/java/com/baeldung/jackson/test => jackson-conversions/src/test/java/com/baeldung/jackson/field}/JacksonFieldUnitTest.java (95%) rename {jackson/src/test/java/com/baeldung/jackson/deserialization => jackson-conversions/src/test/java/com/baeldung/jackson}/immutable/ImmutableObjectDeserializationUnitTest.java (95%) rename {jackson/src/test/java/com/baeldung/jackson/deserialization => jackson-conversions/src/test/java/com/baeldung/jackson/map}/JacksonMapDeserializeUnitTest.java (93%) rename {jackson/src/test/java/com/baeldung/jackson/serialization => jackson-conversions/src/test/java/com/baeldung/jackson/map}/JacksonMapSerializeUnitTest.java (95%) create mode 100644 jackson-conversions/src/test/java/com/baeldung/jackson/mapnull/JacksonMapNullUnitTest.java rename {jackson/src/test/java/com/baeldung/jackson/deserialization => jackson-conversions/src/test/java/com/baeldung/jackson}/nested/DeserializeWithNestedPropertiesUnitTest.java (95%) rename {jackson/src/test/java/com/baeldung/jackson/deserialization => jackson-conversions/src/test/java/com/baeldung/jackson}/nested/Product.java (91%) rename {jackson/src/test/java/com/baeldung/jackson/deserialization => jackson-conversions/src/test/java/com/baeldung/jackson}/nested/ProductDeserializer.java (92%) rename {jackson/src/test/java/com/baeldung/jackson/test => jackson-conversions/src/test/java/com/baeldung/jackson/tocollection}/JacksonCollectionDeserializationUnitTest.java (97%) create mode 100644 jackson-conversions/src/test/java/com/baeldung/jackson/tojsonnode/StringToJsonNodeUnitTest.java rename {jackson => jackson-conversions}/src/test/java/com/baeldung/jackson/xml/XMLSerializeDeserializeUnitTest.java (98%) rename {jackson => jackson-conversions}/src/test/resources/simple_bean.xml (100%) rename jackson/src/{test => main}/java/com/baeldung/jackson/inheritance/IgnoranceAnnotationStructure.java (100%) rename jackson/src/{test => main}/java/com/baeldung/jackson/inheritance/IgnoranceMixinOrIntrospection.java (100%) rename jackson/src/{test => main}/java/com/baeldung/jackson/inheritance/SubTypeConstructorStructure.java (100%) rename jackson/src/{test => main}/java/com/baeldung/jackson/inheritance/SubTypeConversionStructure.java (100%) rename jackson/src/{test => main}/java/com/baeldung/jackson/inheritance/TypeInfoAnnotatedStructure.java (100%) rename jackson/src/{test => main}/java/com/baeldung/jackson/inheritance/TypeInfoStructure.java (100%) rename {jackson-simple/src/test => jackson/src/main}/java/com/baeldung/jackson/objectmapper/CustomCarDeserializer.java (100%) rename {jackson-simple/src/test => jackson/src/main}/java/com/baeldung/jackson/objectmapper/CustomCarSerializer.java (100%) rename {jackson-simple/src/test => jackson/src/main}/java/com/baeldung/jackson/objectmapper/dto/Car.java (100%) rename {jackson-simple/src/test => jackson/src/main}/java/com/baeldung/jackson/objectmapper/dto/Request.java (100%) rename {jackson-simple => jackson}/src/test/java/com/baeldung/jackson/objectmapper/JavaReadWriteJsonExampleUnitTest.java (99%) rename {jackson-simple => jackson}/src/test/java/com/baeldung/jackson/objectmapper/SerializationDeserializationFeatureUnitTest.java (99%) rename {jackson-simple => jackson}/src/test/resources/json_car.json (100%) diff --git a/jackson-2/README.md b/jackson-2/README.md index 5e0cb4ec37..b583b588ef 100644 --- a/jackson-2/README.md +++ b/jackson-2/README.md @@ -6,10 +6,7 @@ This module contains articles about Jackson. The "REST With Spring" Classes: http://bit.ly/restwithspring ### Relevant Articles: -- [Mapping Multiple JSON Fields to a Single Java Field](https://www.baeldung.com/json-multiple-fields-single-java-field) -- [How to Process YAML with Jackson](https://www.baeldung.com/jackson-yaml) - [Working with Tree Model Nodes in Jackson](https://www.baeldung.com/jackson-json-node-tree-model) -- [Converting JSON to CSV in Java](https://www.baeldung.com/java-converting-json-to-csv) - [Compare Two JSON Objects with Jackson](https://www.baeldung.com/jackson-compare-two-json-objects) - [Calling Default Serializer from Custom Serializer in Jackson](https://www.baeldung.com/jackson-call-default-serializer-from-custom-serializer) - More articles: [[<-- prev]](/../jackson) diff --git a/jackson-2/pom.xml b/jackson-2/pom.xml index 0d612c175d..87a050417c 100644 --- a/jackson-2/pom.xml +++ b/jackson-2/pom.xml @@ -14,20 +14,6 @@ - - - com.fasterxml.jackson.dataformat - jackson-dataformat-yaml - ${jackson.version} - - - - - com.fasterxml.jackson.dataformat - jackson-dataformat-csv - ${jackson.version} - - com.fasterxml.jackson.datatype diff --git a/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/JacksonSerializationIgnoreUnitTest.java b/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/JacksonSerializationIgnoreUnitTest.java index ab51fc11e7..c96eef4b05 100644 --- a/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/JacksonSerializationIgnoreUnitTest.java +++ b/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/JacksonSerializationIgnoreUnitTest.java @@ -18,7 +18,6 @@ import com.baeldung.jackson.ignore.dtos.MyMixInForIgnoreType; import com.baeldung.jackson.ignore.dtos.MyDtoIgnoreField; import com.baeldung.jackson.ignore.dtos.MyDtoIgnoreFieldByName; import com.baeldung.jackson.ignore.dtos.MyDtoIgnoreNull; -import com.baeldung.jackson.ignore.dtos.MyDtoNullKeySerializer; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParseException; @@ -191,88 +190,4 @@ public class JacksonSerializationIgnoreUnitTest { System.out.println(dtoAsString); } - // map - - @Test - public final void givenIgnoringMapNullValue_whenWritingMapObjectWithNullValue_thenIgnored() throws JsonProcessingException { - final ObjectMapper mapper = new ObjectMapper(); - // mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); - mapper.setSerializationInclusion(Include.NON_NULL); - - final MyDto dtoObject1 = new MyDto(); - - final Map dtoMap = new HashMap(); - dtoMap.put("dtoObject1", dtoObject1); - dtoMap.put("dtoObject2", null); - - final String dtoMapAsString = mapper.writeValueAsString(dtoMap); - - assertThat(dtoMapAsString, containsString("dtoObject1")); - assertThat(dtoMapAsString, not(containsString("dtoObject2"))); - System.out.println(dtoMapAsString); - } - - @Test - public final void givenIgnoringMapValueObjectWithNullField_whenWritingMapValueObjectWithNullField_thenIgnored() throws JsonProcessingException { - final ObjectMapper mapper = new ObjectMapper(); - mapper.setSerializationInclusion(Include.NON_NULL); - - final MyDto dtoObject = new MyDto(); - - final Map dtoMap = new HashMap(); - dtoMap.put("dtoObject", dtoObject); - - final String dtoMapAsString = mapper.writeValueAsString(dtoMap); - - assertThat(dtoMapAsString, containsString("dtoObject")); - assertThat(dtoMapAsString, not(containsString("stringValue"))); - System.out.println(dtoMapAsString); - } - - @Test - public final void givenAllowingMapObjectWithNullKey_whenWriting_thenCorrect() throws JsonProcessingException { - final ObjectMapper mapper = new ObjectMapper(); - mapper.getSerializerProvider() - .setNullKeySerializer(new MyDtoNullKeySerializer()); - - final MyDto dtoObject1 = new MyDto(); - dtoObject1.setStringValue("dtoObjectString1"); - final MyDto dtoObject2 = new MyDto(); - dtoObject2.setStringValue("dtoObjectString2"); - - final Map dtoMap = new HashMap(); - dtoMap.put(null, dtoObject1); - dtoMap.put("obj2", dtoObject2); - - final String dtoMapAsString = mapper.writeValueAsString(dtoMap); - - System.out.println(dtoMapAsString); - assertThat(dtoMapAsString, containsString("\"\"")); - assertThat(dtoMapAsString, containsString("dtoObjectString1")); - assertThat(dtoMapAsString, containsString("obj2")); - } - - @Test - public final void givenAllowingMapObjectOneNullKey_whenWritingMapObjectWithTwoNullKeys_thenOverride() throws JsonProcessingException { - final ObjectMapper mapper = new ObjectMapper(); - mapper.getSerializerProvider() - .setNullKeySerializer(new MyDtoNullKeySerializer()); - - final MyDto dtoObject1 = new MyDto(); - dtoObject1.setStringValue("dtoObject1String"); - - final MyDto dtoObject2 = new MyDto(); - dtoObject2.setStringValue("dtoObject2String"); - - final Map dtoMap = new HashMap(); - dtoMap.put(null, dtoObject1); - dtoMap.put(null, dtoObject2); - - final String dtoMapAsString = mapper.writeValueAsString(dtoMap); - - assertThat(dtoMapAsString, not(containsString("dtoObject1String"))); - assertThat(dtoMapAsString, containsString("dtoObject2String")); - System.out.println(dtoMapAsString); - } - } diff --git a/jackson-conversions-2/README.md b/jackson-conversions-2/README.md new file mode 100644 index 0000000000..210f5cd5bb --- /dev/null +++ b/jackson-conversions-2/README.md @@ -0,0 +1,14 @@ +## Jackson Conversions + +This module contains articles about Jackson conversions. + +### Relevant Articles: +- [Mapping a Dynamic JSON Object with Jackson](https://www.baeldung.com/jackson-mapping-dynamic-object) +- [Jackson Unmarshalling JSON with Unknown Properties](https://www.baeldung.com/jackson-deserialize-json-unknown-properties) +- [Ignore Null Fields with Jackson](https://www.baeldung.com/jackson-ignore-null-fields) +- [Mapping Multiple JSON Fields to a Single Java Field](https://www.baeldung.com/json-multiple-fields-single-java-field) +- [Convert XML to JSON Using Jackson](https://www.baeldung.com/jackson-convert-xml-json) +- [Converting JSON to CSV in Java](https://www.baeldung.com/java-converting-json-to-csv) +- [How to Process YAML with Jackson](https://www.baeldung.com/jackson-yaml) +- [Jackson Streaming API](https://www.baeldung.com/jackson-streaming-api) +- More articles: [[<-- prev]](../jackson-conversions) diff --git a/jackson-conversions-2/pom.xml b/jackson-conversions-2/pom.xml new file mode 100644 index 0000000000..07104a8972 --- /dev/null +++ b/jackson-conversions-2/pom.xml @@ -0,0 +1,67 @@ + + 4.0.0 + jackson-conversions-2 + 0.0.1-SNAPSHOT + jackson-conversions-2 + + + com.baeldung + parent-java + 0.0.1-SNAPSHOT + ../parent-java + + + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + ${jackson.version} + + + + com.fasterxml.jackson.dataformat + jackson-dataformat-yaml + ${jackson.version} + + + + com.fasterxml.jackson.datatype + jackson-datatype-jsr310 + 2.9.8 + + + + com.fasterxml.jackson.dataformat + jackson-dataformat-csv + ${jackson.version} + + + org.assertj + assertj-core + ${assertj.version} + test + + + + + jackson-conversions-2 + + + src/main/resources + true + + + + + + 3.11.0 + + + diff --git a/jackson-2/src/main/java/com/baeldung/jackson/csv/JsonCsvConverter.java b/jackson-conversions-2/src/main/java/com/baeldung/jackson/csv/JsonCsvConverter.java similarity index 95% rename from jackson-2/src/main/java/com/baeldung/jackson/csv/JsonCsvConverter.java rename to jackson-conversions-2/src/main/java/com/baeldung/jackson/csv/JsonCsvConverter.java index 71c6de4d7e..9d500bf263 100644 --- a/jackson-2/src/main/java/com/baeldung/jackson/csv/JsonCsvConverter.java +++ b/jackson-conversions-2/src/main/java/com/baeldung/jackson/csv/JsonCsvConverter.java @@ -3,8 +3,6 @@ package com.baeldung.jackson.csv; import java.io.File; import java.io.IOException; -import com.baeldung.jackson.entities.OrderLine; -import com.baeldung.jackson.mixin.OrderLineForCsv; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.MappingIterator; import com.fasterxml.jackson.databind.ObjectMapper; diff --git a/jackson-2/src/main/java/com/baeldung/jackson/entities/OrderLine.java b/jackson-conversions-2/src/main/java/com/baeldung/jackson/csv/OrderLine.java similarity index 96% rename from jackson-2/src/main/java/com/baeldung/jackson/entities/OrderLine.java rename to jackson-conversions-2/src/main/java/com/baeldung/jackson/csv/OrderLine.java index 858d094dd1..b6c22b0b1d 100644 --- a/jackson-2/src/main/java/com/baeldung/jackson/entities/OrderLine.java +++ b/jackson-conversions-2/src/main/java/com/baeldung/jackson/csv/OrderLine.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.entities; +package com.baeldung.jackson.csv; import java.math.BigDecimal; diff --git a/jackson-2/src/main/java/com/baeldung/jackson/mixin/OrderLineForCsv.java b/jackson-conversions-2/src/main/java/com/baeldung/jackson/csv/OrderLineForCsv.java similarity index 92% rename from jackson-2/src/main/java/com/baeldung/jackson/mixin/OrderLineForCsv.java rename to jackson-conversions-2/src/main/java/com/baeldung/jackson/csv/OrderLineForCsv.java index 05d70a8053..94a1573a84 100644 --- a/jackson-2/src/main/java/com/baeldung/jackson/mixin/OrderLineForCsv.java +++ b/jackson-conversions-2/src/main/java/com/baeldung/jackson/csv/OrderLineForCsv.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.mixin; +package com.baeldung.jackson.csv; import java.math.BigDecimal; diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/dynamicobject/Product.java b/jackson-conversions-2/src/main/java/com/baeldung/jackson/dynamicobject/Product.java similarity index 92% rename from jackson/src/main/java/com/baeldung/jackson/deserialization/dynamicobject/Product.java rename to jackson-conversions-2/src/main/java/com/baeldung/jackson/dynamicobject/Product.java index 9c997886e5..89a57a124c 100644 --- a/jackson/src/main/java/com/baeldung/jackson/deserialization/dynamicobject/Product.java +++ b/jackson-conversions-2/src/main/java/com/baeldung/jackson/dynamicobject/Product.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.deserialization.dynamicobject; +package com.baeldung.jackson.dynamicobject; import java.util.LinkedHashMap; import java.util.Map; diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/dynamicobject/ProductJsonNode.java b/jackson-conversions-2/src/main/java/com/baeldung/jackson/dynamicobject/ProductJsonNode.java similarity index 90% rename from jackson/src/main/java/com/baeldung/jackson/deserialization/dynamicobject/ProductJsonNode.java rename to jackson-conversions-2/src/main/java/com/baeldung/jackson/dynamicobject/ProductJsonNode.java index 729d542df3..b9142ae6e7 100644 --- a/jackson/src/main/java/com/baeldung/jackson/deserialization/dynamicobject/ProductJsonNode.java +++ b/jackson-conversions-2/src/main/java/com/baeldung/jackson/dynamicobject/ProductJsonNode.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.deserialization.dynamicobject; +package com.baeldung.jackson.dynamicobject; import com.fasterxml.jackson.databind.JsonNode; diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/dynamicobject/ProductMap.java b/jackson-conversions-2/src/main/java/com/baeldung/jackson/dynamicobject/ProductMap.java similarity index 91% rename from jackson/src/main/java/com/baeldung/jackson/deserialization/dynamicobject/ProductMap.java rename to jackson-conversions-2/src/main/java/com/baeldung/jackson/dynamicobject/ProductMap.java index 65d225f49b..62b7a43eae 100644 --- a/jackson/src/main/java/com/baeldung/jackson/deserialization/dynamicobject/ProductMap.java +++ b/jackson-conversions-2/src/main/java/com/baeldung/jackson/dynamicobject/ProductMap.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.deserialization.dynamicobject; +package com.baeldung.jackson.dynamicobject; import java.util.Map; diff --git a/jackson-2/src/main/java/com/baeldung/jackson/entities/Weather.java b/jackson-conversions-2/src/main/java/com/baeldung/jackson/multiplefields/Weather.java similarity index 94% rename from jackson-2/src/main/java/com/baeldung/jackson/entities/Weather.java rename to jackson-conversions-2/src/main/java/com/baeldung/jackson/multiplefields/Weather.java index 4a8cea052f..d1d6905d53 100644 --- a/jackson-2/src/main/java/com/baeldung/jackson/entities/Weather.java +++ b/jackson-conversions-2/src/main/java/com/baeldung/jackson/multiplefields/Weather.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.entities; +package com.baeldung.jackson.multiplefields; import com.fasterxml.jackson.annotation.JsonAlias; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/jackson-conversions-2/src/main/java/com/baeldung/jackson/unknownproperties/MyDto.java b/jackson-conversions-2/src/main/java/com/baeldung/jackson/unknownproperties/MyDto.java new file mode 100644 index 0000000000..7c8046f6be --- /dev/null +++ b/jackson-conversions-2/src/main/java/com/baeldung/jackson/unknownproperties/MyDto.java @@ -0,0 +1,54 @@ +package com.baeldung.jackson.unknownproperties; + +public class MyDto { + + private String stringValue; + private int intValue; + private boolean booleanValue; + + public MyDto() { + super(); + } + + public MyDto(final String stringValue, final int intValue, final boolean booleanValue) { + super(); + + this.stringValue = stringValue; + this.intValue = intValue; + this.booleanValue = booleanValue; + } + + // API + + public String getStringValue() { + return stringValue; + } + + public void setStringValue(final String stringValue) { + this.stringValue = stringValue; + } + + public int getIntValue() { + return intValue; + } + + public void setIntValue(final int intValue) { + this.intValue = intValue; + } + + public boolean isBooleanValue() { + return booleanValue; + } + + public void setBooleanValue(final boolean booleanValue) { + this.booleanValue = booleanValue; + } + + // + + @Override + public String toString() { + return "MyDto [stringValue=" + stringValue + ", intValue=" + intValue + ", booleanValue=" + booleanValue + "]"; + } + +} diff --git a/jackson/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreType.java b/jackson-conversions-2/src/main/java/com/baeldung/jackson/unknownproperties/MyDtoIgnoreType.java similarity index 95% rename from jackson/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreType.java rename to jackson-conversions-2/src/main/java/com/baeldung/jackson/unknownproperties/MyDtoIgnoreType.java index 3c813145f6..89456020b2 100644 --- a/jackson/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreType.java +++ b/jackson-conversions-2/src/main/java/com/baeldung/jackson/unknownproperties/MyDtoIgnoreType.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.dtos.ignore; +package com.baeldung.jackson.unknownproperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; diff --git a/jackson/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreUnknown.java b/jackson-conversions-2/src/main/java/com/baeldung/jackson/unknownproperties/MyDtoIgnoreUnknown.java similarity index 95% rename from jackson/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreUnknown.java rename to jackson-conversions-2/src/main/java/com/baeldung/jackson/unknownproperties/MyDtoIgnoreUnknown.java index c1174a12f5..ebbe528c83 100644 --- a/jackson/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreUnknown.java +++ b/jackson-conversions-2/src/main/java/com/baeldung/jackson/unknownproperties/MyDtoIgnoreUnknown.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.dtos.ignore; +package com.baeldung.jackson.unknownproperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; diff --git a/jackson/src/main/java/com/baeldung/jackson/xmlToJson/Color.java b/jackson-conversions-2/src/main/java/com/baeldung/jackson/xmlToJson/Color.java similarity index 100% rename from jackson/src/main/java/com/baeldung/jackson/xmlToJson/Color.java rename to jackson-conversions-2/src/main/java/com/baeldung/jackson/xmlToJson/Color.java diff --git a/jackson/src/main/java/com/baeldung/jackson/xmlToJson/Flower.java b/jackson-conversions-2/src/main/java/com/baeldung/jackson/xmlToJson/Flower.java similarity index 100% rename from jackson/src/main/java/com/baeldung/jackson/xmlToJson/Flower.java rename to jackson-conversions-2/src/main/java/com/baeldung/jackson/xmlToJson/Flower.java diff --git a/jackson-2/src/main/java/com/baeldung/jackson/entities/Order.java b/jackson-conversions-2/src/main/java/com/baeldung/jackson/yaml/Order.java similarity index 97% rename from jackson-2/src/main/java/com/baeldung/jackson/entities/Order.java rename to jackson-conversions-2/src/main/java/com/baeldung/jackson/yaml/Order.java index 2075b7879b..8c62a0bd3f 100644 --- a/jackson-2/src/main/java/com/baeldung/jackson/entities/Order.java +++ b/jackson-conversions-2/src/main/java/com/baeldung/jackson/yaml/Order.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.entities; +package com.baeldung.jackson.yaml; import java.time.LocalDate; import java.util.ArrayList; diff --git a/jackson-conversions-2/src/main/java/com/baeldung/jackson/yaml/OrderLine.java b/jackson-conversions-2/src/main/java/com/baeldung/jackson/yaml/OrderLine.java new file mode 100644 index 0000000000..c64eaa3832 --- /dev/null +++ b/jackson-conversions-2/src/main/java/com/baeldung/jackson/yaml/OrderLine.java @@ -0,0 +1,49 @@ +package com.baeldung.jackson.yaml; + +import java.math.BigDecimal; + +public class OrderLine { + private String item; + private int quantity; + private BigDecimal unitPrice; + + public OrderLine() { + + } + + public OrderLine(String item, int quantity, BigDecimal unitPrice) { + super(); + this.item = item; + this.quantity = quantity; + this.unitPrice = unitPrice; + } + + public String getItem() { + return item; + } + + public void setItem(String item) { + this.item = item; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public BigDecimal getUnitPrice() { + return unitPrice; + } + + public void setUnitPrice(BigDecimal unitPrice) { + this.unitPrice = unitPrice; + } + + @Override + public String toString() { + return "OrderLine [item=" + item + ", quantity=" + quantity + ", unitPrice=" + unitPrice + "]"; + } +} diff --git a/jackson-2/src/main/resources/orderLines.csv b/jackson-conversions-2/src/main/resources/csv/orderLines.csv similarity index 100% rename from jackson-2/src/main/resources/orderLines.csv rename to jackson-conversions-2/src/main/resources/csv/orderLines.csv diff --git a/jackson-2/src/main/resources/orderLines.json b/jackson-conversions-2/src/main/resources/csv/orderLines.json similarity index 100% rename from jackson-2/src/main/resources/orderLines.json rename to jackson-conversions-2/src/main/resources/csv/orderLines.json diff --git a/jackson-2/src/test/java/com/baeldung/jackson/csv/CsvUnitTest.java b/jackson-conversions-2/src/test/java/com/baeldung/jackson/csv/CsvUnitTest.java similarity index 61% rename from jackson-2/src/test/java/com/baeldung/jackson/csv/CsvUnitTest.java rename to jackson-conversions-2/src/test/java/com/baeldung/jackson/csv/CsvUnitTest.java index 60c8ce79f3..c257cb7dc9 100644 --- a/jackson-2/src/test/java/com/baeldung/jackson/csv/CsvUnitTest.java +++ b/jackson-conversions-2/src/test/java/com/baeldung/jackson/csv/CsvUnitTest.java @@ -18,30 +18,30 @@ public class CsvUnitTest { @Test public void givenJsonInput_thenWriteCsv() throws JsonParseException, JsonMappingException, IOException { - JsonCsvConverter.JsonToCsv(new File("src/main/resources/orderLines.json"), - new File("src/main/resources/csvFromJson.csv")); + JsonCsvConverter.JsonToCsv(new File("src/main/resources/csv/orderLines.json"), + new File("src/main/resources/csv/csvFromJson.csv")); - assertEquals(readFile("src/main/resources/csvFromJson.csv"), - readFile("src/test/resources/expectedCsvFromJson.csv")); + assertEquals(readFile("src/main/resources/csv/csvFromJson.csv"), + readFile("src/test/resources/csv/expectedCsvFromJson.csv")); } @Test public void givenCsvInput_thenWritesJson() throws JsonParseException, JsonMappingException, IOException { - JsonCsvConverter.csvToJson(new File("src/main/resources/orderLines.csv"), - new File("src/main/resources/jsonFromCsv.json")); + JsonCsvConverter.csvToJson(new File("src/main/resources/csv/orderLines.csv"), + new File("src/main/resources/csv/jsonFromCsv.json")); - assertEquals(readFile("src/main/resources/jsonFromCsv.json"), - readFile("src/test/resources/expectedJsonFromCsv.json")); + assertEquals(readFile("src/main/resources/csv/jsonFromCsv.json"), + readFile("src/test/resources/csv/expectedJsonFromCsv.json")); } @Test public void givenJsonInput_thenWriteFormattedCsvOutput() throws JsonParseException, JsonMappingException, IOException { - JsonCsvConverter.JsonToFormattedCsv(new File("src/main/resources/orderLines.json"), - new File("src/main/resources/formattedCsvFromJson.csv")); + JsonCsvConverter.JsonToFormattedCsv(new File("src/main/resources/csv/orderLines.json"), + new File("src/main/resources/csv/formattedCsvFromJson.csv")); - assertEquals(readFile("src/main/resources/formattedCsvFromJson.csv"), - readFile("src/test/resources/expectedFormattedCsvFromJson.csv")); + assertEquals(readFile("src/main/resources/csv/formattedCsvFromJson.csv"), + readFile("src/test/resources/csv/expectedFormattedCsvFromJson.csv")); } diff --git a/jackson/src/test/java/com/baeldung/jackson/deserialization/dynamicobject/DynamicObjectDeserializationUnitTest.java b/jackson-conversions-2/src/test/java/com/baeldung/jackson/dynamicobject/DynamicObjectDeserializationUnitTest.java similarity index 97% rename from jackson/src/test/java/com/baeldung/jackson/deserialization/dynamicobject/DynamicObjectDeserializationUnitTest.java rename to jackson-conversions-2/src/test/java/com/baeldung/jackson/dynamicobject/DynamicObjectDeserializationUnitTest.java index c9536bc35a..0c222bffcc 100644 --- a/jackson/src/test/java/com/baeldung/jackson/deserialization/dynamicobject/DynamicObjectDeserializationUnitTest.java +++ b/jackson-conversions-2/src/test/java/com/baeldung/jackson/dynamicobject/DynamicObjectDeserializationUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.deserialization.dynamicobject; +package com.baeldung.jackson.dynamicobject; import static org.assertj.core.api.Assertions.assertThat; diff --git a/jackson-conversions-2/src/test/java/com/baeldung/jackson/ignorenullfields/IgnoreNullFieldsUnitTest.java b/jackson-conversions-2/src/test/java/com/baeldung/jackson/ignorenullfields/IgnoreNullFieldsUnitTest.java new file mode 100644 index 0000000000..192fe61889 --- /dev/null +++ b/jackson-conversions-2/src/test/java/com/baeldung/jackson/ignorenullfields/IgnoreNullFieldsUnitTest.java @@ -0,0 +1,52 @@ +package com.baeldung.jackson.ignorenullfields; + +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.BeanPropertyWriter; +import com.fasterxml.jackson.databind.ser.FilterProvider; +import com.fasterxml.jackson.databind.ser.PropertyFilter; +import com.fasterxml.jackson.databind.ser.PropertyWriter; +import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter; +import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; +import org.junit.Test; + +import java.io.IOException; + +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertThat; + +public class IgnoreNullFieldsUnitTest { + + @Test + public final void givenNullsIgnoredOnClass_whenWritingObjectWithNullField_thenIgnored() throws JsonProcessingException { + final ObjectMapper mapper = new ObjectMapper(); + final MyDtoIgnoreNull dtoObject = new MyDtoIgnoreNull(); + + final String dtoAsString = mapper.writeValueAsString(dtoObject); + + assertThat(dtoAsString, containsString("intValue")); + assertThat(dtoAsString, containsString("booleanValue")); + assertThat(dtoAsString, not(containsString("stringValue"))); + System.out.println(dtoAsString); + } + + @Test + public final void givenNullsIgnoredGlobally_whenWritingObjectWithNullField_thenIgnored() throws JsonProcessingException { + final ObjectMapper mapper = new ObjectMapper(); + mapper.setSerializationInclusion(Include.NON_NULL); + final MyDto dtoObject = new MyDto(); + + final String dtoAsString = mapper.writeValueAsString(dtoObject); + + assertThat(dtoAsString, containsString("intValue")); + assertThat(dtoAsString, containsString("booleanValue")); + assertThat(dtoAsString, not(containsString("stringValue"))); + System.out.println(dtoAsString); + } + +} diff --git a/jackson-conversions-2/src/test/java/com/baeldung/jackson/ignorenullfields/MyDto.java b/jackson-conversions-2/src/test/java/com/baeldung/jackson/ignorenullfields/MyDto.java new file mode 100644 index 0000000000..3737dd35f6 --- /dev/null +++ b/jackson-conversions-2/src/test/java/com/baeldung/jackson/ignorenullfields/MyDto.java @@ -0,0 +1,54 @@ +package com.baeldung.jackson.ignorenullfields; + +public class MyDto { + + private String stringValue; + private int intValue; + private boolean booleanValue; + + public MyDto() { + super(); + } + + public MyDto(final String stringValue, final int intValue, final boolean booleanValue) { + super(); + + this.stringValue = stringValue; + this.intValue = intValue; + this.booleanValue = booleanValue; + } + + // API + + public String getStringValue() { + return stringValue; + } + + public void setStringValue(final String stringValue) { + this.stringValue = stringValue; + } + + public int getIntValue() { + return intValue; + } + + public void setIntValue(final int intValue) { + this.intValue = intValue; + } + + public boolean isBooleanValue() { + return booleanValue; + } + + public void setBooleanValue(final boolean booleanValue) { + this.booleanValue = booleanValue; + } + + // + + @Override + public String toString() { + return "MyDto [stringValue=" + stringValue + ", intValue=" + intValue + ", booleanValue=" + booleanValue + "]"; + } + +} diff --git a/jackson-conversions-2/src/test/java/com/baeldung/jackson/ignorenullfields/MyDtoIgnoreNull.java b/jackson-conversions-2/src/test/java/com/baeldung/jackson/ignorenullfields/MyDtoIgnoreNull.java new file mode 100644 index 0000000000..6aad3a3413 --- /dev/null +++ b/jackson-conversions-2/src/test/java/com/baeldung/jackson/ignorenullfields/MyDtoIgnoreNull.java @@ -0,0 +1,51 @@ +package com.baeldung.jackson.ignorenullfields; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +@JsonInclude(Include.NON_NULL) +public class MyDtoIgnoreNull { + + private String stringValue; + private int intValue; + private boolean booleanValue; + + public MyDtoIgnoreNull() { + super(); + } + + public MyDtoIgnoreNull(final String stringValue, final int intValue, final boolean booleanValue) { + super(); + + this.stringValue = stringValue; + this.intValue = intValue; + this.booleanValue = booleanValue; + } + + // API + + public String getStringValue() { + return stringValue; + } + + public void setStringValue(final String stringValue) { + this.stringValue = stringValue; + } + + public int getIntValue() { + return intValue; + } + + public void setIntValue(final int intValue) { + this.intValue = intValue; + } + + public boolean isBooleanValue() { + return booleanValue; + } + + public void setBooleanValue(final boolean booleanValue) { + this.booleanValue = booleanValue; + } + +} diff --git a/jackson-2/src/test/java/com/baeldung/jackson/deserialization/jsonalias/JsonAliasUnitTest.java b/jackson-conversions-2/src/test/java/com/baeldung/jackson/multiplefields/MapMultipleFieldsToSingleFieldUnitTest.java similarity index 88% rename from jackson-2/src/test/java/com/baeldung/jackson/deserialization/jsonalias/JsonAliasUnitTest.java rename to jackson-conversions-2/src/test/java/com/baeldung/jackson/multiplefields/MapMultipleFieldsToSingleFieldUnitTest.java index b5940a7bd7..1a50f6cfc1 100644 --- a/jackson-2/src/test/java/com/baeldung/jackson/deserialization/jsonalias/JsonAliasUnitTest.java +++ b/jackson-conversions-2/src/test/java/com/baeldung/jackson/multiplefields/MapMultipleFieldsToSingleFieldUnitTest.java @@ -1,13 +1,12 @@ -package com.baeldung.jackson.deserialization.jsonalias; +package com.baeldung.jackson.multiplefields; import static org.junit.Assert.assertEquals; import org.junit.Test; -import com.baeldung.jackson.entities.Weather; import com.fasterxml.jackson.databind.ObjectMapper; -public class JsonAliasUnitTest { +public class MapMultipleFieldsToSingleFieldUnitTest { @Test public void givenTwoJsonFormats_whenDeserialized_thenWeatherObjectsCreated() throws Exception { diff --git a/jackson/src/test/java/com/baeldung/jackson/streaming/JacksonStreamingAPIUnitTest.java b/jackson-conversions-2/src/test/java/com/baeldung/jackson/streaming/StreamingAPIUnitTest.java similarity index 98% rename from jackson/src/test/java/com/baeldung/jackson/streaming/JacksonStreamingAPIUnitTest.java rename to jackson-conversions-2/src/test/java/com/baeldung/jackson/streaming/StreamingAPIUnitTest.java index ff22682f37..5deb279c5c 100644 --- a/jackson/src/test/java/com/baeldung/jackson/streaming/JacksonStreamingAPIUnitTest.java +++ b/jackson-conversions-2/src/test/java/com/baeldung/jackson/streaming/StreamingAPIUnitTest.java @@ -13,7 +13,7 @@ import static junit.framework.Assert.assertNull; import static junit.framework.Assert.assertTrue; import static junit.framework.TestCase.assertEquals; -public class JacksonStreamingAPIUnitTest { +public class StreamingAPIUnitTest { @Test public void givenJsonGenerator_whenAppendJsonToIt_thenGenerateJson() throws IOException { diff --git a/jackson-conversions-2/src/test/java/com/baeldung/jackson/unknownproperties/UnknownPropertiesUnitTest.java b/jackson-conversions-2/src/test/java/com/baeldung/jackson/unknownproperties/UnknownPropertiesUnitTest.java new file mode 100644 index 0000000000..ecdde4736d --- /dev/null +++ b/jackson-conversions-2/src/test/java/com/baeldung/jackson/unknownproperties/UnknownPropertiesUnitTest.java @@ -0,0 +1,80 @@ +package com.baeldung.jackson.unknownproperties; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; +import org.junit.Test; + +import java.io.IOException; + +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; + +public class UnknownPropertiesUnitTest { + + @Test + public final void givenNotAllFieldsHaveValuesInJson_whenDeserializingAJsonToAClass_thenCorrect() throws JsonParseException, JsonMappingException, IOException { + final String jsonAsString = "{\"stringValue\":\"a\",\"booleanValue\":true}"; + final ObjectMapper mapper = new ObjectMapper(); + + final MyDto readValue = mapper.readValue(jsonAsString, MyDto.class); + + assertNotNull(readValue); + assertThat(readValue.getStringValue(), equalTo("a")); + assertThat(readValue.isBooleanValue(), equalTo(true)); + } + + // tests - json with unknown fields + + @Test(expected = UnrecognizedPropertyException.class) + public final void givenJsonHasUnknownValues_whenDeserializingAJsonToAClass_thenExceptionIsThrown() throws JsonParseException, JsonMappingException, IOException { + final String jsonAsString = "{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":true,\"stringValue2\":\"something\"}"; + final ObjectMapper mapper = new ObjectMapper(); + + final MyDto readValue = mapper.readValue(jsonAsString, MyDto.class); + + assertNotNull(readValue); + assertThat(readValue.getStringValue(), equalTo("a")); + assertThat(readValue.isBooleanValue(), equalTo(true)); + assertThat(readValue.getIntValue(), equalTo(1)); + } + + @Test + public final void givenJsonHasUnknownValuesButJacksonIsIgnoringUnknownFields_whenDeserializing_thenCorrect() throws JsonParseException, JsonMappingException, IOException { + final String jsonAsString = // @formatter:off + "{\"stringValue\":\"a\"," + + "\"intValue\":1," + + "\"booleanValue\":true," + + "\"stringValue2\":\"something\"}"; // @formatter:on + final ObjectMapper mapper = new ObjectMapper(); + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + + final MyDto readValue = mapper.readValue(jsonAsString, MyDto.class); + + assertNotNull(readValue); + assertThat(readValue.getStringValue(), equalTo("a")); + assertThat(readValue.isBooleanValue(), equalTo(true)); + assertThat(readValue.getIntValue(), equalTo(1)); + } + + @Test + public final void givenJsonHasUnknownValuesButUnknownFieldsAreIgnoredOnClass_whenDeserializing_thenCorrect() throws JsonParseException, JsonMappingException, IOException { + final String jsonAsString = // @formatter:off + "{\"stringValue\":\"a\"," + + "\"intValue\":1," + + "\"booleanValue\":true," + + "\"stringValue2\":\"something\"}"; // @formatter:on + final ObjectMapper mapper = new ObjectMapper(); + + final MyDtoIgnoreUnknown readValue = mapper.readValue(jsonAsString, MyDtoIgnoreUnknown.class); + + assertNotNull(readValue); + assertThat(readValue.getStringValue(), equalTo("a")); + assertThat(readValue.isBooleanValue(), equalTo(true)); + assertThat(readValue.getIntValue(), equalTo(1)); + } + +} diff --git a/jackson/src/test/java/com/baeldung/jackson/xmlToJson/XmlToJsonUnitTest.java b/jackson-conversions-2/src/test/java/com/baeldung/jackson/xmlToJson/XmlToJsonUnitTest.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/xmlToJson/XmlToJsonUnitTest.java rename to jackson-conversions-2/src/test/java/com/baeldung/jackson/xmlToJson/XmlToJsonUnitTest.java diff --git a/jackson-2/src/test/java/com/baeldung/jackson/yaml/YamlUnitTest.java b/jackson-conversions-2/src/test/java/com/baeldung/jackson/yaml/YamlUnitTest.java similarity index 86% rename from jackson-2/src/test/java/com/baeldung/jackson/yaml/YamlUnitTest.java rename to jackson-conversions-2/src/test/java/com/baeldung/jackson/yaml/YamlUnitTest.java index 3ed84db60e..17c487ff26 100644 --- a/jackson-2/src/test/java/com/baeldung/jackson/yaml/YamlUnitTest.java +++ b/jackson-conversions-2/src/test/java/com/baeldung/jackson/yaml/YamlUnitTest.java @@ -15,8 +15,6 @@ import java.util.List; import org.junit.Before; import org.junit.Test; -import com.baeldung.jackson.entities.Order; -import com.baeldung.jackson.entities.OrderLine; import com.fasterxml.jackson.core.JsonGenerationException; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; @@ -37,7 +35,7 @@ public class YamlUnitTest { @Test public void givenYamlInput_ObjectCreated() throws JsonParseException, JsonMappingException, IOException { - Order order = mapper.readValue(new File("src/main/resources/orderInput.yaml"), Order.class); + Order order = mapper.readValue(new File("src/test/resources/yaml/orderInput.yaml"), Order.class); assertEquals("A001", order.getOrderNo()); assertEquals(LocalDate.parse("2019-04-17", DateTimeFormatter.ISO_DATE), order.getDate()); assertEquals("Customer, Joe", order.getCustomerName()); @@ -55,9 +53,9 @@ public class YamlUnitTest { LocalDate.parse("2019-04-18", DateTimeFormatter.ISO_DATE), "Customer, Jane", lines); - mapper.writeValue(new File("src/main/resources/orderOutput.yaml"), order); + mapper.writeValue(new File("src/test/resources/yaml/orderOutput.yaml"), order); - File outputYaml = new File("src/main/resources/orderOutput.yaml"); + File outputYaml = new File("src/test/resources/yaml/orderOutput.yaml"); assertTrue(outputYaml.exists()); } } diff --git a/jackson-2/src/test/resources/expectedCsvFromJson.csv b/jackson-conversions-2/src/test/resources/csv/expectedCsvFromJson.csv similarity index 100% rename from jackson-2/src/test/resources/expectedCsvFromJson.csv rename to jackson-conversions-2/src/test/resources/csv/expectedCsvFromJson.csv diff --git a/jackson-2/src/test/resources/expectedFormattedCsvFromJson.csv b/jackson-conversions-2/src/test/resources/csv/expectedFormattedCsvFromJson.csv similarity index 100% rename from jackson-2/src/test/resources/expectedFormattedCsvFromJson.csv rename to jackson-conversions-2/src/test/resources/csv/expectedFormattedCsvFromJson.csv diff --git a/jackson-2/src/test/resources/expectedJsonFromCsv.json b/jackson-conversions-2/src/test/resources/csv/expectedJsonFromCsv.json similarity index 100% rename from jackson-2/src/test/resources/expectedJsonFromCsv.json rename to jackson-conversions-2/src/test/resources/csv/expectedJsonFromCsv.json diff --git a/jackson/src/test/resources/deserialize-dynamic-object/embedded.json b/jackson-conversions-2/src/test/resources/deserialize-dynamic-object/embedded.json similarity index 100% rename from jackson/src/test/resources/deserialize-dynamic-object/embedded.json rename to jackson-conversions-2/src/test/resources/deserialize-dynamic-object/embedded.json diff --git a/jackson/src/test/resources/deserialize-dynamic-object/flat.json b/jackson-conversions-2/src/test/resources/deserialize-dynamic-object/flat.json similarity index 100% rename from jackson/src/test/resources/deserialize-dynamic-object/flat.json rename to jackson-conversions-2/src/test/resources/deserialize-dynamic-object/flat.json diff --git a/jackson-2/src/main/resources/orderInput.yaml b/jackson-conversions-2/src/test/resources/yaml/orderInput.yaml similarity index 100% rename from jackson-2/src/main/resources/orderInput.yaml rename to jackson-conversions-2/src/test/resources/yaml/orderInput.yaml diff --git a/jackson-conversions/README.md b/jackson-conversions/README.md new file mode 100644 index 0000000000..8e8e4af73f --- /dev/null +++ b/jackson-conversions/README.md @@ -0,0 +1,16 @@ +## Jackson Conversions + +This module contains articles about Jackson conversions. + +### Relevant Articles: +- [Jackson – Unmarshall to Collection/Array](https://www.baeldung.com/jackson-collection-array) +- [Jackson Date](https://www.baeldung.com/jackson-serialize-dates) +- [Jackson – Working with Maps and Nulls](https://www.baeldung.com/jackson-map-null-values-or-null-key) +- [Jackson – Decide What Fields Get Serialized/Deserialized](https://www.baeldung.com/jackson-field-serializable-deserializable-or-not) +- [XML Serialization and Deserialization with Jackson](https://www.baeldung.com/jackson-xml-serialization-and-deserialization) +- [Map Serialization and Deserialization with Jackson](https://www.baeldung.com/jackson-map) +- [How to Serialize and Deserialize Enums with Jackson](https://www.baeldung.com/jackson-serialize-enums) +- [Jackson – Marshall String to JsonNode](https://www.baeldung.com/jackson-json-to-jsonnode) +- [Mapping Nested Values with Jackson](https://www.baeldung.com/jackson-nested-values) +- [Deserialize Immutable Objects with Jackson](https://www.baeldung.com/jackson-deserialize-immutable-objects) +- More articles: [[next -->]](../jackson-conversions-2) diff --git a/jackson-conversions/pom.xml b/jackson-conversions/pom.xml new file mode 100644 index 0000000000..58489fc7b5 --- /dev/null +++ b/jackson-conversions/pom.xml @@ -0,0 +1,49 @@ + + 4.0.0 + jackson-conversions + 0.0.1-SNAPSHOT + jackson-conversions + + + com.baeldung + parent-java + 0.0.1-SNAPSHOT + ../parent-java + + + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + com.fasterxml.jackson.datatype + jackson-datatype-joda + ${jackson.version} + + + com.fasterxml.jackson.datatype + jackson-datatype-jsr310 + ${jackson.version} + + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + ${jackson.version} + + + + + jackson-conversions + + + src/main/resources + true + + + + + diff --git a/jackson/src/test/java/com/baeldung/jackson/date/CustomDateDeserializer.java b/jackson-conversions/src/main/java/com/baeldung/jackson/date/CustomDateDeserializer.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/date/CustomDateDeserializer.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/date/CustomDateDeserializer.java diff --git a/jackson/src/test/java/com/baeldung/jackson/date/CustomDateSerializer.java b/jackson-conversions/src/main/java/com/baeldung/jackson/date/CustomDateSerializer.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/date/CustomDateSerializer.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/date/CustomDateSerializer.java diff --git a/jackson/src/test/java/com/baeldung/jackson/date/CustomDateTimeSerializer.java b/jackson-conversions/src/main/java/com/baeldung/jackson/date/CustomDateTimeSerializer.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/date/CustomDateTimeSerializer.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/date/CustomDateTimeSerializer.java diff --git a/jackson/src/test/java/com/baeldung/jackson/date/CustomLocalDateTimeSerializer.java b/jackson-conversions/src/main/java/com/baeldung/jackson/date/CustomLocalDateTimeSerializer.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/date/CustomLocalDateTimeSerializer.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/date/CustomLocalDateTimeSerializer.java diff --git a/jackson/src/test/java/com/baeldung/jackson/date/Event.java b/jackson-conversions/src/main/java/com/baeldung/jackson/date/Event.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/date/Event.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/date/Event.java diff --git a/jackson/src/test/java/com/baeldung/jackson/date/EventWithFormat.java b/jackson-conversions/src/main/java/com/baeldung/jackson/date/EventWithFormat.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/date/EventWithFormat.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/date/EventWithFormat.java diff --git a/jackson/src/test/java/com/baeldung/jackson/date/EventWithJodaTime.java b/jackson-conversions/src/main/java/com/baeldung/jackson/date/EventWithJodaTime.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/date/EventWithJodaTime.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/date/EventWithJodaTime.java diff --git a/jackson/src/test/java/com/baeldung/jackson/date/EventWithLocalDateTime.java b/jackson-conversions/src/main/java/com/baeldung/jackson/date/EventWithLocalDateTime.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/date/EventWithLocalDateTime.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/date/EventWithLocalDateTime.java diff --git a/jackson/src/test/java/com/baeldung/jackson/date/EventWithSerializer.java b/jackson-conversions/src/main/java/com/baeldung/jackson/date/EventWithSerializer.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/date/EventWithSerializer.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/date/EventWithSerializer.java diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/City.java b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/City.java similarity index 80% rename from jackson/src/main/java/com/baeldung/jackson/deserialization/enums/City.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/City.java index 2bbef534c3..794c73738a 100644 --- a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/City.java +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/City.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.deserialization.enums; +package com.baeldung.jackson.enums.deserialization; public class City { diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/Distance.java b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/Distance.java similarity index 92% rename from jackson/src/main/java/com/baeldung/jackson/deserialization/enums/Distance.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/Distance.java index 2c96b6eb80..4e5a62a0a5 100644 --- a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/Distance.java +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/Distance.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.deserialization.enums; +package com.baeldung.jackson.enums.deserialization; public enum Distance { diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/customdeserializer/City.java b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/customdeserializer/City.java similarity index 77% rename from jackson/src/main/java/com/baeldung/jackson/deserialization/enums/customdeserializer/City.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/customdeserializer/City.java index 36ef7e244a..d998c555d1 100644 --- a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/customdeserializer/City.java +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/customdeserializer/City.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.deserialization.enums.customdeserializer; +package com.baeldung.jackson.enums.deserialization.customdeserializer; public class City { diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/customdeserializer/CustomEnumDeserializer.java b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/customdeserializer/CustomEnumDeserializer.java similarity index 94% rename from jackson/src/main/java/com/baeldung/jackson/deserialization/enums/customdeserializer/CustomEnumDeserializer.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/customdeserializer/CustomEnumDeserializer.java index bae0c0df34..5068e7e6ad 100644 --- a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/customdeserializer/CustomEnumDeserializer.java +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/customdeserializer/CustomEnumDeserializer.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.deserialization.enums.customdeserializer; +package com.baeldung.jackson.enums.deserialization.customdeserializer; import java.io.IOException; import com.fasterxml.jackson.core.JsonParser; diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/customdeserializer/Distance.java b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/customdeserializer/Distance.java similarity index 92% rename from jackson/src/main/java/com/baeldung/jackson/deserialization/enums/customdeserializer/Distance.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/customdeserializer/Distance.java index 9cb3a0c391..a9b552a38d 100644 --- a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/customdeserializer/Distance.java +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/customdeserializer/Distance.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.deserialization.enums.customdeserializer; +package com.baeldung.jackson.enums.deserialization.customdeserializer; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsoncreator/City.java b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/jsoncreator/City.java similarity index 77% rename from jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsoncreator/City.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/jsoncreator/City.java index 6a478dd263..a4fe575fa2 100644 --- a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsoncreator/City.java +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/jsoncreator/City.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.deserialization.enums.jsoncreator; +package com.baeldung.jackson.enums.deserialization.jsoncreator; public class City { diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsoncreator/Distance.java b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/jsoncreator/Distance.java similarity index 94% rename from jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsoncreator/Distance.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/jsoncreator/Distance.java index 18235fb5c6..5a49b105d6 100644 --- a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsoncreator/Distance.java +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/jsoncreator/Distance.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.deserialization.enums.jsoncreator; +package com.baeldung.jackson.enums.deserialization.jsoncreator; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsonproperty/City.java b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/jsonproperty/City.java similarity index 77% rename from jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsonproperty/City.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/jsonproperty/City.java index 7f763f97c4..2de67dbdc8 100644 --- a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsonproperty/City.java +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/jsonproperty/City.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.deserialization.enums.jsonproperty; +package com.baeldung.jackson.enums.deserialization.jsonproperty; public class City { diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsonproperty/Distance.java b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/jsonproperty/Distance.java similarity index 93% rename from jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsonproperty/Distance.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/jsonproperty/Distance.java index e671a9ae5b..991aa38e25 100644 --- a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsonproperty/Distance.java +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/jsonproperty/Distance.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.deserialization.enums.jsonproperty; +package com.baeldung.jackson.enums.deserialization.jsonproperty; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsonvalue/City.java b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/jsonvalue/City.java similarity index 78% rename from jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsonvalue/City.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/jsonvalue/City.java index 014668ea8c..e69fed642e 100644 --- a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsonvalue/City.java +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/jsonvalue/City.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.deserialization.enums.jsonvalue; +package com.baeldung.jackson.enums.deserialization.jsonvalue; public class City { diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsonvalue/Distance.java b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/jsonvalue/Distance.java similarity index 91% rename from jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsonvalue/Distance.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/jsonvalue/Distance.java index 44bb5f20c3..c905868a9a 100644 --- a/jackson/src/main/java/com/baeldung/jackson/deserialization/enums/jsonvalue/Distance.java +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/deserialization/jsonvalue/Distance.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.deserialization.enums.jsonvalue; +package com.baeldung.jackson.enums.deserialization.jsonvalue; import com.fasterxml.jackson.annotation.JsonValue; diff --git a/jackson/src/main/java/com/baeldung/jackson/enums/Distance.java b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/serialization/Distance.java similarity index 91% rename from jackson/src/main/java/com/baeldung/jackson/enums/Distance.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/enums/serialization/Distance.java index 8026eedc44..18b154aaf1 100644 --- a/jackson/src/main/java/com/baeldung/jackson/enums/Distance.java +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/serialization/Distance.java @@ -1,6 +1,6 @@ -package com.baeldung.jackson.enums; +package com.baeldung.jackson.enums.serialization; -import com.baeldung.jackson.serialization.DistanceSerializer; +import com.baeldung.jackson.enums.serialization.DistanceSerializer; import com.fasterxml.jackson.databind.annotation.JsonSerialize; /** diff --git a/jackson/src/main/java/com/baeldung/jackson/serialization/DistanceSerializer.java b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/serialization/DistanceSerializer.java similarity index 92% rename from jackson/src/main/java/com/baeldung/jackson/serialization/DistanceSerializer.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/enums/serialization/DistanceSerializer.java index f5951dfd8e..5b7e6374b9 100644 --- a/jackson/src/main/java/com/baeldung/jackson/serialization/DistanceSerializer.java +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/serialization/DistanceSerializer.java @@ -1,8 +1,7 @@ -package com.baeldung.jackson.serialization; +package com.baeldung.jackson.enums.serialization; import java.io.IOException; -import com.baeldung.jackson.enums.Distance; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.SerializerProvider; diff --git a/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/DistanceEnumSimple.java b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/withEnum/DistanceEnumSimple.java similarity index 92% rename from jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/DistanceEnumSimple.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/enums/withEnum/DistanceEnumSimple.java index 1118fb349a..e148f1b481 100644 --- a/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/DistanceEnumSimple.java +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/withEnum/DistanceEnumSimple.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.dtos.withEnum; +package com.baeldung.jackson.enums.withEnum; public enum DistanceEnumSimple { KILOMETER("km", 1000), MILE("miles", 1609.34), METER("meters", 1), INCH("inches", 0.0254), CENTIMETER("cm", 0.01), MILLIMETER("mm", 0.001); diff --git a/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/DistanceEnumWithJsonFormat.java b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/withEnum/DistanceEnumWithJsonFormat.java similarity index 93% rename from jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/DistanceEnumWithJsonFormat.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/enums/withEnum/DistanceEnumWithJsonFormat.java index 7dc6bb559b..2961320e1e 100644 --- a/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/DistanceEnumWithJsonFormat.java +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/withEnum/DistanceEnumWithJsonFormat.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.dtos.withEnum; +package com.baeldung.jackson.enums.withEnum; import com.fasterxml.jackson.annotation.JsonFormat; diff --git a/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/DistanceEnumWithValue.java b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/withEnum/DistanceEnumWithValue.java similarity index 93% rename from jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/DistanceEnumWithValue.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/enums/withEnum/DistanceEnumWithValue.java index 69c476d8a5..6275efc7db 100644 --- a/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/DistanceEnumWithValue.java +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/withEnum/DistanceEnumWithValue.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.dtos.withEnum; +package com.baeldung.jackson.enums.withEnum; import com.fasterxml.jackson.annotation.JsonValue; diff --git a/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/MyDtoWithEnumCustom.java b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/withEnum/MyDtoWithEnumCustom.java similarity index 91% rename from jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/MyDtoWithEnumCustom.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/enums/withEnum/MyDtoWithEnumCustom.java index bf9b7db395..1fe09f22cb 100644 --- a/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/MyDtoWithEnumCustom.java +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/withEnum/MyDtoWithEnumCustom.java @@ -1,6 +1,6 @@ -package com.baeldung.jackson.dtos.withEnum; +package com.baeldung.jackson.enums.withEnum; -import com.baeldung.jackson.enums.Distance; +import com.baeldung.jackson.enums.serialization.Distance; public class MyDtoWithEnumCustom { diff --git a/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/MyDtoWithEnumJsonFormat.java b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/withEnum/MyDtoWithEnumJsonFormat.java similarity index 96% rename from jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/MyDtoWithEnumJsonFormat.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/enums/withEnum/MyDtoWithEnumJsonFormat.java index 8e2f1b835f..d80f457d4b 100644 --- a/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/MyDtoWithEnumJsonFormat.java +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/enums/withEnum/MyDtoWithEnumJsonFormat.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.dtos.withEnum; +package com.baeldung.jackson.enums.withEnum; public class MyDtoWithEnumJsonFormat { diff --git a/jackson/src/test/java/com/baeldung/jackson/field/MyDto.java b/jackson-conversions/src/main/java/com/baeldung/jackson/field/MyDto.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/field/MyDto.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/field/MyDto.java diff --git a/jackson/src/test/java/com/baeldung/jackson/field/MyDtoAccessLevel.java b/jackson-conversions/src/main/java/com/baeldung/jackson/field/MyDtoAccessLevel.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/field/MyDtoAccessLevel.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/field/MyDtoAccessLevel.java diff --git a/jackson/src/test/java/com/baeldung/jackson/field/MyDtoWithGetter.java b/jackson-conversions/src/main/java/com/baeldung/jackson/field/MyDtoWithGetter.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/field/MyDtoWithGetter.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/field/MyDtoWithGetter.java diff --git a/jackson/src/test/java/com/baeldung/jackson/field/MyDtoWithSetter.java b/jackson-conversions/src/main/java/com/baeldung/jackson/field/MyDtoWithSetter.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/field/MyDtoWithSetter.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/field/MyDtoWithSetter.java diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/immutable/Employee.java b/jackson-conversions/src/main/java/com/baeldung/jackson/immutable/Employee.java similarity index 89% rename from jackson/src/main/java/com/baeldung/jackson/deserialization/immutable/Employee.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/immutable/Employee.java index 44b10ee39b..3196f5962e 100644 --- a/jackson/src/main/java/com/baeldung/jackson/deserialization/immutable/Employee.java +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/immutable/Employee.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.deserialization.immutable; +package com.baeldung.jackson.immutable; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/immutable/Person.java b/jackson-conversions/src/main/java/com/baeldung/jackson/immutable/Person.java similarity index 93% rename from jackson/src/main/java/com/baeldung/jackson/deserialization/immutable/Person.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/immutable/Person.java index d9041720b6..17000734fe 100644 --- a/jackson/src/main/java/com/baeldung/jackson/deserialization/immutable/Person.java +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/immutable/Person.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.deserialization.immutable; +package com.baeldung.jackson.immutable; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; diff --git a/jackson/src/main/java/com/baeldung/jackson/entities/ClassWithAMap.java b/jackson-conversions/src/main/java/com/baeldung/jackson/map/ClassWithAMap.java similarity index 83% rename from jackson/src/main/java/com/baeldung/jackson/entities/ClassWithAMap.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/map/ClassWithAMap.java index 107d75eb0d..d945b34cdb 100644 --- a/jackson/src/main/java/com/baeldung/jackson/entities/ClassWithAMap.java +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/map/ClassWithAMap.java @@ -1,8 +1,7 @@ -package com.baeldung.jackson.entities; +package com.baeldung.jackson.map; import java.util.Map; -import com.baeldung.jackson.serialization.MyPairDeserializer; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; diff --git a/jackson/src/main/java/com/baeldung/jackson/entities/MyPair.java b/jackson-conversions/src/main/java/com/baeldung/jackson/map/MyPair.java similarity index 97% rename from jackson/src/main/java/com/baeldung/jackson/entities/MyPair.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/map/MyPair.java index ca5960e0cf..867eb23bc2 100644 --- a/jackson/src/main/java/com/baeldung/jackson/entities/MyPair.java +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/map/MyPair.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.entities; +package com.baeldung.jackson.map; import com.fasterxml.jackson.annotation.JsonValue; diff --git a/jackson/src/main/java/com/baeldung/jackson/serialization/MyPairDeserializer.java b/jackson-conversions/src/main/java/com/baeldung/jackson/map/MyPairDeserializer.java similarity index 83% rename from jackson/src/main/java/com/baeldung/jackson/serialization/MyPairDeserializer.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/map/MyPairDeserializer.java index 43bdc1c500..c631f47ffc 100644 --- a/jackson/src/main/java/com/baeldung/jackson/serialization/MyPairDeserializer.java +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/map/MyPairDeserializer.java @@ -1,8 +1,7 @@ -package com.baeldung.jackson.serialization; +package com.baeldung.jackson.map; import java.io.IOException; -import com.baeldung.jackson.entities.MyPair; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.KeyDeserializer; diff --git a/jackson/src/main/java/com/baeldung/jackson/serialization/MyPairSerializer.java b/jackson-conversions/src/main/java/com/baeldung/jackson/map/MyPairSerializer.java similarity index 89% rename from jackson/src/main/java/com/baeldung/jackson/serialization/MyPairSerializer.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/map/MyPairSerializer.java index fee1cafe16..c84bab104c 100644 --- a/jackson/src/main/java/com/baeldung/jackson/serialization/MyPairSerializer.java +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/map/MyPairSerializer.java @@ -1,9 +1,8 @@ -package com.baeldung.jackson.serialization; +package com.baeldung.jackson.map; import java.io.IOException; import java.io.StringWriter; -import com.baeldung.jackson.entities.MyPair; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonSerializer; diff --git a/jackson-conversions/src/main/java/com/baeldung/jackson/mapnull/MyDto.java b/jackson-conversions/src/main/java/com/baeldung/jackson/mapnull/MyDto.java new file mode 100644 index 0000000000..cb026a50f8 --- /dev/null +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/mapnull/MyDto.java @@ -0,0 +1,54 @@ +package com.baeldung.jackson.mapnull; + +public class MyDto { + + private String stringValue; + private int intValue; + private boolean booleanValue; + + public MyDto() { + super(); + } + + public MyDto(final String stringValue, final int intValue, final boolean booleanValue) { + super(); + + this.stringValue = stringValue; + this.intValue = intValue; + this.booleanValue = booleanValue; + } + + // API + + public String getStringValue() { + return stringValue; + } + + public void setStringValue(final String stringValue) { + this.stringValue = stringValue; + } + + public int getIntValue() { + return intValue; + } + + public void setIntValue(final int intValue) { + this.intValue = intValue; + } + + public boolean isBooleanValue() { + return booleanValue; + } + + public void setBooleanValue(final boolean booleanValue) { + this.booleanValue = booleanValue; + } + + // + + @Override + public String toString() { + return "MyDto [stringValue=" + stringValue + ", intValue=" + intValue + ", booleanValue=" + booleanValue + "]"; + } + +} diff --git a/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos/MyDtoNullKeySerializer.java b/jackson-conversions/src/main/java/com/baeldung/jackson/mapnull/MyDtoNullKeySerializer.java similarity index 94% rename from jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos/MyDtoNullKeySerializer.java rename to jackson-conversions/src/main/java/com/baeldung/jackson/mapnull/MyDtoNullKeySerializer.java index dcd4070dba..1a5d794fac 100644 --- a/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos/MyDtoNullKeySerializer.java +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/mapnull/MyDtoNullKeySerializer.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.ignore.dtos; +package com.baeldung.jackson.mapnull; import java.io.IOException; diff --git a/jackson-conversions/src/main/java/com/baeldung/jackson/tocollection/MyDto.java b/jackson-conversions/src/main/java/com/baeldung/jackson/tocollection/MyDto.java new file mode 100644 index 0000000000..368dea5317 --- /dev/null +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/tocollection/MyDto.java @@ -0,0 +1,54 @@ +package com.baeldung.jackson.tocollection; + +public class MyDto { + + private String stringValue; + private int intValue; + private boolean booleanValue; + + public MyDto() { + super(); + } + + public MyDto(final String stringValue, final int intValue, final boolean booleanValue) { + super(); + + this.stringValue = stringValue; + this.intValue = intValue; + this.booleanValue = booleanValue; + } + + // API + + public String getStringValue() { + return stringValue; + } + + public void setStringValue(final String stringValue) { + this.stringValue = stringValue; + } + + public int getIntValue() { + return intValue; + } + + public void setIntValue(final int intValue) { + this.intValue = intValue; + } + + public boolean isBooleanValue() { + return booleanValue; + } + + public void setBooleanValue(final boolean booleanValue) { + this.booleanValue = booleanValue; + } + + // + + @Override + public String toString() { + return "MyDto [stringValue=" + stringValue + ", intValue=" + intValue + ", booleanValue=" + booleanValue + "]"; + } + +} diff --git a/jackson-conversions/src/main/java/com/baeldung/jackson/xml/Address.java b/jackson-conversions/src/main/java/com/baeldung/jackson/xml/Address.java new file mode 100644 index 0000000000..1d6b24c9c7 --- /dev/null +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/xml/Address.java @@ -0,0 +1,33 @@ +package com.baeldung.jackson.xml; + +public class Address { + + String streetNumber; + String streetName; + String city; + + public String getStreetNumber() { + return streetNumber; + } + + public void setStreetNumber(String streetNumber) { + this.streetNumber = streetNumber; + } + + public String getStreetName() { + return streetName; + } + + public void setStreetName(String streetName) { + this.streetName = streetName; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + +} diff --git a/jackson-conversions/src/main/java/com/baeldung/jackson/xml/Person.java b/jackson-conversions/src/main/java/com/baeldung/jackson/xml/Person.java new file mode 100644 index 0000000000..add53a1cfd --- /dev/null +++ b/jackson-conversions/src/main/java/com/baeldung/jackson/xml/Person.java @@ -0,0 +1,47 @@ +package com.baeldung.jackson.xml; + +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; + +import java.util.ArrayList; +import java.util.List; + +@JacksonXmlRootElement(localName = "person") +public final class Person { + private String firstName; + private String lastName; + private List phoneNumbers = new ArrayList<>(); + private List
address = new ArrayList<>(); + + public List
getAddress() { + return address; + } + + public void setAddress(List
address) { + this.address = address; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public List getPhoneNumbers() { + return phoneNumbers; + } + + public void setPhoneNumbers(List phoneNumbers) { + this.phoneNumbers = phoneNumbers; + } + +} \ No newline at end of file diff --git a/jackson/src/test/java/com/baeldung/jackson/test/JacksonDateUnitTest.java b/jackson-conversions/src/test/java/com/baeldung/jackson/date/JacksonDateUnitTest.java similarity index 99% rename from jackson/src/test/java/com/baeldung/jackson/test/JacksonDateUnitTest.java rename to jackson-conversions/src/test/java/com/baeldung/jackson/date/JacksonDateUnitTest.java index 672ff5c6fd..a264dd52f2 100644 --- a/jackson/src/test/java/com/baeldung/jackson/test/JacksonDateUnitTest.java +++ b/jackson-conversions/src/test/java/com/baeldung/jackson/date/JacksonDateUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.test; +package com.baeldung.jackson.date; import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; diff --git a/jackson/src/test/java/com/baeldung/jackson/deserialization/enums/DefaultEnumDeserializationUnitTest.java b/jackson-conversions/src/test/java/com/baeldung/jackson/enums/deserialization/DefaultEnumDeserializationUnitTest.java similarity index 91% rename from jackson/src/test/java/com/baeldung/jackson/deserialization/enums/DefaultEnumDeserializationUnitTest.java rename to jackson-conversions/src/test/java/com/baeldung/jackson/enums/deserialization/DefaultEnumDeserializationUnitTest.java index c7ce96e013..43e21100ea 100644 --- a/jackson/src/test/java/com/baeldung/jackson/deserialization/enums/DefaultEnumDeserializationUnitTest.java +++ b/jackson-conversions/src/test/java/com/baeldung/jackson/enums/deserialization/DefaultEnumDeserializationUnitTest.java @@ -1,7 +1,8 @@ -package com.baeldung.jackson.deserialization.enums; +package com.baeldung.jackson.enums.deserialization; import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.IOException; + import org.junit.Test; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.ObjectMapper; diff --git a/jackson/src/test/java/com/baeldung/jackson/deserialization/enums/customdeserializer/EnumCustomDeserializationUnitTest.java b/jackson-conversions/src/test/java/com/baeldung/jackson/enums/deserialization/customdeserializer/EnumCustomDeserializationUnitTest.java similarity index 90% rename from jackson/src/test/java/com/baeldung/jackson/deserialization/enums/customdeserializer/EnumCustomDeserializationUnitTest.java rename to jackson-conversions/src/test/java/com/baeldung/jackson/enums/deserialization/customdeserializer/EnumCustomDeserializationUnitTest.java index e8dbfa8df8..b0ceb5a6d8 100644 --- a/jackson/src/test/java/com/baeldung/jackson/deserialization/enums/customdeserializer/EnumCustomDeserializationUnitTest.java +++ b/jackson-conversions/src/test/java/com/baeldung/jackson/enums/deserialization/customdeserializer/EnumCustomDeserializationUnitTest.java @@ -1,7 +1,8 @@ -package com.baeldung.jackson.deserialization.enums.customdeserializer; +package com.baeldung.jackson.enums.deserialization.customdeserializer; import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.IOException; + import org.junit.Test; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.ObjectMapper; diff --git a/jackson/src/test/java/com/baeldung/jackson/deserialization/enums/jsoncreator/EnumDeserializationUsingJsonCreatorUnitTest.java b/jackson-conversions/src/test/java/com/baeldung/jackson/enums/deserialization/jsoncreator/EnumDeserializationUsingJsonCreatorUnitTest.java similarity index 90% rename from jackson/src/test/java/com/baeldung/jackson/deserialization/enums/jsoncreator/EnumDeserializationUsingJsonCreatorUnitTest.java rename to jackson-conversions/src/test/java/com/baeldung/jackson/enums/deserialization/jsoncreator/EnumDeserializationUsingJsonCreatorUnitTest.java index d778cbe26b..1885040916 100644 --- a/jackson/src/test/java/com/baeldung/jackson/deserialization/enums/jsoncreator/EnumDeserializationUsingJsonCreatorUnitTest.java +++ b/jackson-conversions/src/test/java/com/baeldung/jackson/enums/deserialization/jsoncreator/EnumDeserializationUsingJsonCreatorUnitTest.java @@ -1,7 +1,8 @@ -package com.baeldung.jackson.deserialization.enums.jsoncreator; +package com.baeldung.jackson.enums.deserialization.jsoncreator; import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.IOException; + import org.junit.Test; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.ObjectMapper; diff --git a/jackson/src/test/java/com/baeldung/jackson/deserialization/enums/jsonproperty/EnumDeserializationUsingJsonPropertyUnitTest.java b/jackson-conversions/src/test/java/com/baeldung/jackson/enums/deserialization/jsonproperty/EnumDeserializationUsingJsonPropertyUnitTest.java similarity index 90% rename from jackson/src/test/java/com/baeldung/jackson/deserialization/enums/jsonproperty/EnumDeserializationUsingJsonPropertyUnitTest.java rename to jackson-conversions/src/test/java/com/baeldung/jackson/enums/deserialization/jsonproperty/EnumDeserializationUsingJsonPropertyUnitTest.java index 134f4a29cc..3697669447 100644 --- a/jackson/src/test/java/com/baeldung/jackson/deserialization/enums/jsonproperty/EnumDeserializationUsingJsonPropertyUnitTest.java +++ b/jackson-conversions/src/test/java/com/baeldung/jackson/enums/deserialization/jsonproperty/EnumDeserializationUsingJsonPropertyUnitTest.java @@ -1,7 +1,8 @@ -package com.baeldung.jackson.deserialization.enums.jsonproperty; +package com.baeldung.jackson.enums.deserialization.jsonproperty; import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.IOException; + import org.junit.Test; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.ObjectMapper; diff --git a/jackson/src/test/java/com/baeldung/jackson/deserialization/enums/jsonvalue/EnumDeserializationUsingJsonValueUnitTest.java b/jackson-conversions/src/test/java/com/baeldung/jackson/enums/deserialization/jsonvalue/EnumDeserializationUsingJsonValueUnitTest.java similarity index 90% rename from jackson/src/test/java/com/baeldung/jackson/deserialization/enums/jsonvalue/EnumDeserializationUsingJsonValueUnitTest.java rename to jackson-conversions/src/test/java/com/baeldung/jackson/enums/deserialization/jsonvalue/EnumDeserializationUsingJsonValueUnitTest.java index 85afcb9a69..84ba40d018 100644 --- a/jackson/src/test/java/com/baeldung/jackson/deserialization/enums/jsonvalue/EnumDeserializationUsingJsonValueUnitTest.java +++ b/jackson-conversions/src/test/java/com/baeldung/jackson/enums/deserialization/jsonvalue/EnumDeserializationUsingJsonValueUnitTest.java @@ -1,7 +1,8 @@ -package com.baeldung.jackson.deserialization.enums.jsonvalue; +package com.baeldung.jackson.enums.deserialization.jsonvalue; import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.IOException; + import org.junit.Test; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.ObjectMapper; diff --git a/jackson/src/test/java/com/baeldung/jackson/enums/JacksonEnumSerializationUnitTest.java b/jackson-conversions/src/test/java/com/baeldung/jackson/enums/serialization/JacksonEnumSerializationUnitTest.java similarity index 92% rename from jackson/src/test/java/com/baeldung/jackson/enums/JacksonEnumSerializationUnitTest.java rename to jackson-conversions/src/test/java/com/baeldung/jackson/enums/serialization/JacksonEnumSerializationUnitTest.java index d4fb2401ed..b2cce15cfc 100644 --- a/jackson/src/test/java/com/baeldung/jackson/enums/JacksonEnumSerializationUnitTest.java +++ b/jackson-conversions/src/test/java/com/baeldung/jackson/enums/serialization/JacksonEnumSerializationUnitTest.java @@ -1,8 +1,9 @@ -package com.baeldung.jackson.enums; +package com.baeldung.jackson.enums.serialization; import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertThat; import java.io.IOException; + import org.junit.Test; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.ObjectMapper; diff --git a/jackson/src/test/java/com/baeldung/jackson/test/JacksonSerializationEnumsUnitTest.java b/jackson-conversions/src/test/java/com/baeldung/jackson/enums/serialization/JacksonSerializationEnumsUnitTest.java similarity index 85% rename from jackson/src/test/java/com/baeldung/jackson/test/JacksonSerializationEnumsUnitTest.java rename to jackson-conversions/src/test/java/com/baeldung/jackson/enums/serialization/JacksonSerializationEnumsUnitTest.java index 0f57d26d8b..6b4a1ee3d4 100644 --- a/jackson/src/test/java/com/baeldung/jackson/test/JacksonSerializationEnumsUnitTest.java +++ b/jackson-conversions/src/test/java/com/baeldung/jackson/enums/serialization/JacksonSerializationEnumsUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.test; +package com.baeldung.jackson.enums.serialization; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; @@ -8,12 +8,12 @@ import java.io.IOException; import org.junit.Test; -import com.baeldung.jackson.dtos.withEnum.DistanceEnumSimple; -import com.baeldung.jackson.dtos.withEnum.DistanceEnumWithJsonFormat; -import com.baeldung.jackson.dtos.withEnum.DistanceEnumWithValue; -import com.baeldung.jackson.dtos.withEnum.MyDtoWithEnumCustom; -import com.baeldung.jackson.dtos.withEnum.MyDtoWithEnumJsonFormat; -import com.baeldung.jackson.enums.Distance; +import com.baeldung.jackson.enums.withEnum.DistanceEnumSimple; +import com.baeldung.jackson.enums.withEnum.DistanceEnumWithJsonFormat; +import com.baeldung.jackson.enums.withEnum.DistanceEnumWithValue; +import com.baeldung.jackson.enums.withEnum.MyDtoWithEnumCustom; +import com.baeldung.jackson.enums.withEnum.MyDtoWithEnumJsonFormat; +import com.baeldung.jackson.enums.serialization.Distance; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.ObjectMapper; diff --git a/jackson/src/test/java/com/baeldung/jackson/test/JacksonFieldUnitTest.java b/jackson-conversions/src/test/java/com/baeldung/jackson/field/JacksonFieldUnitTest.java similarity index 95% rename from jackson/src/test/java/com/baeldung/jackson/test/JacksonFieldUnitTest.java rename to jackson-conversions/src/test/java/com/baeldung/jackson/field/JacksonFieldUnitTest.java index ccc5905e88..4eb3cd5e0a 100644 --- a/jackson/src/test/java/com/baeldung/jackson/test/JacksonFieldUnitTest.java +++ b/jackson-conversions/src/test/java/com/baeldung/jackson/field/JacksonFieldUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.test; +package com.baeldung.jackson.field; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; @@ -7,10 +7,6 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; import java.io.IOException; - -import com.baeldung.jackson.field.MyDtoAccessLevel; -import com.baeldung.jackson.field.MyDtoWithSetter; -import com.baeldung.jackson.field.MyDtoWithGetter; import org.junit.Test; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; diff --git a/jackson/src/test/java/com/baeldung/jackson/deserialization/immutable/ImmutableObjectDeserializationUnitTest.java b/jackson-conversions/src/test/java/com/baeldung/jackson/immutable/ImmutableObjectDeserializationUnitTest.java similarity index 95% rename from jackson/src/test/java/com/baeldung/jackson/deserialization/immutable/ImmutableObjectDeserializationUnitTest.java rename to jackson-conversions/src/test/java/com/baeldung/jackson/immutable/ImmutableObjectDeserializationUnitTest.java index 1252179e3a..ed80b15b4f 100644 --- a/jackson/src/test/java/com/baeldung/jackson/deserialization/immutable/ImmutableObjectDeserializationUnitTest.java +++ b/jackson-conversions/src/test/java/com/baeldung/jackson/immutable/ImmutableObjectDeserializationUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.deserialization.immutable; +package com.baeldung.jackson.immutable; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Test; diff --git a/jackson/src/test/java/com/baeldung/jackson/deserialization/JacksonMapDeserializeUnitTest.java b/jackson-conversions/src/test/java/com/baeldung/jackson/map/JacksonMapDeserializeUnitTest.java similarity index 93% rename from jackson/src/test/java/com/baeldung/jackson/deserialization/JacksonMapDeserializeUnitTest.java rename to jackson-conversions/src/test/java/com/baeldung/jackson/map/JacksonMapDeserializeUnitTest.java index 1c3e95241a..0e59eb68bf 100644 --- a/jackson/src/test/java/com/baeldung/jackson/deserialization/JacksonMapDeserializeUnitTest.java +++ b/jackson-conversions/src/test/java/com/baeldung/jackson/map/JacksonMapDeserializeUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.deserialization; +package com.baeldung.jackson.map; import java.io.IOException; import java.util.HashMap; @@ -7,8 +7,6 @@ import java.util.Map; import org.junit.Assert; import org.junit.Test; -import com.baeldung.jackson.entities.ClassWithAMap; -import com.baeldung.jackson.entities.MyPair; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonMappingException; diff --git a/jackson/src/test/java/com/baeldung/jackson/serialization/JacksonMapSerializeUnitTest.java b/jackson-conversions/src/test/java/com/baeldung/jackson/map/JacksonMapSerializeUnitTest.java similarity index 95% rename from jackson/src/test/java/com/baeldung/jackson/serialization/JacksonMapSerializeUnitTest.java rename to jackson-conversions/src/test/java/com/baeldung/jackson/map/JacksonMapSerializeUnitTest.java index e67336f6f3..0545e53201 100644 --- a/jackson/src/test/java/com/baeldung/jackson/serialization/JacksonMapSerializeUnitTest.java +++ b/jackson-conversions/src/test/java/com/baeldung/jackson/map/JacksonMapSerializeUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.serialization; +package com.baeldung.jackson.map; import java.util.HashMap; import java.util.Map; @@ -6,7 +6,6 @@ import java.util.Map; import org.junit.Assert; import org.junit.Test; -import com.baeldung.jackson.entities.MyPair; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.annotation.JsonSerialize; diff --git a/jackson-conversions/src/test/java/com/baeldung/jackson/mapnull/JacksonMapNullUnitTest.java b/jackson-conversions/src/test/java/com/baeldung/jackson/mapnull/JacksonMapNullUnitTest.java new file mode 100644 index 0000000000..274100162a --- /dev/null +++ b/jackson-conversions/src/test/java/com/baeldung/jackson/mapnull/JacksonMapNullUnitTest.java @@ -0,0 +1,109 @@ +package com.baeldung.jackson.mapnull; + +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.BeanPropertyWriter; +import com.fasterxml.jackson.databind.ser.FilterProvider; +import com.fasterxml.jackson.databind.ser.PropertyFilter; +import com.fasterxml.jackson.databind.ser.PropertyWriter; +import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter; +import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; +import org.junit.Test; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertThat; + +public class JacksonMapNullUnitTest { + + @Test + public final void givenIgnoringMapNullValue_whenWritingMapObjectWithNullValue_thenIgnored() throws JsonProcessingException { + final ObjectMapper mapper = new ObjectMapper(); + // mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); + mapper.setSerializationInclusion(Include.NON_NULL); + + final MyDto dtoObject1 = new MyDto(); + + final Map dtoMap = new HashMap(); + dtoMap.put("dtoObject1", dtoObject1); + dtoMap.put("dtoObject2", null); + + final String dtoMapAsString = mapper.writeValueAsString(dtoMap); + + assertThat(dtoMapAsString, containsString("dtoObject1")); + assertThat(dtoMapAsString, not(containsString("dtoObject2"))); + System.out.println(dtoMapAsString); + } + + @Test + public final void givenIgnoringMapValueObjectWithNullField_whenWritingMapValueObjectWithNullField_thenIgnored() throws JsonProcessingException { + final ObjectMapper mapper = new ObjectMapper(); + mapper.setSerializationInclusion(Include.NON_NULL); + + final MyDto dtoObject = new MyDto(); + + final Map dtoMap = new HashMap(); + dtoMap.put("dtoObject", dtoObject); + + final String dtoMapAsString = mapper.writeValueAsString(dtoMap); + + assertThat(dtoMapAsString, containsString("dtoObject")); + assertThat(dtoMapAsString, not(containsString("stringValue"))); + System.out.println(dtoMapAsString); + } + + @Test + public final void givenAllowingMapObjectWithNullKey_whenWriting_thenCorrect() throws JsonProcessingException { + final ObjectMapper mapper = new ObjectMapper(); + mapper.getSerializerProvider() + .setNullKeySerializer(new MyDtoNullKeySerializer()); + + final MyDto dtoObject1 = new MyDto(); + dtoObject1.setStringValue("dtoObjectString1"); + final MyDto dtoObject2 = new MyDto(); + dtoObject2.setStringValue("dtoObjectString2"); + + final Map dtoMap = new HashMap(); + dtoMap.put(null, dtoObject1); + dtoMap.put("obj2", dtoObject2); + + final String dtoMapAsString = mapper.writeValueAsString(dtoMap); + + System.out.println(dtoMapAsString); + assertThat(dtoMapAsString, containsString("\"\"")); + assertThat(dtoMapAsString, containsString("dtoObjectString1")); + assertThat(dtoMapAsString, containsString("obj2")); + } + + @Test + public final void givenAllowingMapObjectOneNullKey_whenWritingMapObjectWithTwoNullKeys_thenOverride() throws JsonProcessingException { + final ObjectMapper mapper = new ObjectMapper(); + mapper.getSerializerProvider() + .setNullKeySerializer(new MyDtoNullKeySerializer()); + + final MyDto dtoObject1 = new MyDto(); + dtoObject1.setStringValue("dtoObject1String"); + + final MyDto dtoObject2 = new MyDto(); + dtoObject2.setStringValue("dtoObject2String"); + + final Map dtoMap = new HashMap(); + dtoMap.put(null, dtoObject1); + dtoMap.put(null, dtoObject2); + + final String dtoMapAsString = mapper.writeValueAsString(dtoMap); + + assertThat(dtoMapAsString, not(containsString("dtoObject1String"))); + assertThat(dtoMapAsString, containsString("dtoObject2String")); + System.out.println(dtoMapAsString); + } + +} diff --git a/jackson/src/test/java/com/baeldung/jackson/deserialization/nested/DeserializeWithNestedPropertiesUnitTest.java b/jackson-conversions/src/test/java/com/baeldung/jackson/nested/DeserializeWithNestedPropertiesUnitTest.java similarity index 95% rename from jackson/src/test/java/com/baeldung/jackson/deserialization/nested/DeserializeWithNestedPropertiesUnitTest.java rename to jackson-conversions/src/test/java/com/baeldung/jackson/nested/DeserializeWithNestedPropertiesUnitTest.java index 037bc7e880..18fd2463e9 100644 --- a/jackson/src/test/java/com/baeldung/jackson/deserialization/nested/DeserializeWithNestedPropertiesUnitTest.java +++ b/jackson-conversions/src/test/java/com/baeldung/jackson/nested/DeserializeWithNestedPropertiesUnitTest.java @@ -1,70 +1,70 @@ -package com.baeldung.jackson.deserialization.nested; - -import static org.junit.Assert.*; - -import java.io.IOException; - -import org.junit.Test; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.module.SimpleModule; - -public class DeserializeWithNestedPropertiesUnitTest { - - private String SOURCE_JSON = "{\"id\":\"957c43f2-fa2e-42f9-bf75-6e3d5bb6960a\",\"name\":\"The Best Product\",\"brand\":{\"id\":\"9bcd817d-0141-42e6-8f04-e5aaab0980b6\",\"name\":\"ACME Products\",\"owner\":{\"id\":\"b21a80b1-0c09-4be3-9ebd-ea3653511c13\",\"name\":\"Ultimate Corp, Inc.\"}}}"; - - @Test - public void whenUsingAnnotations_thenOk() throws IOException { - Product product = new ObjectMapper().readerFor(Product.class) - .readValue(SOURCE_JSON); - - assertEquals(product.getName(), "The Best Product"); - assertEquals(product.getBrandName(), "ACME Products"); - assertEquals(product.getOwnerName(), "Ultimate Corp, Inc."); - } - - @Test - public void whenUsingJsonNode_thenOk() throws IOException { - JsonNode productNode = new ObjectMapper().readTree(SOURCE_JSON); - - Product product = new Product(); - product.setId(productNode.get("id") - .textValue()); - product.setName(productNode.get("name") - .textValue()); - product.setBrandName(productNode.get("brand") - .get("name") - .textValue()); - product.setOwnerName(productNode.get("brand") - .get("owner") - .get("name") - .textValue()); - - assertEquals(product.getName(), "The Best Product"); - assertEquals(product.getBrandName(), "ACME Products"); - assertEquals(product.getOwnerName(), "Ultimate Corp, Inc."); - } - - @Test - public void whenUsingDeserializerManuallyRegistered_thenOk() throws IOException { - ObjectMapper mapper = new ObjectMapper(); - SimpleModule module = new SimpleModule(); - module.addDeserializer(Product.class, new ProductDeserializer()); - mapper.registerModule(module); - - Product product = mapper.readValue(SOURCE_JSON, Product.class); - assertEquals(product.getName(), "The Best Product"); - assertEquals(product.getBrandName(), "ACME Products"); - assertEquals(product.getOwnerName(), "Ultimate Corp, Inc."); - } - - @Test - public void whenUsingDeserializerAutoRegistered_thenOk() throws IOException { - ObjectMapper mapper = new ObjectMapper(); - Product product = mapper.readValue(SOURCE_JSON, Product.class); - assertEquals(product.getName(), "The Best Product"); - assertEquals(product.getBrandName(), "ACME Products"); - assertEquals(product.getOwnerName(), "Ultimate Corp, Inc."); - } -} +package com.baeldung.jackson.nested; + +import static org.junit.Assert.*; + +import java.io.IOException; + +import org.junit.Test; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.module.SimpleModule; + +public class DeserializeWithNestedPropertiesUnitTest { + + private String SOURCE_JSON = "{\"id\":\"957c43f2-fa2e-42f9-bf75-6e3d5bb6960a\",\"name\":\"The Best Product\",\"brand\":{\"id\":\"9bcd817d-0141-42e6-8f04-e5aaab0980b6\",\"name\":\"ACME Products\",\"owner\":{\"id\":\"b21a80b1-0c09-4be3-9ebd-ea3653511c13\",\"name\":\"Ultimate Corp, Inc.\"}}}"; + + @Test + public void whenUsingAnnotations_thenOk() throws IOException { + Product product = new ObjectMapper().readerFor(Product.class) + .readValue(SOURCE_JSON); + + assertEquals(product.getName(), "The Best Product"); + assertEquals(product.getBrandName(), "ACME Products"); + assertEquals(product.getOwnerName(), "Ultimate Corp, Inc."); + } + + @Test + public void whenUsingJsonNode_thenOk() throws IOException { + JsonNode productNode = new ObjectMapper().readTree(SOURCE_JSON); + + Product product = new Product(); + product.setId(productNode.get("id") + .textValue()); + product.setName(productNode.get("name") + .textValue()); + product.setBrandName(productNode.get("brand") + .get("name") + .textValue()); + product.setOwnerName(productNode.get("brand") + .get("owner") + .get("name") + .textValue()); + + assertEquals(product.getName(), "The Best Product"); + assertEquals(product.getBrandName(), "ACME Products"); + assertEquals(product.getOwnerName(), "Ultimate Corp, Inc."); + } + + @Test + public void whenUsingDeserializerManuallyRegistered_thenOk() throws IOException { + ObjectMapper mapper = new ObjectMapper(); + SimpleModule module = new SimpleModule(); + module.addDeserializer(Product.class, new ProductDeserializer()); + mapper.registerModule(module); + + Product product = mapper.readValue(SOURCE_JSON, Product.class); + assertEquals(product.getName(), "The Best Product"); + assertEquals(product.getBrandName(), "ACME Products"); + assertEquals(product.getOwnerName(), "Ultimate Corp, Inc."); + } + + @Test + public void whenUsingDeserializerAutoRegistered_thenOk() throws IOException { + ObjectMapper mapper = new ObjectMapper(); + Product product = mapper.readValue(SOURCE_JSON, Product.class); + assertEquals(product.getName(), "The Best Product"); + assertEquals(product.getBrandName(), "ACME Products"); + assertEquals(product.getOwnerName(), "Ultimate Corp, Inc."); + } +} diff --git a/jackson/src/test/java/com/baeldung/jackson/deserialization/nested/Product.java b/jackson-conversions/src/test/java/com/baeldung/jackson/nested/Product.java similarity index 91% rename from jackson/src/test/java/com/baeldung/jackson/deserialization/nested/Product.java rename to jackson-conversions/src/test/java/com/baeldung/jackson/nested/Product.java index 0020afea0f..5aeb53f7e8 100644 --- a/jackson/src/test/java/com/baeldung/jackson/deserialization/nested/Product.java +++ b/jackson-conversions/src/test/java/com/baeldung/jackson/nested/Product.java @@ -1,55 +1,55 @@ -package com.baeldung.jackson.deserialization.nested; - -import java.util.Map; - -import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.databind.annotation.JsonDeserialize; - -@JsonDeserialize(using = ProductDeserializer.class) -public class Product { - - private String id; - private String name; - private String brandName; - private String ownerName; - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getBrandName() { - return brandName; - } - - public void setBrandName(String brandName) { - this.brandName = brandName; - } - - public String getOwnerName() { - return ownerName; - } - - public void setOwnerName(String ownerName) { - this.ownerName = ownerName; - } - - @SuppressWarnings("unchecked") - @JsonProperty("brand") - private void unpackNested(Map brand) { - this.brandName = (String) brand.get("name"); - Map owner = (Map) brand.get("owner"); - this.ownerName = owner.get("name"); - } +package com.baeldung.jackson.nested; + +import java.util.Map; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; + +@JsonDeserialize(using = ProductDeserializer.class) +public class Product { + + private String id; + private String name; + private String brandName; + private String ownerName; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getBrandName() { + return brandName; + } + + public void setBrandName(String brandName) { + this.brandName = brandName; + } + + public String getOwnerName() { + return ownerName; + } + + public void setOwnerName(String ownerName) { + this.ownerName = ownerName; + } + + @SuppressWarnings("unchecked") + @JsonProperty("brand") + private void unpackNested(Map brand) { + this.brandName = (String) brand.get("name"); + Map owner = (Map) brand.get("owner"); + this.ownerName = owner.get("name"); + } } \ No newline at end of file diff --git a/jackson/src/test/java/com/baeldung/jackson/deserialization/nested/ProductDeserializer.java b/jackson-conversions/src/test/java/com/baeldung/jackson/nested/ProductDeserializer.java similarity index 92% rename from jackson/src/test/java/com/baeldung/jackson/deserialization/nested/ProductDeserializer.java rename to jackson-conversions/src/test/java/com/baeldung/jackson/nested/ProductDeserializer.java index daabae6cda..191bfbf0d8 100644 --- a/jackson/src/test/java/com/baeldung/jackson/deserialization/nested/ProductDeserializer.java +++ b/jackson-conversions/src/test/java/com/baeldung/jackson/nested/ProductDeserializer.java @@ -1,40 +1,40 @@ -package com.baeldung.jackson.deserialization.nested; - -import java.io.IOException; - -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.deser.std.StdDeserializer; - -@SuppressWarnings("serial") -public class ProductDeserializer extends StdDeserializer { - - public ProductDeserializer() { - this(null); - } - - public ProductDeserializer(Class vc) { - super(vc); - } - - @Override - public Product deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { - JsonNode productNode = jp.getCodec() - .readTree(jp); - Product product = new Product(); - product.setId(productNode.get("id") - .textValue()); - product.setName(productNode.get("name") - .textValue()); - product.setBrandName(productNode.get("brand") - .get("name") - .textValue()); - product.setOwnerName(productNode.get("brand") - .get("owner") - .get("name") - .textValue()); - return product; - } -} +package com.baeldung.jackson.nested; + +import java.io.IOException; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; + +@SuppressWarnings("serial") +public class ProductDeserializer extends StdDeserializer { + + public ProductDeserializer() { + this(null); + } + + public ProductDeserializer(Class vc) { + super(vc); + } + + @Override + public Product deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { + JsonNode productNode = jp.getCodec() + .readTree(jp); + Product product = new Product(); + product.setId(productNode.get("id") + .textValue()); + product.setName(productNode.get("name") + .textValue()); + product.setBrandName(productNode.get("brand") + .get("name") + .textValue()); + product.setOwnerName(productNode.get("brand") + .get("owner") + .get("name") + .textValue()); + return product; + } +} diff --git a/jackson/src/test/java/com/baeldung/jackson/test/JacksonCollectionDeserializationUnitTest.java b/jackson-conversions/src/test/java/com/baeldung/jackson/tocollection/JacksonCollectionDeserializationUnitTest.java similarity index 97% rename from jackson/src/test/java/com/baeldung/jackson/test/JacksonCollectionDeserializationUnitTest.java rename to jackson-conversions/src/test/java/com/baeldung/jackson/tocollection/JacksonCollectionDeserializationUnitTest.java index 0de3a1de82..b1e8e48a95 100644 --- a/jackson/src/test/java/com/baeldung/jackson/test/JacksonCollectionDeserializationUnitTest.java +++ b/jackson-conversions/src/test/java/com/baeldung/jackson/tocollection/JacksonCollectionDeserializationUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.test; +package com.baeldung.jackson.tocollection; import static org.hamcrest.Matchers.instanceOf; import static org.junit.Assert.assertThat; @@ -6,8 +6,6 @@ import static org.junit.Assert.assertThat; import java.io.IOException; import java.util.LinkedHashMap; import java.util.List; - -import com.baeldung.jackson.dtos.MyDto; import org.junit.Test; import com.fasterxml.jackson.core.JsonParseException; diff --git a/jackson-conversions/src/test/java/com/baeldung/jackson/tojsonnode/StringToJsonNodeUnitTest.java b/jackson-conversions/src/test/java/com/baeldung/jackson/tojsonnode/StringToJsonNodeUnitTest.java new file mode 100644 index 0000000000..3d2423c85c --- /dev/null +++ b/jackson-conversions/src/test/java/com/baeldung/jackson/tojsonnode/StringToJsonNodeUnitTest.java @@ -0,0 +1,51 @@ +package com.baeldung.jackson.tojsonnode; + +import com.fasterxml.jackson.core.JsonFactory; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonParser; +import org.junit.Test; + +import java.io.IOException; + +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class StringToJsonNodeUnitTest { + + @Test + public final void whenParsingJsonStringIntoJsonNode_thenCorrect() throws JsonParseException, IOException { + final String jsonString = "{\"k1\":\"v1\",\"k2\":\"v2\"}"; + + final ObjectMapper mapper = new ObjectMapper(); + final JsonNode actualObj = mapper.readTree(jsonString); + + assertNotNull(actualObj); + } + + @Test + public final void givenUsingLowLevelDetails_whenParsingJsonStringIntoJsonNode_thenCorrect() throws JsonParseException, IOException { + final String jsonString = "{\"k1\":\"v1\",\"k2\":\"v2\"}"; + + final ObjectMapper mapper = new ObjectMapper(); + final JsonFactory factory = mapper.getFactory(); + final JsonParser parser = factory.createParser(jsonString); + final JsonNode actualObj = mapper.readTree(parser); + + assertNotNull(actualObj); + } + + @Test + public final void givenTheJsonNode_whenRetrievingDataFromId_thenCorrect() throws JsonParseException, IOException { + final String jsonString = "{\"k1\":\"v1\",\"k2\":\"v2\"}"; + final ObjectMapper mapper = new ObjectMapper(); + final JsonNode actualObj = mapper.readTree(jsonString); + + // When + final JsonNode jsonNode1 = actualObj.get("k1"); + assertThat(jsonNode1.textValue(), equalTo("v1")); + } + +} diff --git a/jackson/src/test/java/com/baeldung/jackson/xml/XMLSerializeDeserializeUnitTest.java b/jackson-conversions/src/test/java/com/baeldung/jackson/xml/XMLSerializeDeserializeUnitTest.java similarity index 98% rename from jackson/src/test/java/com/baeldung/jackson/xml/XMLSerializeDeserializeUnitTest.java rename to jackson-conversions/src/test/java/com/baeldung/jackson/xml/XMLSerializeDeserializeUnitTest.java index 1d430e9758..74ba28d996 100644 --- a/jackson/src/test/java/com/baeldung/jackson/xml/XMLSerializeDeserializeUnitTest.java +++ b/jackson-conversions/src/test/java/com/baeldung/jackson/xml/XMLSerializeDeserializeUnitTest.java @@ -16,8 +16,6 @@ import java.util.List; import org.junit.Test; -import com.baeldung.jackson.dtos.Address; -import com.baeldung.jackson.dtos.Person; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.dataformat.xml.XmlMapper; diff --git a/jackson/src/test/resources/simple_bean.xml b/jackson-conversions/src/test/resources/simple_bean.xml similarity index 100% rename from jackson/src/test/resources/simple_bean.xml rename to jackson-conversions/src/test/resources/simple_bean.xml diff --git a/jackson-simple/README.md b/jackson-simple/README.md index 8a37dd073f..558b506c8a 100644 --- a/jackson-simple/README.md +++ b/jackson-simple/README.md @@ -7,6 +7,3 @@ This module contains articles about Jackson that are also part of the Jackson Eb The "REST With Spring" Classes: http://bit.ly/restwithspring ### Relevant Articles: -- [Jackson Unmarshalling json with Unknown Properties](https://www.baeldung.com/jackson-deserialize-json-unknown-properties) -- [Intro to the Jackson ObjectMapper](https://www.baeldung.com/jackson-object-mapper-tutorial) -- [Ignore Null Fields with Jackson](https://www.baeldung.com/jackson-ignore-null-fields) diff --git a/jackson/README.md b/jackson/README.md index f2aed46145..72e82c26db 100644 --- a/jackson/README.md +++ b/jackson/README.md @@ -8,26 +8,12 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring ### Relevant Articles: -- [Jackson – Unmarshall to Collection/Array](https://www.baeldung.com/jackson-collection-array) - [Jackson – Custom Serializer](https://www.baeldung.com/jackson-custom-serialization) - [Getting Started with Custom Deserialization in Jackson](https://www.baeldung.com/jackson-deserialization) -- [Jackson Date](https://www.baeldung.com/jackson-serialize-dates) - [Jackson JSON Tutorial](https://www.baeldung.com/jackson) -- [Jackson – Working with Maps and nulls](https://www.baeldung.com/jackson-map-null-values-or-null-key) -- [Jackson – Decide What Fields Get Serialized/Deserialized](https://www.baeldung.com/jackson-field-serializable-deserializable-or-not) - [Jackson vs Gson](https://www.baeldung.com/jackson-vs-gson) -- [XML Serialization and Deserialization with Jackson](https://www.baeldung.com/jackson-xml-serialization-and-deserialization) -- [Inheritance with Jackson](https://www.baeldung.com/jackson-inheritance) - [Using Optional with Jackson](https://www.baeldung.com/jackson-optional) -- [Map Serialization and Deserialization with Jackson](https://www.baeldung.com/jackson-map) -- [Jackson Streaming API](https://www.baeldung.com/jackson-streaming-api) -- [How To Serialize and Deserialize Enums with Jackson](https://www.baeldung.com/jackson-serialize-enums) -- [Jackson – Marshall String to JsonNode](https://www.baeldung.com/jackson-json-to-jsonnode) -- [Jackson – Unmarshall to Collection/Array](https://www.baeldung.com/jackson-collection-array) - [Serialize Only Fields that meet a Custom Criteria with Jackson](https://www.baeldung.com/jackson-serialize-field-custom-criteria) -- [Mapping Nested Values with Jackson](https://www.baeldung.com/jackson-nested-values) -- [Convert XML to JSON Using Jackson](https://www.baeldung.com/jackson-convert-xml-json) -- [Deserialize Immutable Objects with Jackson](https://www.baeldung.com/jackson-deserialize-immutable-objects) -- [Mapping a Dynamic JSON Object with Jackson](https://www.baeldung.com/jackson-mapping-dynamic-object) +- [Intro to the Jackson ObjectMapper](https://www.baeldung.com/jackson-object-mapper-tutorial) +- [Inheritance with Jackson](https://www.baeldung.com/jackson-inheritance) - More articles: [[next -->]](/../jackson-2) - diff --git a/jackson/src/test/java/com/baeldung/jackson/inheritance/IgnoranceAnnotationStructure.java b/jackson/src/main/java/com/baeldung/jackson/inheritance/IgnoranceAnnotationStructure.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/inheritance/IgnoranceAnnotationStructure.java rename to jackson/src/main/java/com/baeldung/jackson/inheritance/IgnoranceAnnotationStructure.java diff --git a/jackson/src/test/java/com/baeldung/jackson/inheritance/IgnoranceMixinOrIntrospection.java b/jackson/src/main/java/com/baeldung/jackson/inheritance/IgnoranceMixinOrIntrospection.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/inheritance/IgnoranceMixinOrIntrospection.java rename to jackson/src/main/java/com/baeldung/jackson/inheritance/IgnoranceMixinOrIntrospection.java diff --git a/jackson/src/test/java/com/baeldung/jackson/inheritance/SubTypeConstructorStructure.java b/jackson/src/main/java/com/baeldung/jackson/inheritance/SubTypeConstructorStructure.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/inheritance/SubTypeConstructorStructure.java rename to jackson/src/main/java/com/baeldung/jackson/inheritance/SubTypeConstructorStructure.java diff --git a/jackson/src/test/java/com/baeldung/jackson/inheritance/SubTypeConversionStructure.java b/jackson/src/main/java/com/baeldung/jackson/inheritance/SubTypeConversionStructure.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/inheritance/SubTypeConversionStructure.java rename to jackson/src/main/java/com/baeldung/jackson/inheritance/SubTypeConversionStructure.java diff --git a/jackson/src/test/java/com/baeldung/jackson/inheritance/TypeInfoAnnotatedStructure.java b/jackson/src/main/java/com/baeldung/jackson/inheritance/TypeInfoAnnotatedStructure.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/inheritance/TypeInfoAnnotatedStructure.java rename to jackson/src/main/java/com/baeldung/jackson/inheritance/TypeInfoAnnotatedStructure.java diff --git a/jackson/src/test/java/com/baeldung/jackson/inheritance/TypeInfoStructure.java b/jackson/src/main/java/com/baeldung/jackson/inheritance/TypeInfoStructure.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/inheritance/TypeInfoStructure.java rename to jackson/src/main/java/com/baeldung/jackson/inheritance/TypeInfoStructure.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/CustomCarDeserializer.java b/jackson/src/main/java/com/baeldung/jackson/objectmapper/CustomCarDeserializer.java similarity index 100% rename from jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/CustomCarDeserializer.java rename to jackson/src/main/java/com/baeldung/jackson/objectmapper/CustomCarDeserializer.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/CustomCarSerializer.java b/jackson/src/main/java/com/baeldung/jackson/objectmapper/CustomCarSerializer.java similarity index 100% rename from jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/CustomCarSerializer.java rename to jackson/src/main/java/com/baeldung/jackson/objectmapper/CustomCarSerializer.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/dto/Car.java b/jackson/src/main/java/com/baeldung/jackson/objectmapper/dto/Car.java similarity index 100% rename from jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/dto/Car.java rename to jackson/src/main/java/com/baeldung/jackson/objectmapper/dto/Car.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/dto/Request.java b/jackson/src/main/java/com/baeldung/jackson/objectmapper/dto/Request.java similarity index 100% rename from jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/dto/Request.java rename to jackson/src/main/java/com/baeldung/jackson/objectmapper/dto/Request.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/JavaReadWriteJsonExampleUnitTest.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JavaReadWriteJsonExampleUnitTest.java similarity index 99% rename from jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/JavaReadWriteJsonExampleUnitTest.java rename to jackson/src/test/java/com/baeldung/jackson/objectmapper/JavaReadWriteJsonExampleUnitTest.java index a339ddf2c5..77940df60b 100644 --- a/jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/JavaReadWriteJsonExampleUnitTest.java +++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JavaReadWriteJsonExampleUnitTest.java @@ -21,6 +21,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; public class JavaReadWriteJsonExampleUnitTest { + @Rule public TemporaryFolder folder = new TemporaryFolder(); diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/SerializationDeserializationFeatureUnitTest.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/SerializationDeserializationFeatureUnitTest.java similarity index 99% rename from jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/SerializationDeserializationFeatureUnitTest.java rename to jackson/src/test/java/com/baeldung/jackson/objectmapper/SerializationDeserializationFeatureUnitTest.java index fcfee98123..52ebd05bfe 100644 --- a/jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/SerializationDeserializationFeatureUnitTest.java +++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/SerializationDeserializationFeatureUnitTest.java @@ -19,6 +19,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; public class SerializationDeserializationFeatureUnitTest { + final String EXAMPLE_JSON = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }"; final String JSON_CAR = "{ \"color\" : \"Black\", \"type\" : \"Fiat\", \"year\" : \"1970\" }"; final String JSON_ARRAY = "[{ \"color\" : \"Black\", \"type\" : \"BMW\" }, { \"color\" : \"Red\", \"type\" : \"BMW\" }]"; diff --git a/jackson/src/test/java/com/baeldung/jackson/test/JacksonDeserializationUnitTest.java b/jackson/src/test/java/com/baeldung/jackson/test/JacksonDeserializationUnitTest.java index 035ff8ca9c..7bb40cbef9 100644 --- a/jackson/src/test/java/com/baeldung/jackson/test/JacksonDeserializationUnitTest.java +++ b/jackson/src/test/java/com/baeldung/jackson/test/JacksonDeserializationUnitTest.java @@ -17,7 +17,6 @@ import com.baeldung.jackson.deserialization.ItemDeserializer; import com.baeldung.jackson.dtos.Item; import com.baeldung.jackson.dtos.ItemWithSerializer; import com.baeldung.jackson.dtos.MyDto; -import com.baeldung.jackson.dtos.ignore.MyDtoIgnoreUnknown; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonParser; @@ -54,91 +53,6 @@ public class JacksonDeserializationUnitTest { assertThat(readValue.isBooleanValue(), equalTo(true)); } - // tests - json with unknown fields - - @Test(expected = UnrecognizedPropertyException.class) - public final void givenJsonHasUnknownValues_whenDeserializingAJsonToAClass_thenExceptionIsThrown() throws JsonParseException, JsonMappingException, IOException { - final String jsonAsString = "{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":true,\"stringValue2\":\"something\"}"; - final ObjectMapper mapper = new ObjectMapper(); - - final MyDto readValue = mapper.readValue(jsonAsString, MyDto.class); - - assertNotNull(readValue); - assertThat(readValue.getStringValue(), equalTo("a")); - assertThat(readValue.isBooleanValue(), equalTo(true)); - assertThat(readValue.getIntValue(), equalTo(1)); - } - - @Test - public final void givenJsonHasUnknownValuesButJacksonIsIgnoringUnknownFields_whenDeserializing_thenCorrect() throws JsonParseException, JsonMappingException, IOException { - final String jsonAsString = // @formatter:off - "{\"stringValue\":\"a\"," + - "\"intValue\":1," + - "\"booleanValue\":true," + - "\"stringValue2\":\"something\"}"; // @formatter:on - final ObjectMapper mapper = new ObjectMapper(); - mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - - final MyDto readValue = mapper.readValue(jsonAsString, MyDto.class); - - assertNotNull(readValue); - assertThat(readValue.getStringValue(), equalTo("a")); - assertThat(readValue.isBooleanValue(), equalTo(true)); - assertThat(readValue.getIntValue(), equalTo(1)); - } - - @Test - public final void givenJsonHasUnknownValuesButUnknownFieldsAreIgnoredOnClass_whenDeserializing_thenCorrect() throws JsonParseException, JsonMappingException, IOException { - final String jsonAsString = // @formatter:off - "{\"stringValue\":\"a\"," + - "\"intValue\":1," + - "\"booleanValue\":true," + - "\"stringValue2\":\"something\"}"; // @formatter:on - final ObjectMapper mapper = new ObjectMapper(); - - final MyDtoIgnoreUnknown readValue = mapper.readValue(jsonAsString, MyDtoIgnoreUnknown.class); - - assertNotNull(readValue); - assertThat(readValue.getStringValue(), equalTo("a")); - assertThat(readValue.isBooleanValue(), equalTo(true)); - assertThat(readValue.getIntValue(), equalTo(1)); - } - - // to JsonNode - - @Test - public final void whenParsingJsonStringIntoJsonNode_thenCorrect() throws JsonParseException, IOException { - final String jsonString = "{\"k1\":\"v1\",\"k2\":\"v2\"}"; - - final ObjectMapper mapper = new ObjectMapper(); - final JsonNode actualObj = mapper.readTree(jsonString); - - assertNotNull(actualObj); - } - - @Test - public final void givenUsingLowLevelDetails_whenParsingJsonStringIntoJsonNode_thenCorrect() throws JsonParseException, IOException { - final String jsonString = "{\"k1\":\"v1\",\"k2\":\"v2\"}"; - - final ObjectMapper mapper = new ObjectMapper(); - final JsonFactory factory = mapper.getFactory(); - final JsonParser parser = factory.createParser(jsonString); - final JsonNode actualObj = mapper.readTree(parser); - - assertNotNull(actualObj); - } - - @Test - public final void givenTheJsonNode_whenRetrievingDataFromId_thenCorrect() throws JsonParseException, IOException { - final String jsonString = "{\"k1\":\"v1\",\"k2\":\"v2\"}"; - final ObjectMapper mapper = new ObjectMapper(); - final JsonNode actualObj = mapper.readTree(jsonString); - - // When - final JsonNode jsonNode1 = actualObj.get("k1"); - assertThat(jsonNode1.textValue(), equalTo("v1")); - } - // custom deserialization @Test diff --git a/jackson/src/test/java/com/baeldung/jackson/test/UnitTestSuite.java b/jackson/src/test/java/com/baeldung/jackson/test/UnitTestSuite.java index e783c67f5b..af99fed5d8 100644 --- a/jackson/src/test/java/com/baeldung/jackson/test/UnitTestSuite.java +++ b/jackson/src/test/java/com/baeldung/jackson/test/UnitTestSuite.java @@ -7,13 +7,10 @@ import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({ // @formatter:off - JacksonCollectionDeserializationUnitTest.class - ,JacksonSerializationEnumsUnitTest.class - ,JacksonDeserializationUnitTest.class + JacksonDeserializationUnitTest.class ,JacksonDeserializationUnitTest.class ,JacksonPrettyPrintUnitTest.class ,SandboxUnitTest.class - ,JacksonFieldUnitTest.class }) // @formatter:on public class UnitTestSuite { } \ No newline at end of file diff --git a/jackson-simple/src/test/resources/json_car.json b/jackson/src/test/resources/json_car.json similarity index 100% rename from jackson-simple/src/test/resources/json_car.json rename to jackson/src/test/resources/json_car.json diff --git a/pom.xml b/pom.xml index 0bd55a3e75..5a05a2a113 100644 --- a/pom.xml +++ b/pom.xml @@ -502,6 +502,8 @@ jackson-simple jackson-annotations jackson-exceptions + jackson-conversions + jackson-conversions-2 java-collections-conversions java-collections-conversions-2 java-collections-maps @@ -1272,6 +1274,8 @@ jackson-simple jackson-annotations jackson-exceptions + jackson-conversions + jackson-conversions-2 java-collections-conversions java-collections-conversions-2 java-collections-maps