Skip to content

Instantly share code, notes, and snippets.

@kazusato
Created September 29, 2019 04:31
Show Gist options
  • Save kazusato/d8176d301d13886734e5d0eb0baeec0d to your computer and use it in GitHub Desktop.
Save kazusato/d8176d301d13886734e5d0eb0baeec0d to your computer and use it in GitHub Desktop.
Utility to generate array expression using Java API for JSON Processing
dependencies {
implementation "org.glassfish:javax.json:1.1.4"
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.5.2'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.5.2'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.5.2'
testCompile group: 'org.assertj', name: 'assertj-core', version: '3.13.2'
}
package kazusato.util
import javax.json.Json
object JsonUtil {
fun toJsonArray(stringList: List<String>): String {
val arrayBuilder = Json.createArrayBuilder()
stringList.forEach { str ->
arrayBuilder.add(str)
}
val arrayObj = arrayBuilder.build()
return arrayObj.toString()
}
}
package kazusato.util
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
class JsonUtilTest {
@Test
fun testToJsonArray() {
val list = listOf("AAA", "BBB", "CCC")
val result = JsonUtil.toJsonArray(list)
assertThat(result).isEqualTo("""["AAA","BBB","CCC"]""")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment