如何在使用Jackson反序列化OffsetDateTime时保留偏移量(How to preserve the offset while deserializing OffsetDateTime with Jackson)
在传入的JSON中,我有一个符合ISO8601标准的日期时间字段,包含区域偏移量。 我想保留这个偏移量,但不幸的是杰克逊默认为GMT / UTC,而反序列化这个字段(我从http://wiki.fasterxml.com/JacksonFAQDateHandling了解到)。
@RunWith(JUnit4.class) public class JacksonOffsetDateTimeTest { private ObjectMapper objectMapper; @Before public void init() { objectMapper = Jackson2ObjectMapperBuilder.json() .modules(new JavaTimeModule()) .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .build(); } @Test public void test() throws IOException { final String json = "{ \"date\": \"2000-01-01T12:00:00.000-04:00\" }"; final JsonType instance = objectMapper.readValue(json, JsonType.class); assertEquals(ZoneOffset.ofHours(-4), instance.getDate().getOffset()); } } public class JsonType { private OffsetDateTime date; // getter, setter }
我在这里得到的是:
java.lang.AssertionError: expected:<-04:00> but was:<Z>
如何使返回的OffsetDateTime包含原始偏移量?
我在杰克逊2.8.3。
In an incoming JSON, I have an ISO8601-compliant datetime field, containing zone offset. I'd like to preserve this offset, but unfortunately Jackson defaults to GMT/UTC while deserializing this field (what I understood from http://wiki.fasterxml.com/JacksonFAQDateHandling).
@RunWith(JUnit4.class) public class JacksonOffsetDateTimeTest { private ObjectMapper objectMapper; @Before public void init() { objectMapper = Jackson2ObjectMapperBuilder.json() .modules(new JavaTimeModule()) .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .build(); } @Test public void test() throws IOException { final String json = "{ \"date\": \"2000-01-01T12:00:00.000-04:00\" }"; final JsonType instance = objectMapper.readValue(json, JsonType.class); assertEquals(ZoneOffset.ofHours(-4), instance.getDate().getOffset()); } } public class JsonType { private OffsetDateTime date; // getter, setter }
What I'm getting here is:
java.lang.AssertionError: expected:<-04:00> but was:<Z>
How can I make the returned OffsetDateTime to contain the original Offset?
I'm on Jackson 2.8.3.
原文:https://stackoverflow.com/questions/40488002
最满意答案
欢迎来到SO!
你似乎在Pub / Sub和
Collection.find
之间感到困惑。你应该首先意识到2是不同的机制,它们提供不同的功能。
- Pub / Sub的确将数据从服务器发送到客户端的Minimongo数据库。 但是这些数据还没有显示出来。
Collection.find
在您的服务器上针对您的实际MongoDB使用,在您的客户端针对您当地的Minimongo DB使用。因此,在您的客户端上,一旦您正确订阅了您的服务器发布(通常在应用程序级别或模板级别/
onCreated
钩子中),您可以直接在助手(或任何其他地方)中调用Jobs.find
来获取文档,改变订阅(除非后者需要新的参数)。你的评论代码应该没有问题:
return Job.find({'_id': { '$in': companyJobs }});
一般来说,避免在助手(如
Meteor.subscribe
)中进行任何昂贵的计算,因为助手可能会执行很多次而不会注意到它。 你的Meteor.subscribe('Companies')
也应该进入模板级别(即在onCreated
钩子)。因此,不要在助手中执行
if / else
条件,只需在模板级别执行一次即可。 为了说明您需要使用另一个集合中其他文档的值,为什么不直接将公司的slug作为参数传递给您的Jobs订阅,并执行计算Server端? 或者甚至只是订阅一切,因为您目前的初始订阅似乎是这样做的。然后,您的帮手将只使用
Jobs.find
,这会查询您客户的本地minimongo数据库,从而让您的服务器不受干扰。Welcome to SO!
You seem to be confused between Pub/Sub and
Collection.find
.You should first realize that the 2 are different mechanisms, which provide different functionalities.
- Pub/Sub indeed sends data from your Server into your Client's Minimongo database. But this data is not displayed yet.
Collection.find
is used on your Server against your actual MongoDB, and on your Client against your local Minimongo DB.Therefore on your client, once you have correctly subscribed to your server publication (typically at app level or template level / in
onCreated
hook), you can directly callJobs.find
in your helpers (or anywhere else) to get your documents, without having to change the subscription (unless the latter needs new parameters).There should be nothing wrong with your commented code:
return Job.find({'_id': { '$in': companyJobs }});
In general, avoid any expensive computation in helpers (like
Meteor.subscribe
), as helpers may be executed many times without you noticing it. YourMeteor.subscribe('Companies')
should also go to template level (i.e. inonCreated
hook).Therefore, instead of doing your
if / else
conditions in your helper, simply do it once at your template level. To account for your need to use a value from another document in another collection, why not just passing directly the company's slug as an argument to your Jobs subscription, and performing the computation Server-side? Or even just subscribing to everything, as your current initial subscription seems to do.Then your helper will just use
Jobs.find
, which queries against your Client's local minimongo DB, leaving your Server unbothered.
相关问答
更多-
Meteor.Collection在0.9.1更名为Mongo.Collection 。 此更改目前是向后兼容的,但是您应该切换到对任何新项目使用Mongo.Collection 。 除了数据和安全部分中的措辞之外,它看起来像文档已经大部分更新。 Meteor.Collection was renamed Mongo.Collection in 0.9.1. The change is currently backward compatible, however you should switch to u ...
-
在Meteor模板中对Mongo集合应用多个过滤器(Applying multiple filters on Mongo collection in Meteor template)[2024-02-08]
有多种方法可以做到这一点,您可以在 Mongo运算符中使用$或$ 。 我更喜欢使用$或: const selector = { $or: [], }; if (canEnglish) { selector.$or.push({'profile.grammarskills': 'english'}); } if (canRussian) { selector.$or.push({'profile.grammarskills': 'russian'}); } return Meteor.use ... -
您可以使用正则表达式元字符包装搜索,如下所示: Collection.find({content:{"$regex": ".*" + currentSearch + ".*", "$options":"i" }}); 点(或句点)匹配任何字符但换行符,*表示“0或更多”。 因此,“搜索词之前和之后的任何字符中的0或更多”将是由此产生的正则表达式。 You could wrap the search with regex metacharacters, like this: Collection.find( ...
-
为什么Collection.find()不能在我的Meteor客户端上运行?(Why is Collection.find() not working on my Meteor client?)[2022-03-24]
这里的问题是订阅操作是异步的,因为它必须从服务器获取数据。 解决方案是将路由器呈现的组件包装在WithTracker以便在数据可用时重新运行并开始呈现给DOM 有关如何执行此操作的详细信息,请参阅以下文档: https : //guide.meteor.com/react.html#using-withTracker The problem here is that a subscribe operation is asynchronous, as it has to fetch data from the ... -
改为使用Meteor.setTimeout : Meteor.setTimeout(test2,5000) 在服务器上,使用光纤运行meteor代码来让你的同步代码写入,javascript的超时也必须让它的回调在光纤中激活,特别是如果它包含流星代码。 Use Meteor.setTimeout instead: Meteor.setTimeout(test2,5000) On the server meteor code is run using fibers to let your write sy ...
-
正如mwarren所说,问题是Sugar +时刻 - 一旦我退了几步并坚持JS日期,我的问题就消失了。 As mwarren stated, the problem was Sugar + moment - once I went a few steps back and stuck to plain JS date, my problems went away.
-
请参阅: https : //guide.meteor.com/react.html#data 您可以使用react-meteor-data官方流星包来响应数据变化 使用此包中的createContainer方法并使用它如下: import { createContainer } from 'meteor/react-meteor-data'; export default FooContainer = createContainer(() => { // Do all your reactive d ...
-
欢迎来到SO! 你似乎在Pub / Sub和Collection.find之间感到困惑。 你应该首先意识到2是不同的机制,它们提供不同的功能。 Pub / Sub的确将数据从服务器发送到客户端的Minimongo数据库。 但是这些数据还没有显示出来。 Collection.find在您的服务器上针对您的实际MongoDB使用,在您的客户端针对您当地的Minimongo DB使用。 因此,在您的客户端上,一旦您正确订阅了您的服务器发布(通常在应用程序级别或模板级别/ onCreated钩子中),您可以直接在助 ...
-
可能发生的情况是您在输入时正在提交表单。 尝试一个preventDefault() 。 可能这样的事情会起作用: Template.menubar.events({ 'keyup input.search-query': function (evt) { console.log("Keyup value: " + evt.which); if (evt.which === 13) { console.log("Got an Enter keyup"); ...
-
首先使用{{#with}}包装您的showPerson模板 {{#with person}}
Show Person Details: {{name}}
name: {{name}}//etc... {{/with}} 然后添加一个帮助程序为{{#with}}提供数据: Template.showPerson.helpers({ ...