com.feilong.json.transformer.CustomJavaIdentifierTransformer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of feilong Show documentation
Show all versions of feilong Show documentation
feilong is a suite of core and expanded libraries that include utility classes, http, excel,cvs, io classes, and much much more.
/*
* Copyright (C) 2008 feilong
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.feilong.json.transformer;
import static com.feilong.core.lang.ObjectUtil.defaultIfNullOrEmpty;
import java.util.Map;
import com.feilong.core.Validate;
import com.feilong.lib.json.util.JavaIdentifierTransformer;
/**
* [json{@code -->}bean],json字符串里面的属性名字可能有部分不符合我们的java属性命名规范,此时可以基于部分属性做转换器.
*
*
* 比如 country_id,但是我们的java bean里面的属性名字是标准的驼峰命名法则,比如 countryId
*
*
* 示例:
*
*
*
*
* 场景: 从相关接口得到的json数据格式如下(注意:部分属性 名字中间是 下划线):
*
*
*
{
"country": "中国",
"country_id": "CN",
"area": "华北",
"area_id": "100000",
"region": "北京市",
"region_id": "110000",
"city": "北京市",
"city_id": "110100",
"county": "",
"county_id": "-1",
"isp": "科技网",
"isp_id": "1000114",
"ip": "210.75.225.254"
}
*
*
*
* 但是我们的类是标准的java bean,属性符合驼峰命名规则,比如:
*
*
*
* public class IpInfoEntity{
*
* //ip比如 210.75.225.254.
* private String ip;
*
* //** "country": "中国".
* private String country;
*
* //** "area": "华北".
* private String area;
*
* //** "region": "北京市".
* private String region;
*
* //** "city": "北京市".
* private String city;
*
* //** "isp": "电信".
* private String isp;
*
* //** "country_id": "86".
* private String countryId;
*
* //** "region_id": "110000".
* private String regionId;
*
* //** "area_id": "100000".
* private String areaId;
*
* //** "city_id": "110000".
* private String cityId;
*
* //** "isp_id": "100017".
* private String ispId;
*
* // setter /getter 方法省略
*
* }
*
*
*
*
* 此时可以使用该类,示例如下:
*
*
*
*
* public void testToBean2(){
* String json = "{"ip":"210.75.225.254","country":"\u4e2d\u56fd","area":"\u534e\u5317",
"region":"\u5317\u4eac\u5e02","city":"\u5317\u4eac\u5e02","county":"","isp":"\u7535\u4fe1",
"country_id":"86","area_id":"100000","region_id":"110000","city_id":"110000",
"county_id":"-1","isp_id":"100017"}";
Map{@code } map = newHashMap();
map.put("area_id", "areaId");
map.put("region_id", "regionId");
map.put("city_id", "cityId");
map.put("county_id", "countyId");
map.put("country_id", "countryId");
map.put("isp_id", "ispId");
JsonToJavaConfig jsonToJavaConfig=new JsonToJavaConfig(IpInfoEntity.class, new CustomJavaIdentifierTransformer(map));
* IpInfoEntity ipInfoEntity = JsonUtil.toBean(data, jsonToJavaConfig);
* //.....
* }
*
*
*
*
*
* @author feilong
* @see JavaIdentifierTransformer
* @see UncapitalizeJavaIdentifierTransformer
* @see issue 1
* @since 1.11.0
*/
public class CustomJavaIdentifierTransformer extends JavaIdentifierTransformer{
/** The property name convert map. */
private final Map propertyNameConvertMap;
//---------------------------------------------------------------
/**
* Instantiates a new map java identifier transformer.
*
* @param map
* 如果 map
是null,抛出 {@link NullPointerException}
* 如果 map
是empty,抛出 {@link IllegalArgumentException}
*/
public CustomJavaIdentifierTransformer(Map map){
Validate.notEmpty(map, "map can't be null/empty!");
this.propertyNameConvertMap = map;
}
//---------------------------------------------------------------
/*
* (non-Javadoc)
*
* @see net.sf.json.util.JavaIdentifierTransformer#transformToJavaIdentifier(java.lang.String)
*/
@Override
public String transformToJavaIdentifier(String propertyName){
Validate.notBlank(propertyName, "propertyName can't be blank!");
return defaultIfNullOrEmpty(propertyNameConvertMap.get(propertyName), propertyName);
}
}