implementation org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:0.9.1
plugins { id 'kotlin-multiplatform' version '1.3.11' id 'kotlinx-serialization' version '1.3.10' } repositories { google() jcenter() mavenCentral() maven { url "https://kotlin.bintray.com/kotlinx" } } apply plugin: 'com.android.library' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 28 defaultConfig { minSdkVersion 15 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' androidTestImplementation 'com.android.support.test:runner:1.0.2' } kotlin { targets { fromPreset(presets.android, 'android') // Пресет для эмулятора iPhone // Поменяйте гп presets.iosArm64 (или iosArm32) чтобы собрать библиотеку для iPhone fromPreset(presets.iosX64, 'ios') { compilations.main.outputKinds('FRAMEWORK') } } sourceSets { commonMain { dependencies { implementation 'org.jetbrains.kotlin:kotlin-stdlib-common' implementation 'org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.9.1' } } commonTest { dependencies { implementation 'org.jetbrains.kotlin:kotlin-test-common' implementation 'org.jetbrains.kotlin:kotlin-test-annotations-common' } } androidMain { dependencies { implementation 'org.jetbrains.kotlin:kotlin-stdlib' } } androidTest { dependencies { } } iosMain { dependencies{ implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:0.9.1" } } iosTest { } } }
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlinx-serialization' android { compileSdkVersion 28 defaultConfig { applicationId "com.example.smile.kotlinxretrosample" minSdkVersion 16 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.9.1" implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.android.support:design:28.0.0' implementation 'com.squareup.retrofit2:retrofit:2.5.0' implementation 'com.squareup.okhttp3:okhttp:3.12.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' }
@Serializable
annotation in front of it @Serializable
import kotlinx.serialization.Serializable @Serializable class Field { var length: Int = 0 var hint: String = "" var required: Boolean = false }
/* * { length = 20 hint = "example" required= false } */ fun toObject(stringValue: String): Field { return JSON.parse(Field.serializer(), stringValue) } fun toJson(field: Field): String { // Обратите внимание, что мы вызываем Serializer, который автоматически сгенерирован из нашего класса // Сразу после того, как мы добавили аннотацию @Serializer return JSON.stringify(Field.serializer(), field) }
@Transient
and @Optional
@Transient
- @Transient
Serializer that the field should be ignored.@Optional
- Serializer will not stop and will not throw an error if the field is absent, but at the same time the default value should still be set. @Optional var isOptional: Boolean = false @Transient var isTransient: Boolean = false
un createRetrofit(): Retrofit { val contentType = MediaType.get("application/json") return Retrofit.Builder() .addConverterFactory(serializationConverterFactory(contentType, JSON)) .baseUrl(BASE_URL) .client(provideOkhttpClient()) .build() }
Source: https://habr.com/ru/post/437726/