mirror of
https://codeberg.org/PGPainless/sop-java.git
synced 2025-09-08 09:49:44 +02:00
Add support for JSON POJOs
This commit is contained in:
parent
bff4423f93
commit
ebfde35422
1 changed files with 78 additions and 0 deletions
|
@ -17,6 +17,7 @@ data class Verification(
|
||||||
val signatureMode: Optional<SignatureMode>,
|
val signatureMode: Optional<SignatureMode>,
|
||||||
val jsonOrDescription: Optional<String>
|
val jsonOrDescription: Optional<String>
|
||||||
) {
|
) {
|
||||||
|
|
||||||
@JvmOverloads
|
@JvmOverloads
|
||||||
constructor(
|
constructor(
|
||||||
creationTime: Date,
|
creationTime: Date,
|
||||||
|
@ -31,11 +32,44 @@ data class Verification(
|
||||||
Optional.ofNullable(signatureMode),
|
Optional.ofNullable(signatureMode),
|
||||||
Optional.ofNullable(description?.trim()))
|
Optional.ofNullable(description?.trim()))
|
||||||
|
|
||||||
|
@JvmOverloads
|
||||||
|
constructor(
|
||||||
|
creationTime: Date,
|
||||||
|
signingKeyFingerprint: String,
|
||||||
|
signingCertFingerprint: String,
|
||||||
|
signatureMode: SignatureMode? = null,
|
||||||
|
json: JSON,
|
||||||
|
jsonSerializer: JSONSerializer
|
||||||
|
) : this(
|
||||||
|
creationTime,
|
||||||
|
signingKeyFingerprint,
|
||||||
|
signingCertFingerprint,
|
||||||
|
Optional.ofNullable(signatureMode),
|
||||||
|
Optional.of(jsonSerializer.serialize(json)))
|
||||||
|
|
||||||
@Deprecated("Replaced by jsonOrDescription",
|
@Deprecated("Replaced by jsonOrDescription",
|
||||||
replaceWith = ReplaceWith("jsonOrDescription")
|
replaceWith = ReplaceWith("jsonOrDescription")
|
||||||
)
|
)
|
||||||
val description = jsonOrDescription
|
val description = jsonOrDescription
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempt to parse the [jsonOrDescription] field using the provided [JSONParser] and return the result.
|
||||||
|
* This method returns `null` if parsing fails.
|
||||||
|
*
|
||||||
|
* @param parser [JSONParser] implementation
|
||||||
|
* @return successfully parsed [JSON] POJO or `null`.
|
||||||
|
*/
|
||||||
|
fun getJson(parser: JSONParser): JSON? {
|
||||||
|
return jsonOrDescription.get()
|
||||||
|
?.let {
|
||||||
|
try {
|
||||||
|
parser.parse(it)
|
||||||
|
} catch (e: ParseException) {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun toString(): String =
|
override fun toString(): String =
|
||||||
"${UTCUtil.formatUTCDate(creationTime)} $signingKeyFingerprint $signingCertFingerprint" +
|
"${UTCUtil.formatUTCDate(creationTime)} $signingKeyFingerprint $signingCertFingerprint" +
|
||||||
(if (signatureMode.isPresent) " mode:${signatureMode.get()}" else "") +
|
(if (signatureMode.isPresent) " mode:${signatureMode.get()}" else "") +
|
||||||
|
@ -78,4 +112,48 @@ data class Verification(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* POJO data class representing JSON metadata.
|
||||||
|
*
|
||||||
|
* @param signers list of supplied CERTS objects that could have issued the signature, identified by
|
||||||
|
* the name given on the command line.
|
||||||
|
* @param comment a freeform UTF-8 encoded text describing the verification
|
||||||
|
* @param ext an extension object containing arbitrary, implementation-specific data
|
||||||
|
*/
|
||||||
|
data class JSON(
|
||||||
|
val signers: List<String>,
|
||||||
|
val comment: String?,
|
||||||
|
val ext: Any?)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface abstracting a JSON parser that parses [JSON] POJOs from single-line strings.
|
||||||
|
*/
|
||||||
|
fun interface JSONParser {
|
||||||
|
/**
|
||||||
|
* Parse a [JSON] POJO from the given single-line [string].
|
||||||
|
* If the string does not represent a JSON object matching the [JSON] definition,
|
||||||
|
* this method throws a [ParseException].
|
||||||
|
*
|
||||||
|
* @param string [String] representation of the [JSON] object.
|
||||||
|
* @return parsed [JSON] POJO
|
||||||
|
* @throws ParseException if the [string] is not a JSON string representing the [JSON] object.
|
||||||
|
*/
|
||||||
|
@Throws(ParseException::class)
|
||||||
|
fun parse(string: String): JSON
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface abstracting a JSON serializer that converts [JSON] POJOs into single-line JSON strings.
|
||||||
|
*/
|
||||||
|
fun interface JSONSerializer {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Serialize the given [JSON] object into a single-line JSON string.
|
||||||
|
*
|
||||||
|
* @param json JSON POJO
|
||||||
|
* @return JSON string
|
||||||
|
*/
|
||||||
|
fun serialize(json: JSON): String
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue