JsonMarshallingService

Use JsonMarshallingService in your CorDapps and Corda services to parse arbitrary content in and out of JSON using standard, approved mappers.

interface JsonMarshallingService : CordaServiceInjectable, CordaFlowInjectable {

    // Parse an [input] into a JSON string.
    fun formatJson(input: Any): String

    // Parse an [input] into an instance of [T].
    // [input] must be a JSON string.
    // Specify the [Class] type of [T].
    fun <T> parseJson(input: String, clazz: Class<T>): T

    // Parse an [input] into a list of instances of [T].
    // [input] must be a JSON string.
    // Specify the [Class] type of [T].
    fun <T> parseJsonList(input: String, clazz: Class<T>): List<T>
}

If you are a Kotlin developer, you also have access to some inline functions:

// Parse an [input] into an instance of [T].
// [input] must be a JSON string.
inline fun <reified T> JsonMarshallingService.parseJson(input: String): T {
    return parseJson(input, T::class.java)
}

// Parse an [input] into a list of instances of [T].
// [input] must be a JSON string.
inline fun <reified T> JsonMarshallingService.parseJsonList(input: String): List<T> {
    return parseJsonList(input, T::class.java)
}

Kotlin example

Here’s an example of how you can use jsonMarshallingService in a flow:

@StartableByRPC
    class InjectCordaServiceFlow @JsonConstructor constructor(private val params: RpcStartFlowRequestParameters) : Flow<Unit> {
        @CordaInject
        lateinit var jsonMarshallingService: JsonMarshallingService

        @Suspendable
        override fun call(): Unit {
            val paramsMap: Map<String, String> = jsonMarshallingService.parseJson(params.parametersInJson)
            ...
        }
    }

Was this page helpful?

Thanks for your feedback!

Chat with us

Chat with us on our #docs channel on slack. You can also join a lot of other slack channels there and have access to 1-on-1 communication with members of the R3 team and the online community.

Propose documentation improvements directly

Help us to improve the docs by contributing directly. It's simple - just fork this repository and raise a PR of your own - R3's Technical Writers will review it and apply the relevant suggestions.

We're sorry this page wasn't helpful. Let us know how we can make it better!

Chat with us

Chat with us on our #docs channel on slack. You can also join a lot of other slack channels there and have access to 1-on-1 communication with members of the R3 team and the online community.

Create an issue

Create a new GitHub issue in this repository - submit technical feedback, draw attention to a potential documentation bug, or share ideas for improvement and general feedback.

Propose documentation improvements directly

Help us to improve the docs by contributing directly. It's simple - just fork this repository and raise a PR of your own - R3's Technical Writers will review it and apply the relevant suggestions.