package com.rc.httpcore.interceptor.formatter

import org.json.JSONObject

class OrgJsonFormatter : JSONFormatter() {

    companion object {

        private const val INDENT_SPACES = 4

        fun buildIfSupported(): OrgJsonFormatter? {
            return try {
                Class.forName("org.json.JSONObject")
                OrgJsonFormatter()
            } catch (ignore: ClassNotFoundException) {
                null
            }
        }
    }

    override fun format(source: String?): String? {
        return if (source.isNullOrEmpty())
            "" else JSONObject(source).toString(INDENT_SPACES)
    }

}