Created
September 29, 2019 04:31
-
-
Save kazusato/d8176d301d13886734e5d0eb0baeec0d to your computer and use it in GitHub Desktop.
Utility to generate array expression using Java API for JSON Processing
This file contains hidden or 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
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' | |
} |
This file contains hidden or 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
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() | |
} | |
} |
This file contains hidden or 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
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