mirror of
https://codeberg.org/PGPainless/sop-java.git
synced 2025-09-07 09:26:23 +02:00
Introduce sop-java-json-gson module
This commit is contained in:
parent
e680f3450a
commit
cc08b76b68
6 changed files with 178 additions and 1 deletions
|
@ -7,5 +7,6 @@ rootProject.name = 'SOP-Java'
|
|||
include 'sop-java',
|
||||
'sop-java-picocli',
|
||||
'sop-java-testfixtures',
|
||||
'external-sop'
|
||||
'external-sop',
|
||||
'sop-java-json-gson'
|
||||
|
||||
|
|
13
sop-java-json-gson/README.md
Normal file
13
sop-java-json-gson/README.md
Normal file
|
@ -0,0 +1,13 @@
|
|||
<!--
|
||||
SPDX-FileCopyrightText: 2025 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
-->
|
||||
|
||||
# SOP-Java-JSON-GSON
|
||||
|
||||
## JSON Parsing VERIFICATION extension JSON using Gson
|
||||
|
||||
Since revision 11, the SOP specification defines VERIFICATIONS extension JSON.
|
||||
|
||||
This module implements the `JSONParser` and `JSONSerializer` interfaces using Googles Gson library.
|
28
sop-java-json-gson/build.gradle
Normal file
28
sop-java-json-gson/build.gradle
Normal file
|
@ -0,0 +1,28 @@
|
|||
// SPDX-FileCopyrightText: 2025 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
plugins {
|
||||
id 'java-library'
|
||||
}
|
||||
|
||||
group 'org.pgpainless'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"
|
||||
implementation "org.junit.jupiter:junit-jupiter-params:$junitVersion"
|
||||
runtimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
|
||||
|
||||
implementation project(":sop-java")
|
||||
api "org.slf4j:slf4j-api:$slf4jVersion"
|
||||
testImplementation "ch.qos.logback:logback-classic:$logbackVersion"
|
||||
|
||||
// @Nonnull, @Nullable...
|
||||
implementation "com.google.code.findbugs:jsr305:$jsrVersion"
|
||||
|
||||
api "com.google.code.gson:gson:$gsonVersion"
|
||||
}
|
23
sop-java-json-gson/src/main/kotlin/sop/GsonParser.kt
Normal file
23
sop-java-json-gson/src/main/kotlin/sop/GsonParser.kt
Normal file
|
@ -0,0 +1,23 @@
|
|||
// SPDX-FileCopyrightText: 2025 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package sop
|
||||
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.JsonSyntaxException
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import java.text.ParseException
|
||||
|
||||
class GsonParser(
|
||||
private val gson: Gson = Gson()
|
||||
) : Verification.JSONParser {
|
||||
|
||||
override fun parse(string: String): Verification.JSON {
|
||||
try {
|
||||
return gson.fromJson(string, object : TypeToken<Verification.JSON>(){}.type)
|
||||
} catch (e: JsonSyntaxException) {
|
||||
throw ParseException(e.message, 0)
|
||||
}
|
||||
}
|
||||
}
|
16
sop-java-json-gson/src/main/kotlin/sop/GsonSerializer.kt
Normal file
16
sop-java-json-gson/src/main/kotlin/sop/GsonSerializer.kt
Normal file
|
@ -0,0 +1,16 @@
|
|||
// SPDX-FileCopyrightText: 2025 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package sop
|
||||
|
||||
import com.google.gson.Gson
|
||||
|
||||
class GsonSerializer(
|
||||
private val gson: Gson = Gson()
|
||||
) : Verification.JSONSerializer {
|
||||
|
||||
override fun serialize(json: Verification.JSON): String {
|
||||
return gson.toJson(json)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,96 @@
|
|||
// SPDX-FileCopyrightText: 2025 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package sop
|
||||
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.assertThrows
|
||||
import java.text.ParseException
|
||||
|
||||
class GsonSerializerAndParserTest {
|
||||
|
||||
private val serializer: GsonSerializer = GsonSerializer()
|
||||
private val parser: GsonParser = GsonParser()
|
||||
|
||||
@Test
|
||||
fun simpleSingleTest() {
|
||||
val before = Verification.JSON("/tmp/alice.pgp")
|
||||
|
||||
val json = serializer.serialize(before)
|
||||
assertEquals("{\"signers\":[\"/tmp/alice.pgp\"]}", json)
|
||||
|
||||
val after = parser.parse(json)
|
||||
|
||||
assertEquals(before, after)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun simpleListTest() {
|
||||
val before = Verification.JSON(listOf("/tmp/alice.pgp", "/tmp/bob.asc"))
|
||||
|
||||
val json = serializer.serialize(before)
|
||||
assertEquals("{\"signers\":[\"/tmp/alice.pgp\",\"/tmp/bob.asc\"]}", json)
|
||||
|
||||
val after = parser.parse(json)
|
||||
|
||||
assertEquals(before, after)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun withCommentTest() {
|
||||
val before = Verification.JSON(
|
||||
listOf("/tmp/alice.pgp"),
|
||||
"This is a comment.",
|
||||
null)
|
||||
|
||||
val json = serializer.serialize(before)
|
||||
assertEquals("{\"signers\":[\"/tmp/alice.pgp\"],\"comment\":\"This is a comment.\"}", json)
|
||||
|
||||
val after = parser.parse(json)
|
||||
|
||||
assertEquals(before, after)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun withExtStringTest() {
|
||||
val before = Verification.JSON(
|
||||
listOf("/tmp/alice.pgp"),
|
||||
"This is a comment.",
|
||||
"This is an ext object string.")
|
||||
|
||||
val json = serializer.serialize(before)
|
||||
assertEquals("{\"signers\":[\"/tmp/alice.pgp\"],\"comment\":\"This is a comment.\",\"ext\":\"This is an ext object string.\"}", json)
|
||||
|
||||
val after = parser.parse(json)
|
||||
|
||||
assertEquals(before, after)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun withExtListTest() {
|
||||
val before = Verification.JSON(
|
||||
listOf("/tmp/alice.pgp"),
|
||||
"This is a comment.",
|
||||
listOf(1.0,2.0,3.0))
|
||||
|
||||
val json = serializer.serialize(before)
|
||||
assertEquals("{\"signers\":[\"/tmp/alice.pgp\"],\"comment\":\"This is a comment.\",\"ext\":[1.0,2.0,3.0]}", json)
|
||||
|
||||
val after = parser.parse(json)
|
||||
|
||||
assertEquals(before, after)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parseInvalidJSON() {
|
||||
assertThrows<ParseException> { parser.parse("Invalid") }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parseMalformedJSON() {
|
||||
// Missing '}'
|
||||
assertThrows<ParseException> { parser.parse("{\"signers\":[\"Alice\"]") }
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue