Comments (5)
Progress?
from kson.
@dri94 WIP
from kson.
when doing it, will the ability for values in json that come down as null be mapped to default values too?
from kson.
Hello, @aafanasev
Can we provide an annotation, suppose it is called @Default
, and the function annotated by it can provide the default value of this data class?
For example, for this data class:
data class Foo(
@SerializedName("str")
val stringValue: String,
@SerializedName("integer")
val intValue: Int
) {
companion object {
@Default
fun provideDefault(): Foo = Foo("string value", 233)
}
}
or data class with default value:
data class Bar(
@SerializedName("str")
val stringValue: String = "default",
@SerializedName("integer") = 233
val intValue: Int
) {
companion object {
@Default
fun provideDefault(): Bar {
return Bar()
}
}
}
And then, the generated class's deserialize function will look like this:
override fun read(reader: JsonReader): Foo? {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull()
return null
}
// Foo.provideDefault() is annotated by @Default
val default = Foo.provideDefault()
var stringValue: kotlin.String? = null
var intValue: kotlin.Int? = null
reader.beginObject()
while (reader.hasNext()) {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull()
continue
}
when (reader.nextName()) {
"str" -> stringValue = string_adapter.read(reader)
"integer" -> intValue = int_adapter.read(reader)
else -> reader.skipValue()
}
}
reader.endObject()
return Foo(
stringValue = stringValue ?: default.stringValue,
intValue = intValue ?: default.intValue
)
}
From the point of caller, this way just add an optional annotation, I think it just ok.
from kson.
@zkw012300 awesome! I'll prepare a POC of your solution.
from kson.
Related Issues (8)
- Support @JsonAdapter annotation
- Customize generated package name
- Unresolved reference @Generated in generated class HOT 1
- Don't use synchronized delegates
- Support gson RuntimeTypeAdapterFactory generation HOT 2
- Generate adapters based on type, not property
- Support incremental annotation processing HOT 4
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 kson.