Removed the JSON closure and replaced with a more Kotlin native solution to generating arbitrary JSON with Jackson.

This commit is contained in:
Clinton Alexander 2016-12-19 14:35:47 +00:00
parent fa1e7cfa15
commit 9f9fa1de1c
3 changed files with 99 additions and 140 deletions

View File

@ -1,20 +0,0 @@
package net.corda.vega.api
/**
* A small JSON DSL to create structures for APIs on the fly that mimic JSON in structure.
* Use: json { obj("a" to 100, "b" to "hello", "c" to arr(1, 2, "c")) }
*/
class JsonBuilder {
fun obj(vararg objs: Pair<String, Any>): Map<String, Any> {
return objs.toMap()
}
fun arr(vararg objs: Any): List<Any> {
return objs.toList()
}
}
fun json(body: JsonBuilder.() -> Map<String, Any>): Map<String, Any> {
val jsonWrapper = JsonBuilder()
return jsonWrapper.body()
}

View File

@ -98,11 +98,9 @@ class PortfolioApi(val rpc: CordaRPCOps) {
@Path("business-date")
@Produces(MediaType.APPLICATION_JSON)
fun getBusinessDate(): Any {
return json {
obj(
"business-date" to LocalDateTime.ofInstant(rpc.currentNodeTime(), ZoneId.systemDefault()).toLocalDate()
)
}
return mapOf(
"business-date" to LocalDateTime.ofInstant(rpc.currentNodeTime(), ZoneId.systemDefault()).toLocalDate()
)
}
/**
@ -195,12 +193,10 @@ class PortfolioApi(val rpc: CordaRPCOps) {
return withParty(partyName) { party ->
val trades = getTradesWith(party)
val portfolio = Portfolio(trades)
val summary = json {
obj(
"trades" to portfolio.trades.size,
"notional" to portfolio.getNotionalForParty(ownParty).toDouble()
)
}
val summary = mapOf(
"trades" to portfolio.trades.size,
"notional" to portfolio.getNotionalForParty(ownParty).toDouble()
)
Response.ok().entity(summary).build()
}
}

View File

@ -41,32 +41,25 @@ class PortfolioApiUtils(private val ownParty: Party) {
val completeSubgroups = subgroups.mapValues { it.value.mapValues { it.value[0].third.toDouble() }.toSortedMap() }
val yieldCurves = json {
obj(
"name" to "EUR",
"values" to completeSubgroups.get("EUR")!!.filter { !it.key.contains("Fixing") }.map {
json {
obj(
"tenor" to it.key,
"rate" to it.value
)
}
}
)
}
val fixings = json {
obj(
"name" to "EUR",
"values" to completeSubgroups.get("EUR")!!.filter { it.key.contains("Fixing") }.map {
json {
obj(
"tenor" to it.key,
"rate" to it.value
)
}
}
)
}
val yieldCurves = mapOf(
"name" to "EUR",
"values" to completeSubgroups.get("EUR")!!.filter { !it.key.contains("Fixing") }.map {
mapOf(
"tenor" to it.key,
"rate" to it.value
)
}
)
val fixings = mapOf(
"name" to "EUR",
"values" to completeSubgroups.get("EUR")!!.filter { it.key.contains("Fixing") }.map {
mapOf(
"tenor" to it.key,
"rate" to it.value
)
}
)
val processedSensitivities = valuation.totalSensivities.sensitivities.map { it.marketDataName to it.parameterMetadata.map { it.label }.zip(it.sensitivity.toList()).toMap() }.toMap()
@ -90,43 +83,35 @@ class PortfolioApiUtils(private val ownParty: Party) {
return ValuationsView(
businessDate = LocalDate.now(),
portfolio = json {
obj(
"trades" to tradeCount,
"baseCurrency" to currency,
"IRFX" to tradeCount,
"commodity" to 0,
"equity" to 0,
"credit" to 0,
"total" to tradeCount,
"agreed" to true
)
},
marketData = json {
obj(
"yieldCurves" to yieldCurves,
"fixings" to fixings,
"agreed" to true
)
},
sensitivities = json {
obj("curves" to processedSensitivities,
"currency" to valuation.currencySensitivies.amounts.toList().map {
obj(
"currency" to it.currency.code,
"amount" to it.amount
)
},
"agreed" to true
)
},
portfolio = mapOf(
"trades" to tradeCount,
"baseCurrency" to currency,
"IRFX" to tradeCount,
"commodity" to 0,
"equity" to 0,
"credit" to 0,
"total" to tradeCount,
"agreed" to true
),
marketData = mapOf(
"yieldCurves" to yieldCurves,
"fixings" to fixings,
"agreed" to true
),
sensitivities = mapOf("curves" to processedSensitivities,
"currency" to valuation.currencySensitivies.amounts.toList().map {
mapOf(
"currency" to it.currency.code,
"amount" to it.amount
)
},
"agreed" to true
),
initialMargin = initialMarginView,
confirmation = json {
obj(
"hash" to state.hash().toString(),
"agreed" to true
)
}
confirmation = mapOf(
"hash" to state.hash().toString(),
"agreed" to true
)
)
}
@ -137,52 +122,50 @@ class PortfolioApiUtils(private val ownParty: Party) {
val fixedRate = fixedLeg.calculation as FixedRateCalculation
val floatingRate = floatingLeg.calculation as IborRateCalculation
return json {
obj(
"fixedLeg" to obj(
"fixedRatePayer" to state.buyer.name,
"notional" to obj(
"token" to fixedLeg.currency.code,
"quantity" to fixedLeg.notionalSchedule.amount.initialValue
),
"paymentFrequency" to fixedLeg.paymentSchedule.paymentFrequency.toString(),
"effectiveDate" to fixedLeg.startDate.unadjusted,
"terminationDate" to fixedLeg.endDate.unadjusted,
"fixedRate" to obj(
"value" to fixedRate.rate.initialValue
),
"paymentRule" to fixedLeg.paymentSchedule.paymentRelativeTo.name,
"calendar" to arr("TODO"),
"paymentCalendar" to obj() // TODO
),
"floatingLeg" to obj(
"floatingRatePayer" to state.seller.name,
"notional" to obj(
"token" to floatingLeg.currency.code,
"quantity" to floatingLeg.notionalSchedule.amount.initialValue
),
"paymentFrequency" to floatingLeg.paymentSchedule.paymentFrequency.toString(),
"effectiveDate" to floatingLeg.startDate.unadjusted,
"terminationDate" to floatingLeg.endDate.unadjusted,
"index" to floatingRate.index.name,
"paymentRule" to floatingLeg.paymentSchedule.paymentRelativeTo,
"calendar" to arr("TODO"),
"paymentCalendar" to arr("TODO"),
"fixingCalendar" to obj() // TODO
),
"common" to obj(
"valuationDate" to trade.product.startDate.unadjusted,
"hashLegalDocs" to state.contract.legalContractReference.toString(),
"interestRate" to obj(
"name" to "TODO",
"oracle" to "TODO",
"tenor" to obj(
"name" to "TODO"
)
)
),
"ref" to trade.info.id.get().value
)
}
return mapOf(
"fixedLeg" to mapOf(
"fixedRatePayer" to state.buyer.name,
"notional" to mapOf(
"token" to fixedLeg.currency.code,
"quantity" to fixedLeg.notionalSchedule.amount.initialValue
),
"paymentFrequency" to fixedLeg.paymentSchedule.paymentFrequency.toString(),
"effectiveDate" to fixedLeg.startDate.unadjusted,
"terminationDate" to fixedLeg.endDate.unadjusted,
"fixedRate" to mapOf(
"value" to fixedRate.rate.initialValue
),
"paymentRule" to fixedLeg.paymentSchedule.paymentRelativeTo.name,
"calendar" to listOf("TODO"),
"paymentCalendar" to mapOf<String, Any>() // TODO
),
"floatingLeg" to mapOf(
"floatingRatePayer" to state.seller.name,
"notional" to mapOf(
"token" to floatingLeg.currency.code,
"quantity" to floatingLeg.notionalSchedule.amount.initialValue
),
"paymentFrequency" to floatingLeg.paymentSchedule.paymentFrequency.toString(),
"effectiveDate" to floatingLeg.startDate.unadjusted,
"terminationDate" to floatingLeg.endDate.unadjusted,
"index" to floatingRate.index.name,
"paymentRule" to floatingLeg.paymentSchedule.paymentRelativeTo,
"calendar" to listOf("TODO"),
"paymentCalendar" to listOf("TODO"),
"fixingCalendar" to mapOf<String, Any>() // TODO
),
"common" to mapOf(
"valuationDate" to trade.product.startDate.unadjusted,
"hashLegalDocs" to state.contract.legalContractReference.toString(),
"interestRate" to mapOf(
"name" to "TODO",
"oracle" to "TODO",
"tenor" to mapOf(
"name" to "TODO"
)
)
),
"ref" to trade.info.id.get().value
)
}
}