@@ -402,7 +402,7 @@ println(CustomPPrinter5(PPrinterConfig()).invoke(p))
402402// > PersonBorn(name = "Joe")
403403```
404404
405- ## Using `elementName` metadata - Kotlin Multiplatform
405+ ## Using `elementName` metadata in Kotlin Multiplatform
406406
407407If you want to filter out fields based on `elementName` in Kotlin Multiplatform inside of the PPrinter you need to override
408408the `treeifyComposite` method.
@@ -430,7 +430,7 @@ println(CustomPPrinter6<PersonBorn>(PersonBorn.serializer(), PPrinterConfig()).i
430430// > PersonBorn(name = "Joe")
431431```
432432
433- #### Sealed Hierarchies in KMP
433+ #### Sealed Hierarchies in Kotlin Multiplatform
434434
435435According to the `kotlinx- serialization` documentation, every member of a sealed hierarchy must be annotated with `@Serializable`.
436436For example, in the following hierarchy:
@@ -448,7 +448,57 @@ Every member is annotated with `@Serializable`.
448448This requirement extends to PPrint - Multiplatform as well since it relies on `kotlinx- serialization`
449449to traverse the hierarchy.
450450
451- #### How do deal with Custom Fields in KMP
451+ ## Manual Printing in Kotlin Multiplatform
452+
453+ If you want to print values with a simple and well- defined structure in Kotlin Multiplatform , you can use the the PPrinterManual class .
454+ For example:
455+ ```kotlin
456+ data class Person (val name : String , val age : Int )
457+
458+ class CustomPPrinter7 : PPrinterManual <Person >() {
459+ override fun treeify (x : Any? , elementName : String? , escapeUnicode : Boolean , showFieldNames : Boolean ): Tree =
460+ when (x) {
461+ is Person -> Tree .Apply (" Person" , listOf (Tree .KeyValue (" name" , Tree .Literal (x.name)), Tree .KeyValue (" age" , Tree .Literal (x.age))).iterator())
462+ else -> super .treeify(x, elementName, escapeUnicode, showFieldNames) // will just use Tree.Literal(x.toString())
463+ }
464+ }
465+
466+ val p = Person (" Joe" , 42 )
467+ println (CustomPPrinter7 ().invoke(p))
468+ // > Person(name = "Joe", age = 42)
469+ ```
470+
471+ Frequently , it will be useful to combine manual printers and automatic printers. For example:
472+ ```kotlin
473+ data class PersonFavorite (val name : String , val age : Int , val favoriteColor : Colors ) /// See the Sealed Hierarchies section above
474+
475+ class ColorsPrinter (config : PPrinterConfig ): PPrinter<Colors>(Colors .serializer(), config)
476+
477+ class CustomPPrinter7 (config : PPrinterConfig ): PPrinterManual<Any?>(config) {
478+ fun treeifyThis (x : Any? , elementName : String? ) =
479+ treeify(x, elementName, config.defaultEscapeUnicode, config.defaultShowFieldNames)
480+
481+ override fun treeify (x : Any? , elementName : String? , escapeUnicode : Boolean , showFieldNames : Boolean ): Tree =
482+ when (x) {
483+ is PersonFavorite ->
484+ Tree .Apply (
485+ " PersonFavorite" ,
486+ iteratorOf(treeifyThis(x.name, " name" ), treeifyThis(x.age, " age" ), treeifyThis(x.favoriteColor, " favoriteColor" )),
487+ elementName
488+ )
489+ is Colors -> ColorsPrinter (config).treeify(x, elementName, escapeUnicode, showFieldNames)
490+ else -> super .treeify(x, elementName, escapeUnicode, showFieldNames)
491+ }
492+ }
493+
494+ val joe = PersonFavorite (" Joe" , 123 , Colors .Custom (" FF0000" ))
495+ val printer = CustomPPrinter7 (PPrinterConfig ())
496+ val p = printer(joe)
497+ println (p)
498+ // > PersonFavorite("Joe", 123, Custom(value = "FF0000"))
499+ ```
500+
501+ #### How do deal with Custom Fields in Kotlin Multiplatform
452502
453503In general whenever you have a atom- property i.e. something not generic you can just mark the field as @Contextual
454504so long as there is a specific case defined for it in `treeifyWith`. However if you are using a type such as
0 commit comments