All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetlinks.rule.engine.defaults.codec.JsonCodec Maven / Gradle / Ivy

There is a newer version: 1.2.2
Show newest version
package org.jetlinks.rule.engine.defaults.codec;

import com.alibaba.fastjson.JSON;
import io.netty.buffer.Unpooled;
import org.jetlinks.rule.engine.api.Payload;
import org.jetlinks.rule.engine.api.codec.Codec;

import javax.annotation.Nonnull;

public class JsonCodec implements Codec {

    private final Class type;

    private JsonCodec(Class type) {
        this.type = type;
    }

    public static  JsonCodec of(Class type) {
        return new JsonCodec<>(type);
    }

    @Override
    public T decode(@Nonnull Payload payload) {
        return JSON.parseObject(payload.bodyAsBytes(), type);
    }

    @Override
    public Payload encode(T body) {
        return () -> Unpooled.wrappedBuffer(JSON.toJSONBytes(body));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy