Skip to content
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

refactor: migrating from the org.json Library to Gson #248

Merged
merged 15 commits into from
Jan 29, 2024
Merged
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
sanao1006 marked this conversation as resolved.
Show resolved Hide resolved
}
}
jvmTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.github.takahirom.roborazzi

import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
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,12 +22,12 @@ 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)
return json
override fun toJson(): JsonObject {
val recorded = object {
val golden_file_path = goldenFile.absolutePath
val timestamp = timestampNs
sanao1006 marked this conversation as resolved.
Show resolved Hide resolved
}
return JsonParser.parseString(Gson().toJson(recorded)).asJsonObject
}
}

Expand All @@ -35,14 +37,14 @@ 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)
return json
override fun toJson(): JsonObject {
val added = object {
val compare_file_path: String = compareFile.absolutePath
val actual_file_path: String = actualFile.absolutePath
val golden_file_path: String = goldenFile.absolutePath
val timestamp: Long = timestampNs
}
return JsonParser.parseString(Gson().toJson(added)).asJsonObject
}
}

Expand All @@ -52,14 +54,14 @@ 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)
return json
override fun toJson(): JsonObject {
val changed = object {
val compare_file_path: String = compareFile.absolutePath
val actual_file_path: String = actualFile.absolutePath
val golden_file_path: String = goldenFile.absolutePath
val timestamp: Long = timestampNs
}
return JsonParser.parseString(Gson().toJson(changed)).asJsonObject
}
}

Expand All @@ -72,27 +74,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)
return json
override fun toJson(): JsonObject {
val unChanged = object {
val golden_file_path: String = goldenFile.absolutePath
val timestamp: Long = timestampNs
}
return JsonParser.parseString(Gson().toJson(unChanged)).asJsonObject
}
}

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,22 +1,21 @@
package com.github.takahirom.roborazzi

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

data class CaptureResults(
val summary: ResultSummary,
val resultSummary: ResultSummary,
val captureResults: List<CaptureResult>
) {
fun toJson(): JSONObject {
val json = JSONObject()
json.put("summary", summary.toJson())
val resultsArray = JSONArray()
captureResults.forEach { result ->
resultsArray.put(result.toJson())
fun toJson(): JsonObject {
val captureResultsObject = object {
val summary = resultSummary
val results = captureResults.map { it.toJson() }
}
json.put("results", resultsArray)
return json
return JsonParser.parseString(Gson().toJson(captureResultsObject)).asJsonObject
}

fun toHtml(reportDirectoryPath: String): String {
Expand Down Expand Up @@ -78,7 +77,7 @@ data class CaptureResults(
}
}
return buildString {
append(summary.toHtml())
append(resultSummary.toHtml())
append(buildTable("Recorded images", "recorded", recordedImages))
append(buildTable("Added images", "added", addedImages))
append(buildTable("Changed images", "changed", changedImages))
Expand All @@ -88,25 +87,23 @@ 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)
}

fun from(results: List<CaptureResult>): CaptureResults {
return CaptureResults(
summary = ResultSummary(
resultSummary = ResultSummary(
total = results.size,
recorded = results.count { it is CaptureResult.Recorded },
added = results.count { it is CaptureResult.Added },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.github.takahirom.roborazzi

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

data class ResultSummary(
val total: Int,
Expand All @@ -9,14 +11,8 @@ 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)
return json
fun toJson(): JsonObject {
return JsonParser.parseString(Gson().toJson(this)).asJsonObject
}

fun toHtml(): String {
Expand Down Expand Up @@ -46,12 +42,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,32 +86,33 @@ class CaptureResultTest {
{
"type": "recorded",
"golden_file_path": "golden_file",
"timestamp": 123456789,
sanao1006 marked this conversation as resolved.
Show resolved Hide resolved
"timestamp": 123456789
},
{
"type": "added",
"compare_file_path": "compare_file",
"actual_file_path": "actual_file",
"timestamp": 123456789,
"golden_file_path": "golden_file",
sanao1006 marked this conversation as resolved.
Show resolved Hide resolved
"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 summary = compareReportResult.summary
val jsonObject = JsonParser.parseString(jsonString).asJsonObject
val compareReportResult = CaptureResults.fromJson(jsonObject)
val summary = compareReportResult.resultSummary
val captureResults = compareReportResult.captureResults

// Test summary
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
Loading
Loading