根据以下文件JsonNode
:
但是,大多数mutator需要通过特定的子类(例如
ObjectNode
和ArrayNode)进行访问.
但是我仍然感到困惑,因为一些stackoverflow答案似乎可以互换使用它们.他们有什么不同的目的?
我已将Spring Boot应用程序配置为将日期序列化为ISO8601字符串:
spring:
jackson:
serialization:
write-dates-as-timestamps: false
Run Code Online (Sandbox Code Playgroud)
这就是我得到的:
"someDate": "2017-09-11T07:53:27.000+0000"
Run Code Online (Sandbox Code Playgroud)
不过我的时区是欧洲/马德里.事实上,如果我打印TimeZone.getDefault()
这是我得到的.
如何让杰克逊使用实际时区序列化这些日期时间值?GMT + 2
"someDate": "2017-09-11T09:53:27.000+0200"
Run Code Online (Sandbox Code Playgroud) 可以使用私有字段和自定义参数构造函数反序列化为不使用注释而不使用Jackson修改类的类?
我知道在使用这种组合时杰克逊有可能:1)Java 8,2)用"-parameters"选项编译,3)参数名称与JSON匹配.但是在没有所有这些限制的情况下,默认情况下也可以在GSON中使用.
例如:
public class Person {
private final String firstName;
private final String lastName;
private final int age;
public Person(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public static void main(String[] args) throws IOException {
String json = "{firstName: \"Foo\", lastName: \"Bar\", age: 30}";
System.out.println("GSON: " + deserializeGson(json)); // works fine
System.out.println("Jackson: " + deserializeJackson(json)); // error
}
public static Person deserializeJackson(String json) throws IOException {
ObjectMapper mapper = …
Run Code Online (Sandbox Code Playgroud) 问题是当将 Spring 缓存与 Redis 缓存管理器一起使用时,由于没有默认构造函数,无法反序列化 Spring Pageable 响应
使用的spring boot版本是2.1.4.RELEASE
使用序列化器的 Redis 配置类
@Bean
public RedisCacheManager redisCacheManager(LettuceConnectionFactory lettuceConnectionFactory) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().disableCachingNullValues()
.serializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json()));
redisCacheConfiguration.usePrefix();
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(lettuceConnectionFactory)
.cacheDefaults(redisCacheConfiguration).build();
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用 Spring 缓存和 Redis 作为缓存后端在 Redis 缓存中缓存 Spring REST API 页面结果响应
@GetMapping
@Cacheable("Article_Response_Page")
public Page<Article> findAll(Pageable pageable) {
return articleRepository.findAll(pageable);
}
Run Code Online (Sandbox Code Playgroud)
我可以Page<Article>
使用序列化程序在 Redis 缓存中看到以 JSON 形式缓存,RedisSerializer.json()
但在下一次调用期间,当从缓存中读取数据时,出现以下异常
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot
construct instance of `org.springframework.data.domain.PageImpl` (no
Creators, like default construct, exist): cannot deserialize from Object
value (no delegate- or …
Run Code Online (Sandbox Code Playgroud) fasterxml spring-boot spring-cache spring-data-commons jackson2
我正在使用Jackson 2.8并且需要与ISO 8601时间戳内不允许毫秒的API进行通信.
预期的格式是这样的: "2016-12-24T00:00:00Z"
我正在使用Jackson的JavaTimeModule WRITE_DATES_AS_TIMESTAMPS
设置为false
.
但这将打印毫秒.
所以我尝试使用objectMapper.setDateFormat
哪个没有改变任何东西.
我目前的解决方法是:
ObjectMapper om = new ObjectMapper();
DateTimeFormatter dtf = new DateTimeFormatterBuilder()
.appendInstant(0)
.toFormatter();
JavaTimeModule jtm = new JavaTimeModule();
jtm.addSerializer(Instant.class, new JsonSerializer<Instant>() {
@Override
public void serialize(Instant value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
gen.writeString(dtf.format(value));
}
});
om.registerModule(jtm);
Run Code Online (Sandbox Code Playgroud)
我正在覆盖默认的序列化工具Instant.class
.
有没有什么好办法使用一些配置参数来解决这个问题?
在我使用的项目中,我有一些与使用Jackson的JSON序列化相关的问题Spring Boot 2.0.0.M6
,Spring Framework 5.0.1.RELEASE
以及Jackson 2.9.2
.
我在以下位置配置了以下与Jackson相关的设置application.properties
:
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false
Run Code Online (Sandbox Code Playgroud)
序列化主要按照我的需要工作.尽管如此,我注意到杰克逊似乎已经截止了毫秒数000
.
测试1:序列化Instant,毫秒设置为000
:
Instant.parse("2017-09-14T04:28:48.000Z")
"2017-09-14T04:28:48Z"
测试2:使用设置为某些非000
值的毫秒序列化Instant :
Instant.parse("2017-09-14T04:28:48.100Z")
"2017-09-14T04:28:48.100Z"
问题:
000
?我试图描述使用@JsonIdentityInfo杰克逊2 这里.
出于测试目的,我创建了以下两个类:
public class A
{
private B b;
// constructor(s) and getter/setter omitted
}
public class B
{
private A a;
// see above
}
Run Code Online (Sandbox Code Playgroud)
当然,天真的方法很糟糕:
@Test
public void testJacksonJr() throws Exception
{
A a = new A();
B b = new B(a);
a.setB(b);
String s = JSON.std.asString(a);// throws StackOverflowError
Assert.assertEquals("{\"@id\":1,\"b\":{\"@id\":2,\"a\":1}}", s);
}
Run Code Online (Sandbox Code Playgroud)
添加@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
到A类和/或B类也不起作用.
我希望我可以序列化(后来反序列化)a
到这样的东西:(虽然不太确定JSON)
{
"b": {
"@id": 1,
"a": {
"@id": 2,
"b": 1
}
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我正在使用带有spring boot的jackson,从json转换为java对象,反之亦然,但我发现这需要花费很多时间,因为json的大小很大,就像2 MB json到相关java对象的编组一样,我们可以使用JSONITER,它表现良好吗?如何在Spring启动时用JSONITER替换JACKSON,目前在弹簧启动应用程序中是否可以使用它?
嘿,我也有问题,这是我的 Json
[
{
"aimid": "12345"
},
{
"aimid": "333674"
},
{
"aimid": [
"4568999",
"6789345"
]
}]
Run Code Online (Sandbox Code Playgroud)
这是我的 Pojo 课:-
@JsonProperty("aimid")
private String aimid;
public String getAimid() {
return aimid;
}
public void setAimid(String aimid) {
this.aimid = aimid;
}
Run Code Online (Sandbox Code Playgroud)
我想将 aimid 存储在 pojo 中。当我在我的应用程序中像上面那样写时,我收到错误。
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_ARRAY token.
Run Code Online (Sandbox Code Playgroud)
根据我的理解,由于 Array 元素,我遇到了错误,所以任何人都可以建议我如何捕获这两个东西,如果它是作为字符串来的,或者它是作为一个数组字符串来的
jackson2 ×10
java ×8
jackson ×7
json ×6
spring-boot ×5
pojo ×2
spring ×2
fasterxml ×1
java-time ×1
spring-cache ×1
spring-mvc ×1