Comments (5)
This seems to be a limitation of Moshi:
https://github.com/square/moshi/blob/03323ae998f597f3cf94e1e66a2bbeecc2256054/moshi/src/main/java/com/squareup/moshi/Types.java#L182
from kotshi.
Given some JSON:
{
"id":"some-id",
"name":"John Doe",
"status": {
"code":"verified"
},
"some_field":{
"name":"a name"
}
}
I want to achieve something like
@JsonQualifier
annotation class WrappedInObject(val name: String)
class WrappedInObjectAdapter(private val name: String) : JsonAdapter<String>() {
override fun fromJson(reader: JsonReader): String? {
reader.beginObject()
var value: String? = null
while (reader.hasNext()) {
when (reader.nextName()) {
name -> value = reader.nextString()
else -> reader.skipValue()
}
}
reader.endObject()
return value
}
override fun toJson(writer: JsonWriter, value: String?) {
writer.beginObject()
writer.name(name)
if (value == null) {
writer.nullValue()
} else {
writer.value(value)
}
writer.endObject()
}
class Factory : JsonAdapter.Factory {
override fun create(type: Type, annotations: Set<Annotation>, moshi: Moshi): JsonAdapter<*>? {
val annotation = annotations.filterIsInstance<WrappedInObject>().firstOrNull()
if(annotation != null) {
return WrappedInObjectAdapter(annotation.name)
}
return null
}
}
}
data class Foo(
val id: String,
val name: String,
@WrappedInObject(name = "code")
@Json(name = "status")
val statusCode: String,
@WrappedInObject(name = "name")
@Json(name = "some_field")
val someFieldName: String
)
Kotshi needs to pass annotation received from JsonAdapter.Factory.create
method into moshi.adapter
moshi.adapter(Type type, Set<? extends Annotation> annotations)
instead of
moshi.adapter(Type type, Class<? extends Annotation> annotationType)
Maybe it's difficult to achieve with code generation because we need the instance of WrappedInObject
when instantiating KotshiFooJsonAdapter but it isn't available yet, please close this issue if it's something Kotshi can't support
from kotshi.
Well, we could always use our own implementation but I’m sure there is a reason why Moshi doesn’t allow it to define methods.
Just one thing to try, try to put @JvmField
on the property. It’s a long shot but it might work.
from kotshi.
This is supported by Moshi. (See my 2 example PRs from last night.) It's easier to look at the annotations reflectively.
I would like try implementing this in Kotshi tonight. I'm not sure how it's going to work, so I'm hoping to learn something new.
from kotshi.
I use @JvmField
, but I think its not the case
With kotshi I need different qualifiers for different names
Something like this will work with kotshi
@JsonQualifer
@NameWrappedInObject
@JsonQualifer
@CodeWrappedInObject
class Factory : JsonAdapter.Factory {
override fun create(type: Type, annotations: Set<Annotation>, moshi: Moshi): JsonAdapter<*> {
if(annotations.any { it.annotationClass == NameWrappedInObject::class }) {
val annotation = annotations.filterIsInstance<NameWrappedInObject>().first()
return WrappedInObjectAdapter("name")
}
if(annotations.any { it.annotationClass == CodeWrappedInObject::class }) {
val annotation = annotations.filterIsInstance<CodeWrappedInObject>().first()
return WrappedInObjectAdapter("code")
}
return null
}
}
from kotshi.
Related Issues (20)
- invalid code in xxxJsonAdapter generated with @JsonSerializable HOT 1
- Unable to build project with Jetifier enabled (2.0.2) HOT 1
- Investigate support for inline classes HOT 1
- all of the objects have one companion object in ram ?!!?! HOT 3
- Migrate to KSP
- Implementation documentation or sample app? HOT 5
- Kotlin 1.5 borked HOT 3
- Generated annotation not found when compiling against Java 11 HOT 10
- Alternative annotation for custom names HOT 9
- v2.9.0: Excess code generated in adapters (Kapt) HOT 4
- Possible race condition in KSP factory generation HOT 12
- 2.10.0 KSP generates non-compilable classes when nullable field with same name as class and default value HOT 4
- 2.10.1 KSP generated adapters have trouble with generic typealiases when there is a default value HOT 3
- Serialisation issue is sallowed HOT 2
- Unresolved reference: KotshiAppJsonAdapterFactory HOT 2
- KotshiApplicationJsonAdapterFactory does not have a companion object HOT 6
- Usage with JVM 11 HOT 3
- Backend Internal error HOT 5
- [Intermittent] No JsonAdapter is getting created for random response classes after migrating to ksp from kapt HOT 4
- KSP: Failed to analyze class HOT 5
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 kotshi.