BAEL-19947: Migrate gson module to the com.baeldung package

This commit is contained in:
Krzysiek 2019-12-15 21:16:48 +01:00
parent 4a46ad8991
commit a73d7872b5
55 changed files with 144 additions and 147 deletions

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.entities;
package com.baeldung.gson.entities;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.entities;
package com.baeldung.gson.entities;
public abstract class Animal {
public String type = "Animal";

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.entities;
package com.baeldung.gson.entities;
public class Cow extends Animal {
private String breed;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.entities;
package com.baeldung.gson.entities;
public class Dog extends Animal {
private String petName;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.entities;
package com.baeldung.gson.entities;
public class Employee {
private int id;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.entities;
package com.baeldung.gson.entities;
import java.util.List;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.entities;
package com.baeldung.gson.entities;
import com.google.gson.annotations.Expose;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.entities;
package com.baeldung.gson.entities;
import java.util.Objects;

View File

@ -1,19 +1,19 @@
package org.baeldung.gson.entities;
public class User {
private int id;
private String name;
private transient String nationality;
public User(int id, String name, String nationality) {
this.id = id;
this.name = name;
this.nationality = nationality;
}
public User(int id, String name) {
this(id, name, null);
}
}
package com.baeldung.gson.entities;
public class User {
private int id;
private String name;
private transient String nationality;
public User(int id, String name, String nationality) {
this.id = id;
this.name = name;
this.nationality = nationality;
}
public User(int id, String name) {
this(id, name, null);
}
}

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.entities;
package com.baeldung.gson.entities;
import com.google.gson.annotations.SerializedName;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.primitives.models;
package com.baeldung.gson.primitives.models;
public class BooleanExample {
public boolean value;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.primitives.models;
package com.baeldung.gson.primitives.models;
public class ByteExample {
public byte value = (byte) 1;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.primitives.models;
package com.baeldung.gson.primitives.models;
public class CharExample {
public char value;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.primitives.models;
package com.baeldung.gson.primitives.models;
public class DoubleExample {
public double value;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.primitives.models;
package com.baeldung.gson.primitives.models;
public class FloatExample {
public float value;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.primitives.models;
package com.baeldung.gson.primitives.models;
public class InfinityValuesExample {
public float negativeInfinity;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.primitives.models;
package com.baeldung.gson.primitives.models;
public class LongExample {
public long value = 1;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.primitives.models;
package com.baeldung.gson.primitives.models;
public class PrimitiveBundle {
public byte byteValue;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.primitives.models;
package com.baeldung.gson.primitives.models;
public class PrimitiveBundleInitialized {
// @formatter:off

View File

@ -1,11 +1,11 @@
package org.baeldung.gson.serialization;
package com.baeldung.gson.serialization;
import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import org.baeldung.gson.entities.ActorGson;
import com.baeldung.gson.entities.ActorGson;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;

View File

@ -1,10 +1,10 @@
package org.baeldung.gson.serialization;
package com.baeldung.gson.serialization;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import org.baeldung.gson.entities.ActorGson;
import com.baeldung.gson.entities.ActorGson;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serialization;
package com.baeldung.gson.serialization;
import com.google.gson.Gson;
import com.google.gson.JsonDeserializationContext;
@ -8,7 +8,7 @@ import com.google.gson.JsonObject;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import org.baeldung.gson.entities.Animal;
import com.baeldung.gson.entities.Animal;
public class AnimalDeserializer implements JsonDeserializer<Animal> {
private String animalTypeElementName;

View File

@ -1,12 +1,11 @@
package org.baeldung.gson.serialization;
package com.baeldung.gson.serialization;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import org.baeldung.gson.entities.Employee;
import com.baeldung.gson.entities.Employee;
import com.google.gson.*;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serialization;
package com.baeldung.gson.serialization;
import java.lang.reflect.Type;
import java.text.ParseException;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serializationwithexclusions;
package com.baeldung.gson.serializationwithexclusions;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serializationwithexclusions;
package com.baeldung.gson.serializationwithexclusions;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serializationwithexclusions;
package com.baeldung.gson.serializationwithexclusions;
import com.google.gson.annotations.Expose;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serializationwithexclusions;
package com.baeldung.gson.serializationwithexclusions;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serializationwithexclusions;
package com.baeldung.gson.serializationwithexclusions;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serializationwithexclusions;
package com.baeldung.gson.serializationwithexclusions;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serializationwithexclusions;
package com.baeldung.gson.serializationwithexclusions;
import com.google.gson.annotations.Expose;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serializationwithexclusions;
package com.baeldung.gson.serializationwithexclusions;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serializationwithexclusions;
package com.baeldung.gson.serializationwithexclusions;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@ -1,8 +1,9 @@
package org.baeldung.gson.advance;
package com.baeldung.gson.advance;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import com.baeldung.gson.entities.Dog;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
@ -10,11 +11,10 @@ import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.baeldung.gson.entities.Animal;
import org.baeldung.gson.entities.Cow;
import org.baeldung.gson.entities.Dog;
import org.baeldung.gson.entities.MyClass;
import org.baeldung.gson.serialization.AnimalDeserializer;
import com.baeldung.gson.entities.Animal;
import com.baeldung.gson.entities.Cow;
import com.baeldung.gson.entities.MyClass;
import com.baeldung.gson.serialization.AnimalDeserializer;
import org.junit.Test;
public class GsonAdvanceUnitTest {

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.advance;
package com.baeldung.gson.advance;
/*
* Copyright (C) 2011 Google Inc.

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.conversion;
package com.baeldung.gson.conversion;
import com.google.gson.*;
import org.junit.Assert;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.deserialization;
package com.baeldung.gson.deserialization;
public class Foo {
public int intValue;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.deserialization;
package com.baeldung.gson.deserialization;
import java.lang.reflect.Type;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.deserialization;
package com.baeldung.gson.deserialization;
import java.lang.reflect.Type;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.deserialization;
package com.baeldung.gson.deserialization;
import java.lang.reflect.Type;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.deserialization;
package com.baeldung.gson.deserialization;
public class FooWithInner {
public int intValue;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.deserialization;
package com.baeldung.gson.deserialization;
public class GenericFoo<T> {

View File

@ -1,8 +1,8 @@
package org.baeldung.gson.deserialization;
package com.baeldung.gson.deserialization;
import static org.junit.Assert.assertEquals;
import org.baeldung.gson.entities.Weather;
import com.baeldung.gson.entities.Weather;
import org.junit.Test;
import com.google.gson.Gson;

View File

@ -1,10 +1,10 @@
package org.baeldung.gson.deserialization;
package com.baeldung.gson.deserialization;
import java.text.ParseException;
import org.baeldung.gson.entities.ActorGson;
import org.baeldung.gson.entities.Movie;
import org.baeldung.gson.serialization.ActorGsonDeserializer;
import com.baeldung.gson.entities.Movie;
import com.baeldung.gson.serialization.ActorGsonDeserializer;
import com.baeldung.gson.entities.ActorGson;
import org.junit.Assert;
import org.junit.Test;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.deserialization;
package com.baeldung.gson.deserialization;
import java.lang.reflect.Type;
import java.text.ParseException;
@ -6,9 +6,9 @@ import java.util.Date;
import java.util.Map;
import org.apache.commons.lang3.time.DateUtils;
import org.baeldung.gson.entities.Employee;
import org.baeldung.gson.serialization.MapDeserializer;
import org.baeldung.gson.serialization.StringDateMapDeserializer;
import com.baeldung.gson.entities.Employee;
import com.baeldung.gson.serialization.MapDeserializer;
import com.baeldung.gson.serialization.StringDateMapDeserializer;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.deserialization.test;
package com.baeldung.gson.deserialization.test;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.instanceOf;
@ -10,11 +10,11 @@ import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import org.baeldung.gson.deserialization.Foo;
import org.baeldung.gson.deserialization.FooDeserializerFromJsonWithDifferentFields;
import org.baeldung.gson.deserialization.FooInstanceCreator;
import org.baeldung.gson.deserialization.FooWithInner;
import org.baeldung.gson.deserialization.GenericFoo;
import com.baeldung.gson.deserialization.Foo;
import com.baeldung.gson.deserialization.FooDeserializerFromJsonWithDifferentFields;
import com.baeldung.gson.deserialization.FooInstanceCreator;
import com.baeldung.gson.deserialization.FooWithInner;
import com.baeldung.gson.deserialization.GenericFoo;
import org.junit.Test;
import com.google.common.collect.Lists;

View File

@ -1,7 +1,7 @@
package org.baeldung.gson.primitives;
package com.baeldung.gson.primitives;
import com.baeldung.gson.primitives.models.*;
import com.google.gson.*;
import org.baeldung.gson.primitives.models.*;
import org.junit.Test;
import java.lang.reflect.Type;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serialization;
package com.baeldung.gson.serialization;
import java.lang.reflect.Type;

View File

@ -1,11 +1,11 @@
package org.baeldung.gson.serialization;
package com.baeldung.gson.serialization;
import com.baeldung.gson.entities.Movie;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParser;
import org.baeldung.gson.entities.ActorGson;
import org.baeldung.gson.entities.Movie;
import org.baeldung.gson.entities.MovieWithNullValue;
import com.baeldung.gson.entities.ActorGson;
import com.baeldung.gson.entities.MovieWithNullValue;
import org.junit.Assert;
import org.junit.Test;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serialization;
package com.baeldung.gson.serialization;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serialization;
package com.baeldung.gson.serialization;
import java.lang.reflect.Type;

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serialization;
package com.baeldung.gson.serialization;
public class SourceClass {
private int intValue;

View File

@ -1,18 +1,16 @@
package org.baeldung.gson.serialization.test;
package com.baeldung.gson.serialization.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.lang.reflect.Type;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import org.baeldung.gson.serialization.DifferentNameSerializer;
import org.baeldung.gson.serialization.IgnoringFieldsNotMatchingCriteriaSerializer;
import org.baeldung.gson.serialization.IgnoringFieldsSerializer;
import org.baeldung.gson.serialization.SourceClass;
import com.baeldung.gson.serialization.DifferentNameSerializer;
import com.baeldung.gson.serialization.IgnoringFieldsNotMatchingCriteriaSerializer;
import com.baeldung.gson.serialization.IgnoringFieldsSerializer;
import com.baeldung.gson.serialization.SourceClass;
import org.joda.time.DateTime;
import org.junit.Test;

View File

@ -1,43 +1,43 @@
package org.baeldung.gson.serialization.test;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.baeldung.gson.entities.User;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@RunWith(Parameterized.class)
public class JsonFileUnitTest {
@Parameter
public Object object;
@Parameters
public static Object[] data() {
return new Object[] { 123.45, new User(1, "Tom", "American") };
}
@Test
public void givenProperData_whenStoredInFile_shouldSaveJsonSuccessfully() {
String filePath = "target/output.json";
try (Writer writer = new FileWriter(filePath)) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
gson.toJson(object, writer);
Assert.assertTrue(Files.exists(Paths.get(filePath)));
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.baeldung.gson.serialization.test;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Paths;
import com.baeldung.gson.entities.User;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@RunWith(Parameterized.class)
public class JsonFileUnitTest {
@Parameter
public Object object;
@Parameters
public static Object[] data() {
return new Object[] { 123.45, new User(1, "Tom", "American") };
}
@Test
public void givenProperData_whenStoredInFile_shouldSaveJsonSuccessfully() {
String filePath = "target/output.json";
try (Writer writer = new FileWriter(filePath)) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
gson.toJson(object, writer);
Assert.assertTrue(Files.exists(Paths.get(filePath)));
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -1,4 +1,4 @@
package org.baeldung.gson.serializationwithexclusions;
package com.baeldung.gson.serializationwithexclusions;
import static org.junit.jupiter.api.Assertions.assertEquals;