Tag: gson

Gson使用递归依赖关系反序列化复杂对象

我有几个类包含彼此的递归依赖,我使用Gson GraphAdapterBuilder将它们序列化为JSON格式,并且它完美地工作。 现在我想将它们反序列化为相同的结构,但无法找到方法。 我举了一个例子: class ClassA{ public int field; public ClassB parent; public ClassA(int f, ClassB p){ field = f; parent = p; } } class ClassB{ public Vector vector = new Vector(); } … ClassB b = new ClassB(); ClassA a1 = new ClassA(1,b); ClassA a2 = new ClassA(2,b); ClassA a3 = new ClassA(3,b); […]

让Gson在错误的类型上抛出exception

我在我的项目中使用Gson将JSON-Strings反序列化为Java-Objects。 如果我发出请求,我希望服务器有明确定义的响应。 服务器将返回我期望的明确定义的响应,或者它将返回一个(也定义的)错误对象。 为了清楚起见:假设我有一个像这样的简单对象: class Dummy{ private String foo; private int bar; } 和像这样的错误对象: class ErrorHolder{ private RequestError error; } class RequestError{ private String publicMsg; private String msg; } 如果我得到像服务器响应 {“foo”:”Hello World”, “bar”:3 } 一切都按预期工作。 但如果回应是这样的话 {“error”:{“publicMsg”:”Something bad happened”, msg:”you forgot requesting some parameter”}} 我会得到一个Dummy对象,其中foo为null且bar为0! Gson文档(来自Json)明确指出: 抛出JsonSyntaxException – 如果json不是classOfT类型的对象的有效表示 所以如果我尝试解析第二个响应,我希望得到一个JsonSyntaxException: Dummy dummy = Gson.fromJson(secondResponse, Dummy.class); […]

GSON序列化非常慢

我正在尝试使用GSON序列化7000 POJO的数组,并且序列化时间非常慢。 序列化以下对象的数组大约为3-5秒: public class Case { private Long caseId; private Key orgKey; private Key workflowKey; private Key creatorKey; private Date creationTimestamp; private Date lastUpdatedTimestamp; private String name; private String stage; private String notes; } 使用自定义序列化器/反序列化器序列化关键字段: public class GsonKeySerializerDeserializer implements JsonSerializer<Key>, JsonDeserializer<Key>{ @Override public JsonElement serialize(Key src, Type typeOfSrc, JsonSerializationContext arg2) { return new JsonPrimitive(src.getString()); […]

如何使用Gson序列化和反序列化Java 8的java.time类型?

我正在使用GSON将一些对象图序列化为JSON。 这些对象图使用新的Java 8 java.time实体( ZonedDateTime , OffsetDateTime , OffsetDateTime等)。 我在这里找到了一个Joda Time序列化器库 – 是否有一个JDK java.time类的等效库? ( 这个人在使用带有java.time没有运气 – 他们的问题仍然没有答案)。

如何在Java中将自定义类的ArrayList转换为JsonArray?

我试图将自定义类的ArrayList转换为JsonArray。 以下是我的代码。 它执行正常,但一些JsonArray元素为零,即使它们是ArrayList中的数字。 我试图将它们打印出来。 就像ArrayList中的customerOne年龄是35但在JsonArray中它是0。 可能有什么不对? ArrayList customerList = CustomerDB.selectAll(); Gson gson = new Gson(); JsonElement element = gson.toJsonTree(customerList , new TypeToken<List>() {}.getType()); JsonArray jsonArray = element.getAsJsonArray();

jackson – 由于构造函数导致的JsonMappingException

尝试反序列化JSON时,我遇到以下exception 没有为类型[simple type, class MyObj$obj$Card]:找到合适的构造函数[simple type, class MyObj$obj$Card]:无法在[Source: java.io.StringReader@4344ee21; line: 1, column: 201]来自JSON对象(需要添加/启用类型信息?)实例化) [Source: java.io.StringReader@4344ee21; line: 1, column: 201] (通过参考链: MyObj[“obj”]->Obj[“cards”] ) JSON就是 { “obj”:{ “api”:”OK”, “cache”:false, “cards”:[ { “id”:1232995897, “items”:[ { “id”:”vmdSJLpnY”, “cat”:50, “rating”:0.0 } ] }, { “id”:0005897, “items”:[ { “id”:”vxdSJLpnY”, “cat”:50, “rating”:0.0 } ] } ] } } 在Obj课程中,我有以下声明 @JsonProperty(“cards”) private Card[] […]

GSON整数到特定字段的布尔值

我正在处理一个API,它发送回整数(1 = true,other = false)来表示布尔值。 我已经看到了这个问题和答案 ,但我需要能够指定应该应用哪个字段,因为有时整数实际上是一个整数。 编辑:传入的JSON可能看起来像这样(也可能是String而不是int等…): { “regular_int”: 1234, “int_that_should_be_a_boolean”: 1 } 我需要一种方法来指定int_that_should_be_a_boolean应该被解析为布尔值,而regular_int应该被解析为整数。

GSON没有为我的TypeAdapter调用接口类型

GSON似乎在做某种技巧,它查看我的JavaBeans的内部字段,而不是使用可公开访问的属性信息。 不幸的是,这不会为我们飞行,因为我们神奇创造的豆子充满了私人田地,我不希望它存储。 @Test public void testJson() throws Exception { Player player = new MagicPlayer(); //BeanUtils.createDefault(Player.class); player.setName(“Alice”); Gson gson = new GsonBuilder() .registerTypeAdapter(Player.class, new PlayerTypeAdapter()) .create(); System.out.println(gson.toJson(bean)); } private static class PlayerTypeAdapter implements JsonSerializer { @Override public JsonElement serialize(Player player, Type type, JsonSerializationContext context) { throw new RuntimeException(“I got called, woohoo”); } } public static interface […]

GSON将布尔值序列化为0或1

所有, 我正在尝试执行以下操作: public class SomClass { public boolean x; public int y; public String z; } SomClass s = new SomClass(); sx = true; sy = 10; sz = “ZZZ”; Gson gson = new Gson(); String retVal = gson.toJson(s); return retVal; 所以这个小片段会产生: {“x”:true,”y”:10,”z”:”ZZZ”} 但我需要它生产的是: {“x”:0, “y”:10,”z”:”ZZZ”} 有人可以给我一些选择吗? 我不希望将我的布尔值重写为整数,因为这将导致现有代码存在若干问题(非显而易见,难以阅读,难以执行等)

优化Gson反序列化

优化反序列化的最佳方法是什么? 我目前正在使用标准的Gson.toJson和Gson.fromJson方法对一些复杂对象进行序列化和反序列化,我希望尽可能减少反序列化时间。 如果重要的话,我的对象中最复杂的包含43个变量。