Skip to content

Commit

Permalink
fixes #119
Browse files Browse the repository at this point in the history
  • Loading branch information
erosb committed Oct 30, 2024
1 parent 2fc9624 commit a3b836a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/main/kotlin/com/github/erosb/jsonsKema/JsonParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@ import java.math.BigInteger
import java.net.URI

private class SourceWalker(
input: InputStream,
private val documentSource: URI,
private val reader: Reader = BufferedReader(InputStreamReader(input)),
inputReader: Reader,
private val documentSource: URI
) {

private val reader = inputReader.let { if (it.markSupported()) it else BufferedReader(it) }

constructor(input: InputStream, documentSource: URI) : this(
BufferedReader(InputStreamReader(input)),
documentSource
)

init {
reader.markSupported()
}

private var lineNumber = 1
private var position = 1

Expand Down Expand Up @@ -107,6 +117,12 @@ class JsonParser {
this.documentSource = documentSource
}

@JvmOverloads
constructor(schemaJson: Reader, documentSource: URI = DEFAULT_BASE_URI) {
this.walker = SourceWalker(schemaJson, documentSource)
this.documentSource = documentSource
}

constructor(schemaInputStream: InputStream, documentSource: URI) {
this.walker = SourceWalker(schemaInputStream, documentSource)
this.documentSource = documentSource
Expand Down
21 changes: 21 additions & 0 deletions src/test/kotlin/com/github/erosb/jsonsKema/JsonParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import java.io.BufferedReader
import java.io.ByteArrayInputStream
import java.io.InputStreamReader
import java.math.BigDecimal
import java.math.BigInteger

Expand Down Expand Up @@ -39,6 +42,24 @@ class JsonParserTest {
assertEquals("Extraneous character found: n", exception.message)
}

@Test
fun `create from Reader`() {
val parser = JsonParser(
BufferedReader(
InputStreamReader(ByteArrayInputStream("true".toByteArray()))
)
)
assertEquals(JsonBoolean(true, SourceLocation(1, 1, JsonPointer())), parser.parse())
}

@Test
fun `create from non-buffered`() {
val parser = JsonParser(
InputStreamReader(ByteArrayInputStream(" true".toByteArray()))
)
assertEquals(JsonBoolean(true, SourceLocation(1, 3, JsonPointer())), parser.parse())
}

@Test
fun `null token mismatch`() {
val exception = assertThrows(JsonParseException::class.java) {
Expand Down

0 comments on commit a3b836a

Please sign in to comment.