gson

Json字符串与Object对象相互转换的几种方式

一世执手 提交于 2020-04-17 02:21:53
【推荐阅读】微服务还能火多久?>>> Json-Lib、Org.Json、Jackson、Gson、FastJson五种方式转换json类型 只列举了最省事的方式。不涉及复制情况和速度。 测试用例,一个User类,属性name,age,location。重写toString()。 public class User { private String name; private Integer age; private String location; public User() { } public User(String name) { this.name = name; } public User(String name, Integer age) { this.name = name; this.age = age; } public User(String name, Integer age, String location) { this.name = name; this.age = age; this.location = location; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer

SpringBoot大文件上传解决方案

微笑、不失礼 提交于 2020-04-17 01:04:04
【推荐阅读】微服务还能火多久?>>> 这里只写后端的代码,基本的思想就是,前端将文件分片,然后每次访问上传接口的时候,向后端传入参数:当前为第几块文件,和分片总数 下面直接贴代码吧,一些难懂的我大部分都加上注释了: 上传文件实体类: 看得出来,实体类中已经有很多我们需要的功能了,还有实用的属性。如MD5秒传的信息。 public class FileInf { public FileInf(){} public String id=""; public String pid=""; public String pidRoot=""; /** * 表示当前项是否是一个文件夹项。 */ public boolean fdTask=false; // /// 是否是文件夹中的子文件 /// </summary> public boolean fdChild=false; /** * 用户ID。与第三方系统整合使用。 */ public int uid=0; /** * 文件在本地电脑中的名称 */ public String nameLoc=""; /** * 文件在服务器中的名称。 */ public String nameSvr=""; /** * 文件在本地电脑中的完整路径。示例:D:\Soft\QQ2012.exe */ public String pathLoc=""; /**

Flink的DataSource三部曲之二:内置connector

帅比萌擦擦* 提交于 2020-04-13 18:19:32
【今日推荐】:为什么一到面试就懵逼!>>> 本文是《Flink的DataSource三部曲》系列的第二篇,上一篇 《Flink的DataSource三部曲之一:直接API》 学习了StreamExecutionEnvironment的API创建DataSource,今天要练习的是Flink内置的connector,即下图的红框位置,这些connector可以通过StreamExecutionEnvironment的addSource方法使用: 今天的实战选择Kafka作为数据源来操作,先尝试接收和处理String型的消息,再接收JSON类型的消息,将JSON反序列化成bean实例; Flink的DataSource三部曲文章链接 《Flink的DataSource三部曲之一:直接API》 《Flink的DataSource三部曲之二:内置connector》 《Flink的DataSource三部曲之三:自定义》 源码下载 如果您不想写代码,整个系列的源码可在GitHub下载到,地址和链接信息如下表所示(https://github.com/zq2599/blog_demos): 名称 链接 备注 项目主页 https://github.com/zq2599/blog_demos 该项目在GitHub上的主页 git仓库地址(https) https://github.com

JAVA WEB项目大文件上传下载代码

血红的双手。 提交于 2020-04-10 17:32:48
1,项目调研 因为需要研究下断点上传的问题。找了很久终于找到一个比较好的项目。 在GoogleCode上面,代码弄下来超级不方便,还是配置hosts才好,把代码重新上传到了github上面。 https://github.com/freewebsys/java-large-file-uploader-demo 效果: 上传中,显示进度,时间,百分比。 点击【Pause】暂停,点击【Resume】继续。 2,代码分析 原始项目: https://code.google.com/p/java-large-file-uploader/ 这个项目最后更新的时间是 2012 年,项目进行了封装使用最简单的方法实现了http的断点上传。 因为html5 里面有读取文件分割文件的类库,所以才可以支持断点上传,所以这个只能在html5 支持的浏览器上面展示。 同时,在js 和 java 同时使用 cr32 进行文件块的校验,保证数据上传正确。 代码在使用了最新的servlet 3.0 的api,使用了异步执行,监听等方法。 上传类UploadServlet @Component("javaLargeFileUploaderServlet") @WebServlet(name = "javaLargeFileUploaderServlet", urlPatterns = { "

RxJava2+Retrofit2+RxLifecycle3+OkHttp3网络请求封装(动态演示)

感情迁移 提交于 2020-04-10 11:21:11
入职公司后,公司要求组件化开发,经过讨论后我将网络请求框架单独进行了封装,不过当时框架里将常用的 util 和 ui 均放入到了共同的 Common 包下,导致里面部分代码耦合,后来为了降低耦合性又将 Common 拆分为了lib_common和lib_ui,但是 lib_ui 依赖了 lib_common,还是导致部分代码耦合,最新一期为了降低组件之间的耦合性,所以单独将 lib_common 中的网络请求单独拆分,并且我又做了新的封装和完善,总之网络框架经过3次大的改造后,使用已经非常稳定了。 使用步骤 1.在Application类中进行初始化操作 ApiConfig build = new ApiConfig.Builder() .setBaseUrl(baseUrl)//BaseUrl,这个地方加入后项目中默认使用该url .setInvalidateToken(0)//Token失效码 .setSucceedCode(200)//成功返回码 .setFilter("com.mp5a5.quit.broadcastFilter")//失效广播Filter设置 //.setDefaultTimeout(2000)//响应时间,可以不设置,默认为2000毫秒 //.setHeads(headMap)//动态添加的header,也可以在其他地方通过ApiConfig

Gson的使用和注解

試著忘記壹切 提交于 2020-04-08 12:11:08
目前比较流行的处理json数据的工具是Jackson和Fastjson,只有少数的公司使用Gson(一些公司对外部插件的安全性要求问题,如某些银行),这里对Gson的使用作个记录。 1、引进Gson jar包 <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.6</version> </dependency> 2、Gson工具类 Q: 直接Gson gson = new Gson; 也是可以使用的,那为什么要自己写Gson工具类呢? A: 写工具类是为了统一处理一些特殊情况,如null值的处理等。 import java.io.IOException; import java.math.BigDecimal; import java.math.BigInteger; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import

HibernateProxy. Forgot to register a type adapter?

给你一囗甜甜゛ 提交于 2020-04-08 11:08:06
使用Gson转换Hibernate对象遇到一个问题,当对象的Lazy加载的,就会出现上面的错误。处理方式摘抄自网上,留存一份以后自己看。 /** * This TypeAdapter unproxies Hibernate proxied objects, and serializes them * through the registered (or default) TypeAdapter of the base class. */ public class HibernateProxyTypeAdapter extends TypeAdapter<HibernateProxy> { public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() { @Override @SuppressWarnings("unchecked") public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { return (HibernateProxy.class.isAssignableFrom(type.getRawType()) ? (TypeAdapter<T>) new HibernateProxyTypeAdapter(gson)

Gson how to get serialized name

拟墨画扇 提交于 2020-04-07 18:40:43
问题 When we define a class with following format public class Field { @SerializedName("name") public String name; @SerializedName("category") public String category; } for the JsonObject content { "name" : "string", "category" : "string", } and using Gson to parse the content Field field = new GsonBuilder().create().fromJson( content, Field.class); So,my question is can we use Gson to get the @Serialized name. For in this instance I want to know what @Serialized name is used for field.name ,which

Gson how to get serialized name

天涯浪子 提交于 2020-04-07 18:39:02
问题 When we define a class with following format public class Field { @SerializedName("name") public String name; @SerializedName("category") public String category; } for the JsonObject content { "name" : "string", "category" : "string", } and using Gson to parse the content Field field = new GsonBuilder().create().fromJson( content, Field.class); So,my question is can we use Gson to get the @Serialized name. For in this instance I want to know what @Serialized name is used for field.name ,which

Fastest way to parse JSON from String when format is known

微笑、不失礼 提交于 2020-04-06 09:13:25
问题 I want to parse a String into an internal JSON object (or equivalent) in Java. The usual libraries, Gson and Jackson , are way too slow for my needs (> 100us for each String to Json parse, according to my benchmarks). I know there are slightly faster libraries, but looking at the benchmarks online, the gains available will be small (less than an order of magnitude improvement). If I know the format of the JSON in advance, is there a way I can parse it much faster? For example, I know the