Giter Site home page Giter Site logo

Comments (14)

JakeWharton avatar JakeWharton commented on July 22, 2024 5
public @Rfc1123 OffsetDateTime createdAt;
public final class ModelAdapters {
  @FromJson //
  public @Rfc1123 OffsetDateTime rfc1123OffsetDateTimeFromJson(String value) {
    return OffsetDateTime.parse(value, RFC_1123_DATE_TIME);
  }

  @ToJson //
  public String rfc1123OffsetDateTimeToJson(@Rfc1123 OffsetDateTime value) {
    return RFC_1123_DATE_TIME.format(value);
  }
}
Moshi moshi = new Moshi.Builder() //
    .add(new ModelAdapters()) //
    .build();

// ...

.addConverterFactory(MoshiConverterFactory.create(moshi)) //

from threetenabp.

JakeWharton avatar JakeWharton commented on July 22, 2024 1

from threetenabp.

JakeWharton avatar JakeWharton commented on July 22, 2024

Looks like https://github.com/aaronhe42/ThreeTen-Backport-Gson-Adapter exists.

Regardless, though, this is a request for ThreeTenBP and not this library which merely repackages its timezone database.

from threetenabp.

feinstein avatar feinstein commented on July 22, 2024

I found that adapter, but I thought that maybe since you are one of the developers of Retrofit and this library, maybe you would want to make something streamlined for Retrofit, but I will use that adapter anyways, thank you Jake.

from threetenabp.

JakeWharton avatar JakeWharton commented on July 22, 2024

from threetenabp.

feinstein avatar feinstein commented on July 22, 2024

I know, but since it's so easy to add a Converter on Retrofit, I was hoping there could be an easy plug and play solution for this as well, like a wrapper around the GsonConverterFactory, or something similar.... Not that registering an adapter with GSON is a great deal of work, just not as simple as it could be.

from threetenabp.

feinstein avatar feinstein commented on July 22, 2024

FYI: The https://github.com/aaronhe42/ThreeTen-Backport-Gson-Adapter doesn't work.

It calls GSON JsonElement.getAsString() which is basically an UnsupportedOperationException.

So now I will have to write my own adapter :/

from threetenabp.

JakeWharton avatar JakeWharton commented on July 22, 2024

from threetenabp.

feinstein avatar feinstein commented on July 22, 2024

When I was searching for JSON solutions I decided to not use Moshi because it appeared to be 2x slower than GSON, at least from this benchmark: https://medium.com/@IlyaEremin/android-json-parsers-comparison-2017-8b5221721e31

Maybe the benchmark was flawed?

I will try Moshi then, if it fixes my issue I can cope with the slower performance. Thank you for the recommendation Jake!

from threetenabp.

feinstein avatar feinstein commented on July 22, 2024

Oh, that makes perfect sense!

And yes, adapter examples would be most welcome :D

from threetenabp.

feinstein avatar feinstein commented on July 22, 2024

Thanks Jake.

I already have it working but I made it differently. I used Jackson's Java time module on my server, so it will convert automatically a ZonedDateTime to a timestamp, like an Instant. Then I made a Moshi adapter to convert that timestamp back to a ZonedDateTime.

The only caveat though is that the timestamp is sent as a double with nanoseconds as the decimals. When Moshi grabs this double with will crash on Parsing, since it's a big double and somehow appears a scientific notation in the string behind the covers, so it won't convert it to a string automatically either, so I had to use a JsonReader as my Adapter's input, in order to get the timestamp double as a string.

It would be nice to have the Moshi adapters to automatically pass numeric values as strings if the adapter has a string parameter, like the JsonReader does.

from threetenabp.

JakeWharton avatar JakeWharton commented on July 22, 2024

from threetenabp.

feinstein avatar feinstein commented on July 22, 2024

Sure, I will make it later today.

from threetenabp.

feinstein avatar feinstein commented on July 22, 2024

I tried to reproduce the issue and roll back my code, but now it works, so I have no idea what was going on haha.

Even the @FromJson ZonedDateTime fromJson(String timestamp) Adapter that would crash saying timestamp isn't a String, but a double is now passing the original numeric value as a String so I can simplify my code and ditch the JsonReader apparently.

Thank you for your help @JakeWharton!

from threetenabp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.