Parse dates

This commit is contained in:
Johannes Frohnmeyer 2022-11-01 16:58:16 +01:00
parent ca86f98ae4
commit fe2b1fbe24
Signed by: Johannes
GPG Key ID: E76429612C2929F4
6 changed files with 52 additions and 2 deletions

View File

@ -12,6 +12,7 @@ The goal of this AP is to
- Nested serializable types
- Arrays
- Collections (Sets, Lists, Queues, Deques)
- java.util.Date as iso8601
## Used properties
Use `@GPrefer` to choose one construction method if multiple are available
@ -29,7 +30,6 @@ Use `@GPrefer` to choose one construction method if multiple are available
## TODO
- Maps with string, primitive or enum keys
- Date via ISO8601Utils
- Enums
- Support for nested types from libraries
- Static classes (for configs)
@ -37,4 +37,4 @@ Use `@GPrefer` to choose one construction method if multiple are available
## Credit
The Gson-Compile processor is based on [gsonvalue](https://github.com/evant/gsonvalue) and [value-processor](https://github.com/evant/value-processor) by Eva Tatarka.
The API was inspired by [kotlinx.serialization](https://github.com/Kotlin/kotlinx.serialization) by Jetbrains
The serialization is powered by [my fork](https://gitlab.com/JFronny/gson-comments) of [gson](https://github.com/google/gson) by Google
The serialization is powered by [my fork](https://gitlab.com/JFronny/gson-comments) of [gson](https://github.com/google/gson) by Google

View File

@ -1,7 +1,21 @@
package io.gitlab.jfronny.gson.compile.core;
import io.gitlab.jfronny.commons.serialize.gson.api.v1.GsonHolder;
import io.gitlab.jfronny.gson.JsonSyntaxException;
import io.gitlab.jfronny.gson.internal.bind.util.ISO8601Utils;
import java.text.ParseException;
import java.text.ParsePosition;
import java.util.Date;
public class CCore {
public static final GsonHolder HOLDER = new GsonHolder();
public static Date parseDate(String date) {
try {
return ISO8601Utils.parse(date, new ParsePosition(0));
} catch (ParseException e) {
throw new JsonSyntaxException("Failed parsing '" + date + "' as Date", e);
}
}
}

View File

@ -31,6 +31,8 @@ public class Main {
public Queue<String> queue;
public Date date;
public void setJoe(String joe) {
}

View File

@ -16,5 +16,6 @@ public class Cl {
public static final ClassName SERIALIZED_NAME = ClassName.get("io.gitlab.jfronny.gson.annotations", "SerializedName");
public static final ClassName GCOMMENT = ClassName.get("io.gitlab.jfronny.gson.compile.annotations", "GComment");
public static final ClassName GISO8601UTILS = ClassName.get("io.gitlab.jfronny.gson.internal.bind.util", "ISO8601Utils");
public static final ClassName CCORE = ClassName.get("io.gitlab.jfronny.gson.compile.core", "CCore");
}

View File

@ -19,6 +19,7 @@ public class Adapters {
new DeclaredAdapter(),
new PrimitiveAdapter(),
new StringAdapter(),
new DateAdapter(),
new ArrayAdapter(),
new CollectionAdapter(),
new OtherSerializableAdapter(),

View File

@ -0,0 +1,32 @@
package io.gitlab.jfronny.gson.compile.processor.adapter.impl;
import io.gitlab.jfronny.gson.compile.processor.Cl;
import io.gitlab.jfronny.gson.compile.processor.adapter.Adapter;
import java.util.Date;
public class DateAdapter extends Adapter<DateAdapter.Hydrated> {
@Override
public Hydrated instantiate() {
return new Hydrated();
}
public class Hydrated extends Adapter<Hydrated>.Hydrated {
@Override
public boolean applies() {
return type.toString().equals(Date.class.getCanonicalName());
}
@Override
public void generateWrite(Runnable writeGet) {
code.add("writer.value($T.format(", Cl.GISO8601UTILS);
writeGet.run();
code.add("));\n");
}
@Override
public void generateRead() {
code.add("$T.parseDate(reader.nextString())", Cl.CCORE);
}
}
}