gson

Can I use Gson to serialize method-local classes and anonymous classes?

落爺英雄遲暮 提交于 2021-02-07 07:37:32
问题 Example: import com.google.gson.Gson; class GsonDemo { private static class Static {String key = "static";} private class NotStatic {String key = "not static";} void testGson() { Gson gson = new Gson(); System.out.println(gson.toJson(new Static())); // expected = actual: {"key":"static"} System.out.println(gson.toJson(new NotStatic())); // expected = actual: {"key":"not static"} class MethodLocal {String key = "method local";} System.out.println(gson.toJson(new MethodLocal())); // expected: {

GSON disableHtmlEscaping - why GSON HTML-escapes by default in the first place?

Deadly 提交于 2021-02-06 15:29:30
问题 I noticed that GSON HTML-escapes < and > characters and this can be disabled by using disableHtmlEscaping() builder configuration method. But my question is - why GSON does HTML-escaping by default? What are the risks of not HTML-escaping anything? Thanks. 回答1: Actually, the disableHtmlEscaping() method tells Gson not to escape HTML characters such as < , > , & , = , and ' . An example in which a single quote which cause trouble: rendering unescaped JSON in a <script/> tag in an HTML page

GSON disableHtmlEscaping - why GSON HTML-escapes by default in the first place?

∥☆過路亽.° 提交于 2021-02-06 15:27:29
问题 I noticed that GSON HTML-escapes < and > characters and this can be disabled by using disableHtmlEscaping() builder configuration method. But my question is - why GSON does HTML-escaping by default? What are the risks of not HTML-escaping anything? Thanks. 回答1: Actually, the disableHtmlEscaping() method tells Gson not to escape HTML characters such as < , > , & , = , and ' . An example in which a single quote which cause trouble: rendering unescaped JSON in a <script/> tag in an HTML page

Create JSON String using GSON

最后都变了- 提交于 2021-02-06 08:40:09
问题 I am having a class like following, public class Student { public int id; public String name; public int age; } Now I want to create new Student, //while create new student Student stu = new Student(); stu.age = 25; stu.name = "Guna"; System.out.println(new Gson().toJson(stu)); This gives me the following output, {"id":0,"name":"Guna","age":25} //Here I want string without id, So this is wrong So here I want String like {"name":"Guna","age":25} If I want to edit old Student //While edit old

How to have Retrofit to unescape HTML escaped symbols?

假如想象 提交于 2021-02-05 11:46:38
问题 I use Retrofit2 and GSON to deserialize incoming JSON. Here is my code in Android app: public class RestClientFactory { private static GsonBuilder gsonBuilder = GsonUtil.gsonbuilder; private static Gson gson; private static OkHttpClient.Builder httpClient; private static HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor() .setLevel(HttpLoggingInterceptor.Level.BASIC); static { gsonBuilder.setDateFormat(DateUtil.DATETIME_FORMAT); httpClient = new OkHttpClient.Builder()

Java使用OkHttps工具类调用外部接口

时光毁灭记忆、已成空白 提交于 2021-02-05 10:01:55
前言 现在公司业务已止不是传统的增删改查等简单的业务处理操作,而是对数据各种联调三方接口与其他系统进行交互等,那么就需要我们在后台java中进行外部接口的调用,本文采用OkHttps工具类对接微信接口为大家简单介绍下,java调用外部接口进行数据交互。 第一步新建接口Demo 本文采用Idea作为开发工具 依次按照 file---new---project 紧接着如下 如果这样写就会提示 因为Idea中创建项目-项目名称必须小写 接着继续勾选Jar依赖 Jar依赖选择完成之后,确认项目名称跟所在目录 更具体的步骤请查看公众号之前写的 学习栈新年献礼-SpringBoot第一弹 项目创建好之后,创建两个package,用来存放我们的工具类,与接口 先引入项目所需jar包依赖 buildscript { ext { springBootVersion = '2.1.2.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'org.springframework.boot'

Deserializing map key with Gson expects an object

∥☆過路亽.° 提交于 2021-02-05 05:32:29
问题 I get the error: Exception in thread "main" com.google.gson.JsonParseException: Expecting object found: "com.shagie.app.SimpleMap$Data@24a37368" when trying to deseralize a Map that uses non-trivial keys: package com.shagie.app; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import java.util.HashMap; public class SimpleMap { public static void main(String[] args) { Wrapper w = new Wrapper(); w.m.put(new Data("f", 1), new Data("foo", 3)); w.m.put(new Data("b", 2), new Data(

Google Gson treats every JSON local file as a string even when its not

落爺英雄遲暮 提交于 2021-02-02 09:55:39
问题 I am constantly getting Expected BEGIN_TYPE but was STRING at line 1 column 1 path $ error. I've read about that error, but I am experiencing something different. When I try to use gson.fromJson() on a JSON string I've created in my app it compiles fine. ArrayList<MyCar> cars = new ArrayList<>(); cars.add(new MyCar()); cars.add(new MyCar()); String json = gson.toJson(cars); This compiles. Type carList = new TypeToken<ArrayList<MyCar>>(){}.getType(); ArrayList<MyCar> myCars = gson.fromJson

Why GSON fails to convert Object when it's a field of another Object? [duplicate]

故事扮演 提交于 2021-01-29 15:34:48
问题 This question already has answers here : Gson doesn't serialize fields defined in subclasses (4 answers) Closed yesterday . GSON fails to convert Errorneous to JSON properly when it's inside of other Object . But it works well when it's converted as a top level object. Why, and how to fix it? Example: import com.google.gson.GsonBuilder sealed class Errorneous<R> {} data class Success<R>(val result: R) : Errorneous<R>() data class Fail<R>(val error: String) : Errorneous<R>() class Container

How to get GSON & Retrofit to serialize and map an array from a JSON response into a String?

半腔热情 提交于 2021-01-29 12:18:19
问题 I am making an API request which returns some array values. I need to serialize these array values so that I can assign them to their corresponding class attributes (which are String types). Now I know how to use GSON to serialize and deserialize lists, but with Retrofit the mapping is done automatically. This means that if my attribute is of type String, the API call returns the error "Expected a String but received an Array instead". How do I get around this so that I can receive them as