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

com.feilong.json.transformer.UncapitalizeJavaIdentifierTransformer Maven / Gradle / Ivy

Go to download

feilong is a suite of core and expanded libraries that include utility classes, http, excel,cvs, io classes, and much much more.

There is a newer version: 4.3.0
Show newest version
/*
 * 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 com.feilong.core.Validate;
import com.feilong.lib.json.util.JavaIdentifierTransformer;
import com.feilong.lib.lang3.StringUtils;

/**
 * [json{@code -->}bean],json字符串里面的属性名字可能首字母是大写的,转成bean里面属性首字母小写的转换器.
 * 
 * 

* 比如 MemberId,但是我们的java bean里面的属性名字是标准的 驼峰命名法则,比如 memberId *

* *

示例:

* *
* *

* 场景: 从相关接口得到的json数据格式如下(注意:属性首字母是大写的): *

* *
 * {
 * "MemberNo":"11105000009",
 * "Name":null,
 * "Gender":"",
 * "Phone":"15036334567",
 * "Email":null,
 * "Birthday":""
 * }
 * 
* *

* 但是我们的类是标准的java bean,属性符合驼峰命名规则,比如: *

* *
 * public class CrmMemberInfoCommand{
 * 
 *     //** 会员编号
 *     private String memberNo;
 * 
 *     //** 姓名/昵称
 *     private String name;
 * 
 *     //** 电话号码
 *     private String phone;
 * 
 *     //** 性别:男;女
 *     private String gender;
 * 
 *     //** 生日:Format:yyyy-MM-dd
 *     private String birthday;
 * 
 *     //** 邮箱
 *     private String email;
 * 
 *     // setter getter
 * }
 * 
* *

* 此时可以使用该类,示例如下: *

* *
 * 
 * public void testToBean2(){
 *     String json = "{'MemberNo':'11105000009','Name':null,'Gender':'','Phone':'15036334567','Email':null,'Birthday':''}";
 * 
 *     JsonToJavaConfig jsonToJavaConfig = new JsonToJavaConfig();
 *     jsonToJavaConfig.setRootClass(CrmMemberInfoCommand.class);
 *     jsonToJavaConfig.setJavaIdentifierTransformer(UncapitalizeJavaIdentifierTransformer.UNCAPITALIZE);
 * 
 *     CrmMemberInfoCommand crmMemberInfoCommand = JsonUtil.toBean(json, jsonToJavaConfig);
 *     //.....
 * }
 * 
 * 
* *
* * @author feilong * @see issue 509 * @since 1.9.4 * @since 1.11.0 change package */ public class UncapitalizeJavaIdentifierTransformer extends JavaIdentifierTransformer{ /** 首字母小写 transformer 'MemberNo' {@code =>} 'memberNo'. */ public static final JavaIdentifierTransformer UNCAPITALIZE = new UncapitalizeJavaIdentifierTransformer(); //--------------------------------------------------------------- /* * (non-Javadoc) * * @see net.sf.json.util.JavaIdentifierTransformer#transformToJavaIdentifier(java.lang.String) */ @Override public String transformToJavaIdentifier(String str){ Validate.notBlank(str, "str can't be blank!"); return StringUtils.uncapitalize(str); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy