-
Notifications
You must be signed in to change notification settings - Fork 419
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the ability to pretty print DokkaConfiguration (#2872)
- Loading branch information
1 parent
dbff38b
commit fa22175
Showing
9 changed files
with
137 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.jetbrains.dokka | ||
|
||
import org.jetbrains.dokka.plugability.ConfigurableBlock | ||
import org.jetbrains.dokka.utilities.parseJson | ||
import org.jetbrains.dokka.utilities.serializeAsCompactJson | ||
import org.jetbrains.dokka.utilities.serializeAsPrettyJson | ||
|
||
fun DokkaConfigurationImpl(json: String): DokkaConfigurationImpl = parseJson(json) | ||
|
||
fun GlobalDokkaConfiguration(json: String): GlobalDokkaConfiguration = parseJson(json) | ||
|
||
@Deprecated("Renamed to better distinguish between compact/pretty prints", ReplaceWith("this.toCompactJsonString()")) | ||
fun DokkaConfiguration.toJsonString(): String = this.toCompactJsonString() | ||
|
||
@Deprecated("Renamed to better distinguish between compact/pretty prints", ReplaceWith("this.toCompactJsonString()")) | ||
fun <T : ConfigurableBlock> T.toJsonString(): String = this.toCompactJsonString() | ||
|
||
/** | ||
* Serializes [DokkaConfiguration] as a machine-readable and compact JSON string. | ||
* | ||
* The returned string is not very human friendly as it will be difficult to parse by eyes due to it | ||
* being compact and in one line. If you want to show the output to a human being, see [toPrettyJsonString]. | ||
*/ | ||
fun DokkaConfiguration.toCompactJsonString(): String = serializeAsCompactJson(this) | ||
|
||
/** | ||
* Serializes [DokkaConfiguration] as a human-readable (pretty printed) JSON string. | ||
* | ||
* The returned string will have excessive line breaks and indents, which might not be | ||
* desirable when passing this value between API consumers/producers. If you want | ||
* a machine-readable and compact json string, see [toCompactJsonString]. | ||
*/ | ||
fun DokkaConfiguration.toPrettyJsonString(): String = serializeAsPrettyJson(this) | ||
|
||
/** | ||
* Serializes a [ConfigurableBlock] as a machine-readable and compact JSON string. | ||
* | ||
* The returned string is not very human friendly as it will be difficult to parse by eyes due to it | ||
* being compact and in one line. If you want to show the output to a human being, see [toPrettyJsonString]. | ||
*/ | ||
fun <T : ConfigurableBlock> T.toCompactJsonString(): String = serializeAsCompactJson(this) | ||
|
||
/** | ||
* Serializes a [ConfigurableBlock] as a human-readable (pretty printed) JSON string. | ||
* | ||
* The returned string will have excessive line breaks and indents, which might not be | ||
* desirable when passing this value between API consumers/producers. If you want | ||
* a machine-readable and compact json string, see [toCompactJsonString]. | ||
*/ | ||
fun <T : ConfigurableBlock> T.toPrettyJsonString(): String = serializeAsCompactJson(this) |
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
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,57 @@ | ||
package utilities | ||
|
||
import org.jetbrains.dokka.utilities.serializeAsCompactJson | ||
import org.jetbrains.dokka.utilities.serializeAsPrettyJson | ||
import kotlin.test.assertEquals | ||
import kotlin.test.Test | ||
|
||
class JsonTest { | ||
|
||
@Test | ||
fun `should serialize an object as compact json`() { | ||
val testObject = SimpleTestDataClass( | ||
someString = "Foo", | ||
someInt = 42, | ||
someDouble = 42.0 | ||
) | ||
|
||
val actual = serializeAsCompactJson(testObject) | ||
val expected = "{\"someString\":\"Foo\",\"someInt\":42,\"someIntWithDefaultValue\":42,\"someDouble\":42.0}" | ||
|
||
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun `should serialize an object as pretty json`() { | ||
val testObject = SimpleTestDataClass( | ||
someString = "Foo", | ||
someInt = 42, | ||
someDouble = 42.0 | ||
) | ||
|
||
val actual = serializeAsPrettyJson(testObject) | ||
|
||
val expected = """ | ||
{ | ||
"someString" : "Foo", | ||
"someInt" : 42, | ||
"someIntWithDefaultValue" : 42, | ||
"someDouble" : 42.0 | ||
}""".trimIndent().withSystemLineSeparator() | ||
|
||
assertEquals(expected, actual) | ||
} | ||
|
||
/** | ||
* If the expected output was generated on Linux, but the tests are run under Windows, | ||
* the test might fail when comparing the strings due to different separators. | ||
*/ | ||
private fun String.withSystemLineSeparator(): String = this.replace("\n", System.lineSeparator()) | ||
} | ||
|
||
data class SimpleTestDataClass( | ||
val someString: String, | ||
val someInt: Int, | ||
val someIntWithDefaultValue: Int = 42, | ||
val someDouble: Double | ||
) |
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