com.alibaba.fastjson.PropertyNamingStrategy Java Examples

The following examples show how to use com.alibaba.fastjson.PropertyNamingStrategy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JsonUtilConfig.java    From actframework with Apache License 2.0 5 votes vote down vote up
private SerializeConfig initConfig(ActContext context) {
    SerializeConfig config = SerializeConfig.getGlobalInstance();
    PropertyNamingStrategy propertyNamingStrategy = context.fastjsonPropertyNamingStrategy();
    if (null == propertyNamingStrategy) {
        return config;
    }
    config = new SerializeConfig();
    config.propertyNamingStrategy = propertyNamingStrategy;
    return config;
}
 
Example #2
Source File: TestController.java    From yue-library with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param paramJson
 * @return
 */
@GetMapping("/get")
public Result<?> get(@RequestParam JSONObject paramJson) {
	JSONObject a = MapUtils.toPropertyNamingStrategy(paramJson, PropertyNamingStrategy.SnakeCase);
	System.out.println(a);
	return ResultInfo.success(a);
}
 
Example #3
Source File: TestController.java    From yue-library with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param paramJson
 * @return
 */
@GetMapping("/get")
public Result<?> get(@RequestParam JSONObject paramJson) {
	JSONObject a = MapUtils.toPropertyNamingStrategy(paramJson, PropertyNamingStrategy.SnakeCase);
	System.out.println(a);
	return ResultInfo.success(a);
}
 
Example #4
Source File: TypeUtils.java    From uavstack with Apache License 2.0 5 votes vote down vote up
public static List<FieldInfo> computeGettersWithFieldBase(
        Class<?> clazz, //
        Map<String,String> aliasMap, //
        boolean sorted, //
        PropertyNamingStrategy propertyNamingStrategy){
    Map<String,FieldInfo> fieldInfoMap = new LinkedHashMap<String,FieldInfo>();
    for(Class<?> currentClass = clazz; currentClass != null; currentClass = currentClass.getSuperclass()){
        Field[] fields = currentClass.getDeclaredFields();
        computeFields(currentClass, aliasMap, propertyNamingStrategy, fieldInfoMap, fields);
    }
    return getFieldInfos(clazz, sorted, fieldInfoMap);
}
 
Example #5
Source File: FastJsonExample.java    From pragmatic-java-engineer with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) {
    User user = new User();
    user.setName("testUser");
    user.setGender("M");
    user.setNickName("nickTest");

    SerializeConfig config = new SerializeConfig();
    config.propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;
    String str = JSON.toJSONString(user, config);
    System.out.println(str);

    user = JSON.parseObject(str, User.class);

    JSONObject jo = JSON.parseObject("{\"name\":\"test\"}");
    String name = jo.getString("name");
    String nick = jo.getString("nickName");

    System.out.println(nick);

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("name", "test");

    String jsonStr = "{\"name\":\"testName\",\"interests\":[\"music\",\"basketball\"]," +
            "\"notes\":[{\"title\":\"note1\",\"contentLength\":200},{\"title\":\"note2\",\"contentLength\":100}]}";
    JSONObject jsonObject1 = JSON.parseObject(jsonStr);
    System.out.println(JSONPath.eval(jsonObject1, "$.interests.size()"));
    System.out.println(JSONPath.eval(jsonObject1, "$.interests[0]"));
    System.out.println(JSONPath.eval(jsonObject1, "$.notes[contentLength > 100].title"));
    System.out.println(JSONPath.eval(jsonObject1, "$.notes['title']"));
}
 
Example #6
Source File: AbstractRepository.java    From yue-library with Apache License 2.0 5 votes vote down vote up
/**
 * 插入数据-实体
 * 
 * @param paramIPO 参数IPO(POJO-IPO对象)
 * @param databaseFieldNamingStrategyEnum 数据库字段命名策略
 * @return 返回主键值
 */
public Long insert(Object paramIPO, FieldNamingStrategyEnum databaseFieldNamingStrategyEnum) {
	PropertyNamingStrategy propertyNamingStrategy = databaseFieldNamingStrategyEnum.getPropertyNamingStrategy();
	SerializeConfig serializeConfig = new SerializeConfig();
	serializeConfig.setPropertyNamingStrategy(propertyNamingStrategy);
	JSONObject paramJson = (JSONObject) JSONObject.toJSON(paramIPO, serializeConfig);
	return insert(paramJson);
}
 
Example #7
Source File: JavaBeanInfo.java    From uavstack with Apache License 2.0 5 votes vote down vote up
public static JavaBeanInfo build(Class<?> clazz //
        , Type type //
        , PropertyNamingStrategy propertyNamingStrategy //
        , boolean fieldBased //
        , boolean compatibleWithJavaBean
) {
    return build(clazz, type, propertyNamingStrategy, fieldBased, compatibleWithJavaBean, false);
}
 
Example #8
Source File: JavaBeanInfo.java    From uavstack with Apache License 2.0 4 votes vote down vote up
public static JavaBeanInfo build(Class<?> clazz, Type type, PropertyNamingStrategy propertyNamingStrategy) {
    return build(clazz, type, propertyNamingStrategy, false, TypeUtils.compatibleWithJavaBean, false);
}
 
Example #9
Source File: ActContext.java    From actframework with Apache License 2.0 4 votes vote down vote up
public PropertyNamingStrategy fastjsonPropertyNamingStrategy() {
    return fastJsonPropertyNamingStrategy;
}
 
Example #10
Source File: ActContext.java    From actframework with Apache License 2.0 4 votes vote down vote up
public CTX fastjsonPropertyNamingStrategy(PropertyNamingStrategy strategy) {
    this.fastJsonPropertyNamingStrategy = strategy;
    return me();
}
 
Example #11
Source File: Gh1130.java    From actframework with Apache License 2.0 4 votes vote down vote up
@GetAction
@FastJsonPropertyNamingStrategy(PropertyNamingStrategy.SnakeCase)
public Gh1130Model test() {
    return new Gh1130Model();
}
 
Example #12
Source File: JavaBeanInfo.java    From uavstack with Apache License 2.0 4 votes vote down vote up
private static void computeFields(Class<?> clazz, Type type, PropertyNamingStrategy propertyNamingStrategy, List<FieldInfo> fieldList, Field[] fields) {
    for (Field field : fields) { // public static fields
        int modifiers = field.getModifiers();
        if ((modifiers & Modifier.STATIC) != 0) {
            continue;
        }

        if ((modifiers & Modifier.FINAL) != 0) {
            Class<?> fieldType = field.getType();
            boolean supportReadOnly = Map.class.isAssignableFrom(fieldType)
                    || Collection.class.isAssignableFrom(fieldType)
                    || AtomicLong.class.equals(fieldType) //
                    || AtomicInteger.class.equals(fieldType) //
                    || AtomicBoolean.class.equals(fieldType);
            if (!supportReadOnly) {
                continue;
            }
        }

        boolean contains = false;
        for (FieldInfo item : fieldList) {
            if (item.name.equals(field.getName())) {
                contains = true;
                break; // 已经是 contains = true,无需继续遍历
            }
        }

        if (contains) {
            continue;
        }

        int ordinal = 0, serialzeFeatures = 0, parserFeatures = 0;
        String propertyName = field.getName();

        JSONField fieldAnnotation = field.getAnnotation(JSONField.class);

        if (fieldAnnotation != null) {
            if (!fieldAnnotation.deserialize()) {
                continue;
            }

            ordinal = fieldAnnotation.ordinal();
            serialzeFeatures = SerializerFeature.of(fieldAnnotation.serialzeFeatures());
            parserFeatures = Feature.of(fieldAnnotation.parseFeatures());

            if (fieldAnnotation.name().length() != 0) {
                propertyName = fieldAnnotation.name();
            }
        }

        if (propertyNamingStrategy != null) {
            propertyName = propertyNamingStrategy.translate(propertyName);
        }

        add(fieldList, new FieldInfo(propertyName, null, field, clazz, type, ordinal, serialzeFeatures, parserFeatures, null,
                fieldAnnotation, null));
    }
}
 
Example #13
Source File: TypeUtils.java    From uavstack with Apache License 2.0 4 votes vote down vote up
private static void computeFields(
        Class<?> clazz, //
        Map<String,String> aliasMap, //
        PropertyNamingStrategy propertyNamingStrategy, //
        Map<String,FieldInfo> fieldInfoMap, //
        Field[] fields){
    for(Field field : fields){
        if(Modifier.isStatic(field.getModifiers())){
            continue;
        }
        JSONField fieldAnnotation = field.getAnnotation(JSONField.class);
        int ordinal = 0, serialzeFeatures = 0, parserFeatures = 0;
        String propertyName = field.getName();
        String label = null;
        if(fieldAnnotation != null){
            if(!fieldAnnotation.serialize()){
                continue;
            }
            ordinal = fieldAnnotation.ordinal();
            serialzeFeatures = SerializerFeature.of(fieldAnnotation.serialzeFeatures());
            parserFeatures = Feature.of(fieldAnnotation.parseFeatures());
            if(fieldAnnotation.name().length() != 0){
                propertyName = fieldAnnotation.name();
            }
            if(fieldAnnotation.label().length() != 0){
                label = fieldAnnotation.label();
            }
        }
        if(aliasMap != null){
            propertyName = aliasMap.get(propertyName);
            if(propertyName == null){
                continue;
            }
        }
        if(propertyNamingStrategy != null){
            propertyName = propertyNamingStrategy.translate(propertyName);
        }
        if(!fieldInfoMap.containsKey(propertyName)){
            FieldInfo fieldInfo = new FieldInfo(propertyName, null, field, clazz, null, ordinal, serialzeFeatures, parserFeatures,
                    null, fieldAnnotation, label);
            fieldInfoMap.put(propertyName, fieldInfo);
        }
    }
}
 
Example #14
Source File: TypeUtils.java    From uavstack with Apache License 2.0 4 votes vote down vote up
public static List<FieldInfo> computeGetters(Class<?> clazz, Map<String,String> aliasMap, boolean sorted){
    JSONType jsonType = TypeUtils.getAnnotation(clazz,JSONType.class);
    Map<String,Field> fieldCacheMap = new HashMap<String,Field>();
    ParserConfig.parserAllFieldToCache(clazz, fieldCacheMap);
    return computeGetters(clazz, jsonType, aliasMap, fieldCacheMap, sorted, PropertyNamingStrategy.CamelCase);
}
 
Example #15
Source File: TypeUtils.java    From uavstack with Apache License 2.0 4 votes vote down vote up
public static SerializeBeanInfo buildBeanInfo(Class<?> beanType //
        , Map<String,String> aliasMap //
        , PropertyNamingStrategy propertyNamingStrategy){
    return buildBeanInfo(beanType, aliasMap, propertyNamingStrategy, false);
}
 
Example #16
Source File: MapUtils.java    From yue-library with Apache License 2.0 2 votes vote down vote up
/**
 * 属性命名策略转换-下划线命名法
 * 
 * @param param Json参数 或 POJO对象
 * @return 经过属性命名策略转换后的 JSONObject
 */
public static JSONObject toSnakeCase(Object param) {
	return toPropertyNamingStrategy(param, PropertyNamingStrategy.SnakeCase);
}
 
Example #17
Source File: MapUtils.java    From yue-library with Apache License 2.0 2 votes vote down vote up
/**
 * 属性命名策略转换-下划线命名法
 * 
 * @param param Json参数 或 POJO对象
 * @return 经过属性命名策略转换后的 JSONObject
 */
public static JSONObject toUnderlineCase(Object param) {
	return toPropertyNamingStrategy(param, PropertyNamingStrategy.SnakeCase);
}
 
Example #18
Source File: MapUtils.java    From yue-library with Apache License 2.0 2 votes vote down vote up
/**
 * 属性命名策略转换-驼峰命名法
 * 
 * @param param Json参数 或 POJO对象
 * @return 经过属性命名策略转换后的 JSONObject
 */
public static JSONObject toCamelCase(Object param) {
	return toPropertyNamingStrategy(param, PropertyNamingStrategy.CamelCase);
}
 
Example #19
Source File: ActContext.java    From actframework with Apache License 2.0 votes vote down vote up
CTX_TYPE fastjsonPropertyNamingStrategy(PropertyNamingStrategy strategy); 
Example #20
Source File: ActContext.java    From actframework with Apache License 2.0 votes vote down vote up
PropertyNamingStrategy fastjsonPropertyNamingStrategy();