Skip to content

Commit 5a8795a

Browse files
Added FormatLanguage annotation to JSON (#2234)
Resolves #2166 Co-authored-by: Leonid Startsev <[email protected]>
1 parent 40eb277 commit 5a8795a

File tree

7 files changed

+157
-2
lines changed

7 files changed

+157
-2
lines changed

formats/json/commonMain/src/kotlinx/serialization/json/Json.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,23 @@ public sealed class Json(
8585
}
8686
}
8787

88+
/**
89+
* Decodes and deserializes the given JSON [string] to the value of type [T] using deserializer
90+
* retrieved from the reified type parameter.
91+
*
92+
* @throws SerializationException in case of any decoding-specific error
93+
* @throws IllegalArgumentException if the decoded input is not a valid instance of [T]
94+
*/
95+
public inline fun <reified T> decodeFromString(@FormatLanguage("json", "", "") string: String): T =
96+
decodeFromString(serializersModule.serializer(), string)
97+
8898
/**
8999
* Deserializes the given JSON [string] into a value of type [T] using the given [deserializer].
90100
*
91101
* @throws [SerializationException] if the given JSON string is not a valid JSON input for the type [T]
92102
* @throws [IllegalArgumentException] if the decoded input cannot be represented as a valid instance of type [T]
93103
*/
94-
public final override fun <T> decodeFromString(deserializer: DeserializationStrategy<T>, string: String): T {
104+
public final override fun <T> decodeFromString(deserializer: DeserializationStrategy<T>, @FormatLanguage("json", "", "") string: String): T {
95105
val lexer = StringJsonLexer(string)
96106
val input = StreamingJsonDecoder(this, WriteMode.OBJ, lexer, deserializer.descriptor, null)
97107
val result = input.decodeSerializableValue(deserializer)
@@ -122,7 +132,7 @@ public sealed class Json(
122132
*
123133
* @throws [SerializationException] if the given string is not a valid JSON
124134
*/
125-
public fun parseToJsonElement(string: String): JsonElement {
135+
public fun parseToJsonElement(@FormatLanguage("json", "", "") string: String): JsonElement {
126136
return decodeFromString(JsonElementSerializer, string)
127137
}
128138
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package kotlinx.serialization.json.internal;
2+
3+
import kotlinx.serialization.InternalSerializationApi
4+
5+
/**
6+
* Multiplatform analogue of `org.intellij.lang.annotations.Language` annotation.
7+
*
8+
* An alias is used instead of class, because the actual class in the JVM will conflict with the class from the stdlib -
9+
* we want to avoid the situation with different classes having the same fully-qualified name.
10+
* [see](https://github.com/JetBrains/java-annotations/issues/34)
11+
*
12+
* Specifies that an element of the program represents a string that is a source code on a specified language.
13+
* Code editors may use this annotation to enable syntax highlighting, code completion and other features
14+
* inside the literals that assigned to the annotated variables, passed as arguments to the annotated parameters,
15+
* or returned from the annotated methods.
16+
* <p>
17+
* This annotation also could be used as a meta-annotation, to define derived annotations for convenience.
18+
* E.g. the following annotation could be defined to annotate the strings that represent Java methods:
19+
*
20+
* <pre>
21+
* &#64;Language(value = "JAVA", prefix = "class X{", suffix = "}")
22+
* &#64;interface JavaMethod {}
23+
* </pre>
24+
* <p>
25+
* Note that using the derived annotation as meta-annotation is not supported.
26+
* Meta-annotation works only one level deep.
27+
*/
28+
29+
@InternalSerializationApi
30+
@Retention(AnnotationRetention.BINARY)
31+
@Target(
32+
AnnotationTarget.FUNCTION,
33+
AnnotationTarget.PROPERTY_GETTER,
34+
AnnotationTarget.PROPERTY_SETTER,
35+
AnnotationTarget.FIELD,
36+
AnnotationTarget.VALUE_PARAMETER,
37+
AnnotationTarget.LOCAL_VARIABLE,
38+
AnnotationTarget.ANNOTATION_CLASS
39+
)
40+
public expect annotation class FormatLanguage(
41+
public val value: String,
42+
// default parameters are not used due to https://youtrack.jetbrains.com/issue/KT-25946/
43+
public val prefix: String,
44+
public val suffix: String,
45+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* Copyright 2017-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.serialization.json.internal;
6+
7+
import kotlinx.serialization.InternalSerializationApi
8+
9+
@InternalSerializationApi
10+
public actual typealias FormatLanguage = org.intellij.lang.annotations.Language
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2017-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
@file:Suppress("PackageDirectoryMismatch")
6+
7+
package org.intellij.lang.annotations
8+
9+
import kotlinx.serialization.InternalSerializationApi
10+
11+
/**
12+
* JS implementation of JVM-only `org.intellij.lang.annotations.Language` class, adds syntax support by IDE.
13+
*
14+
* This class is missing from the Kotlin/JS targets, so it needs to be distributed along with the serialization runtime.
15+
*
16+
* Copy-paste from [https://github.com/JetBrains/java-annotations](https://github.com/JetBrains/java-annotations).
17+
*
18+
* @see [kotlinx.serialization.json.internal.FormatLanguage]
19+
*/
20+
@InternalSerializationApi
21+
@Retention(AnnotationRetention.BINARY)
22+
@Target(
23+
AnnotationTarget.FUNCTION,
24+
AnnotationTarget.PROPERTY_GETTER,
25+
AnnotationTarget.PROPERTY_SETTER,
26+
AnnotationTarget.FIELD,
27+
AnnotationTarget.VALUE_PARAMETER,
28+
AnnotationTarget.LOCAL_VARIABLE,
29+
AnnotationTarget.ANNOTATION_CLASS,
30+
)
31+
public annotation class Language(
32+
val value: String,
33+
val prefix: String = "",
34+
val suffix: String = "",
35+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* Copyright 2017-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.serialization.json.internal;
6+
7+
import kotlinx.serialization.InternalSerializationApi
8+
9+
@InternalSerializationApi
10+
public actual typealias FormatLanguage = org.intellij.lang.annotations.Language
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* Copyright 2017-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.serialization.json.internal;
6+
7+
import kotlinx.serialization.InternalSerializationApi
8+
9+
@InternalSerializationApi
10+
public actual typealias FormatLanguage = org.intellij.lang.annotations.Language
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2017-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
@file:Suppress("PackageDirectoryMismatch")
6+
7+
package org.intellij.lang.annotations
8+
9+
import kotlinx.serialization.InternalSerializationApi
10+
11+
/**
12+
* Native implementation of JVM-only `org.intellij.lang.annotations.Language` class, adds syntax support by IDE.
13+
*
14+
* This class is missing from the Kotlin/Native targets, so it needs to be distributed along with the serialization runtime.
15+
*
16+
* Copy-paste from [https://github.com/JetBrains/java-annotations](https://github.com/JetBrains/java-annotations).
17+
*
18+
* @see [kotlinx.serialization.json.internal.FormatLanguage]
19+
*/
20+
@InternalSerializationApi
21+
@Retention(AnnotationRetention.BINARY)
22+
@Target(
23+
AnnotationTarget.FUNCTION,
24+
AnnotationTarget.PROPERTY_GETTER,
25+
AnnotationTarget.PROPERTY_SETTER,
26+
AnnotationTarget.FIELD,
27+
AnnotationTarget.VALUE_PARAMETER,
28+
AnnotationTarget.LOCAL_VARIABLE,
29+
AnnotationTarget.ANNOTATION_CLASS,
30+
)
31+
public annotation class Language(
32+
val value: String,
33+
val prefix: String = "",
34+
val suffix: String = "",
35+
)

0 commit comments

Comments
 (0)