Skip to content

Commit

Permalink
refactor: migrating from the org.json Library to Gson
Browse files Browse the repository at this point in the history
  • Loading branch information
sanao1006 committed Jan 19, 2024
1 parent 33ebe87 commit 16a7831
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 91 deletions.
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ androidx-test-ext-junit = "1.1.5"

dropbox-differ = "0.0.1"
google-android-material = "1.5.0"
json = "20231013"
gson = "2.10.1"
junit = "4.13.2"
ktor-serialization-kotlinx-xml = "2.3.0"
squareup-okhttp = "5.0.0-alpha.11"
Expand Down Expand Up @@ -77,7 +77,7 @@ compose-ui-graphics-desktop = { module = "org.jetbrains.compose.ui:ui-graphics-d
compose-ui-test-junit4-desktop = { module = "org.jetbrains.compose.ui:ui-test-junit4-desktop", version.ref = "composeDesktop" }
dropbox-differ = { module = "com.dropbox.differ:differ", version.ref = "dropbox-differ" }
google-android-material = { module = "com.google.android.material:material", version.ref = "google-android-material" }
json = { module = "org.json:json", version.ref = "json" }
gson = { module = "com.google.code.gson:gson", version.ref = "gson"}
junit = { module = "junit:junit", version.ref = "junit" }
ktor-serialization-kotlinx-xml = { module = "io.ktor:ktor-serialization-kotlinx-xml", version.ref = "ktor-serialization-kotlinx-xml" }
squareup-okhttp = { module = "com.squareup.okhttp3:okhttp", version.ref = "squareup-okhttp" }
Expand Down
4 changes: 2 additions & 2 deletions include-build/roborazzi-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ kotlin {
}
commonJvmMain {
dependencies {
compileOnly libs.json
compileOnly libs.gson
api libs.dropbox.differ
implementation libs.junit
}
Expand All @@ -46,7 +46,7 @@ kotlin {
}
jvmMain {
dependencies {
implementation libs.json
implementation libs.gson
}
}
jvmTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.github.takahirom.roborazzi

import com.google.gson.Gson
import com.google.gson.JsonObject
import java.io.File
import java.io.FileReader
import org.json.JSONObject

sealed interface CaptureResult {
fun toJson(): JSONObject
fun toJson(): JsonObject
val timestampNs: Long
val compareFile: File?
val actualFile: File?
Expand All @@ -20,11 +21,11 @@ sealed interface CaptureResult {
override val compareFile: File?
get() = null

override fun toJson(): JSONObject {
val json = JSONObject()
json.put("type", "recorded")
json.put("golden_file_path", goldenFile.absolutePath)
json.put("timestamp", timestampNs)
override fun toJson(): JsonObject {
val json = JsonObject()
json.addProperty("type", "recorded")
json.addProperty("golden_file_path", goldenFile.absolutePath)
json.addProperty("timestamp", timestampNs)
return json
}
}
Expand All @@ -35,13 +36,13 @@ sealed interface CaptureResult {
override val goldenFile: File,
override val timestampNs: Long,
) : CaptureResult {
override fun toJson(): JSONObject {
val json = JSONObject()
json.put("type", "added")
json.put("compare_file_path", compareFile.absolutePath)
json.put("actual_file_path", actualFile.absolutePath)
json.put("golden_file_path", goldenFile.absolutePath)
json.put("timestamp", timestampNs)
override fun toJson(): JsonObject {
val json = JsonObject()
json.addProperty("type", "added")
json.addProperty("compare_file_path", compareFile.absolutePath)
json.addProperty("actual_file_path", actualFile.absolutePath)
json.addProperty("golden_file_path", goldenFile.absolutePath)
json.addProperty("timestamp", timestampNs)
return json
}
}
Expand All @@ -52,13 +53,13 @@ sealed interface CaptureResult {
override val actualFile: File,
override val timestampNs: Long
) : CaptureResult {
override fun toJson(): JSONObject {
val json = JSONObject()
json.put("type", "changed")
json.put("compare_file_path", compareFile.absolutePath)
json.put("actual_file_path", actualFile.absolutePath)
json.put("golden_file_path", goldenFile.absolutePath)
json.put("timestamp", timestampNs)
override fun toJson(): JsonObject {
val json = JsonObject()
json.addProperty("type", "changed")
json.addProperty("compare_file_path", compareFile.absolutePath)
json.addProperty("actual_file_path", actualFile.absolutePath)
json.addProperty("golden_file_path", goldenFile.absolutePath)
json.addProperty("timestamp", timestampNs)
return json
}
}
Expand All @@ -72,27 +73,26 @@ sealed interface CaptureResult {
override val compareFile: File?
get() = null

override fun toJson(): JSONObject {
val json = JSONObject()
json.put("type", "unchanged")
json.put("golden_file_path", goldenFile.absolutePath)
json.put("timestamp", timestampNs)
override fun toJson(): JsonObject {
val json = JsonObject()
json.addProperty("type", "unchanged")
json.addProperty("golden_file_path", goldenFile.absolutePath)
json.addProperty("timestamp", timestampNs)
return json
}
}

companion object {
fun fromJsonFile(inputPath: String): CaptureResult {
val json = JSONObject(FileReader(inputPath).readText())
return fromJson(json)
return Gson().fromJson(FileReader(inputPath).readText(), CaptureResult::class.java)
}

fun fromJson(json: JSONObject): CaptureResult {
val type = json.getString("type")
val compareFile = json.optString("compare_file_path")?.let { File(it) }
val goldenFile = json.optString("golden_file_path")?.let { File(it) }
val actualFile = json.optString("actual_file_path")?.let { File(it) }
val timestampNs = json.getLong("timestamp")
fun fromJson(json: JsonObject): CaptureResult {
val type = json.get("type").asString
val compareFile = json.get("compare_file_path")?.asString?.let{ File(it) }
val goldenFile = json.get("golden_file_path")?.asString?.let{ File(it) }
val actualFile = json.get("actual_file_path")?.asString?.let{ File(it) }
val timestampNs = json.get("timestamp").asLong

return when (type) {
"recorded" -> Recorded(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package com.github.takahirom.roborazzi

import org.json.JSONArray
import org.json.JSONObject
import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import java.io.File
import java.io.FileReader

data class CaptureResults(
val summary: ResultSummary,
val captureResults: List<CaptureResult>
) {
fun toJson(): JSONObject {
val json = JSONObject()
json.put("summary", summary.toJson())
val resultsArray = JSONArray()
fun toJson(): JsonObject {
val json = JsonObject()
json.add("summary", summary.toJson())
val resultsArray = JsonArray()
captureResults.forEach { result ->
resultsArray.put(result.toJson())
resultsArray.add(result.toJson())
}
json.put("results", resultsArray)
json.add("results", resultsArray)
return json
}

Expand Down Expand Up @@ -88,17 +90,15 @@ data class CaptureResults(

companion object {
fun fromJsonFile(inputPath: String): CaptureResults {
val fileContents = File(inputPath).readText()
val jsonObject = JSONObject(fileContents)
return fromJson(jsonObject)
return Gson().fromJson(FileReader(inputPath).readText(), CaptureResults::class.java)
}

fun fromJson(jsonObject: JSONObject): CaptureResults {
val summary = ResultSummary.fromJson(jsonObject.getJSONObject("summary"))
val resultsArray = jsonObject.getJSONArray("results")
fun fromJson(jsonObject: JsonObject): CaptureResults {
val summary = ResultSummary.fromJson(jsonObject.get("summary").asJsonObject)
val resultsArray = jsonObject.get("results").asJsonArray
val captureResults = mutableListOf<CaptureResult>()
for (i in 0 until resultsArray.length()) {
val resultJson = resultsArray.getJSONObject(i)
for (i in 0 until resultsArray.size()) {
val resultJson = resultsArray.get(i).asJsonObject
captureResults.add(CaptureResult.fromJson(resultJson))
}
return CaptureResults(summary, captureResults)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.takahirom.roborazzi

import org.json.JSONObject
import com.google.gson.JsonObject

data class ResultSummary(
val total: Int,
Expand All @@ -9,13 +9,13 @@ data class ResultSummary(
val changed: Int,
val unchanged: Int
) {
fun toJson(): JSONObject {
val json = JSONObject()
json.put("total", total)
json.put("recorded", recorded)
json.put("added", added)
json.put("changed", changed)
json.put("unchanged", unchanged)
fun toJson(): JsonObject {
val json = JsonObject()
json.addProperty("total", total)
json.addProperty("recorded", recorded)
json.addProperty("added", added)
json.addProperty("changed", changed)
json.addProperty("unchanged", unchanged)
return json
}

Expand Down Expand Up @@ -46,12 +46,12 @@ data class ResultSummary(
}

companion object {
fun fromJson(jsonObject: JSONObject): ResultSummary {
val total = jsonObject.getInt("total")
val recorded = jsonObject.getInt("recorded")
val added = jsonObject.getInt("added")
val changed = jsonObject.getInt("changed")
val unchanged = jsonObject.getInt("unchanged")
fun fromJson(jsonObject: JsonObject): ResultSummary {
val total = jsonObject.get("total").asInt
val recorded = jsonObject.get("recorded").asInt
val added = jsonObject.get("added").asInt
val changed = jsonObject.get("changed").asInt
val unchanged = jsonObject.get("unchanged").asInt
return ResultSummary(
total = total,
recorded = recorded,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.github.takahirom.roborazzi
import com.github.takahirom.roborazzi.CaptureResult
import com.github.takahirom.roborazzi.CaptureResults
import com.github.takahirom.roborazzi.ResultSummary
import org.json.JSONObject
import com.google.gson.JsonParser
import org.junit.Assert.assertEquals
import org.junit.Test
import java.io.File
Expand Down Expand Up @@ -39,35 +39,35 @@ class CaptureResultTest {
val compareReportResult = CaptureResults(summary, captureResults)

val json = compareReportResult.toJson()
val jsonSummary = json.getJSONObject("summary")
val jsonResults = json.getJSONArray("results")
val jsonSummary = json.get("summary").asJsonObject
val jsonResults = json.get("results").asJsonArray

// Test summary
assertEquals(summary.total, jsonSummary.getInt("total"))
assertEquals(summary.recorded, jsonSummary.getInt("recorded"))
assertEquals(summary.added, jsonSummary.getInt("added"))
assertEquals(summary.changed, jsonSummary.getInt("changed"))
assertEquals(summary.unchanged, jsonSummary.getInt("unchanged"))
assertEquals(summary.total, jsonSummary.get("total").asInt)
assertEquals(summary.recorded, jsonSummary.get("recorded").asInt)
assertEquals(summary.added, jsonSummary.get("added").asInt)
assertEquals(summary.changed, jsonSummary.get("changed").asInt)
assertEquals(summary.unchanged, jsonSummary.get("unchanged").asInt)

// Test capture results
assertEquals(captureResults.size, jsonResults.length())
assertEquals(captureResults.size, jsonResults.size())

for (i in 0 until jsonResults.length()) {
val jsonResult = jsonResults.getJSONObject(i)
for (i in 0 until jsonResults.size()) {
val jsonResult = jsonResults.get(i).asJsonObject
val captureResult = captureResults[i]

assertEquals(
captureResult.compareFile?.absolutePath, jsonResult.optString("compare_file_path", null)
captureResult.compareFile?.absolutePath, jsonResult.get("compare_file_path")?.asString
)
assertEquals(
captureResult.goldenFile?.absolutePath,
jsonResult.optString("golden_file_path", null)
jsonResult.get("golden_file_path")?.asString
)
assertEquals(
captureResult.actualFile?.absolutePath,
jsonResult.optString("actual_file_path", null)
jsonResult.get("actual_file_path")?.asString
)
assertEquals(captureResult.timestampNs, jsonResult.getLong("timestamp"))
assertEquals(captureResult.timestampNs, jsonResult.get("timestamp").asLong)
}
}

Expand All @@ -86,31 +86,32 @@ class CaptureResultTest {
{
"type": "recorded",
"golden_file_path": "golden_file",
"timestamp": 123456789,
"timestamp": 123456789
},
{
"type": "added",
"compare_file_path": "compare_file",
"actual_file_path": "actual_file",
"timestamp": 123456789,
"golden_file_path": "golden_file",
"timestamp": 123456789
},
{
"type": "changed",
"compare_file_path": "compare_file",
"actual_file_path": "actual_file",
"golden_file_path": "golden_file",
"timestamp": 123456789,
"timestamp": 123456789
},
{
"type": "unchanged",
"golden_file_path": "golden_file",
"timestamp": 123456789,
"timestamp": 123456789
}
]
}
""".trimIndent()

val compareReportResult = CaptureResults.fromJson(JSONObject(jsonString))
val jsonObject = JsonParser.parseString(jsonString).asJsonObject
val compareReportResult = CaptureResults.fromJson(jsonObject)
val summary = compareReportResult.summary
val captureResults = compareReportResult.captureResults

Expand Down
2 changes: 1 addition & 1 deletion include-build/roborazzi-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ dependencies {
// We don't use junit for plugin
exclude group: 'junit', module: 'junit'
}
implementation libs.json
implementation libs.gson
integrationTestDepImplementation libs.android.tools.build.gradle
integrationTestDepImplementation libs.kotlin.gradle.plugin
integrationTestDepImplementation libs.compose.gradle.plugin
Expand Down
2 changes: 1 addition & 1 deletion roborazzi-compose-desktop/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ kotlin {
dependencies {
// Please see settings.gradle
api "io.github.takahirom.roborazzi:roborazzi-core:$VERSION_NAME"
implementation libs.json
implementation libs.gson

implementation(compose.runtime)
implementation(compose.desktop.currentOs)
Expand Down

0 comments on commit 16a7831

Please sign in to comment.