-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use JSON Serialization to provide the value returned by the metaquery #24
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
219 changes: 219 additions & 0 deletions
219
src/main/kotlin/gov/nist/secauto/oscal/tools/server/core/writer/JsonItemWriter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
package gov.nist.secauto.metaschema.core.metapath.item | ||
import gov.nist.secauto.metaschema.databind.io.SerializationFeature; | ||
import gov.nist.secauto.metaschema.core.model.IBoundObject; | ||
import gov.nist.secauto.metaschema.core.metapath.ICollectionValue | ||
import gov.nist.secauto.metaschema.core.metapath.ISequence | ||
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem | ||
import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem | ||
import gov.nist.secauto.metaschema.core.metapath.item.function.IMapItem | ||
import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem | ||
import gov.nist.secauto.metaschema.databind.IBindingContext | ||
import gov.nist.secauto.metaschema.databind.io.Format | ||
import java.io.PrintWriter | ||
import java.io.StringWriter | ||
|
||
/** | ||
* Produces a JSON representation of a Metapath sequence. | ||
*/ | ||
class JsonItemWriter( | ||
private val writer: PrintWriter, | ||
private val bindingContext: IBindingContext | ||
) : IItemWriter { | ||
private var indentLevel = 0 | ||
private val visitor = Visitor() | ||
|
||
companion object { | ||
private const val INDENT = " " // 2 spaces for indentation | ||
} | ||
|
||
private fun writeIndent() { | ||
repeat(indentLevel) { | ||
writer.append(INDENT) | ||
} | ||
} | ||
|
||
private fun Any?.serializeValue(): String { | ||
return try { | ||
when (this) { | ||
null -> "null" | ||
is String -> "\"${escapeJson()}\"" | ||
is Number, is Boolean -> toString() | ||
else -> try { | ||
StringWriter().use { stringWriter -> | ||
val boundObject = this as? IBoundObject ?: run { | ||
// Add debug information before potential NPE | ||
println("Debug: toString() called on object of type: ${this?.javaClass}") | ||
return "\"${toString()?.escapeJson() ?: "null"}\"" | ||
} | ||
|
||
val boundClass = boundObject::class.java | ||
println("Debug: Processing bound class: ${boundClass.name}") | ||
|
||
val boundDefinition = bindingContext.getBoundDefinitionForClass(boundClass) | ||
println("Debug: Bound definition: ${boundDefinition != null}") | ||
if (boundDefinition != null) { | ||
val serializer = bindingContext.newSerializer(Format.JSON, boundClass) | ||
serializer.set(SerializationFeature.SERIALIZE_ROOT, false); | ||
serializer.serialize(boundObject, stringWriter) | ||
println(stringWriter.toString()); | ||
stringWriter.toString() | ||
} else { | ||
"\"${boundObject.toString()?.escapeJson() ?: "null"}\"" | ||
} | ||
} | ||
} catch (e: Exception) { | ||
StringWriter().use { sw -> | ||
PrintWriter(sw).use { pw -> | ||
e.printStackTrace(pw) | ||
println("Inner Exception Stack Trace:") | ||
println(sw.toString()) | ||
"\"Error during serialization: ${e.message}\nStack trace: ${sw.toString().escapeJson()}\"" | ||
} | ||
} | ||
} | ||
} | ||
} catch (e: Exception) { | ||
StringWriter().use { sw -> | ||
PrintWriter(sw).use { pw -> | ||
e.printStackTrace(pw) | ||
println("Outer Exception Stack Trace:") | ||
println(sw.toString()) | ||
"\"Error in main serializeValue: ${e.message}\nStack trace: ${sw.toString().escapeJson()}\"" | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun writeJsonObject( | ||
type: String, | ||
item: IItem, | ||
additionalContent: JsonItemWriter.() -> Unit = {} | ||
) { | ||
writer.append("{\n") | ||
indentLevel++ | ||
writeIndent() | ||
writer.append("\"type\": \"$type\",\n") | ||
writeIndent() | ||
writer.append("\"value\": ${item.getValue().serializeValue()},\n") | ||
additionalContent() | ||
indentLevel-- | ||
writeIndent() | ||
writer.append("}") | ||
} | ||
|
||
private fun writeJsonArray( | ||
name: String, | ||
content: JsonItemWriter.() -> Unit | ||
) { | ||
writeIndent() | ||
writer.append("\"$name\": [\n") | ||
indentLevel++ | ||
content() | ||
writer.append('\n') | ||
indentLevel-- | ||
writeIndent() | ||
writer.append("]\n") | ||
} | ||
|
||
override fun writeSequence(sequence: ISequence<*>) { | ||
writer.append("{\n") | ||
indentLevel++ | ||
writeIndent() | ||
writer.append("\"type\": \"sequence\",\n") | ||
writeJsonArray("items") { | ||
var first = true | ||
sequence.forEach { item -> | ||
if (!first) writer.append(",\n") | ||
writeIndent() | ||
item.accept(visitor) | ||
first = false | ||
} | ||
} | ||
indentLevel-- | ||
writeIndent() | ||
writer.append("}") | ||
} | ||
|
||
override fun writeArray(array: IArrayItem<*>) { | ||
writeJsonObject("array", array) { | ||
writeJsonArray("values") { | ||
var first = true | ||
array.forEach { value -> | ||
checkNotNull(value) | ||
if (!first) writer.append(",\n") | ||
writeIndent() | ||
writeCollectionValue(value) | ||
first = false | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun writeMap(map: IMapItem<*>) { | ||
writeJsonObject("map", map) { | ||
writeJsonArray("entries") { | ||
var first = true | ||
val mapValues = map.values | ||
mapValues.forEach { value -> | ||
checkNotNull(value) | ||
if (!first) writer.append(",\n") | ||
writeIndent() | ||
writeCollectionValue(value) | ||
first = false | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun writeNode(node: INodeItem) { | ||
writeJsonObject("node", node) { | ||
writeIndent() | ||
writer.append("\"baseUri\": \"${node.baseUri.toString().escapeJson()}\",\n") | ||
writeIndent() | ||
writer.append("\"path\": \"${node.metapath.escapeJson()}\"\n") | ||
} | ||
} | ||
|
||
override fun writeAtomicValue(node: IAnyAtomicItem) { | ||
writeJsonObject("atomic", node) { | ||
writeIndent() | ||
writer.append("\"text\": \"${node.asString().escapeJson()}\"\n") | ||
} | ||
} | ||
|
||
protected fun writeCollectionValue(value: ICollectionValue) { | ||
when (value) { | ||
is IItem -> value.accept(visitor) | ||
is ISequence<*> -> writeSequence(value) | ||
} | ||
} | ||
|
||
/** | ||
* Escapes special characters in JSON strings. | ||
*/ | ||
private fun String.escapeJson(): String = buildString { | ||
[email protected] { char -> | ||
when (char) { | ||
'"' -> append("\\\"") | ||
'\\' -> append("\\\\") | ||
'\b' -> append("\\b") | ||
'\u000C' -> append("\\f") | ||
'\n' -> append("\\n") | ||
'\r' -> append("\\r") | ||
'\t' -> append("\\t") | ||
else -> if (char < ' ') { | ||
append(String.format("\\u%04x", char.code)) | ||
} else { | ||
append(char) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private inner class Visitor : IItemVisitor { | ||
override fun visit(array: IArrayItem<*>) = writeArray(array) | ||
override fun visit(map: IMapItem<*>) = writeMap(map) | ||
override fun visit(node: INodeItem) = writeNode(node) | ||
override fun visit(node: IAnyAtomicItem) = writeAtomicValue(node) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-blocking: in some cases this should work, there has been a regression potentially. FYSA:
metaschema-framework/metaschema-java#228
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should pursue this in another PR, but this is good to know, so a metapath just executing inline?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, feel free to resolve.
Correct, when there are lower-level syntax errors, I actually use this, you may have noticed I also use it during our pairing sessions, the local API approach will make it much faster. 😉
I use this to check syntax before/with constraint authoring. This found some bugs with the output wrapping with sequence
()
and other output weirdness in #228.