-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support using of kotlinx serialization for persistence state component
GitOrigin-RevId: bf9ff3e6be6c8ad4712c68eda903371af0e647d7
- Loading branch information
1 parent
89e6984
commit 90c5cbb
Showing
11 changed files
with
245 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
platform/object-serializer/src/xml/KotlinxSerializationBinding.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
package com.intellij.serialization.xml | ||
|
||
import com.intellij.util.XmlElement | ||
import com.intellij.util.xmlb.NotNullDeserializeBinding | ||
import com.intellij.util.xmlb.SerializationFilter | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.json.Json | ||
import org.jdom.CDATA | ||
import org.jdom.Element | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
private val json = Json { | ||
prettyPrint = true | ||
prettyPrintIndent = " " | ||
ignoreUnknownKeys = true | ||
} | ||
|
||
internal class KotlinxSerializationBinding(private val serializer: KSerializer<Any>) : NotNullDeserializeBinding() { | ||
override fun serialize(o: Any, context: Any?, filter: SerializationFilter?): Any { | ||
val element = Element("state") | ||
element.addContent(CDATA(json.encodeToString(serializer, o))) | ||
return element | ||
} | ||
|
||
override fun isBoundTo(element: Element): Boolean { | ||
throw UnsupportedOperationException("Only root object is supported") | ||
} | ||
|
||
override fun isBoundTo(element: XmlElement): Boolean { | ||
throw UnsupportedOperationException("Only root object is supported") | ||
} | ||
|
||
override fun deserialize(context: Any?, element: Element): Any { | ||
val cdata = element.content.firstOrNull() as? CDATA | ||
if (cdata == null) { | ||
LOG.debug("incorrect data (old format?) for $serializer") | ||
return json.decodeFromString(serializer, "{}") | ||
} | ||
return json.decodeFromString(serializer, cdata.text) | ||
} | ||
|
||
override fun deserialize(context: Any?, element: XmlElement): Any { | ||
throw UnsupportedOperationException("Only JDOM is supported for now") | ||
} | ||
} |
Oops, something went wrong.