Grails json string converter with Dates - grails

I'm POSTing json data to a Grails controller which I then parse using JSON.parse.
It all works fine except for date fields. I don't believe there is an explicit syntax in json to represent a Date but I've tried a number of formats with no luck. Can anyone tell me what format I should use so that the grails JSON parser can create a Date object.

There isn't a specific format, but you can define your own. For example, these guys here are adding a '#' to the beginning and the end of the string.
According to Grails docs here, you can define:
grails.converters.json.date (String) - Configure how Date values
are serialized to JSON
"default" - String representation according to the JSON specification
"javascript" - new Date(...)
Update: It appears that there is no mapping to Java Date objects. If you know the fields that are dates, you can parse them into Dates

Related

thymeleaf 3 and th:field syntaxe

I have a form template using thymeleaf 3 and I want to use th:field="*{dueDate}" syntax to bind my model property. I try to do the same as th:value="${#strings.substring(model.dueDate, 0, 19)}" but all my attempts fail.
It is possible to do this into a th:field instead of a th:value ?
EDIT:
I have to substring my date from java.sql.timestamp because it's a little bit different as the format used by my datepicker (2020-01-30 13:00:00.0 instead of 2020-01-30 13:00:00). I understand it isn't a good idea to substring the date string but It is difficult to have a entity method to bind the value as well with th:field. Probably I will use th:value in this case to give me the possibility to create that entity method providing the good format.

How to store byte array field in Elasticsearch using spring data elasticsearch without indexing?

I need to store byte array field in Elasticsearch without indexing it. I'm using spring data elasticsearch module. What is the right way? Thanks
The correct fieldtype in Elasticsearch for this would be the binary field type. Alas this is currently not available in Spring Data Elasticsearch, I just created a Jira issue for this.
Even if the binary field type were implemented, you still would need to base64 encode the binary data, so that in Elasticsearch it would be stored in a text representation.
Until this binary field type is implemented, you might try to use a field definition like:
#Field(type = FieldType.Keyword, index = false)
private String base64Data;
Like with the binary field type you have to encode your data as base64 String and decode it when it's coming back from the search. Even better wer if you could add the doc_values=false argument to #Field annotation, to have support for this, there is currently a PullRequest open, but which is not yet ready to be merged; not sure, if this will make it into the 3.2.0 release.
Edit March 2020:
in version 4.0.0, you can user the FieldType.Binary type together with a byte[] and this will be converted to a base64 encoded string (and back again).

Parsing DoneDone API time

DoneDone's API returns dates as strings that look like "/Date(1447595230347)/". Trying to figure out how and why it's in this format and how I can parse it in JS. Any ideas?
https://www.getdonedone.com/api/

XML Schema - Allow Invalid Dates

Hi I am using biztalk's FlatFile parser (using XML schema) to part a CSV file. The CSV File sometimes contains invalid date - 1/1/1900. Currently the schema validation for the flat file fails because of invalid date. Is there any setting that I can use to allow the date to be used?
I dont want to read the date as string. I might be forced to if there is no other way.
You could change it to a valid XML date time (e.g., 1900-01-00:00:00Z) using a custom pipeline component (see examples here). Or you can just treat it as a string in your schema and deal with converting it later in a map, in an orchestration, or in a downstream system.
Here is a a C# snippet that you could put into a scripting functoid inside a BizTalk map to convert the string to an xs:dateTime, though you'll need to do some more work if you want to handle the potential for bad input data:
public string ConvertStringDateToDateTime(string param1)
{
return DateTime.Parse(inputDate).ToString("s",System.Globalization.DateTimeFormatInfo.InvariantInfo);
}
Also see this blog post if you're looking to do that in multiple places in a single map.

How to format a date to JSON in Rails?

I need to produce a date in Rails which looks like this:
/Date(1294268400000)/
I have tried various combinations of DateTime, to_i, to_json but never managed to get the /Date()/ thing.
Do I have to simply get my date in ms and then wrap the /Date(and )/ manually, or is there a built in method?
What about (ruby 1.9.x)?:
Time.now.strftime("/Date(%s%L)/")
=> "/Date(1335280866211)/"
You should try
new Date(posixMillisecondsHere)
first. MDN says that calling the Date function outside of the constructor context (i.e., without the new) will always return a string containing a formatted date rather than a Date object.
Strictly speaking, when you do that, you are writing JavaScript and not JSON. JSON cannot contain Date objects.
RFC 4627 says
2.1. Values
A JSON value MUST be an object, array, number, or string, or one of
the following three literal names:
false null true
If you want to put a Date into what is strictly considered JSON and then get it back out, you must choose some way of using the JSON primitives (to wit, objects, arrays, numbers, strings, etc.) to encode a Date.
If you want to get a Date back out of JSON, whatever parses your JSON must understand the convention that you used to encode the Date.
Hope these are credible and/or official enough to help.
What about something like this:
in your config/en.yml file:
en:
time:
formats:
json: "/Date(%s%L)/"
and than in the view:
<%= l(Time.now, :format => :json) %>
Please note that you would need access to the helpers in the method that renders json. So it won't work if you are using ActiveRecord#to_json method for generating jsons.
Check out this question:
c# serialized JSON date to ruby
... simple answer seems to be to create a parse_date method.
It's the UNIX Epoch (seconds since 1970-01-01) right? What about using DateTime#strftime method?
# Taken from the Ruby documentation
seconds_since_1970 = your_date.strftime("%s")
UPDATE: OK, it's milliseconds, according to the documentation you can use your_date.strftime("%Q") to get the ms (but I've not tried yet).

Resources