
com.feilong.context.converter.JsonStringToBeanConverter 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.context.converter;
import com.feilong.json.JsonToJavaConfig;
import com.feilong.json.JsonUtil;
/**
* json 字符串结果转换成bean.
*
*
* 场景:
* 比如doku 支付方式, 在GeneratePayCode流程中,返回的结果json字符串是
*
*
*
* 原始结果:
{
"res_pay_code":"00001014",
"res_pairing_code":"280618203138833110",
"res_response_msg":"SUCCESS",
"res_payment_code":"5511",
"res_response_code":"0000",
"res_session_id":"888"
}
*
*
* 此时有java bean
*
*
* public class DokuGeneratePayCodeResultCommand implements GeneratePayCodeResultCommand{
*
* private String resPayCode;
*
* private String resPairingCode;
*
* private String resResponseMsg;
*
* private String resPaymentCode;
*
* private String resResponseCode;
*
* private String resSessionId;
*
* //setter getter省略
* }
*
*
* 返回的json字符串属性带有下划线, 而javabean 是标准的驼峰命名,你可以使用以下配置:
*
*
{@code
}
*
*
*
* @author feilong
* @param
* the generic type
* @since 1.11.3
*/
public class JsonStringToBeanConverter extends AbstractStringToBeanConverter{
/** The json to java config. */
private JsonToJavaConfig jsonToJavaConfig;
//---------------------------------------------------------------
/**
* Instantiates a new json string to bean converter.
*/
public JsonStringToBeanConverter(){
super();
}
/**
* Instantiates a new json string to bean converter.
*
* @param rootClass
* the root class
*/
public JsonStringToBeanConverter(Class rootClass){
super();
this.jsonToJavaConfig = new JsonToJavaConfig(rootClass);
}
/**
* Instantiates a new json string to bean converter.
*
* @param jsonToJavaConfig
* the json to java config
*/
public JsonStringToBeanConverter(JsonToJavaConfig jsonToJavaConfig){
super();
this.jsonToJavaConfig = jsonToJavaConfig;
}
//---------------------------------------------------------------
/*
* (non-Javadoc)
*
* @see com.feilong.bind.AbstractBeanClassStringToBeanConverter#handler(java.lang.Class, java.lang.String)
*/
@Override
protected T handler(String inputString){
return JsonUtil.toBean(inputString, jsonToJavaConfig);
}
//---------------------------------------------------------------
@Override
protected String formatValue(String value){
return JsonUtil.toString(value);
}
//---------------------------------------------------------------
/**
* Sets the json to java config.
*
* @param jsonToJavaConfig
* the jsonToJavaConfig to set
*/
public void setJsonToJavaConfig(JsonToJavaConfig jsonToJavaConfig){
this.jsonToJavaConfig = jsonToJavaConfig;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy