Gson to json conversion with two DateFormat

I was facing the same issue. Here is my solution via custom deserialization: new GsonBuilder().registerTypeAdapter(Date.class, new DateDeserializer()); private static final String[] DATE_FORMATS = new String[] { “MMM dd, yyyy HH:mm:ss”, “MMM dd, yyyy” }; private class DateDeserializer implements JsonDeserializer<Date> { @Override public Date deserialize(JsonElement jsonElement, Type typeOF, JsonDeserializationContext context) throws JsonParseException { for (String format … Read more

Gson Serialize field only if not null or not empty

Create your own TypeAdapter public class MyTypeAdapter extends TypeAdapter<TestObject>() { @Override public void write(JsonWriter out, TestObject value) throws IOException { out.beginObject(); if (!Strings.isNullOrEmpty(value.test1)) { out.name(“test1”); out.value(value.test1); } if (!Strings.isNullOrEmpty(value.test2)) { out.name(“test2”); out.value(value.test1); } /* similar check for otherObject */ out.endObject(); } @Override public TestObject read(JsonReader in) throws IOException { // do something similar, but the … Read more

What is the purpose of using @field:SerializedName annotation instead of @SerializedName?

TL;DR: The field: part is known as a “use-site target”, and makes it clear that the annotation is applied to the backing field of the Kotlin property. It is not strictly necessary in this case, though it can make the code more readable. Explanation To simplify, let’s say you have: data class Repo( @field:SerializedName(“name”) var … Read more

Why do I get Gson builder error when starting a Spring Boot application?

I would write this as a comment, but I still haven’t got enough rep. The problem must be with your dependencies. What happens here is that SpringBoot loads the GsonAutoConfiguration @Configuration class, which tries to call GsonBuilder‘s setLenient() method. SpringBoot already has the correct gson jar set as a dependency which should automatically be included … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)