slackhq / moshi-gson-interop Goto Github PK
View Code? Open in Web Editor NEWAn interop tool for safely mixing Moshi and Gson models in JSON serialization.
License: Apache License 2.0
An interop tool for safely mixing Moshi and Gson models in JSON serialization.
License: Apache License 2.0
Similar to Moshi's @JsonClass
checker, but for GSON
/** Checks for classes annotated with GSON's [JsonAdapter] annotation. */
private class JsonAdapterAnnotationChecker : ClassChecker {
override fun serializerFor(rawType: Class<*>): Serializer? {
if (rawType.isAnnotationPresent(JsonAdapter::class.java)) {
return Serializer.GSON
}
return null
}
}
This would allow for some similar logic to how gson and moshi builders are done.
// Simple case
moshi.interopWith(gson)
// Advanced case
moshi.interopBuilder(gson)
.addMoshiType(TypeA::class.java) // with reified extensions
.addGsonType(TypeB::class.java)
.addMoshiFactory(TypeAFactory::class.java) // take a moshi adapter factory. If it returns non-null, it's a moshi type
.addGsonFactory(TypeBFactory::class.java)
.addClassChecker(...)
Then in a conventional setup where someone is maybe wiring a multibinding set of adapters, they can easily add the same types
Another idea is to take JsonAdapter/TypeAdapter methods directly and resolve their generics with reflection to indicate their type?
We're not 1.0 yet but we might be some day! We can integrate it now but allow breaking changes until 1.0 obviously.
I think it should be fine to allow later newBuilder()'d instances because they would just further delegate to any adapters added later, but we should test this.
Users should use Moshi.Builder.addLast
instead
These are a little too expensive to check in the default one, but we could add one for those that need it and allow them to compose it with the default checks
When the incoming GSON JsonWriter
is JsonTreeWriter
, jsonValue()
APIs will no longer work because GSON doesn't support writing raw JSON in this environment. Not sure if this is something we really can support, vs not allowing Moshi serializers to be preferred when we're in this condition. Not sure we can infer this ahead of time though.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.