Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9113f0a

Browse files
use inputstream for input
1 parent 6cd40d0 commit 9113f0a

File tree

6 files changed

+21
-12
lines changed

6 files changed

+21
-12
lines changed

‎core/src/main/kotlin/com/fractalwrench/json2kotlin/JsonReader.kt‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package com.fractalwrench.json2kotlin
33
import com.google.gson.JsonArray
44
import com.google.gson.JsonObject
55
import com.google.gson.JsonParser
6+
import java.io.BufferedReader
7+
import java.io.InputStream
8+
import java.io.InputStreamReader
69

710
/**
811
* Reads and serialises a JSON string to a JSONElement, using GSON.
@@ -14,8 +17,10 @@ class JsonReader(private val jsonParser: JsonParser) {
1417
/**
1518
* Reads a JSON string using GSON.
1619
*/
17-
internal fun readJsonTree(input: String, args: ConversionArgs): JsonObject {
18-
with(jsonParser.parse(input)) {
20+
internal fun readJsonTree(input: InputStream, args: ConversionArgs): JsonObject {
21+
// TODO check IO is closed properly everywhere!
22+
23+
with(jsonParser.parse(BufferedReader(InputStreamReader(input)))) {
1924
return when {
2025
isJsonObject -> asJsonObject
2126
isJsonArray -> addRootWrapper(asJsonArray, args.rootClassName)

‎core/src/main/kotlin/com/fractalwrench/json2kotlin/Kotlin2JsonConverter.kt‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.fractalwrench.json2kotlin
22

33
import com.google.gson.*
4+
import java.io.InputStream
45
import java.io.OutputStream
56

67
/**
@@ -17,12 +18,8 @@ class Kotlin2JsonConverter(val buildDelegate: SourceBuildDelegate = GsonBuildDel
1718
/**
1819
* Converts a JSON string to Kotlin, writing it to the OutputStream.
1920
*/
20-
fun convert(input: String, output: OutputStream, args: ConversionArgs) {// FIXME should take an InputStream as input
21+
fun convert(input: InputStream, output: OutputStream, args: ConversionArgs) {
2122
try {
22-
if (input.isEmpty()) {
23-
throw IllegalArgumentException("Json input empty")
24-
}
25-
2623
val jsonRoot = jsonReader.readJsonTree(input, args)
2724
val stack = traverser.traverse(jsonRoot, args.rootClassName)
2825
val typeHolder = ClassTypeHolder(buildDelegate, ::defaultGroupingStrategy)

‎core/src/test/kotlin/com/fractalwrench/json2kotlin/ConverterArgsTest.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ConverterArgsTest {
1717
fun testPackageName() {
1818
val jsonFilename = "args/Package.json"
1919
val expectedFilename = "args/PackageExample.kt"
20-
val json = fileReader.readContents(jsonFilename)
20+
val json = fileReader.inputStream(jsonFilename)
2121

2222
val outputStream = ByteArrayOutputStream()
2323
jsonConverter.convert(json, outputStream, ConversionArgs("PackageExample", "com.fractalwrench.foo"))

‎core/src/test/kotlin/com/fractalwrench/json2kotlin/InvalidJsonConverterTest.kt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class InvalidJsonConverterTest(val jsonFilename: String) {
2525
/**
2626
* Takes a JSON file and converts it into the equivalent Kotlin class, then compares to expected output.
2727
*/
28-
@Test(expected = IllegalArgumentException::class)
28+
@Test(expected = RuntimeException::class)
2929
fun testJsonToKotlinConversion() {
30-
val json = fileReader.readContents(jsonFilename)
30+
val json = fileReader.inputStream(jsonFilename)
3131
val outputStream = ByteArrayOutputStream()
3232
val args = ConversionArgs(jsonFilename.replace(".kt", ""))
3333
jsonConverter.convert(json, outputStream, args)

‎core/src/test/kotlin/com/fractalwrench/json2kotlin/JsonConverterTest.kt‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@ import org.junit.runner.RunWith
88
import org.junit.runners.Parameterized
99
import java.io.ByteArrayOutputStream
1010
import java.io.File
11+
import java.io.InputStream
1112
import java.util.*
1213

1314
@RunWith(Parameterized::class)
1415
open class JsonConverterTest(val expectedFilename: String, val jsonFilename: String) {
1516

1617
private val fileReader = ResourceFileReader()
1718
private val jsonConverter = Kotlin2JsonConverter()
18-
internal lateinit var json: String
19+
internal lateinit var json: InputStream
1920

2021
@Before
2122
fun setUp() {
22-
json = fileReader.readContents(jsonFilename)
23+
json = fileReader.inputStream(jsonFilename)
2324
}
2425

2526
companion object {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package com.fractalwrench.json2kotlin
22

3+
import java.io.InputStream
4+
35
internal class ResourceFileReader {
46
fun readContents(resourceName: String): String {
57
val resource = ResourceFileReader::class.java.classLoader.getResource(resourceName)
68
return resource?.readText() ?: ""
79
}
10+
11+
fun inputStream(resourceName: String): InputStream {
12+
return ResourceFileReader::class.java.classLoader.getResourceAsStream(resourceName)
13+
}
814
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /