1 /* 2 * Copyright 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.room.migration.bundle 18 19 import androidx.annotation.RestrictTo 20 import java.io.InputStream 21 import java.io.OutputStream 22 import kotlinx.serialization.ExperimentalSerializationApi 23 import kotlinx.serialization.SerialName 24 import kotlinx.serialization.Serializable 25 import kotlinx.serialization.json.encodeToStream 26 27 /** Data class that holds the information about a database schema export. */ 28 @Serializable 29 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) 30 actual class SchemaBundle 31 actual constructor( 32 @SerialName("formatVersion") actual val formatVersion: Int, 33 @SerialName("database") actual val database: DatabaseBundle 34 ) : SchemaEquality<SchemaBundle> { 35 isSchemaEqualnull36 actual override fun isSchemaEqual(other: SchemaBundle): Boolean { 37 return formatVersion == other.formatVersion && 38 SchemaEqualityUtil.checkSchemaEquality(database, other.database) 39 } 40 41 companion object { deserializenull42 fun deserialize(fis: InputStream): SchemaBundle = 43 fis.use { 44 // TODO: Try to use decodeFromStream if possible, currently blocked by 45 // https://github.com/Kotlin/kotlinx.serialization/issues/2457 46 json.decodeFromString(it.bufferedReader().readText()) 47 } 48 49 @OptIn(ExperimentalSerializationApi::class) // For encodeToStream() with OutputStream serializenull50 fun serialize(bundle: SchemaBundle, outputStream: OutputStream) { 51 outputStream.use { json.encodeToStream(bundle, it) } 52 } 53 } 54 } 55