Comments (14)
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.
from threetenabp.
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.
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.
from threetenabp.
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.
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.
from threetenabp.
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.
Oh, that makes perfect sense!
And yes, adapter examples would be most welcome :D
from threetenabp.
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.
from threetenabp.
Sure, I will make it later today.
from threetenabp.
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)
- Add Usage info HOT 1
- DateTimeParseException when parse HOT 4
- Update tzdb.dat to new version. HOT 3
- Duplicate ThreeTen Classes in AndroidTest HOT 2
- Calculation problem HOT 4
- IllegalStateException: Already initialized HOT 14
- minusDays() returns wrong date for specific use case HOT 2
- R8 issue with Ser HOT 14
- Contribution: Gson.toJson with @Expose annotations error HOT 1
- Will we still need this library after java.time will be desugared? HOT 6
- Do I actually want to switch to desugared native apis? HOT 7
- Wrong Format OffsetDateTime HOT 1
- Bug in DateTimeFormatter - Wrong year. HOT 1
- Help for specific pattern HOT 1
- Support Compose Preview HOT 2
- Wrong time zone offset HOT 1
- NoClassDefFoundError in android 4.4 even with multidex enabled HOT 2
- Returns the Value to 1926 HOT 3
- Please *do not* consider switching to core library desugaring HOT 2
- Fatal Exception: n30.g Unknown time-zone ID: America/Ciudad_Juarez HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from threetenabp.