Tag: jackson

如何在不通过ObjectMapper时在JsonParser上设置ObjectCodec?

注意:这是使用Jackson 2.3.2 为了满足我的一个项目的需要,我正在编写一个自定义的JsonParser ,它记录了一个Map ,其中键是JsonPointer ,值是Integer s(指针指向的行号)。 该类名为LineRecorderJsonParser 。 它由LineRecorderJsonFactory生成,它是一个简单的委托: public final class LineRecorderJsonFactory extends JsonFactory { @Override protected JsonParser _createParser(final InputStream in, final IOContext ctxt) throws IOException, JsonParseException { final JsonParser parser = super._createParser(in, ctxt); return new LineRecorderJsonParser(parser); } @Override protected JsonParser _createParser(final Reader r, final IOContext ctxt) throws IOException, JsonParseException { final JsonParser […]

Jackson xml空元素序列化

我很难用这个。 Jackson 2.2 XML序列化器似乎以这种方式序列化空对象字段: 如何强制它序列化这样的空字段,如下所示: 非常感谢你的时间和答案。

@JsonView注释如何用于嵌套实体?

我试图为我的嵌套实体使用@JsonView注释。 更清楚的是,假设我们有2个实体,每个实体都有自己的视图类。 public class JsonViewAddress { //some view classes } public class Address { //fields annotated by JsonViewAddress’s classes and @JsonProperty } public class JsonViewPerson { //some view classes } public class Person { //some fields (yes annotated with JsonViewPerson classes and @JsonProperty) //also assume that this is annotated with any JsonViewPerson’s class. private Address […]

Java&Json:序列化时包装元素

只是想知道是否有办法做到这一点 – 我有一个类,类似于 class MyClass { private String name; private String address; private String number; } 当我使用Jackson将它序列化为Json时,我想将String变量包装在一起,所以它看起来像 { “Strings”: { “name” : “value”, “address” : “value” } } 没有将这些变量包装在MyClass中的List或Map类中……这可能吗?

使用键作为值反序列化Jackson

我有一个类似于这样的JSON结构: “teams”: { “team1Id”: “team1Name”, “team2Id”: “team2Name” } 我想将它反序列化为这些Java类: class Teams { Team team1; Team team2; } class Team { String id; String name; } 正如您所见,team1Id和team2Id(它们是JSON密钥)应该转换为Java字段的值。 此外,第一个teamId / teamName对应该归因于存储在team1中的对象,而第二个对应存储在team2字段中。 是否有任何本地JACKSON映射器可以执行此操作,还是需要为此创建自己的自定义反序列化器?

Java制作了一个单独的注释,它结合了其他注释

使用Play Framework 2.2,制作RESTfull API。 在我正在使用的模型中,我想输出(Json with Jackson)只有相关对象的Id,而不是整个对象。 我发现如何做到这一点,如下: @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = “id”) @JsonIdentityReference(alwaysAsId = true) @JsonIgnore public Object myObject; Json输出将包含JsonNode“myObjectId:1”。 例如。 IdentityInfo和IdentityReference会处理这个问题。 问题是,无论我想要什么,我都必须粘贴这3行,以及需要为某个字段提供的任何其他注释。 这变得太大了,我正在尝试创建一个自定义注释来完成所有这些事情。 这有可能,怎么样? 我可以阅读一个链接,我可以阅读它或一个例子。

如何使用Android上的Jackson将JSON数组解析为不同的对象?

我正在尝试使用Android上的Jackson解析JSON,如下所示(注意:我无法控制JSON格式 – 格式来自Yammer) “references”: [ { “type”: “user”, “id”: 12345678, “name”: “Wex” }, { “type”: “message”, “id”: 12345679, “body”: { “plain”:”A short message” } }, { “type”: “thread”, “id”: 12345670, “thread_starter_id”: 428181699 } ] 问题是references中的每个条目都是具有不同属性的不同类型的对象。 作为一个开始,我有: public static class Reference { public String type; public String id; } 我宁愿避免将所有潜在属性放在一个对象中,如: public static class Reference { public […]

如何使用jackson序列化声明性链接(泽西)

我在我的项目中使用声明性链接。 我的jackson映射器配置是 final ObjectMapper mapper = new ObjectMapper(); mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false); mapper.configure(MapperFeature.AUTO_DETECT_FIELDS, false); mapper.configure(MapperFeature.AUTO_DETECT_IS_GETTERS, false); mapper.configure(MapperFeature.AUTO_DETECT_GETTERS, false); mapper.configure(MapperFeature.AUTO_DETECT_SETTERS, false); mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); mapper.configure(SerializationFeature.INDENT_OUTPUT, true); 因为我已禁用任何类型的自动检测,注入的链接就像 @InjectLinks({ @InjectLink(rel = “bookmark”, resource = ConnectionsResource.class, style = Style.ABSOLUTE_PATH) }) @JsonProperty(“links”) Link[] links; 被序列化为一个空的JSON对象(因为“Link”中的所有字段都没有用@JsonProperty注释)。 如何在不更改全局映射器配置的情况下为字段rel和href启用链接的序列化?

从LinkedHashMap中检索值

我有一个看起来像这样的对象: { /test1: { get: { tags: [ “restcalls” ] } }, /test2: { put: { tags: [ “restcalls” ] } } } 我正在检索上面这样的对象: HashMap json = new ObjectMapper().readValue(str, HashMap.class); 但是,检索tags并用其他关键字替换它们的最佳方法是”my rest calls” 。 请注意,get,put也可以是任何其他变量名,所以它的动态但标签总是在get,put下。

序列化已经有了id的POJO(java.lang.String)

我有两个实体,我使用spring framework 4.2.5 + jacksonjson 2.7.5将其序列化并发送给客户端。 我的实体如下: @Entity @Table(name = “entrada”) @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property=”idEntrada”) public class EntradaTest implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name = “id_entrada”, unique = true, nullable = false) private String idEntrada; @Column(nullable = false, length = 5) private String codigo; private Integer recibidos; @ManyToOne @JsonIdentityReference(alwaysAsId = true) […]