Tag: jackson

找不到类型类型的(Map)密钥反序列化器

我有一个具有Map的域对象: private Map<AutoHandlingSlotKey, LinkedHashSet> autoHandling; 当我序列化对象时,我得到了这个: “autoHandling” : [ “java.util.HashMap”, { } ], 这个Map的关键是一个自定义对象: public class AutoHandlingSlotKey implements Serializable { private FunctionalArea slot; // ENUM private String returnView; // ENUM 所以,我不知道如何纠正我在反序列化对象时遇到的这个问题: org.codehaus.jackson.map.JsonMappingException: Can not find a (Map) Key deserializer for type [simple type, class com.comcast.ivr.core.domain.AutoHandlingSlotKey] 如果我无法访问要修改的域对象,有人可以帮助我理解如何纠正此问题吗?

Jackson映射器如何知道每个Json对象中的哪个字段要分配给类对象?

假设我有一个像这样的Json对象: { “name”: “Bob Dole”, “company”: “Bob Dole Industries”, “phone”: { “work”: “123-456-7890”, “home”: “234-567-8901”, “mobile”: “345-678-9012” } } 为了帮助我阅读它,我使用Jackson的Object Mapper与以下类: public class Contact { public static class Phone { private String work; private String home; private String mobile; public String getWork() { return work; } public String getHome() { return home; } public String […]

将JSON反序列化为类

服务器返回JSON的这一部分: {“condition”: { “or”: [ { “and”: [ { “operand”: “a”, “operator”: “==”, “value”: “true” }, { “not”: { “operand”: “b”, “operator”: “==”, “value”: “true” } } ] }, { “and”: [ { “operand”: “b”, “operator”: “==”, “value”: “true” }, { “not”: { “operand”: “a”, “operator”: “==”, “value”: “true” } } ] } ] }} […]

使用jackson将双向JPA实体序列化为JSON

我正在使用Jackson将我的JPA模型序列化为JSON。 我有以下课程: import com.fasterxml.jackson.annotation.*; import javax.persistence.*; import java.util.Set; @JsonInclude(JsonInclude.Include.NON_NULL) @JsonIgnoreProperties(ignoreUnknown = true) @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class) @Entity public class Parent { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; @JsonManagedReference @OneToMany(mappedBy = “parent”, cascade = CascadeType.ALL, fetch = FetchType.EAGER) private Set children; //Getters and setters } 和 import com.fasterxml.jackson.annotation.*; import javax.persistence.*; import java.util.HashSet; import […]

将Jackson ObjectMapper类设置为不使用双重科学记数法

我正在为JsonSchema使用库com.fasterxml.jackson库,我正在创建一个IntegerSchema对象,当我使用下面的代码设置整数模式的范围时: main(){ IntegerSchema intSchema = new IntegerSchema(); // setMaximum accepts Double object intSchema.setMaximum(new Double(102000000)); // setMaximum accepts Double object intSchema.setMinimum(new Double(100)); printJsonSchema(intSchema); } public void printJsonSchema(JsonSchema schema){ ObjectMapper mapper = new ObjectMapper(); try { logger.info(mapper.writeValueAsString(schema)); } catch (JsonProcessingException e) { throw new IllegalStateException(e); } } 当我使用ObjectMapper将IntegerSchema转换为字符串时得到以下响应: {“type”:”integer”,”maximum”:1.02E8,”minimum”:100.0} 最大值和最小值将转换为科学计数法。 但是我需要非科学记谱法输出如下: {“type”:”integer”,”maximum”:102000000,”minimum”:100} 我无法更改IntegerSchema类。 请建议如何在不扩展IntegerSchema类的情况下获得所需的输出? 提前致谢

jackson推迟反序列化领域

我有一个这样的课: public class DeserializedHeader int typeToClassId; Object obj 我知道obj基于typeToClassId是什么类型的对象,遗憾的是,它只在运行时才知道。 我想基于typeToClassId解析obj – 这里最好的方法是什么? 注释似乎已经出局了,基于ObjectMapper的东西似乎是正确的,但我很难弄清楚最好的方法可能是什么。 Class clazz = lookUpClassBasedOnId(typeToClassId)objectMapper.readValue(obj,clazz) 显然,这不起作用,因为obj已经被反序列化…但是我可以用两个步骤以某种方式执行此操作,也许使用convertValue?

Lazy One-To-One Spring JPA并构建“动态”JSON

我正在使用Spring Boot开发一个相对较大的项目,一般来说我对它很满意,但是我遇到了一些问题,在我看来应该不是问题。 首先,一对一的关系。 令人沮丧的是它不能正常工作(至少在我看来)。 我有两个实体,例如User和UserProfile 。 他们有一对一的关系,但大多数时候我只需要User数据,但它取而代之(无论我尝试什么,哦,男孩,我在5页Google的每篇post上尝试了世界建议)。 所以我的第一个问题是,有没有办法能够在JPA和Spring中懒惰地获取一对一的关系? (因为大多数post都超过2 – 3年)。 我遇到的另一个问题是以“动态”方式构建JSON响应。 我使用Rails做了一些事情,并且对JBuilder甚至to_json非常满意,这让我能够根据控制器和我目前的需求构建json响应。 在Spring中,我看到了以下解决方案: Jackson @JsonView (它不能完全解决我的问题,因为响应不是静态的,并且属性不能分配给多个视图(据我理解的概念)); 设置为null我不想要的响应属性(使用它,但我太丑了,看起来像一个错误的演练); 或者构建HashMap就像我在Rails上构建.json.jbuilder一样(但这会.json.jbuilder我的表现,因为有时它会有很多关系来构建json,而且这看起来像是一个丑陋的演练)。 我正在寻找某人的某些指示,有朝一日可能会遇到其中一个问题,因为它让我无法解决问题,而在我看来这不应该是这么难。 编辑1 已经尝试在@OneToOne注释上添加optional = false来解决@snovelli建议的OneToOne关系的Eager负载。 例: @OneToOne(optional=false, fetch = FetchType.LAZY) public UserProfile getUserProfile(){ … }

使用不同的对象名称映射JSON

我对JSON很陌生,而且我已经四处寻找尝试做什么但不确定我是否完全理解。 我正在进行外部API调用返回: 2015-12-21 01:22:09 INFO RiotURLSender:60 – Total json: {“USERNAME”:{“profileIconId”:984,”revisionDate”:1450655430000,”name”:”USERNAME2″,”id”:38584682,”summonerLevel”:30}} 其中’USERNAME’(和USERNAME2 – 可能与USERNAME略有不同)将根据您传递调用参数的内容而有所不同。 我使用Jackson Object Mapper映射USERNAME对象中的各个值 – 但是没有意识到我也必须映射该对象。 我一直在使用DTO中的注释,如: @JsonProperty(“profileIconId”) private Long profileIconId; 和映射使用: summonerRankedInfoDTO = mapper.readValue(jsonString, SummonerRankedInfoDTO.class); 如何使用每次都在更改的USERNAME值进行映射? 这看起来有点奇怪,这是一个不好的做法,实际变化的关键,而不是只有相同的键和不同的值? 谢谢

如何将JSON字符串转换为Java对象的列表?

这是我的JSON数组: – [ { “firstName” : “abc”, “lastName” : “xyz” }, { “firstName” : “pqr”, “lastName” : “str” } ] 我在我的String对象中有这个。 现在我想将其转换为Java对象并将其存储在Java对象的List中。 例如在Student对象中。 我使用下面的代码将其转换为Java对象列表: – ObjectMapper mapper = new ObjectMapper(); StudentList studentList = mapper.readValue(jsonString, StudentList.class); 我的列表类是: – public class StudentList { private List participantList = new ArrayList(); //getters and setters } 我的学生对象是: – class Student […]

使用Jackson进行序列化和反序列化:如何以编程方式忽略字段?

我正在使用Jackson来序列化和反序列化对象。 问题是有时候我想要显示一个字段,有时候不显示。 实际上我正在使用@JsonIgnore来避免在我不需要时打印属性。 当我需要它时,我正在禁用该物业 mapper.getSerializationConfig().disable(SerializationConfig.Feature.USE_ANNOTATIONS); 但是这也会禁用我需要的其他注释。 我怎样才能得到我需要的结果? 使用视图? 任何例子? 有点儿了解我想要的东西: class User { private String username; @JsonIgnore private String password; // getter setter } writeToDB() { mapper.getSerializationConfig().disable(SerializationConfig.Feature.USE_ANNOTATIONS); mapper.writeValueAsString(user); } 通过REST API,您可以获得没有密码的用户名(感谢JsonIgnore)