Skip to content

Commit

Permalink
Merge pull request #12 from CDCgov/serde
Browse files Browse the repository at this point in the history
better serde handling - v 1.3.10
  • Loading branch information
marcia-schulman authored Nov 1, 2024
2 parents 6b9af6b + 83c49a6 commit 2be03ce
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 35 deletions.
8 changes: 0 additions & 8 deletions .vscode/settings.json

This file was deleted.

10 changes: 10 additions & 0 deletions change-log.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Change Log - lib-hl7v2-nist-validator

## v 1.3.10 - 2024/10/31

- Improving Json serialization and deserialization to circumvent issues with mixing scala and kotlin classes
- Created a Gson Entity Adapter for the scala Entry interface.
- Created a Gson Exclusion strategy class to remove stackTrace and metaData from serialization.
- Encapsulated a gson instance (nistGson) on NistReport with the appropriate initialization of the adapter above and other configs.
-

30 changes: 15 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<artifactId>lib-hl7v2-nist-validator</artifactId>
<name>lib-hl7v2-nist-validator</name>
<description>Library wrapper around NIST HL7v2 Validator for validation of HL7 v2.x messages.</description>
<version>1.3.9</version>
<version>1.3.10</version>
<packaging>jar</packaging>
<url>https://github.com/CDCgov/lib-hl7v2-nist-validator</url>
<licenses>
Expand Down Expand Up @@ -80,20 +80,20 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>3.2.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-gpg-plugin</artifactId>-->
<!-- <version>3.2.5</version>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <id>sign-artifacts</id>-->
<!-- <phase>verify</phase>-->
<!-- <goals>-->
<!-- <goal>sign</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->
<plugin>
<groupId>org.sonatype.central</groupId>
<artifactId>central-publishing-maven-plugin</artifactId>
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/gov/cdc/EntryInterfaceAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package gov.cdc

import com.google.gson.*
import gov.nist.validation.report.impl.EntryImpl
import java.lang.reflect.Type

class EntryInterfaceAdapter<T> : JsonSerializer<T>, JsonDeserializer<T> {
override fun deserialize(jsonElement: JsonElement, type: Type, context: JsonDeserializationContext): T {
val jsonObject = jsonElement.asJsonObject
return context.deserialize(jsonObject, EntryImpl::class.java)
}

override fun serialize(src: T, type: Type, context: JsonSerializationContext): JsonElement {
return context.serialize(src)
}
}
15 changes: 15 additions & 0 deletions src/main/java/gov/cdc/GsonExclusionStrategy.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package gov.cdc

import com.google.gson.ExclusionStrategy
import com.google.gson.FieldAttributes
import gov.nist.validation.report.impl.EntryImpl

class GsonExclusionStrategy : ExclusionStrategy {
override fun shouldSkipClass(clazz: Class<*>?): Boolean {
return false
}
override fun shouldSkipField(f: FieldAttributes): Boolean {
return (f.declaringClass === EntryImpl::class.java && f.name.equals("stackTrace")) ||
(f.declaringClass === EntryImpl::class.java && f.name.equals("metaData"))
}
}
27 changes: 15 additions & 12 deletions src/main/java/gov/cdc/NistReport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@ package gov.cdc

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.annotation.JsonProperty
import com.google.gson.GsonBuilder
import com.google.gson.annotations.SerializedName
import gov.nist.validation.report.Entry
import java.util.*
import java.util.concurrent.atomic.AtomicInteger

//JsonProperty is used for jackson
//SerialiedName is used for Gson.
@JsonInclude(JsonInclude.Include.NON_NULL)
class NistReport {
companion object {
val nistGson = GsonBuilder().disableHtmlEscaping().serializeNulls().registerTypeAdapter(
gov.nist.validation.report.Entry::class.java,
EntryInterfaceAdapter<Entry>()
).setExclusionStrategies(GsonExclusionStrategy()
).create()
}
val entries: Entries = Entries()
@JsonProperty("error-count")
@SerializedName("error-count")
Expand Down Expand Up @@ -39,10 +46,14 @@ class NistReport {
}

data class SummaryCount(
@JsonProperty("structure")
@SerializedName("structure")
val structure: Int ,
@JsonProperty ("value-set")
@SerializedName("value-set")
val valueset: Int,
@JsonProperty("content")
@SerializedName("content")
val content: Int
)

Expand All @@ -53,14 +64,6 @@ class Entries {
@SerializedName("value-set")
var valueset = ArrayList<Entry>()
}
//
//@JsonIgnoreProperties(ignoreUnknown = true)
//class Entry {
// var line = 0
// var column = 0
// var path: String? = null
// var description: String? = null
// var category: String? = null
// var classification: String? = null
//
//}



39 changes: 39 additions & 0 deletions src/test/java/gov/cdc/NistReportTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package gov.cdc

import gov.nist.validation.report.impl.EntryImpl
import org.junit.jupiter.api.Test

class NistReportTest {

@Test
fun testEntryToText() {
val entry = EntryImpl(
3,
33,
"SFT[1]-4",
"Unit Test error",
"Cat-1",
"Error")
println("\n\n")
println(entry.toText())
}

@Test
fun testMinimalEntry() {
val entry = EntryImpl(3,33, null, null, null, null)
println("\n\n")
println(entry.toText())

}
@Test
fun fullReportJSON() {
val report = this::class.java.getResource("/reportExample.json")?.readText()
val nistReport = NistReport.nistGson.fromJson(report, NistReport::class.java)

println(nistReport)

val serializeIt = NistReport.nistGson.toJson(nistReport)
println(serializeIt)

}
}
39 changes: 39 additions & 0 deletions src/test/resources/reportExample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"entries": {
"structure": [
{
"line": 1,
"column": 1,
"path": "PATIENT_RESULT[1]",
"description": "The required Group PATIENT_RESULT (PATIENT_RESULT) is missing",
"category": "Usage",
"classification": "Error",
"stackTrace": null,
"metaData": null
},
{
"line": 1,
"column": 1,
"path": "SFT[1]",
"description": "The required Segment SFT (Software Segment) is missing",
"category": "Usage",
"classification": "Error",
"stackTrace": null,
"metaData": null
}
],
"content": [],
"value-set": []
},
"error-count": {
"structure": 2,
"value-set": 0,
"content": 0
},
"warning-count": {
"structure": 0,
"value-set": 0,
"content": 0
},
"status": "STRUCTURE_ERRORS"
}

0 comments on commit 2be03ce

Please sign in to comment.