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

com.foreveross.springboot.dubbo.utils.JsonViewConfig Maven / Gradle / Ivy

There is a newer version: 2.3.8
Show newest version
package com.foreveross.springboot.dubbo.utils;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;

import java.io.IOException;

/**
 * Created by VectorHo on 16/6/1.
 *
 * JSONView注解工具类.
 */
@Deprecated
public class JsonViewConfig {

    private final static ObjectMapper mapper = new ObjectMapper();

    private JsonViewConfig() {}

    static {
        mapper.configure(SerializationConfig.Feature.DEFAULT_VIEW_INCLUSION, false); // 设置为false,默认为true(会把没有JsonView的也输出)
    }

    public static String writerWithView(Object payload) throws IOException {
        // JsonFilterView.Output.class的field,并输出
        return JsonViewConfig.mapper.writerWithView(JsonFilterView.Output.class).writeValueAsString(payload); // 扫描User中@JsonView有
//        String json = JsonViewConfig.mapper.writerWithView(JsonFilterView.Output.class).writeValueAsString(obj); // 扫描User中@JsonView有
//        return format(json);
    }


//    /**
//     * 得到格式化json数据  退格用\t 换行用\r
//     */
//    public static String format(String jsonStr) {
//        int level = 0;
//        StringBuffer jsonForMatStr = new StringBuffer();
//        for (int i = 0; i < jsonStr.length(); i++) {
//            char c = jsonStr.charAt(i);
//            if (level > 0 && '\n' == jsonForMatStr.charAt(jsonForMatStr.length() - 1)) {
//                jsonForMatStr.append(getLevelStr(level));
//            }
//            switch (c) {
//                case '{':
//                case '[':
//                    jsonForMatStr.append(c + "\n");
//                    level++;
//                    break;
//                case ',':
//                    jsonForMatStr.append(c + "\n");
//                    break;
//                case '}':
//                case ']':
//                    jsonForMatStr.append("\n");
//                    level--;
//                    jsonForMatStr.append(getLevelStr(level));
//                    jsonForMatStr.append(c);
//                    break;
//                default:
//                    jsonForMatStr.append(c);
//                    break;
//            }
//        }
//        return jsonForMatStr.toString();
//    }
//
//
//    private static String getLevelStr(int level) {
//        StringBuffer levelStr = new StringBuffer();
//        for (int levelI = 0; levelI < level; levelI++) {
//            levelStr.append("\t");
//        }
//        return levelStr.toString();
//    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy