Spring roo not recognizing date format before saving json - mysql

I'm using Spring Roo on top of MySQL. I pull dates out via the Roo-generated JSON methods, make changes to that data in a browser form, and save it back via the Roo-generated JSON methods.
The date format I'm getting out is yyyy-MM-dd, standard MySQL date format. I'm using a calendaring widget on the browser to ensure the date I'm submitting is the same format.
Unfortunately my data doesn't go right through the ...FromJson() method, failing with the error:
Parsing date 2007-12-12 was not recognized as a date format
I presume that the problem is that it's coming out as a string, but JPA feels like it needs to generate a Date object to update.
I'll happily show my code about this, but it's nothing Roo didn't build for me.
It occurs to me that there's something it's referring to when it says "recognized as a date format". Is there somewhere I can change what date formats it knows?
EDIT: With #nowaq's help, here's the ultimate answer:
public static Lease fromJsonToLease(String json) {
return new JSONDeserializer<Lease>()
.use(null, Lease.class)
.use(Date.class, new DateFormatter("yyyy-MM-dd"))
.deserialize(json);
}
That way JSONDeserializer knows what class it's dealing with AND builds a formatter for all the dates in that class. Wicked!

Your question is very related to this one: Spring-roo REST JSON controllers mangle date fields Take a look and make sure you're using correct DateTrasformer(s) with your JSON deserializers. E.g.
new JSONDeserializer()
.use(Date.class, new DateTransformer("yyyy-MM-dd") )
.deserialize( people );

Related

How to save Timestamp in Firebase in Appsmith app or using JSON?

I am trying to save the date/time in Firebase in timestamp format. I have referred answers from below posts.
How to get current timestamp of firebase server in milliseconds?
https://www.reddit.com/r/Firebase/comments/n32ys2/how_can_i_save_the_values_in_the_datepicker_as/
I have tried saving data by adding different date formats in Body but it is ultimately getting added as a string or number depending upon string or timestamp.
Does anyone have idea how to send the date to Firestore using JSON or anything using which i can directly add it to Appsmith app?
As of now Appsmith supports adding a server timestamp to any field in Firestore. Server timestamp is the timestamp provided by the Firestore server when that particular query was executed.
e.g. if there is entry like
{
id : {name: foo, lastUpdated: ts}
}
then the lastUpdated field could be updated with the last update timestamp via setting the Timestamp Value Path to ["id.lastUpdated"]
Please checkout the video here and comment by user tazmaniax to understand more: https://github.com/appsmithorg/appsmith/pull/3693
Reference:
https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/FieldValue#serverTimestamp()
Appsmith's documentation for this is under progress and will be available on the Appsmith documentation site soon. Apologies for not putting it over there sooner.
For any other query or help please join Appsmith's discord channel
I have the impression that there is an error with the Firestore and Appsmith connector.
I have tried different combinations and if you do not send a string, int or bool in the json request, errors are generated.
However Appsmith added an additional field for handling timestamp (now)

AngularJS deserialize $http JSON dates i18n

I have a problem and I don't quite know how to deal with it. It must be a common problem, but I didn't find a good answer yet.
I'm coding a MEAN stack app, using Mongoose, and the problem I have is with the dates format when changing the language in my app.
The scenario is: The user can insert some data that has a Date field, for example their birthday. That field is then sent to mongoose in a specific format (yyyy-MM-dd) and it gets stored correctly in a Date field in mongoose. When the user searches for the data stored, I have an $http get request that get back the data in JSON format. Right before the data is send from the server, I have this in the JSON:
{datetime: '2015-11-13T20:00:00.000Z'}
And send it from nodejs like this:
return res.status(201).send(obj);
And when the Angular gets it, depending on the language I'm using in that moment, it resolves to a Date or an Invalid date. If the language I'm using is English, the resulting JSON has a valid Date in that field. If I change it to Spanish for example, then it results into an Invalid date field.
The code in Angular is this:
$http.get(url).then(function (result) {
//here the result.data may have an Invalid Date field
deferred.resolve(result.data);
}, function (error) {
deferred.reject(error);
});
I set the language in my app with the help of angular-translate, and I use angular-i18n and momentjs also.
I've read on this and here https://docs.angularjs.org/api/ng/service/$http it say that
If JSON response is detected, deserialize it using a JSON parser.
I understand that the default parser detects the Spanish i18n and tries to parse the date as yyyy-dd-MM, and obviously gets an Invalid Date.
The question here is: How can I explicitly set all the dates parsed in Angular, to follow a specific format, like yyyy-MM-dd, regardless of the i18n used? I've seen this solution already, but doesn't work for me: http://aboutcode.net/2013/07/27/json-date-parsing-angularjs.html
Is there a better way to handle all this? Maybe store the dates in Mongo not as Date fields, but like Strings, using the JS getTime() function?
This last method means that I would have to change all the dates in my app. It's a lot more work, but the result will be more standard, right?
Thank you!

How to deserialize asp.net datetime as JSON datetime with client's timezone

I'm struggling with datetimes a bit. I'm using asp.net mvc api controllers, a microsoft sql server and AngularJS.
On some button click I'm sending a JSON formatted date to an api-controller. When I post 2015-11-31 00:00 and I look in Fiddler to see what's really posted, I see that the date is formatted as such: 2015-11-30T23:00:00.000Z. (2015-11-31 - 1 hour UTC+01:00 Amsterdam, Berlin, Ber....) This is perfect because there might be a difference between the timezone the sql server might be in and the client. (Or is it?)
The problem is though: When I get the date back from the sql server it doesn't take the client's time zone into account. When I read the DateTime object from the sql server and I return it JSON formatted, the date that's being displayed is: 2015-11-30T23:00:00.000Z. I want it to add 1 hour to be in the timezone where the client is.
My question is: What do I do to get it to keep the timezone in to account while deserializing the JSON string that comes back from my api-controller?
TIA!
Problem turns out to be that when the object is being deserialized, the date property is not of type DateTime. It is of type string. Simply converting it to date by using new Date("2015-11-30T23:00:00.000Z") will do the trick.
I made filter for it:
.filter('from_gmt_to_local_date', [function () {
return function (text) {
return new Date(text);
};
}])
Usage:
{{contract.StartDate | from_gmt_to_local_date | date:'dd-MM-yyyy'}}
Hope this helps anybody.

Typescript mapping Json to interface parsing date

I'm mapping my JSON responses from the Server side code into an Interface, in this way;
objectFromJson: IMyObject = <IMyObject>jsonData;
The problem is, that the Json contains a Date, is there a way to automatic cast the date to an real Typescript Date without something like this:
new Date(parseInt(incident["CreatedOn"].substr(6)));
that the Json contains a Date, is there a way to automatic cast the date to an real Typescript Date without something like this
Your json seems to create date as a number. BAD IDEA. Reason:
how about dates before 1970
it is unreliable based on time zones
Prefer you return dates as strings. More API recommendations : https://github.com/interagent/http-api-design
If all you have is it returned as number than what you have is okay. Else if you have dates in a JavaScript recommended format e.g. 2012-01-01T12:00:00Z you would do var date = new Date('2012-01-01T12:00:00Z')

ServiceNow - JSON Web Service, display related tables

I'm working on a C# program that retrieves data from a ServiceNow database and converts that data into C# .NET objects. I'm using the JSON Web Service to return my data in JSON format.
What I want to achieve is as follows: If there is a relational mapping between a value (for
example: I have a table called Company, where CEO is not a TEXT field but an sys_id to a Employee Table) I want to be able to output that data not with an sys_id (or just displaying the name property by using the 'displayvariable' parameter) but by an object displayed in JSON.
This means that the value of a property should be an object in JSON instead of just a single value.
A few examples:
// I don't want the JSON like this
{"Company":{"CEO":"b181e841c9212c008aeb36850331fab2"}}
// Or by displaying the name of the sys_id table
{"Company":{"CEO":"James Henderson" }}
// I want the data as follows, so I can have all the data I need inside a single JSON record.
{"Company":{"CEO":{"name":"James Henderson", "age":34, "sex":"male", "office":"SBN Left Floor 23"}}}
From reading the documentation I couldn't find anything in the JSON Web Service that allowed me to display the information like this nor
find any other alternative. It should have something to do with joining the tables and displaying it all in the right format.
I have been using SNC for almost three years and have not found you can automatically join tables in a web service. Your best option would be to use a scripted web service which possibly takes a query parameter and table parameter. Then you can json serialized your result as you see fit.
Or, another option would be to generate a new processor that will traverse the GlideRecord object. The ?JSON parameter you pass in to the URL is merely a flag to pass your request to a particular processor. Unfortunately the OOB one I believe is a Java class not a JS script, so you would need to write a script much like I mentioned earlier to traverse the object path serializing the object graph as far down as your want to go.