-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: yuanzicong <[email protected]>
- Loading branch information
yuanzicong
committed
May 20, 2021
1 parent
260e84a
commit c71b6c3
Showing
5 changed files
with
101 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
Enable this extension,you can make a kotlin data class with a static function to build from JSONObject. | ||
|
||
e.g. | ||
<br> | ||
json: | ||
```json | ||
{ | ||
"a":1, | ||
"b":"abc", | ||
"c":true | ||
} | ||
``` | ||
|
||
It can convert to the following kotlin data class | ||
|
||
```kotlin | ||
data class Test( | ||
val a: Int?, | ||
val b: String?, | ||
val c: Boolean? | ||
) { | ||
|
||
companion object { | ||
@JvmStatic | ||
fun buildFromJson(jsonObject: JSONObject?): Test? { | ||
|
||
jsonObject?.run { | ||
return Test( | ||
optInt("a"), | ||
optString("b"), | ||
optBoolean("c") | ||
) | ||
} | ||
return null | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Then you can use this function to make a instance from a JSONObjcet, for example | ||
```kotlin | ||
val jsonObject: JSONObject = TestApi.rquestTest() | ||
val test: Test = Test.buildFromJson(jsonObject) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
Enable this extension and type the classes' names into the input area, you can make the classes you typed non-nullable. | ||
|
||
In the text area, the classes should split with ','. | ||
|
||
Such as | ||
``` | ||
Int,Double,Boolean,Long,List | ||
``` | ||
In particular, the ``List<Any>`` only need to type ``List`` | ||
|
||
e.g. | ||
<br> | ||
json: | ||
```json | ||
{ | ||
"a": 1, | ||
"b": true, | ||
"c": "hhhh", | ||
"d": [ | ||
"a","b","c","d" | ||
], | ||
"e": [ | ||
1,2,3,4 | ||
] | ||
} | ||
``` | ||
|
||
It can convert to the following kotlin data class | ||
```kotlin | ||
data class Test( | ||
val a: Int, | ||
val b: Boolean, | ||
val c: String?, | ||
val d: List<String?>, | ||
val e: List<Int> | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters