package org.nutz.json;

import java.text.SimpleDateFormat;
import java.util.regex.Pattern;

import org.nutz.castor.Castors;

/**
 * 描述Json输出的格式
 * 
 * @author zozoh(zozohtnt@gmail.com)
 * @author Wendal(wendal1985@gmail.com)
 * 
 */
public class JsonFormat {

    /**
     * 紧凑模式 -- 无换行,忽略null值
     */
    public static JsonFormat compact() {
        return new JsonFormat(true).setIgnoreNull(true);
    }

    /**
     * 全部输出模式 -- 换行,不忽略null值
     */
    public static JsonFormat full() {
        return new JsonFormat(false).setIgnoreNull(false);
    }

    /**
     * 一般模式 -- 换行,但忽略null值
     */
    public static JsonFormat nice() {
        return new JsonFormat(false).setIgnoreNull(true);
    }

    /**
     * 为了打印出来容易看，把名字去掉引号
     */
    public static JsonFormat forLook() {
        return new JsonFormat(false).setQuoteName(false).setIgnoreNull(true);
    }

    /**
     * 不换行,不忽略控制
     */
    public static JsonFormat tidy() {
        return new JsonFormat(true).setIgnoreNull(false);
    }

    public JsonFormat() {
        this(true);
    }

    public JsonFormat(boolean compact) {
        this.compact = compact;
        this.indentBy = "   ";
        this.quoteName = true;
        this.castors = Castors.me();
        this.separator = '\"';
    }

    /**
     * 缩进
     */
    private int indent;
    /**
     * 缩进时用的字符串
     */
    private String indentBy;
    /**
     * 紧凑
     */
    private boolean compact;
    private boolean quoteName;
    /**
     * 是否忽略null值
     */
    private boolean ignoreNull;
    private Pattern actived;
    private Pattern locked;
    /**
     * 用到的类型转换器
     */
    @JsonField(ignore=true)
    private Castors castors;
    /**
     * 分隔符
     */
    private char separator;
    /**
     * 是否自动将值应用Unicode编码
     */
    private boolean autoUnicode;
    /**
     * unicode编码用大写还是小写
     */
    private boolean unicodeLower;

    private SimpleDateFormat dateFormat;

    public boolean ignore(String name) {
        if (null != actived)
            return !actived.matcher(name).find();
        if (null != locked)
            return locked.matcher(name).find();
        return false;
    }

    // ===================================================================
    // getter setter

    public boolean isCompact() {
        return compact;
    }

    public JsonFormat setCompact(boolean compact) {
        this.compact = compact;
        return this;
    }

    public int getIndent() {
        return indent;
    }

    public JsonFormat setIndent(int indent) {
        this.indent = indent;
        return this;
    }

    public JsonFormat increaseIndent() {
        this.indent++;
        return this;
    }

    public JsonFormat decreaseIndent() {
        this.indent--;
        return this;
    }

    public String getIndentBy() {
        return indentBy;
    }

    public JsonFormat setIndentBy(String indentBy) {
        this.indentBy = indentBy;
        return this;
    }

    public boolean isQuoteName() {
        return quoteName;
    }

    public JsonFormat setQuoteName(boolean qn) {
        this.quoteName = qn;
        return this;
    }

    public boolean isIgnoreNull() {
        return ignoreNull;
    }

    public JsonFormat setIgnoreNull(boolean ignoreNull) {
        this.ignoreNull = ignoreNull;
        return this;
    }

    public JsonFormat setActived(String regex) {
        this.actived = Pattern.compile(regex);
        return this;
    }

    public JsonFormat setLocked(String regex) {
        this.locked = Pattern.compile(regex);
        return this;
    }

    public Castors getCastors() {
        return castors == null ? Castors.me() : castors;
    }

    public JsonFormat setCastors(Castors castors) {
        this.castors = castors;
        return this;
    }

    public JsonFormat setSeparator(char separator) {
        this.separator = separator;
        return this;
    }

    public char getSeparator() {
        return separator;
    }

    public JsonFormat setAutoUnicode(boolean autoUnicode) {
        this.autoUnicode = autoUnicode;
        return this;
    }

    public boolean isAutoUnicode() {
        return autoUnicode;
    }

    public boolean isUnicodeLower() {
        return unicodeLower;
    }

    public void setUnicodeLower(boolean unicodeLower) {
        this.unicodeLower = unicodeLower;
    }

    public JsonFormat setDateFormat(String df) {
        if (df == null) {
            this.dateFormat = null;
        } else {
            this.dateFormat = new SimpleDateFormat(df);
        }
        return this;
    }

    public void setDateFormat(SimpleDateFormat df) {
        this.dateFormat = df;
    }

    public SimpleDateFormat getDateFormat() {
        return dateFormat == null ? null : (SimpleDateFormat) dateFormat.clone();
    }
}
@@ -889,10 +889,10 @@
 ,%E4%B8%8D%E5%BF%BD%E7%95%A5
-%E6%8E%A7%E5%88%B6
+%E7%A9%BA%E5%80%BC
 %0A   

fix typo