JsonToKotlinClass
JsonToKotlinClass copied to clipboard
Generate JSON from a Kotlin data class
Hi, is there any chance you can add the reverse operation as well? Taking a Kotlin data class and generating valid JSON from it? It would be really useful to be able to go both directions.
@snowe2010 Em!? Very amazing idea. can you point out the real case for convert Kotlin to Json?
We need to use postman or rabbit to hit our api. We already have the code built, but trying to reproduce bugs is a pain in the butt when we have very large objects that we have to create json from. For example, we have an object that looks like this:
data class UpdateMessage(
val someId: UUID,
val someNumber: String?,
val someGuid: UUID?,
val someNumberType: SomeNumberType?,
val someStatusType: SomeStatusType?,
val somePercent: BigDecimal?,
val someMonthlyAmount: Money?,
val someTotalAmount: Money?,
val someValue: Int?,
val basePercent: BigDecimal?,
val proposedAmount: Money?,
val currentPercent: BigDecimal?,
val anotherPercent: BigDecimal?,
val someType: SomeType?,
val estimatedAmount: Money?,
val address: PostalAddress?,
val indicator: Boolean?,
val usageType: UsageType?,
val somePurposeType: SomePurpose?,
val anotherType: AnotherType?,
val anotherAmount: Money?,
val valueAmount: Money?,
val originalSomeAmount: Money?,
val moreType: PropertyType?,
val someDate: LocalDate?,
val personName: String?,
val personPhoneNumber: TelephoneNumber?,
val anotherPersonName: String?,
val anotherPersonPhoneNumber: TelephoneNumber?,
val anotherPersonAddress: PostalAddress?,
val denialDate: LocalDate? = null,
val statusType: StatusType = StatusType.NOT_LOCKED,
val statusExpirationDate: LocalDate? = null,
val someOfficerEmail: String? = null,
val name: Name? = null,
val emailAddress: String? = null,
val people: List<Person>? = null
)
and I need to send this object as json over our api in order to test some bugs. I don't care what most of the values are, I can fill them in, but creating the 'template' is really difficult for an object this large.
@snowe2010 Hi,If you make the Init with value option on, It will be very easy to create a demo json by using Gson or some other libraries.
data class Demo(
val name: String = "",
val url: String = ""
)
val json = Gson().toJson(Demo())
The json will be like this:
{ "name":"" , "url":"" }
If you have large object which has much refrenced class, It will also be no problem. Hope it works for you. 😄
Yeah I know I can do that, I'd rather just have a plugin that does it though. Then I can set it to a shortcut to trigger when I need stuff. Thanks anyway.
Em..It seems that this feature is still useful and we will try to add it in the next version.But we should initially only support nested classes.Thank you very much for your advice, thank you!
Awesome! I know it's probably not easy, it would just be fantastic for generating json for our team.