Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Joda DateTime in my custom object #35

Open
tarchen opened this issue Aug 8, 2017 · 1 comment
Open

Using Joda DateTime in my custom object #35

tarchen opened this issue Aug 8, 2017 · 1 comment

Comments

@tarchen
Copy link

tarchen commented Aug 8, 2017

Is there any way to modify the tinyDB class to be able to save a custom object with a DateTime field? I tried adding the following:
`
private class DateTimeSerializer implements JsonSerializer {
public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.toString());
}
}

private class DateTimeDeserializer implements JsonDeserializer<DateTime> {
    public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
            throws JsonParseException {
        return new DateTime(json.getAsJsonPrimitive().getAsString());
    }
}

public void retrieveGlobalDataFromStorage(Context context) {

// ...

    final GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(DateTime.class, new DateTimeSerializer());
    builder.registerTypeAdapter(DateTime.class, new DateTimeDeserializer());

    final Gson gson = builder.create();

// ...
}` but I am still getting a fatal exception
Caused by: java.lang.RuntimeException: Failed to invoke public org.joda.time.Chronology() with no args
at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:111)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:210)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
at com.google.gson.Gson.fromJson(Gson.java:887)
at com.google.gson.Gson.fromJson(Gson.java:852)
at com.google.gson.Gson.fromJson(Gson.java:801)
at com.google.gson.Gson.fromJson(Gson.java:773)
at dtcj.badgerme.TinyDB.getListObject(TinyDB.java:358)

Thanks for any advice!
Taryn

@kcochibili
Copy link
Owner

kcochibili commented Aug 8, 2017

Using this Converter:
https://github.com/gkopff/gson-javatime-serialisers

do something like this JodaOrg/joda-time#294 (comment)

The suggestion above is from a quick google search, I did not test it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants