4267a229创建于 2024年8月2日历史提交
/*
    Copyright (c) [2023] [squallzhao]
    fountain is licensed under APACHE LICENSE, VERSION 2.0.
    You can use this software according to the terms and conditions of the APACHE LICENSE, VERSION 2.0.
    You may obtain a copy of APACHE LICENSE, VERSION 2.0 at: https://www.apache.org/licenses/LICENSE-2.0
*/
package microservice.web.codecs
import std.collection.*
import encoding.json.*
import microservice.exception.*

public class JsonCodecs <: ICodecs{
    public func fromBody(body: String): Object{
        var j = JsonValue.fromStr(body).asObject()
        return j
    }

    public func toBody(object: Object): String{
        var r = match(object) {
            case jo: JsonObject => jo.toJsonString()
            case jr: ResponseJsonObject => jr.toJsonString()
            case _ => throw UnSupportException("unsupport json codecs")
        }
        return r
    }

    public func getContentType(): String{
        return "application/json"
    }
}