Skip to content

Commit

Permalink
android-bindings: added a workaround to coerce JSON names to non-strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Miha-x64 committed Dec 5, 2019
1 parent 64daca9 commit a4866bb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fun TokenStream.writeTo(writer: JsonWriter): Unit =
val tokToJson = enumMapOf(
Token.Null, JsonToken.NULL,
Token.Bool, JsonToken.BOOLEAN,
/*Token.I8, Token.I16, Token.I32, Token.I64, Token.F32, Token.F64, Token.Str, Token.Blob,*/
// Token.I8, Token.I16, Token.I32, Token.I64, Token.F32, Token.F64, Token.Str, Token.Blob,
Token.BeginSequence, JsonToken.BEGIN_ARRAY,
Token.EndSequence, JsonToken.END_ARRAY,
Token.BeginDictionary, JsonToken.BEGIN_OBJECT,
Expand Down Expand Up @@ -118,7 +118,14 @@ fun TokenStream.writeTo(writer: JsonWriter): Unit =
return Token.EndDictionary
}

val nextTok = if (coerceTo == null) nextToken else tokToJson[coerceTo]
val nextTok = if (coerceTo == null) nextToken else {
if (nextToken == JsonToken.NAME && coerceTo != Token.Str)
// JsonReader can't coerce names to other types, let's do it ourselves
return coerceTo.coerce(reader.nextName().also(_path::onName))

tokToJson[coerceTo]
}

return if (nextTok == null) {
val value = when (coerceTo!!) {
Token.I8 -> reader.nextInt().assertFitsByte()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import net.aquadc.persistence.extended.partial
import net.aquadc.persistence.struct.Schema
import net.aquadc.persistence.struct.Struct
import net.aquadc.persistence.struct.build
import net.aquadc.persistence.tokens.Token
import net.aquadc.persistence.tokens.readAs
import net.aquadc.persistence.tokens.readListOf
import net.aquadc.persistence.tokens.tokens
Expand All @@ -31,11 +32,13 @@ import net.aquadc.persistence.type.set
import net.aquadc.persistence.type.string
import net.aquadc.properties.persistence.enum
import okio.ByteString.Companion.decodeHex
import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotSame
import org.junit.Assert.assertSame
import org.junit.Test
import java.io.StringWriter
import java.util.Base64
import java.util.EnumSet


Expand Down Expand Up @@ -153,8 +156,8 @@ class PersistenceTest {
)
}

val smallSchema = Tuple3("a", string, "b", string, "c", string)
val partialSmallSchema = partial(smallSchema)
private val smallSchema = Tuple3("a", string, "b", string, "c", string)
private val partialSmallSchema = partial(smallSchema)

@Test fun `json empty partial`() {
assertEquals(
Expand Down Expand Up @@ -238,6 +241,27 @@ class PersistenceTest {
)
}

@Test fun coercions() {
val blob = byteArrayOf(1, 2, 3)
val base = Base64.getEncoder().encodeToString(blob)
val tokens = """{"1":"456.789","$base":"123"}""".reader().json().tokens()

tokens.poll(Token.BeginDictionary)

assertEquals(Token.Str, tokens.peek())
assertEquals(1, tokens.poll(Token.I32))

assertEquals(Token.Str, tokens.peek())
assertEquals(456.789, tokens.poll(Token.F64))

assertEquals(Token.Str, tokens.peek())
assertArrayEquals(blob, tokens.poll(Token.Blob) as ByteArray)

assertEquals(Token.Str, tokens.peek())
assertEquals(123, tokens.poll(Token.I32))

}

private fun <T> read(json: String, type: DataType<T>, lenient: Boolean = false): T =
json.reader().json().also {
it.isLenient = lenient
Expand Down

0 comments on commit a4866bb

Please sign in to comment.