Skip to content

Commit

Permalink
Deprecate unaryPlus for Json array builder in favor of add.
Browse files Browse the repository at this point in the history
Mainly because of #418
Fixes #418
  • Loading branch information
sandwwraith committed May 21, 2019
1 parent f850cae commit 16f25b4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2017-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

@file:Suppress("RedundantVisibilityModifier")
Expand Down Expand Up @@ -34,30 +34,62 @@ public class JsonArrayBuilder internal constructor() {
/**
* Adds [this] value to the current [JsonArray] as [JsonPrimitive].
*/
@Deprecated("Deprecated due to ambiguity", ReplaceWith("add(this)"), DeprecationLevel.ERROR)
public operator fun String?.unaryPlus() {
content.add(JsonPrimitive(this))
}

/**
* Adds [this] value to the current [JsonArray] as [JsonPrimitive].
*/
@Deprecated("Deprecated due to ambiguity", ReplaceWith("add(this)"), DeprecationLevel.ERROR)
public operator fun Number?.unaryPlus() {
content.add(JsonPrimitive(this))
}

/**
* Adds [this] value to the current [JsonArray] as [JsonPrimitive].
*/
@Deprecated("Deprecated due to ambiguity", ReplaceWith("add(this)"), DeprecationLevel.ERROR)
public operator fun Boolean?.unaryPlus() {
content.add(JsonPrimitive(this))
}

/**
* Adds [this] value to the current [JsonArray].
*/
@Deprecated("Deprecated due to ambiguity", ReplaceWith("add(this)"), DeprecationLevel.ERROR)
public operator fun JsonElement.unaryPlus() {
this@JsonArrayBuilder.content.add(this)
}

/**
* Adds [string] value to the current [JsonArray] as [JsonPrimitive].
*/
public fun add(string: String?) {
content.add(JsonPrimitive(string))
}

/**
* Adds [number] value to the current [JsonArray] as [JsonPrimitive].
*/
public fun add(number: Number?) {
content.add(JsonPrimitive(number))
}

/**
* Adds [bool] value to the current [JsonArray] as [JsonPrimitive].
*/
public fun add(bool: Boolean?) {
content.add(JsonPrimitive(bool))
}

/**
* Adds [element] to the current [JsonArray].
*/
public fun add(element: JsonElement) {
this@JsonArrayBuilder.content.add(element)
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ class JsonArraySerializerTest : JsonTestBase() {

private fun prebuiltJson(): JsonArray {
return jsonArray {
+JsonLiteral(1)
+JsonNull
+jsonArray {
+JsonLiteral("nested literal")
}
+jsonArray { }
+json {
add(1)
add(JsonNull)
add(jsonArray {
add("nested literal")
})
add(jsonArray { })
add(json {
"key" to "value"
}
})
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class JsonSerializerInGenericsTest : JsonTestBase() {

private fun create(): NonTrivialClass {
return NonTrivialClass(
arrayListOf(JsonPrimitive(42), jsonArray { +json { "key" to "value" } }, null),
arrayListOf(JsonPrimitive(42), jsonArray { add(json { "key" to "value" }) }, null),
null,
mapOf("key1" to mapOf("nested" to json {
"first" to "second"
Expand Down

0 comments on commit 16f25b4

Please sign in to comment.