「Kotlinx Json」の登場でサードパーティJSONライブラリは不要となる。

JSONのライブラリ何を使ってますか?

👉 square/moshi: A modern JSON library for Kotlin and Java. 
👉 FasterXML/jackson: Main Portal page for the Jackson project 
👉 google/gson: A Java serialization/deserialization library to convert Java Objects into JSON and back 

これらは、Javaベースで書かれています。Kotlin は100%相互運用可能ですが微妙に期待しない挙動をします。


data class User(
    val name: String,
    val email: String,
    val age: Int = 13,
    val role: Role = Role.Viewer
)

enum class Role { Viewer, Editor, Owner }


{
    "name" : "John Doe",
    "email" : "[email protected]"
}


class JsonUnitTest {

    private val jsonString = """
            {
                "name" : "John Doe",
                "email" : "[email protected]"
            }
        """.trimIndent()

    @Test
    fun gsonTest() {
        val user = Gson().fromJson(jsonString, User::class.java)

        assertEquals("John Doe",user.name)
        assertEquals(null, user.role)
        assertEquals(0, user.age)

//      User(name=John Doe, 
//           [email protected], 
//           age=0, 
//           role=null)

    }

}

デフォルト値が期待通りにパースできません。

kotlinx.serialization が登場!!

JetBrains産です。間違いないでしょう。



👉 Kotlin/kotlinx.serialization: Kotlin multiplatform / multi-format serialization 

- クロスプラットフォーム
- 非リフレクション
- アノテーション @Serializable
- Kotlin v1.3.30+


@Serializable
data class User(
    val name: String,
    val email: String,
    val age: Int = 13,
    val role: Role = Role.Viewer
)

enum class Role { Viewer, Editor, Owner }

class JsonUnitTest {

    private val jsonString = """
            {
                "name" : "John Doe",
                "email" : "[email protected]"
            }
        """.trimIndent()

    @Test
    fun jsonTest() {
        val user = Json.parse(User.serializer(), jsonString)

        assertEquals("John Doe", user.name)
        assertEquals(Role.Viewer, user.role)
        assertEquals(13, user.age)

//      User(name=John Doe, 
//           [email protected], 
//           age=13, 
//           role=Viewer)

    }
}

Gson では無視されていたデフォルト値がきちんと使用されます。

以下のセットアップでどうぞ。


buildscript {
    ext.kotlin_version = '1.3.60'
    repositories { jcenter() }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
    }
}


apply plugin: 'kotlin' 
apply plugin: 'kotlinx-serialization'


repositories {
    jcenter()
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.14.0" // JVM dependency
}

👉 Kotlinx Json vs Gson - Juraj Kušnier - Medium 



GoPro 分割された動画ファイルの命名規則



対象製品
- HERO7 (White、Silver、Black)
- HERO6 Black
- Fusion
- HERO (2018)
- HERO5 Black
- HERO5 Session
- HERO4 Black & Silver
- HERO Session / HERO4 Session
- HERO (2014)
- HERO3+
- HERO3
- HD HERO2


👉 GoProカメラのファイルの命名規則 

GHzzxxxx.mp4

ということですが、なぜにファイル名による並び替えが可能な

GHxxxxzz.mp4

にしなかったのか?

何らかの理由はあったのだろうと思うけども。

一括で変更する

スクリプトで一括で変更します。

以下動画のように使います。




Related Categories :  Recommended


設定「画面の自動回転」は OFF で良い。

急いでいるときに、

スマホを操作するとき、

画面が回転してイラッとする。

以前に考え方について書いてはみましたが。

👉 【Android Pie】Auto-rotate (自動回転) OFF のときの挙動 

今現在のAndroid最新は「Anroid10」=「Android Q」ですが、画面回転の設定に関しては同様なことが言えます。

自動画面回転はOFFにしておくべき

考え方としては、

画面固定(自動回転OFF)にしておいて、画面回転するかしないかはサジェスチョンボタンで操作する

がオススメです。

これは、「Android OS 9 Pie」でも「Android OS 10 Q」でも同様です。


今どきの Retrofit と LiveData で Coroutine

ありがとうございます。


👉 Retrofit 

ご存知の通り Retrofit2 では、サスペンドな関数も利用できるようになっております。

👉 SpaceX REST API で試す Retrofit の coroutine 対応 


// NewModel.kt
@GET("/feed/here/")
suspend fun getData(@Query("token") token : String) : Status


// NewRepository.kt
class Repository {
  var client = RetrofitService.createService(JsonApi::class.java)
  suspend fun getData(token : String) = client.getData(token)
}

ので、以下のようなこれまでのコードは、


// OldViewModel.kt
val data = MutableLiveData<Status>()

private fun loadData(token: String){
  viewModelScope.launch {
    val retrievedData = withContext(Dispatchers.IO) {
      repository.getData(token)
    }
    data.value = retrievedData
  }
}

シンプルに以下のように書けます。


// NewViewModel.kt
val data : LiveData<Status> = liveData(Dispatchers.IO) {
      val retrievedData = repository.getData(token)
      emit(retrievedData)
    }

ありがとうございます。

👉 Exploring new Coroutines and Lifecycle Architectural Components integration on Android 
👉 Using Retrofit 2 with Kotlin coroutines - ProAndroidDev 


Android A/B パーテーション と Lineage OS インストール

大まかでもイメージしておくと得をすると思います。

👉 Here's a list of Android devices that support Seamless Updates 


👉 Download TWRP for sailfish 
👉 サポートが切れた Pixel に Android 10 相当の Lineage OS 17 を 

分かりやすく簡単に言いますが。

昔のパーテーション。

- recovery
- boot
- cache
- system
- vendor
- data

これが、Pixel時代以降の、A/B スタイルでは以下のようになります。

- boot_a
- boot_b
- system_a
- system_b
- vendor_a
- vendor_b
- data

キャッシュディレクトリは data/cache、リカバリパーテーションは boot_a と boot_b 以下に統合されました。

TWRP の flash 時には考慮が必須となります。

そして、TWRPのバージョンは、OpenGapps の 「Signature Verification」にも関係してきます。

👉 Help understanding A/B partitions | Google Pixel XL 
👉 TWRP lead explains why it'll take time for the custom recovery to support Android 10 

さらに、Android10では「ダイナミックパーテーション」などになるようです。

👉 Implementing Dynamic Partitions  |  Android Open Source Project 

よって、

Android10で初回のTWRP適用は、いきなり fastboot で flash するのはむずい。

なので、まず fastboot boot /path/to/twrp.img でメモリブートする。

そして、TWRPの端末への適用はインストーラ入りのzipで行う。

のが良いようです。

Pixelで以下の順でハマり続けた結果です。

Android10

非公式LineageOS17(Android10相当)

公式LineageOS16(Android9相当)