下面列出了怎么用com.alibaba.fastjson.PropertyNamingStrategy的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 插入数据-实体
*
* @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);
}
/**
*
* @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);
}
/**
*
* @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);
}
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);
}
public static JavaBeanInfo build(Class<?> clazz //
, Type type //
, PropertyNamingStrategy propertyNamingStrategy //
, boolean fieldBased //
, boolean compatibleWithJavaBean
) {
return build(clazz, type, propertyNamingStrategy, fieldBased, compatibleWithJavaBean, false);
}
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']"));
}
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;
}
public static SerializeBeanInfo buildBeanInfo(Class<?> beanType //
, Map<String,String> aliasMap //
, PropertyNamingStrategy propertyNamingStrategy){
return buildBeanInfo(beanType, aliasMap, propertyNamingStrategy, false);
}
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);
}
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);
}
}
}
public static JavaBeanInfo build(Class<?> clazz, Type type, PropertyNamingStrategy propertyNamingStrategy) {
return build(clazz, type, propertyNamingStrategy, false, TypeUtils.compatibleWithJavaBean, false);
}
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));
}
}
@GetAction
@FastJsonPropertyNamingStrategy(PropertyNamingStrategy.SnakeCase)
public Gh1130Model test() {
return new Gh1130Model();
}
public CTX fastjsonPropertyNamingStrategy(PropertyNamingStrategy strategy) {
this.fastJsonPropertyNamingStrategy = strategy;
return me();
}
public PropertyNamingStrategy fastjsonPropertyNamingStrategy() {
return fastJsonPropertyNamingStrategy;
}
/**
* 属性命名策略转换-驼峰命名法
*
* @param param Json参数 或 POJO对象
* @return 经过属性命名策略转换后的 JSONObject
*/
public static JSONObject toCamelCase(Object param) {
return toPropertyNamingStrategy(param, PropertyNamingStrategy.CamelCase);
}
/**
* 属性命名策略转换-下划线命名法
*
* @param param Json参数 或 POJO对象
* @return 经过属性命名策略转换后的 JSONObject
*/
public static JSONObject toUnderlineCase(Object param) {
return toPropertyNamingStrategy(param, PropertyNamingStrategy.SnakeCase);
}
/**
* 属性命名策略转换-下划线命名法
*
* @param param Json参数 或 POJO对象
* @return 经过属性命名策略转换后的 JSONObject
*/
public static JSONObject toSnakeCase(Object param) {
return toPropertyNamingStrategy(param, PropertyNamingStrategy.SnakeCase);
}
CTX_TYPE fastjsonPropertyNamingStrategy(PropertyNamingStrategy strategy);
PropertyNamingStrategy fastjsonPropertyNamingStrategy();