Skip to content

Commit c77b0d7

Browse files
committed
begin docs improvement
1 parent 6417773 commit c77b0d7

File tree

13 files changed

+66
-38
lines changed

13 files changed

+66
-38
lines changed

core/src/main/kotlin/com/fractalwrench/json2kotlin/ClassTypeHolder.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.fractalwrench.json2kotlin
33
import com.squareup.kotlinpoet.*
44
import java.util.*
55

6-
6+
// TODO docs
77
internal class ClassTypeHolder(val delegate: SourceBuildDelegate, groupingStrategy: GroupingStrategy) { // TODO rename, bad ontology
88

99
internal val stack = Stack<TypeSpec>()
@@ -41,7 +41,7 @@ internal class ClassTypeHolder(val delegate: SourceBuildDelegate, groupingStrate
4141
private fun processTreeLevel(levelQueue: LinkedList<TypedJsonElement>) {
4242
val fieldValues = levelQueue.filter { it.isJsonObject }.toMutableList()
4343

44-
jsonFieldGrouper.groupCommonFieldValues(fieldValues)
44+
jsonFieldGrouper.groupJsonObjects(fieldValues)
4545
.flatMap { convertFieldsToTypes(it) }
4646
.sortedByDescending { it.name }
4747
.forEach { stack += it }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
package com.fractalwrench.json2kotlin
22

3+
/**
4+
* Holds arguments which are used to control the source code generation.
5+
*
6+
* For example, specifying a non-empty package name will set a package name in the generated source file.
7+
*/
38
data class ConversionArgs(val rootClassName: String = "Example", val packageName: String? = null)

core/src/main/kotlin/com/fractalwrench/json2kotlin/GsonBuildDelegate.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import com.google.gson.annotations.SerializedName
44
import com.squareup.kotlinpoet.AnnotationSpec
55
import com.squareup.kotlinpoet.PropertySpec
66
import com.squareup.kotlinpoet.TypeSpec
7-
7+
// TODO docs
88
class GsonBuildDelegate: SourceBuildDelegate {
99

1010
private val regex = "%".toRegex()

core/src/main/kotlin/com/fractalwrench/json2kotlin/JsonFieldGrouper.kt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
package com.fractalwrench.json2kotlin
22

3-
internal class JsonFieldGrouper(val groupingStrategy: GroupingStrategy) {
3+
/**
4+
* Determines whether multiple JsonObjects share any commonality (i.e. whether they should be
5+
* represented by the same type).
6+
*
7+
* The strategy used to determine commonality can be supplied as a constructor parameter.
8+
*/
9+
internal class JsonFieldGrouper(private val groupingStrategy: GroupingStrategy = ::defaultGroupingStrategy) {
410

5-
fun groupCommonFieldValues(allObjects: MutableList<TypedJsonElement>): List<List<TypedJsonElement>> {
11+
/**
12+
* Recursively groups a List of JSONElementstogether by any commonality (i.e. whether they should be
13+
* represented by the same type)
14+
*/
15+
fun groupJsonObjects(jsonElements: MutableList<TypedJsonElement>): List<List<TypedJsonElement>> {
616
val allTypes: MutableList<MutableList<TypedJsonElement>> = mutableListOf()
717

8-
while (allObjects.isNotEmpty()) {
18+
while (jsonElements.isNotEmpty()) {
919
val typeList = mutableListOf<TypedJsonElement>()
1020
allTypes.add(typeList)
11-
findCommonTypesForElement(allObjects.first(), allObjects, typeList)
21+
findCommonTypesForElement(jsonElements.first(), jsonElements, typeList)
1222
}
1323
return allTypes
1424
}
1525

1626
/**
17-
* Recursively finds any commonality between types in a collection of JSON objects. Commonality between
18-
* two objects is defined as them sharing one or more key value.
27+
* Recursively finds any commonality between types in a collection of JSON objects.
1928
*
2029
* Recursion is necessary to detect transitive relationships. For example, an object that only contains a
2130
* key of "foo" may be the same type as an object that only contains a key of "bar", if another object exists

core/src/main/kotlin/com/fractalwrench/json2kotlin/JsonNames.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.fractalwrench.json2kotlin
22

3-
3+
// TODO docs
44
internal fun nameForArrayField(index: Int, identifier: String): String =
55
if (index == 0) identifier else "$identifier${index + 1}"
66

core/src/main/kotlin/com/fractalwrench/json2kotlin/JsonProcessor.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import com.google.gson.JsonElement
44
import com.squareup.kotlinpoet.*
55
import java.util.HashMap
66

7-
7+
// TODO docs
88
internal class JsonProcessor(private val typeDetector: JsonTypeDetector) { // TODO crappy name
99

1010
internal val jsonElementMap = HashMap<JsonElement, TypeSpec>() // FIXME feels wrong having this exposed, return instead?
@@ -24,7 +24,7 @@ internal class JsonProcessor(private val typeDetector: JsonTypeDetector) { // TO
2424
private fun findDistinctTypesForField(commonElements: List<TypedJsonElement>, key: String): List<TypeName?> {
2525
return commonElements.map {
2626
val fieldValue = it.asJsonObject.get(key)
27-
if (fieldValue != null) typeDetector.typeForJsonField(fieldValue, key, jsonElementMap) else null
27+
if (fieldValue != null) typeDetector.typeForJsonElement(fieldValue, key, jsonElementMap) else null
2828
}.distinct()
2929
}
3030

core/src/main/kotlin/com/fractalwrench/json2kotlin/JsonReader.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class JsonReader(private val jsonParser: JsonParser) {
1616

1717
/**
1818
* Reads a JSON string using GSON.
19+
*
20+
* If the root value is an array, a wrapper element will be added to the tree.
1921
*/
2022
internal fun readJsonTree(input: InputStream, args: ConversionArgs): JsonObject {
2123

@@ -34,7 +36,10 @@ class JsonReader(private val jsonParser: JsonParser) {
3436
* Adds an object as root which wraps the array
3537
*/
3638
private fun addRootWrapper(jsonArray: JsonArray, className: String): JsonObject {
37-
return JsonObject().apply { add(nameForArrayField(0, "${className}Array").decapitalize(), jsonArray) }
39+
return JsonObject().apply {
40+
val identifier = nameForArrayField(0, "${className}Array").decapitalize()
41+
add(identifier, jsonArray)
42+
}
3843
}
3944

4045
}

core/src/main/kotlin/com/fractalwrench/json2kotlin/JsonTypeDetector.kt

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,24 @@ import com.google.gson.JsonPrimitive
77
import com.squareup.kotlinpoet.*
88
import java.util.HashSet
99

10+
/**
11+
* Deduces the TypeName for a JSON field value. For a primitive such as a String, this is a simple operation.
12+
* For complex values such as Arrays, Objects, and Nulls, this requires various considerations,
13+
* some of which are enumerated below:
14+
*
15+
* - Whether the type already exists
16+
* - Nullability of other elements
17+
* - Generic Parameters
18+
* - Object nesting
19+
*/
1020
internal class JsonTypeDetector {
11-
12-
internal fun typeForJsonField(jsonElement: JsonElement,
13-
key: String,
14-
jsonElementMap: Map<JsonElement, TypeSpec>): TypeName {
21+
22+
/**
23+
* Determines an returns the TypeName for a JSONElement
24+
*/
25+
internal fun typeForJsonElement(jsonElement: JsonElement,
26+
key: String,
27+
jsonElementMap: Map<JsonElement, TypeSpec>): TypeName {
1528
with(jsonElement) {
1629
return when {
1730
isJsonPrimitive -> typeForJsonPrimitive(asJsonPrimitive)

core/src/main/kotlin/com/fractalwrench/json2kotlin/Kotlin2JsonConverter.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@ import java.io.OutputStream
77
/**
88
* Converts JSON to Kotlin
99
*/
10-
class Kotlin2JsonConverter(val buildDelegate: SourceBuildDelegate = GsonBuildDelegate()) {
11-
12-
// TODO (general: update KDocs!)
10+
class Kotlin2JsonConverter(private val buildDelegate: SourceBuildDelegate = GsonBuildDelegate()) {
1311

1412
private val jsonReader = JsonReader(JsonParser())
1513
private val sourceFileWriter = SourceFileWriter()
1614
private val traverser = ReverseJsonTreeTraverser()
1715

1816
/**
19-
* Converts a JSON string to Kotlin, writing it to the OutputStream.
17+
* Converts an InputStream of JSON to Kotlin source code, writing the result to the OutputStream.
2018
*/
2119
fun convert(input: InputStream, output: OutputStream, args: ConversionArgs) {
2220
try {

core/src/main/kotlin/com/fractalwrench/json2kotlin/ReverseJsonTreeTraverser.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ import java.util.*
77

88

99
/**
10-
* Traverses a JSON tree from the bottom up in level order.
10+
* Traverses a JSON tree in level order (BFS starting from the top) and adds each element to a
11+
* Stack, with additional metadata.
1112
*/
1213
internal class ReverseJsonTreeTraverser {
1314

14-
fun traverse(element: JsonElement, rootName: String): Stack<TypedJsonElement> {
15+
/**
16+
* Traverses a JSON tree in level order (BFS starting from the top)
17+
*/
18+
fun traverse(element: JsonElement, rootElementName: String): Stack<TypedJsonElement> {
1519
val bfsStack: Stack<TypedJsonElement> = Stack()
16-
buildStack(bfsStack, element, rootName)
20+
buildStack(bfsStack, element, rootElementName)
1721
return bfsStack
1822
}
1923

0 commit comments

Comments
 (0)