Merge branch 'master' into BAEL-6818-check-value-exists-json-array

This commit is contained in:
Sam Gardner
2023-08-22 10:05:00 +01:00
327 changed files with 10693 additions and 2511 deletions
+1
View File
@@ -12,5 +12,6 @@ This module contains articles about JSON.
- [Check Whether a String Is Valid JSON in Java](https://www.baeldung.com/java-validate-json-string)
- [Getting a Value in JSONObject](https://www.baeldung.com/java-jsonobject-get-value)
- [Pretty-Print a JSON in Java](https://www.baeldung.com/java-json-pretty-print)
- [Remove Whitespaces From a JSON in Java](https://www.baeldung.com/java-json-minify-remove-whitespaces)
- More Articles: [[<-- prev]](/json-modules/json)
+73 -2
View File
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>json-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
@@ -123,6 +123,26 @@
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.github.victools</groupId>
<artifactId>jsonschema-generator</artifactId>
<version>${jsonschema-generator.version}</version>
</dependency>
<dependency>
<groupId>com.github.victools</groupId>
<artifactId>jsonschema-module-jackson</artifactId>
<version>${jsonschema-generator.version}</version>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>${jakarta.validation.version}</version>
</dependency>
<dependency>
<groupId>com.github.victools</groupId>
<artifactId>jsonschema-module-jakarta-validation</artifactId>
<version>${jsonschema-generator.version}</version>
</dependency>
</dependencies>
<build>
@@ -162,6 +182,55 @@
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>com.github.victools</groupId>
<artifactId>jsonschema-maven-plugin</artifactId>
<version>${jsonschema-generator.version}</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<packageNames>
<packageName>com.baeldung.jsonschemageneration.plugin</packageName>
</packageNames>
<classNames>
<className>com.baeldung.jsonschemageneration.plugin.Person</className>
</classNames>
<schemaVersion>DRAFT_2020_12</schemaVersion>
<schemaFilePath>src/main/resources/schemas</schemaFilePath>
<schemaFileName>{1}/{0}.json</schemaFileName>
<failIfNoClassesMatch>true</failIfNoClassesMatch>
<options>
<preset>PLAIN_JSON</preset>
<enabled>
<option>DEFINITIONS_FOR_ALL_OBJECTS</option>
<option>FORBIDDEN_ADDITIONAL_PROPERTIES_BY_DEFAULT</option>
</enabled>
<disabled>SCHEMA_VERSION_INDICATOR</disabled>
</options>
<modules>
<module>
<name>Jackson</name>
<options>
<option>RESPECT_JSONPROPERTY_REQUIRED</option>
</options>
</module>
<module>
<name>JakartaValidation</name>
<options>
<option>NOT_NULLABLE_FIELD_IS_REQUIRED</option>
<option>INCLUDE_PATTERN_EXPRESSIONS</option>
</options>
</module>
</modules>
</configuration>
</plugin>
</plugins>
</build>
<properties>
@@ -173,6 +242,8 @@
<jackson-jsonld.version>0.1.1</jackson-jsonld.version>
<hydra-jsonld.version>0.4.2</hydra-jsonld.version>
<jsonld-java.version>0.13.0</jsonld-java.version>
<jsonschema-generator.version>4.31.1</jsonschema-generator.version>
<jakarta.validation.version>3.0.2</jakarta.validation.version>
</properties>
</project>
@@ -0,0 +1,70 @@
package com.baeldung.jsonschemageneration.configuration;
import java.sql.Timestamp;
import java.util.Date;
import java.util.UUID;
enum Area {
JAVA("JAVA"), KOTLIN("KOTLIN"), SCALA("SCALA"), LINUX("LINUX");
private final String area;
Area(String area) {
this.area = area;
}
public String getArea() {
return area;
}
}
public class AdvancedArticle {
private UUID id;
private String title;
private String content;
@AllowedTypes({ Timestamp.class, Date.class })
private Object createdAt;
private Area area;
public Area getArea() {
return area;
}
public void setArea(Area area) {
this.area = area;
}
public Object getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Object createdAt) {
this.createdAt = createdAt;
}
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
@@ -0,0 +1,39 @@
package com.baeldung.jsonschemageneration.configuration;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.victools.jsonschema.generator.Option;
import com.github.victools.jsonschema.generator.OptionPreset;
import com.github.victools.jsonschema.generator.SchemaGenerator;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfig;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder;
import com.github.victools.jsonschema.generator.SchemaVersion;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class AdvancedConfigurationSchemaGenerator {
public static void main(String[] args) {
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12, OptionPreset.PLAIN_JSON);
configBuilder.forFields().withInstanceAttributeOverride((node, field, context) -> node.put("readOnly", field.getDeclaredType().isInstanceOf(UUID.class)));
configBuilder.forFields()
.withTargetTypeOverridesResolver(field -> Optional.ofNullable(field.getAnnotationConsideringFieldAndGetterIfSupported(AllowedTypes.class))
.map(AllowedTypes::value)
.map(Stream::of)
.map(stream -> stream.map(subtype -> field.getContext().resolve(subtype)))
.map(stream -> stream.collect(Collectors.toList()))
.orElse(null));
SchemaGeneratorConfig config = configBuilder.with(Option.EXTRA_OPEN_API_FORMAT_VALUES).build();
SchemaGenerator generator = new SchemaGenerator(config);
JsonNode jsonSchema = generator.generateSchema(AdvancedArticle.class);
System.out.println(jsonSchema.toPrettyString());
}
}
@@ -0,0 +1,13 @@
package com.baeldung.jsonschemageneration.configuration;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface AllowedTypes {
Class<?>[] value();
}
@@ -0,0 +1,44 @@
package com.baeldung.jsonschemageneration.configuration;
import com.baeldung.jsonschemageneration.recursive.Author;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.victools.jsonschema.generator.Option;
import com.github.victools.jsonschema.generator.OptionPreset;
import com.github.victools.jsonschema.generator.SchemaGenerator;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfig;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder;
import com.github.victools.jsonschema.generator.SchemaVersion;
import javax.annotation.Nullable;
import javax.validation.constraints.NotNull;
import java.time.Instant;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class IndividualConfigurationSchemaGenerator {
public static void main(String[] args) {
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12, OptionPreset.PLAIN_JSON);
configBuilder.forFields().withRequiredCheck(field -> field.getAnnotationConsideringFieldAndGetter(Nullable.class) == null).withArrayUniqueItemsResolver(scope -> scope.getType().getErasedType() == (List.class) ? true : null);
configBuilder.forMethods().withRequiredCheck(method -> method.getAnnotationConsideringFieldAndGetter(NotNull.class) != null);
configBuilder.forTypesInGeneral()
.withArrayUniqueItemsResolver(scope -> scope.getType().getErasedType() == (List.class) ? true : null)
.withDefaultResolver(scope -> scope.getType().getErasedType() == List.class ? Collections.EMPTY_LIST : null)
.withDefaultResolver(scope -> scope.getType().getErasedType() == Date.class ? Date.from(Instant.now()) : null)
.withEnumResolver(scope -> scope.getType().getErasedType().isEnum() ? Stream.of(scope.getType().getErasedType().getEnumConstants()).map(v -> ((Enum<?>) v).name()).collect(Collectors.toList()) : null);
SchemaGeneratorConfig config = configBuilder.with(Option.EXTRA_OPEN_API_FORMAT_VALUES).build();
SchemaGenerator generator = new SchemaGenerator(config);
JsonNode jsonSchema = generator.generateSchema(Author.class);
System.out.println(jsonSchema.toPrettyString());
}
}
@@ -0,0 +1,68 @@
package com.baeldung.jsonschemageneration.modules;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.victools.jsonschema.generator.SchemaGenerator;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder;
import com.github.victools.jsonschema.module.jackson.JacksonModule;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import static com.github.victools.jsonschema.generator.Option.EXTRA_OPEN_API_FORMAT_VALUES;
import static com.github.victools.jsonschema.generator.OptionPreset.PLAIN_JSON;
import static com.github.victools.jsonschema.generator.SchemaVersion.DRAFT_2020_12;
import static com.github.victools.jsonschema.module.jackson.JacksonOption.RESPECT_JSONPROPERTY_REQUIRED;
public class JacksonModuleSchemaGenerator {
public static void main(String[] args) {
JacksonModule module = new JacksonModule(RESPECT_JSONPROPERTY_REQUIRED);
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(DRAFT_2020_12, PLAIN_JSON).with(module).with(EXTRA_OPEN_API_FORMAT_VALUES);
SchemaGenerator generator = new SchemaGenerator(configBuilder.build());
JsonNode jsonSchema = generator.generateSchema(Person.class);
System.out.println(jsonSchema.toPrettyString());
}
static class Person {
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
UUID id;
@JsonProperty(access = JsonProperty.Access.READ_WRITE, required = true)
String name;
@JsonProperty(access = JsonProperty.Access.READ_WRITE, required = true)
String surname;
@JsonProperty(access = JsonProperty.Access.READ_WRITE, required = true)
Address address;
@JsonIgnore
String fullName;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
Date createdAt;
@JsonProperty(access = JsonProperty.Access.READ_WRITE)
List<Person> friends;
}
static class Address {
@JsonProperty()
String street;
@JsonProperty(required = true)
String city;
@JsonProperty(required = true)
String country;
}
}
@@ -0,0 +1,63 @@
package com.baeldung.jsonschemageneration.modules;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.victools.jsonschema.generator.SchemaGenerator;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder;
import com.github.victools.jsonschema.module.jakarta.validation.JakartaValidationModule;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Null;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import static com.github.victools.jsonschema.generator.OptionPreset.PLAIN_JSON;
import static com.github.victools.jsonschema.generator.SchemaVersion.DRAFT_2020_12;
import static com.github.victools.jsonschema.module.jakarta.validation.JakartaValidationOption.INCLUDE_PATTERN_EXPRESSIONS;
import static com.github.victools.jsonschema.module.jakarta.validation.JakartaValidationOption.NOT_NULLABLE_FIELD_IS_REQUIRED;
public class JakartaValidationModuleSchemaGenerator {
public static void main(String[] args) {
JakartaValidationModule module = new JakartaValidationModule(NOT_NULLABLE_FIELD_IS_REQUIRED, INCLUDE_PATTERN_EXPRESSIONS);
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(DRAFT_2020_12, PLAIN_JSON).with(module);
SchemaGenerator generator = new SchemaGenerator(configBuilder.build());
JsonNode jsonSchema = generator.generateSchema(Person.class);
System.out.println(jsonSchema.toPrettyString());
}
static class Person {
@NotNull UUID id;
@NotNull String name;
@NotNull @Email @Pattern(regexp = "\\b[A-Za-z0-9._%+-]+@baeldung\\.com\\b") String email;
@NotNull String surname;
@NotNull Address address;
@Null String fullName;
@NotNull Date createdAt;
@Size(max = 10) List<Person> friends;
}
static class Address {
@Null String street;
@NotNull String city;
@NotNull String country;
}
}
@@ -0,0 +1,61 @@
package com.baeldung.jsonschemageneration.plugin;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Null;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import java.util.Date;
import java.util.List;
import java.util.UUID;
public class Person {
@NotNull
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
UUID id;
@NotNull
@JsonProperty(access = JsonProperty.Access.READ_WRITE, required = true)
String name;
@NotNull
@JsonProperty(access = JsonProperty.Access.READ_WRITE, required = true)
@Email @Pattern(regexp = "\\b[A-Za-z0-9._%+-]+@baeldung\\.com\\b") String email;
@NotNull
@JsonProperty(access = JsonProperty.Access.READ_WRITE, required = true)
String surname;
@NotNull
@JsonProperty(access = JsonProperty.Access.READ_WRITE, required = true)
Address address;
@Null String fullName;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
Date createdAt;
@Size(max = 10)
@JsonProperty(access = JsonProperty.Access.READ_WRITE)
List<Person> friends;
}
class Address {
@Null
@JsonProperty
String street;
@NotNull
@JsonProperty(required = true)
String city;
@NotNull
@JsonProperty(required = true)
String country;
}
@@ -0,0 +1,12 @@
package com.baeldung.jsonschemageneration.recursive;
import java.util.List;
import java.util.UUID;
public class Author {
private UUID id;
private String name;
private String role;
private List<AuthoredArticle> articles;
}
@@ -0,0 +1,68 @@
package com.baeldung.jsonschemageneration.recursive;
import java.util.Date;
import java.util.UUID;
enum Area {
JAVA("JAVA"), KOTLIN("KOTLIN"), SCALA("SCALA"), LINUX("LINUX");
private final String area;
Area(String area) {
this.area = area;
}
public String getArea() {
return area;
}
}
public class AuthoredArticle {
private UUID id;
private String title;
private String content;
private Date createdAt;
private Area area;
private Author author;
public Area getArea() {
return area;
}
public void setArea(Area area) {
this.area = area;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
@@ -0,0 +1,23 @@
package com.baeldung.jsonschemageneration.recursive;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.victools.jsonschema.generator.Option;
import com.github.victools.jsonschema.generator.OptionPreset;
import com.github.victools.jsonschema.generator.SchemaGenerator;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfig;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder;
import com.github.victools.jsonschema.generator.SchemaVersion;
public class RecursiveSchemaGenerator {
public static void main(String[] args) {
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12, OptionPreset.PLAIN_JSON);
SchemaGeneratorConfig config = configBuilder.with(Option.EXTRA_OPEN_API_FORMAT_VALUES).without(Option.FLATTENED_ENUMS_FROM_TOSTRING).build();
SchemaGenerator generator = new SchemaGenerator(config);
JsonNode jsonSchema = generator.generateSchema(AuthoredArticle.class);
System.out.println(jsonSchema.toPrettyString());
}
}
@@ -0,0 +1,66 @@
package com.baeldung.jsonschemageneration.simple;
import java.util.Date;
import java.util.UUID;
enum Area {
JAVA("JAVA"), KOTLIN("KOTLIN"), SCALA("SCALA"), LINUX("LINUX");
private final String area;
Area(String area) {
this.area = area;
}
public String getArea() {
return area;
}
}
public class Article {
private UUID id;
private String title;
private String content;
private Date createdAt;
private Area area;
public Area getArea() {
return area;
}
public void setArea(Area area) {
this.area = area;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
@@ -0,0 +1,23 @@
package com.baeldung.jsonschemageneration.simple;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.victools.jsonschema.generator.Option;
import com.github.victools.jsonschema.generator.OptionPreset;
import com.github.victools.jsonschema.generator.SchemaGenerator;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfig;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder;
import com.github.victools.jsonschema.generator.SchemaVersion;
public class SimpleSchemaGenerator {
public static void main(String[] args) {
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12, OptionPreset.PLAIN_JSON);
SchemaGeneratorConfig config = configBuilder.with(Option.EXTRA_OPEN_API_FORMAT_VALUES).without(Option.FLATTENED_ENUMS_FROM_TOSTRING).build();
SchemaGenerator generator = new SchemaGenerator(config);
JsonNode jsonSchema = generator.generateSchema(Article.class);
System.out.println(jsonSchema.toPrettyString());
}
}
@@ -0,0 +1,54 @@
{
"$defs" : {
"Address" : {
"type" : "object",
"properties" : {
"city" : {
"type" : "string"
},
"country" : {
"type" : "string"
},
"street" : {
"type" : [ "string", "null" ]
}
},
"required" : [ "city", "country" ],
"additionalProperties" : false
}
},
"type" : "object",
"properties" : {
"address" : {
"$ref" : "#/$defs/Address"
},
"createdAt" : {
"type" : "string",
"readOnly" : true
},
"email" : {
"type" : "string",
"format" : "email",
"pattern" : "\\b[A-Za-z0-9._%+-]+@baeldung\\.com\\b"
},
"friends" : {
"maxItems" : 10,
"type" : "array",
"items" : {
"$ref" : "#"
}
},
"id" : {
"type" : "string",
"readOnly" : true
},
"name" : {
"type" : "string"
},
"surname" : {
"type" : "string"
}
},
"required" : [ "address", "email", "id", "name", "surname" ],
"additionalProperties" : false
}
+6
View File
@@ -0,0 +1,6 @@
## JSON-CONVERSIONS
This module contains articles about JSON Conversions
### Relevant Articles:
- [Convert JSON Array to Java List](https://www.baeldung.com/java-convert-json-array-to-list)
+39
View File
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung</groupId>
<artifactId>json-conversion</artifactId>
<name>json-conversion</name>
<parent>
<artifactId>json-modules</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>${json.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies>
<properties>
<json.version>20211205</json.version>
<gson.version>2.10.1</gson.version>
</properties>
</project>
@@ -0,0 +1,31 @@
package com.baeldung.jsonarraytolist;
import java.lang.reflect.Type;
import java.util.List;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import entities.Product;
public class ConvertJsonArrayToList {
public List<Product> convertJsonArrayUsingGsonLibrary(String jsonArray) {
Gson gson = new Gson();
Type listType = new TypeToken<List<Product>>() {}.getType();
List<Product> gsonList = gson.fromJson(jsonArray, listType);
return gsonList;
}
public List<Product> convertJsonArrayUsingJacksonLibrary(String jsonArray) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
List<Product> typeReferenceList = objectMapper.readValue(jsonArray, new TypeReference<List<Product>>() {});
return typeReferenceList;
}
}
@@ -0,0 +1,29 @@
package entities;
public class Product {
private int id;
private String name;
private String description;
public Product() {}
public Product(int id, String name, String description) {
this.id = id;
this.name = name;
this.description = description;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
}
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
@@ -0,0 +1,60 @@
package com.baeldung.jsonarraytolist;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import entities.Product;
public class ConvertJsonArrayToListUnitTest {
private static ObjectMapper objectMapper;
private static List<Product> productList;
private ConvertJsonArrayToList obj;
@Before
public void setup() {
obj = new ConvertJsonArrayToList();
productList = getProducts();
objectMapper = new ObjectMapper();
}
private List<Product> getProducts() {
List<Product> productList = new ArrayList<>();
Product prod1 = new Product(1, "Icecream", "Sweet and cold");
Product prod2 = new Product(2, "Apple", "Red and sweet");
Product prod3 = new Product(3, "Carrot", "Good for eyes");
productList.add(prod1);
productList.add(prod2);
productList.add(prod3);
return productList;
}
@Test
public void whenUsingGsonLibrary_thenCompareTwoProducts() {
Gson gson = new Gson();
String jsonArray = gson.toJson(productList);
List<Product> arrList = obj.convertJsonArrayUsingGsonLibrary(jsonArray);
Assert.assertEquals(productList.get(0).getId(), arrList.get(0).getId());
Assert.assertEquals(productList.get(1).getDescription(), arrList.get(1).getDescription());
Assert.assertEquals(productList.get(2).getName(), arrList.get(2).getName());
}
@Test
public void whenUsingJacksonLibrary_thenCompareTwoProducts() throws JsonProcessingException {
String jsonArray = objectMapper.writeValueAsString(productList);
List<Product> arrList = obj.convertJsonArrayUsingJacksonLibrary(jsonArray);
Assert.assertEquals(productList.get(0).getId(), arrList.get(0).getId());
Assert.assertEquals(productList.get(1).getDescription(), arrList.get(1).getDescription());
Assert.assertEquals(productList.get(2).getName(), arrList.get(2).getName());
}
}
@@ -0,0 +1,13 @@
*.class
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
# Packaged files #
*.jar
*.war
*.ear
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="WARN"/>
<logger name="org.springframework.transaction" level="WARN"/>
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
<logger name="org.springframework.web.servlet.mvc" level="WARN"/>
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
+1 -1
View File
@@ -17,10 +17,10 @@
<module>json</module>
<module>json-2</module>
<module>json-arrays</module>
<module>json-conversion</module>
<module>json-path</module>
<module>gson</module>
<module>gson-2</module>
</modules>
<build>