Skip to content

Commit 5e51a5c

Browse files
authored
Merge pull request #259 from theapache64/master
Added library version
2 parents 4c3d3e4 + 84bf471 commit 5e51a5c

File tree

204 files changed

+1830
-29
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

204 files changed

+1830
-29
lines changed

.gitignore

100644100755
File mode changed.

.travis.yml

100644100755
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
language: java
2+
before_install:
3+
- chmod +x library/gradlew
4+
- chmod +x library/gradle/wrapper/gradle-wrapper.jar
5+
script:
6+
- cd library && ./gradlew build test
27
deploy:
38
- provider: releases
49
api_key:

ChangeLog.md

100644100755
File mode changed.

LIBRARY.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# JsonToKoltinBuilder - Java/Kotlin Library
2+
3+
We also have a `Java/Kotlin` library. With this you can convert `JSON` to `Kotlin` code programmatically.
4+
5+
## Installation
6+
7+
Download `jar` file from [here](library/output/JsonToKotlinClassLibrary-v3.5.1.jar) and add it to your project
8+
9+
## Usage
10+
11+
```kotlin
12+
val actualOutput = JsonToKotlinBuilder()
13+
.setPackageName("com.my.package.name")
14+
.enableVarProperties(false) // optional, default : false
15+
.setPropertyTypeStrategy(PropertyTypeStrategy.AutoDeterMineNullableOrNot) // optional, default : PropertyTypeStrategy.NotNullable
16+
.setDefaultValueStrategy(DefaultValueStrategy.AvoidNull) // optional, default : DefaultValueStrategy.AvoidNull
17+
.setAnnotationLib(TargetJsonConverter.MoshiCodeGen) // optional, default: TargetJsonConverter.None
18+
.enableComments(true) // optional, default : false
19+
.enableOrderByAlphabetic(true) // optional : default : false
20+
.enableInnerClassModel(true) // optional, default : false
21+
.enabelMapType(true)// optional, default : false
22+
.enableCreateAnnotationOnlyWhenNeeded(true) // optional, default : false
23+
.setIndent(4)// optional, default : 4
24+
.setParentClassTemplate("android.os.Parcelable") // optional, default : ""
25+
.enableKeepAnnotationOnClass(true) // optional, default : false
26+
.enableKeepAnnotationOnClassAndroidX(true) // optional, default : false
27+
.enableAnnotationAndPropertyInSameLine(true) // optional, default : false
28+
.enableParcelableSupport(true) // optional, default : false
29+
.setPropertyPrefix("MyPrefix") // optional, default : ""
30+
.setPropertySuffix("MySuffix") // optional, default : ""
31+
.setClassSuffix("MyClassSuffix")// optional, default : ""
32+
.enableForceInitDefaultValueWithOriginJsonValue(true) // optional, default : false
33+
.enableForcePrimitiveTypeNonNullable(true) // optional, default : false
34+
.build(json1, "GlossResponse") // finally, get KotlinClassCode string
35+
```
36+
37+
## Demo
38+
39+
**Code**
40+
41+
```kotlin
42+
43+
fun main() {
44+
45+
46+
val json1 = """{ "programmers": [
47+
{ "isFirstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
48+
{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },
49+
{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
50+
],
51+
"authors": [
52+
{ "firstName": null, "lastName": "Asimov", "genre": "science fiction" },
53+
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
54+
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
55+
],
56+
"musicians": [
57+
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
58+
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
59+
] } """
60+
61+
val output = JsonToKotlinBuilder()
62+
.build(json1, "GlossResponse")
63+
64+
println(output)
65+
}
66+
```
67+
68+
**Output**
69+
70+
```kotlin
71+
data class GlossResponse(
72+
val programmers: List<Programmer>,
73+
val authors: List<Author>,
74+
val musicians: List<Musician>
75+
)
76+
77+
data class Programmer(
78+
val isFirstName: String,
79+
val lastName: String,
80+
val email: String,
81+
val firstName: String
82+
)
83+
84+
data class Author(
85+
val lastName: String,
86+
val genre: String,
87+
val firstName: String?
88+
)
89+
90+
data class Musician(
91+
val firstName: String,
92+
val lastName: String,
93+
val instrument: String
94+
)
95+
```
96+
97+
## Custom Annotation
98+
99+
To set custom annotation, you can call the `JsonToKotlinBuilder#setCustomAnnotation` method.
100+
101+
```kotlin
102+
val output = JsonToKotlinBuilder()
103+
.setCustomAnnotation(
104+
"import kotlinx.serialization.SerialName\n" +
105+
"import kotlinx.serialization.Serializable" + "\n" + "import kotlinx.serialization.Optional",
106+
"@Serializable",
107+
"@Optional\n@SerialName(\"%s\")"
108+
)
109+
.build(input, "MyResponse")
110+
```

LICENSE

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,4 +671,4 @@ into proprietary programs. If your program is a subroutine library, you
671671
may consider it more useful to permit linking proprietary applications with
672672
the library. If this is what you want to do, use the GNU Lesser General
673673
Public License instead of this License. But first, please read
674-
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
674+
<http://www.gnu.org/philosophy/why-not-lgpl.html>.

README.md

100644100755
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
# JsonToKotlinClass
1515

16-
Hi, Welcome! This is a plugin to generate Kotlin `data class` from JSON string, in another word, a plugin that converts JSON string to Kotlin `data class` (Json to Kotlin)
16+
Hi, Welcome! This is a plugin to generate Kotlin `data class` from JSON string, in another word, a plugin that converts JSON string to Kotlin `data class` (Json to Kotlin).
17+
We also have a `Java/Kotlin` [library](LIBRARY.md). With this you can generate Kotlin `data class` from JSON string **programmatically**.
18+
1719

1820
### Overview
1921

_config.yml

100644100755
File mode changed.

build.gradle

100644100755
File mode changed.

doc_for_extensions.md

100644100755
File mode changed.

doc_for_json_sources.md

100644100755
File mode changed.

0 commit comments

Comments
 (0)