Package org.nutz.mapl.impl.convert

Source Code of org.nutz.mapl.impl.convert.JsonConvertImpl

package org.nutz.mapl.impl.convert;

import java.io.IOException;
import java.io.Writer;

import org.nutz.json.JsonException;
import org.nutz.json.JsonFormat;
import org.nutz.json.impl.JsonRenderImpl;
import org.nutz.lang.Lang;
import org.nutz.lang.stream.StringWriter;
import org.nutz.mapl.MaplConvert;

/**
* 将MapList转换成Json
* @author juqkai(juqkai@gmail.com)
*/
public class JsonConvertImpl implements MaplConvert{
    private JsonFormat format = null;
   
    public JsonConvertImpl() {
        format = new JsonFormat();
    }
    public JsonConvertImpl(JsonFormat format) {
        this.format = format;
    }
   
    public Object convert(Object obj) {
        StringBuilder sb = new StringBuilder();
        Writer writer = new StringWriter(sb);
        try {
            new JsonRenderImpl(writer, format).render(obj);
            writer.flush();
            return sb.toString();
        } catch (IOException e) {
            throw Lang.wrapThrow(e, JsonException.class);
        }
    };
}
TOP

Related Classes of org.nutz.mapl.impl.convert.JsonConvertImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.