Gson.Json引发StackOverFlowException

时间:2019-10-14 13:36:41

标签: android gson

以前曾有人问过这个问题,但我无法弄清楚我的情况。为什么Gson.Json(上面的订单对象列表)会引发StackOverflow异常?我已经看到递归调用存在一个Gson不喜欢的问题,但我无法弄清楚。 以下是我的Order类。

    data class Order(
    var branch_id: Int,
    var orderno: String = randomOrderNumber(),
    var customer_id: String = randomCustomerId(),
    var timestamp: String = getCurrentTime(),
    var status: Int = 10,
    var type: Int = 1,
    var scheduled: Boolean = true,
    var order_detail: OrderDetail,
    var items: MutableList<OrderItem>
)

data class OrderItem(
    var cost_price: Int = 0,
    var amount: Any,
    var id: Int = 2345,
    var tax: Int = 124,
    var quantity: Int = 1,
    var selling_price: Any,
    var timestamp: String = getCurrentTime(),
    var discount: Int = 0,
    var status: Int = 1,
    var customer_id: String = randomCustomerId()
)

@SuppressLint("SimpleDateFormat")
data class OrderDetail(
    var delivery_time: String = getCurrentTime(),
    var delivery_address: String = "Mountain View, California, United States"
)

@SuppressLint("SimpleDateFormat")
private fun getCurrentTime(): String {
    val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    return formatter.format(Date())
}

private fun randomCustomerId(): String {
    val ab = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-"

    val sb = StringBuilder(36)
    for (i in 0 until 36) sb.append(ab[SecureRandom().nextInt(ab.length)])
    return sb.toString()
}

private fun randomOrderNumber(): String {
    val ab = "0123456789abcdefghijklmnopqrstuvwxyz"

    val sb = StringBuilder(6)
    for (i in 0 until 6) sb.append(ab[SecureRandom().nextInt(ab.length)])
    return "RQ-$sb"
}

0 个答案:

没有答案