Leonid Startsev | fa569a8 | 2021-08-17 22:47:24 +0300 | [diff] [blame] | 1 | // This file was automatically generated from json.md by Knit tool. Do not edit. |
| 2 | package example.exampleJson20 |
| 3 | |
| 4 | import kotlinx.serialization.* |
| 5 | import kotlinx.serialization.json.* |
| 6 | |
Clara Fok | 93e1812 | 2024-04-23 10:28:10 -0700 | [diff] [blame^] | 7 | import java.math.BigDecimal |
Leonid Startsev | fa569a8 | 2021-08-17 22:47:24 +0300 | [diff] [blame] | 8 | |
Clara Fok | 93e1812 | 2024-04-23 10:28:10 -0700 | [diff] [blame^] | 9 | val format = Json { prettyPrint = true } |
Leonid Startsev | 3ac9b89 | 2022-01-26 12:45:00 +0300 | [diff] [blame] | 10 | |
Leonid Startsev | fa569a8 | 2021-08-17 22:47:24 +0300 | [diff] [blame] | 11 | fun main() { |
Clara Fok | 93e1812 | 2024-04-23 10:28:10 -0700 | [diff] [blame^] | 12 | val pi = BigDecimal("3.141592653589793238462643383279") |
| 13 | |
| 14 | // use JsonUnquotedLiteral to encode raw JSON content |
| 15 | val piJsonLiteral = JsonUnquotedLiteral(pi.toString()) |
| 16 | |
| 17 | val piJsonDouble = JsonPrimitive(pi.toDouble()) |
| 18 | val piJsonString = JsonPrimitive(pi.toString()) |
| 19 | |
| 20 | val piObject = buildJsonObject { |
| 21 | put("pi_literal", piJsonLiteral) |
| 22 | put("pi_double", piJsonDouble) |
| 23 | put("pi_string", piJsonString) |
| 24 | } |
| 25 | |
| 26 | println(format.encodeToString(piObject)) |
Leonid Startsev | fa569a8 | 2021-08-17 22:47:24 +0300 | [diff] [blame] | 27 | } |