jackson工具类详解编程语言
2023-06-13 09:20:28 时间
JpaLazyIntrospector.java
package com.rf.dtd.systemsupport.reportmodel.Util; import java.lang.annotation.Annotation; import javax.persistence.Basic; import javax.persistence.ElementCollection; import javax.persistence.FetchType; import javax.persistence.ManyToMany; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.OneToOne; import org.codehaus.jackson.map.introspect.Annotated; import org.codehaus.jackson.map.introspect.JacksonAnnotationIntrospector; class JpaLazyIntrospector extends JacksonAnnotationIntrospector public boolean isHandled(Annotation ann) boolean handled = super.isHandled(ann); if (!handled) { handled = ann.toString().startsWith("@javax.persistence"); if (!handled) { handled = ann.annotationType().equals(JsonLazy.class); return handled; protected boolean _isIgnorable(Annotated a) boolean ignor = super._isIgnorable(a); if (!ignor) { ignor = isLazy(a); return ignor; private boolean isLazy(Annotated a) { boolean lazy = false; JsonLazy jsonLazy = (JsonLazy)a.getAnnotation(JsonLazy.class); if (jsonLazy != null) { return false; Basic basic = (Basic)a.getAnnotation(Basic.class); if ((basic != null) (basic.fetch() == FetchType.LAZY)) { lazy = true; ElementCollection elementCollection = (ElementCollection)a.getAnnotation(ElementCollection.class); if ((elementCollection != null) (elementCollection.fetch() != FetchType.EAGER)) { lazy = true; ManyToMany manyToMany = (ManyToMany)a.getAnnotation(ManyToMany.class); if ((manyToMany != null) (manyToMany.fetch() != FetchType.EAGER)) { lazy = true; OneToMany oneToMany = (OneToMany)a.getAnnotation(OneToMany.class); if ((oneToMany != null) (oneToMany.fetch() != FetchType.EAGER)) { lazy = true; ManyToOne manyToOne = (ManyToOne)a.getAnnotation(ManyToOne.class); if ((manyToOne != null) (manyToOne.fetch() == FetchType.LAZY)) { lazy = true; OneToOne oneToOne = (OneToOne)a.getAnnotation(OneToOne.class); if ((oneToOne != null) (oneToOne.fetch() == FetchType.LAZY)) { lazy = true; return lazy; }JsonLazy.java
package com.rf.dtd.systemsupport.reportmodel.Util; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) public @interface JsonLazy public abstract boolean value(); }JsonUtil.java ~ 9KB
package com.rf.dtd.systemsupport.reportmodel.Util; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.Writer; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.JsonParser; import org.codehaus.jackson.map.DeserializationConfig; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.SerializationConfig; import org.codehaus.jackson.node.TextNode; import org.codehaus.jackson.type.TypeReference; public abstract class JsonUtil public static void main(String[] args) Object ss = parseJson("{/"datasourceId/":12201,/"dsName/":/"bj/",/"jndiName/":/"/",/"driverClass/":/"oracle.jdbc.OracleDriver/",/"jdbcURL/":/"jdbc:oracle:thin:@10.88.107.250:1521:orcl/",/"userName/":/"devtest/",/"password/":/"devtest/"}"); String josnString = "{ /"excelName/" : /"组织结构模板.xls/", /"sheet/" : [ { /"entryCode/" : /"epdm_pcOrganizationT/", /"columns/" : [ { /"field/" : /"orgName/", /"title/" : /"机构名称/" }, { /"field/" : /"orgCode/", /"title/" : /"机构代码/" }, { /"field/" : /"canton/", /"title/" : /"行政区名称/" }, { /"field/" : /"cantonCode/", /"title/" : /"行政区代码/" }, { /"field/" : /"mailingAddress/", /"title/" : /"邮递地址/" }, { /"field/" : /"orgId/", /"title/" : /"ORG_ID/" }, { /"field/" : /"orgLevel/", /"title/" : /"级别/" }, { /"field/" : /"parentId/", /"title/" : /"上级机构名称/" } ], /"sheetName/" : /"Sheet1/", /"rows/" : [ { /"/" : /"/", /"orgId/" : /"444.0/", /"parentId/" : /"雍和宫/", /"cantonCode/" : /"7777.0/", /"orgCode/" : /"9999.0/", /"canton/" : /"石景山/", /"orgName/" : /"八角/", /"mailingAddress/" : /"102300.0/", /"orgLevel/" : /"中级/" }, { /"/" : /"/", /"orgId/" : /"444.0/", /"parentId/" : /"雍和宫/", /"cantonCode/" : /"7777.0/", /"orgCode/" : /"10000.0/", /"canton/" : /"石景山/", /"orgName/" : /"八角/", /"mailingAddress/" : /"102300.0/", /"orgLevel/" : /"中级/" }, { /"/" : /"/", /"orgId/" : /"444.0/", /"parentId/" : /"雍和宫/", /"cantonCode/" : /"7777.0/", /"orgCode/" : /"10001.0/", /"canton/" : /"石景山/", /"orgName/" : /"八角/", /"mailingAddress/" : /"102300.0/", /"orgLevel/" : /"中级/" }, { /"/" : /"/", /"orgId/" : /"444.0/", /"parentId/" : /"雍和宫/", /"cantonCode/" : /"7777.0/", /"orgCode/" : /"10002.0/", /"canton/" : /"石景山/", /"orgName/" : /"八角/", /"mailingAddress/" : /"102300.0/", /"orgLevel/" : /"中级/" } ] }, { /"entryCode/" : /"epdm_pcOrganizationT/", /"columns/" : [ { /"field/" : /"orgName/", /"title/" : /"机构名称/" }, { /"field/" : /"orgCode/", /"title/" : /"机构代码/" }, { /"field/" : /"canton/", /"title/" : /"行政区名称/" }, { /"field/" : /"cantonCode/", /"title/" : /"行政区代码/" }, { /"field/" : /"mailingAddress/", /"title/" : /"邮递地址/" }, { /"field/" : /"orgId/", /"title/" : /"ORG_ID/" }, { /"field/" : /"orgLevel/", /"title/" : /"级别/" }, { /"field/" : /"parentId/", /"title/" : /"上级机构名称/" } ], /"sheetName/" : /"Sheet2/", /"rows/" : [ { /"/" : /"/", /"orgId/" : /"444.0/", /"parentId/" : /"雍和宫/", /"cantonCode/" : /"7777.0/", /"orgCode/" : /"888.0/", /"canton/" : /"石景山/", /"orgName/" : /"八角/", /"mailingAddress/" : /"102300.0/", /"orgLevel/" : /"中级/" }, { /"/" : /"/", /"orgId/" : /"444.0/", /"parentId/" : /"雍和宫/", /"cantonCode/" : /"7777.0/", /"orgCode/" : /"889.0/", /"canton/" : /"石景山/", /"orgName/" : /"八角/", /"mailingAddress/" : /"102300.0/", /"orgLevel/" : /"中级/" }, { /"/" : /"/", /"orgId/" : /"444.0/", /"parentId/" : /"雍和宫/", /"cantonCode/" : /"7777.0/", /"orgCode/" : /"890.0/", /"canton/" : /"石景山/", /"orgName/" : /"八角/", /"mailingAddress/" : /"102300.0/", /"orgLevel/" : /"中级/" }, { /"/" : /"/", /"orgId/" : /"444.0/", /"parentId/" : /"雍和宫/", /"cantonCode/" : /"7777.0/", /"orgCode/" : /"891.0/", /"canton/" : /"石景山/", /"orgName/" : /"八角/", /"mailingAddress/" : /"102300.0/", /"orgLevel/" : /"中级/" } ] } ] }"; Object sssw = parseJson(josnString); System.out.println(sssw); System.out.println(ss); public static String getJSONString(String filePath) throws Exception{ StringBuffer data = new StringBuffer(); BufferedReader reader = null; reader = new BufferedReader(new FileReader(new File(filePath))); String temp = null; while((temp = reader.readLine()) != null){ data.append(temp); if (reader != null){ reader.close(); return data.toString(); public static ObjectMapper getObjectMapper() { ObjectMapper om = new ObjectMapper().setAnnotationIntrospector(new JpaLazyIntrospector()); om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); om.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); om.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false); om.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); om.configure(DeserializationConfig.Feature.FAIL_ON_NULL_FOR_PRIMITIVES, false); om.configure(DeserializationConfig.Feature.FAIL_ON_NUMBERS_FOR_ENUMS, false); om.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); om.configure(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true); om.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true); return om; public static String toJsonString(Object object) try return getObjectMapper().writeValueAsString(object); } catch (IOException ex) { throw new RuntimeException(ex); public static void writeJsonString(Object object, Writer writer) try getObjectMapper().writeValue(writer, object); } catch (IOException ex) { throw new RuntimeException(ex); public static Map ?, ? parseJson(String jsonString) JsonNode jn = null; try { jn = getObjectMapper().readTree(jsonString); } catch (IOException ex) { ex.printStackTrace(); return (Map ?, ? )JsonNodeToMap(jn); public static Object parseJson2MapOrList(String jsonString) JsonNode jn = null; try { jn = getObjectMapper().readTree(jsonString); } catch (IOException ex) { ex.printStackTrace(); return JsonNodeToMap(jn); public static T T parseJson(String jsonString, Class T classType) try { return getObjectMapper().readValue(jsonString, classType); } catch (Exception ex) { ex.printStackTrace(); }return null; public static T T parseJson(String jsonString, TypeReference T typeReference) try return getObjectMapper().readValue(jsonString, typeReference); } catch (Exception ex) { ex.printStackTrace(); }return null; public static T T parseJsonT(String jsonString) try return getObjectMapper().readValue(jsonString, new TypeReference Object () { } ); catch (Exception ex) { ex.printStackTrace(); }return null; public static T Map ?, ? bean2Map(Object bean) try return (Map ?, ? )getObjectMapper().convertValue(bean, Map.class); } catch (Exception ex) { ex.printStackTrace(); }return null; public static T T map2Bean(Map ?, ? map, Class T clazz) try return getObjectMapper().convertValue(map, clazz); } catch (Exception ex) { ex.printStackTrace(); }return null; public static Object JsonNodeToMap(JsonNode root) Map String, Object map = new LinkedHashMap String, Object if (root == null) { return map; if (root.isArray()) { List Object list = new ArrayList Object for (JsonNode node : root) { Object nmp = JsonNodeToMap(node); list.add(nmp); return list; }if (root.isTextual()) { try { return ((TextNode)root).asText(); } catch (Exception e) { return root.toString(); Iterator ? iter = root.getFields(); while (iter.hasNext()) { @SuppressWarnings("rawtypes") Map.Entry entry = (Map.Entry)iter.next(); String key = (String)entry.getKey(); JsonNode ele = (JsonNode)entry.getValue(); if (ele.isObject()) map.put(key, JsonNodeToMap(ele)); else if (ele.isTextual()) map.put(key, ((TextNode)ele).asText()); else if (ele.isArray()) map.put(key, JsonNodeToMap(ele)); else { map.put(key, ele.toString()); return map; }
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/10333.html
cjavaoracle相关文章
- python多线程ping和arpping扫描工具详解编程语言
- Ajax的工具类AjaxUtils,使用struts返回Json类型详解编程语言
- Java导出excel工具类详解编程语言
- Java处理XSS漏洞的工具类代码详解编程语言
- java 简单的H2数据库工具类详解编程语言
- 用SharedPreferences写的存储工具类详解编程语言
- Java字符串验证工具类详解编程语言
- 一个有用的正则表达式判定Java工具类详解编程语言
- 一些格式的Java工具类详解编程语言
- Java导出防止小数显示不全工具类详解编程语言
- Android 异步处理工具类(AsyncTask)详解编程语言
- Java制作证书的工具keytool用法总结详解编程语言
- js调试工具console.log()方法查看js代码的执行情况详解编程语言
- java代码实现访问网络外部接口并获取数据的工具类详解编程语言
- java实现excel下载和上传的工具类详解编程语言
- DateUtil日期时间相关的工具类常用方法详解编程语言
- .NET 工具生成引擎概述详解编程语言
- java socket简单写了一个即时通讯工具(代码有待优化。)详解编程语言
- java解压缩zip和rar的工具类详解编程语言
- Java开发常用Util工具类-StringUtil、CastUtil、CollectionUtil、ArrayUtil、PropsUtil详解编程语言
- 日期工具类详解编程语言
- 数字工具类详解编程语言
- python工具——Mimesis详解编程语言
- python工具——diagrams详解编程语言
- python工具——Py-Spy详解编程语言
- python工具——jieba详解编程语言
- python工具——Tesseract详解编程语言