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

com.feilong.json.JsonToJavaConfig 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;

import java.util.Map;

import com.feilong.lib.json.util.JavaIdentifierTransformer;
import com.feilong.lib.lang3.builder.ToStringBuilder;
import com.feilong.lib.lang3.builder.ToStringStyle;

/**
 * 将 json转成java时候的配置.
 * 
 * @author feilong
 * @since 1.9.4
 */
public class JsonToJavaConfig extends AbstractConfig{

    /**
     * The root class.
     * 
     * @see com.feilong.lib.json.JsonConfig#setRootClass(Class)
     */
    private Class                  rootClass;

    //---------------------------------------------------------------

    /**
     * 用来设置复杂自定义对象转换.
     * 
     * 

示例:

* *
* 有以下两个bean * *
     * 
     * public class MyBean{
     * 
     *     private Long id;
     * 
     *     private Person person;
     * 
     *     //setter/getter省略
     * 
     * }
     * 
     * public class Person{
     * 
     *     private String name;
     * 
     *     private Date dateAttr;
     * 
     *     //setter/getter省略
     * }
     * 
     * 现在需要把这段json 转成MyBean
     * 
     * String json = "{'mybean':{'data':[{'name':'get'}]}}";
     * 
     * 代码写法如下:
     * 
     * Map{@code >} classMap = newHashMap();
     * classMap.put("data", Person.class);
     * 
     * JsonToJavaConfig jsonToJavaConfig = new JsonToJavaConfig(MyBean.class);
     * jsonToJavaConfig.setClassMap(classMap);
     * 
     * MyBean myBean = JsonUtil.toBean(json, jsonToJavaConfig);
     * 
* *
* * @see com.feilong.lib.json.JsonConfig#setClassMap(Map) */ private Map> classMap; /** * java标识符号转换器. * *

* JSON 规范指出一个对象的key是个字符串,并且该字符串是零个或多个Unicode字符的集合,用双引号括起来,使用反斜杠转义。
* 字符表示为单个字符串。 *

* *

* 这就意味着,当你从json格式转成java的时候,你可能有一个有效的 JSON key ,但是他是个无效的java 标识符.此时你可以设置 {@link JavaIdentifierTransformer} *

* *

实现:

* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
字段实现说明
{@link com.feilong.json.transformer.SeparatorToCamelCaseJavaIdentifierTransformer * SeparatorToCamelCaseJavaIdentifierTransformer}feilong分隔符(下划线或者自定义分隔符)转驼峰
* transformer 'member_no' {@code =>} 'memberNo'.
{@link com.feilong.json.transformer.UncapitalizeJavaIdentifierTransformer UncapitalizeJavaIdentifierTransformer}feilong首字母小写
* transformer 'MemberNo' {@code =>} 'memberNo'.
{@link com.feilong.json.transformer.CustomJavaIdentifierTransformer CustomJavaIdentifierTransformer}feilong自定义转换.支持自定义map转换
{@link JavaIdentifierTransformer#NOOP}json-lib自带什么都不转换.
* (Noop transformer '@invalid' {@code =>} '@invalid')
{@link JavaIdentifierTransformer#STRICT}json-lib自带抛出JSONException if a non JavaIdentifier character is found.
* ('@invalid' {@code =>} JSONException)
{@link JavaIdentifierTransformer#CAMEL_CASE}json-lib自带将使用非Java标识符和空格字符作为词边界,新单词的第一个字符大写。
* ('camel case' {@code =>} 'camelCase')
{@link JavaIdentifierTransformer#WHITESPACE}json-lib自带删除所有空格以及不符合java规范的字符.
* ('white space' {@code =>}'whitespace')
{@link JavaIdentifierTransformer#UNDERSCORE}json-lib自带将所有空格以及不符合java属性规范的字符转成 下划线'_'.
* ('under score' {@code =>} 'under_score') *
* *
* * @see com.feilong.lib.json.JsonConfig#setJavaIdentifierTransformer(JavaIdentifierTransformer) */ private JavaIdentifierTransformer javaIdentifierTransformer; //--------------------------------------------------------------- /** * Instantiates a new json to java config. */ public JsonToJavaConfig(){ super(); } /** * Instantiates a new json to java config. * * @param rootClass * the root class */ public JsonToJavaConfig(Class rootClass){ super(); this.rootClass = rootClass; } /** * Instantiates a new json to java config. * * @param rootClass * the root class * @param javaIdentifierTransformer * java标识符号转换器. * *

* JSON 规范指出一个对象的key是个字符串,并且该字符串是零个或多个Unicode字符的集合,用双引号括起来,使用反斜杠转义。
* 字符表示为单个字符串。 *

* *

* 这就意味着,当你从json格式转成java的时候,你可能有一个有效的 JSON key ,但是他是个无效的java 标识符.此时你可以设置 {@link JavaIdentifierTransformer} *

* *

实现:

* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
字段实现说明
{@link com.feilong.json.transformer.SeparatorToCamelCaseJavaIdentifierTransformer * SeparatorToCamelCaseJavaIdentifierTransformer}feilong分隔符(下划线或者自定义分隔符)转驼峰
* transformer 'member_no' {@code =>} 'memberNo'.
{@link com.feilong.json.transformer.UncapitalizeJavaIdentifierTransformer UncapitalizeJavaIdentifierTransformer}feilong首字母小写
* transformer 'MemberNo' {@code =>} 'memberNo'.
{@link com.feilong.json.transformer.CustomJavaIdentifierTransformer CustomJavaIdentifierTransformer}feilong自定义转换.支持自定义map转换
{@link JavaIdentifierTransformer#NOOP}json-lib自带什么都不转换.
* (Noop transformer '@invalid' {@code =>} '@invalid')
{@link JavaIdentifierTransformer#STRICT}json-lib自带抛出JSONException if a non JavaIdentifier character is found.
* ('@invalid' {@code =>} JSONException)
{@link JavaIdentifierTransformer#CAMEL_CASE}json-lib自带将使用非Java标识符和空格字符作为词边界,新单词的第一个字符大写。
* ('camel case' {@code =>} 'camelCase')
{@link JavaIdentifierTransformer#WHITESPACE}json-lib自带删除所有空格以及不符合java规范的字符.
* ('white space' {@code =>}'whitespace')
{@link JavaIdentifierTransformer#UNDERSCORE}json-lib自带将所有空格以及不符合java属性规范的字符转成 下划线'_'.
* ('under score' {@code =>} 'under_score') *
* *
*/ public JsonToJavaConfig(Class rootClass, JavaIdentifierTransformer javaIdentifierTransformer){ super(); this.rootClass = rootClass; this.javaIdentifierTransformer = javaIdentifierTransformer; } /** * Instantiates a new json to java config. * * @param rootClass * the root class * @param classMap * 用来设置复杂自定义对象转换. * *

示例:

* *
* 有以下两个bean * *
     * 
     * public class MyBean{
     * 
     *     private Long id;
     * 
     *     private Person person;
     * 
     *     //setter/getter省略
     * 
     * }
     * 
     * public class Person{
     * 
     *     private String name;
     * 
     *     private Date dateAttr;
     * 
     *     //setter/getter省略
     * }
     * 
     * 现在需要把这段json 转成MyBean
     * 
     * String json = "{'mybean':{'data':[{'name':'get'}]}}";
     * 
     * 代码写法如下:
     * 
     * Map{@code >} classMap = newHashMap();
     * classMap.put("data", Person.class);
     * 
     * JsonToJavaConfig jsonToJavaConfig = new JsonToJavaConfig(MyBean.class);
     * jsonToJavaConfig.setClassMap(classMap);
     * 
     * MyBean myBean = JsonUtil.toBean(json, jsonToJavaConfig);
     *            
* *
*/ public JsonToJavaConfig(Class rootClass, Map> classMap){ super(); this.rootClass = rootClass; this.classMap = classMap; } //--------------------------------------------------------------- /** * java标识符号转换器. * *

* JSON 规范指出一个对象的key是个字符串,并且该字符串是零个或多个Unicode字符的集合,用双引号括起来,使用反斜杠转义。
* 字符表示为单个字符串。 *

* *

* 这就意味着,当你从json格式转成java的时候,你可能有一个有效的 JSON key ,但是他是个无效的java 标识符.此时你可以设置 {@link JavaIdentifierTransformer} *

* *

实现:

* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
字段实现说明
{@link com.feilong.json.transformer.SeparatorToCamelCaseJavaIdentifierTransformer * SeparatorToCamelCaseJavaIdentifierTransformer}feilong分隔符(下划线或者自定义分隔符)转驼峰
* transformer 'member_no' {@code =>} 'memberNo'.
{@link com.feilong.json.transformer.UncapitalizeJavaIdentifierTransformer UncapitalizeJavaIdentifierTransformer}feilong首字母小写
* transformer 'MemberNo' {@code =>} 'memberNo'.
{@link com.feilong.json.transformer.CustomJavaIdentifierTransformer CustomJavaIdentifierTransformer}feilong自定义转换.支持自定义map转换
{@link JavaIdentifierTransformer#NOOP}json-lib自带什么都不转换.
* (Noop transformer '@invalid' {@code =>} '@invalid')
{@link JavaIdentifierTransformer#STRICT}json-lib自带抛出JSONException if a non JavaIdentifier character is found.
* ('@invalid' {@code =>} JSONException)
{@link JavaIdentifierTransformer#CAMEL_CASE}json-lib自带将使用非Java标识符和空格字符作为词边界,新单词的第一个字符大写。
* ('camel case' {@code =>} 'camelCase')
{@link JavaIdentifierTransformer#WHITESPACE}json-lib自带删除所有空格以及不符合java规范的字符.
* ('white space' {@code =>}'whitespace')
{@link JavaIdentifierTransformer#UNDERSCORE}json-lib自带将所有空格以及不符合java属性规范的字符转成 下划线'_'.
* ('under score' {@code =>} 'under_score') *
* *
* * @return java标识符号转换器 * @see com.feilong.lib.json.JsonConfig#setJavaIdentifierTransformer(JavaIdentifierTransformer) */ public JavaIdentifierTransformer getJavaIdentifierTransformer(){ return javaIdentifierTransformer; } /** * java标识符号转换器. * *

* JSON 规范指出一个对象的key是个字符串,并且该字符串是零个或多个Unicode字符的集合,用双引号括起来,使用反斜杠转义。
* 字符表示为单个字符串。 *

* *

* 这就意味着,当你从json格式转成java的时候,你可能有一个有效的 JSON key ,但是他是个无效的java 标识符.此时你可以设置 {@link JavaIdentifierTransformer} *

* *

实现:

* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
字段实现说明
{@link com.feilong.json.transformer.SeparatorToCamelCaseJavaIdentifierTransformer * SeparatorToCamelCaseJavaIdentifierTransformer}feilong分隔符(下划线或者自定义分隔符)转驼峰
* transformer 'member_no' {@code =>} 'memberNo'.
{@link com.feilong.json.transformer.UncapitalizeJavaIdentifierTransformer UncapitalizeJavaIdentifierTransformer}feilong首字母小写
* transformer 'MemberNo' {@code =>} 'memberNo'.
{@link com.feilong.json.transformer.CustomJavaIdentifierTransformer CustomJavaIdentifierTransformer}feilong自定义转换.支持自定义map转换
{@link JavaIdentifierTransformer#NOOP}json-lib自带什么都不转换.
* (Noop transformer '@invalid' {@code =>} '@invalid')
{@link JavaIdentifierTransformer#STRICT}json-lib自带抛出JSONException if a non JavaIdentifier character is found.
* ('@invalid' {@code =>} JSONException)
{@link JavaIdentifierTransformer#CAMEL_CASE}json-lib自带将使用非Java标识符和空格字符作为词边界,新单词的第一个字符大写。
* ('camel case' {@code =>} 'camelCase')
{@link JavaIdentifierTransformer#WHITESPACE}json-lib自带删除所有空格以及不符合java规范的字符.
* ('white space' {@code =>}'whitespace')
{@link JavaIdentifierTransformer#UNDERSCORE}json-lib自带将所有空格以及不符合java属性规范的字符转成 下划线'_'.
* ('under score' {@code =>} 'under_score') *
* *
* * @param javaIdentifierTransformer * java标识符号转换器 * @see com.feilong.lib.json.JsonConfig#setJavaIdentifierTransformer(JavaIdentifierTransformer) */ public void setJavaIdentifierTransformer(JavaIdentifierTransformer javaIdentifierTransformer){ this.javaIdentifierTransformer = javaIdentifierTransformer; } /** * Gets the root class. * * @return the rootClass */ public Class getRootClass(){ return rootClass; } /** * Sets the root class. * * @param rootClass * the rootClass to set */ public void setRootClass(Class rootClass){ this.rootClass = rootClass; } /** * 用来设置复杂自定义对象转换. * *

示例:

* *
* 有以下两个bean * *
     * 
     * public class MyBean{
     * 
     *     private Long id;
     * 
     *     private Person person;
     * 
     *     //setter/getter省略
     * 
     * }
     * 
     * public class Person{
     * 
     *     private String name;
     * 
     *     private Date dateAttr;
     * 
     *     //setter/getter省略
     * }
     * 
     * 现在需要把这段json 转成MyBean
     * 
     * String json = "{'mybean':{'data':[{'name':'get'}]}}";
     * 
     * 代码写法如下:
     * 
     * Map{@code >} classMap = newHashMap();
     * classMap.put("data", Person.class);
     * 
     * JsonToJavaConfig jsonToJavaConfig = new JsonToJavaConfig(MyBean.class);
     * jsonToJavaConfig.setClassMap(classMap);
     * 
     * MyBean myBean = JsonUtil.toBean(json, jsonToJavaConfig);
     * 
* *
* * @see com.feilong.lib.json.JsonConfig#setClassMap(Map) * * @return the classMap */ public Map> getClassMap(){ return classMap; } /** * 用来设置复杂自定义对象转换. * *

示例:

* *
* 有以下两个bean * *
     * 
     * public class MyBean{
     * 
     *     private Long id;
     * 
     *     private Person person;
     * 
     *     //setter/getter省略
     * 
     * }
     * 
     * public class Person{
     * 
     *     private String name;
     * 
     *     private Date dateAttr;
     * 
     *     //setter/getter省略
     * }
     * 
     * 现在需要把这段json 转成MyBean
     * 
     * String json = "{'mybean':{'data':[{'name':'get'}]}}";
     * 
     * 代码写法如下:
     * 
     * Map{@code >} classMap = newHashMap();
     * classMap.put("data", Person.class);
     * 
     * JsonToJavaConfig jsonToJavaConfig = new JsonToJavaConfig(MyBean.class);
     * jsonToJavaConfig.setClassMap(classMap);
     * 
     * MyBean myBean = JsonUtil.toBean(json, jsonToJavaConfig);
     * 
* *
* * @see com.feilong.lib.json.JsonConfig#setClassMap(Map) * * @param classMap * the classMap to set */ public void setClassMap(Map> classMap){ this.classMap = classMap; } //--------------------------------------------------------------- /* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString(){ return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy