All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.avito.report.serialize.GsonReportSerializer.kt Maven / Gradle / Ivy

Go to download

Collection of infrastructure libraries and gradle plugins of Avito Android project

There is a newer version: 2023.22
Show newest version
package com.avito.report.serialize

import com.avito.android.Result
import com.avito.report.model.TestRuntimeData
import com.avito.report.model.TestRuntimeDataPackage
import com.google.gson.Gson
import java.io.File
import java.io.FileReader

internal class GsonReportSerializer(private val gson: Gson) : ReportSerializer {

    override fun serialize(testRuntimeData: TestRuntimeData, reportFile: File): Result {
        return Result.tryCatch {
            val json = gson.toJson(testRuntimeData)
            reportFile.writeText(json)
            reportFile
        }
    }

    @Suppress("IfThenToElvis")
    override fun deserialize(reportFile: File): Result {
        return Result.tryCatch {
            val testRuntimeData: TestRuntimeData? = FileReader(reportFile).use { reader ->
                gson.fromJson(reader, TestRuntimeDataPackage::class.java)
            }

            if (testRuntimeData == null) {
                throw IllegalStateException("Report file is empty: ${reportFile.path}")
            } else {
                testRuntimeData
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy