When using Marshallers, I could use something like:
JSON.registerObjectMarshaller(Date) {
return it?.format("yyyy-MM-dd'T'HH:mm:ss")
}
and it would make all Date objects render to JSON with that format. Is there an equivalent I can use with JSON Views to display Date objects?
The format used for Date is hard coded in JsonOutput.java (https://github.com/grails/grails-views/blob/master/json/src/main/groovy/grails/plugin/json/builder/JsonOutput.java)
I've created an issue about this https://github.com/grails/grails-views/issues/92
A temporary workaround is to duplicate JsonOutput.java and place it in src/main/grails/plugin/json/builder with the format modified to your need.
Related
In thingsboard I have a post-processing function for an server attribute update dialog.
update server attribute widget
post-processing date value
post-processing date value fails
I need to convert a text value (entered by the user in a widget) into a unix time stamp (milliseconds precision) to store it into an thingsboard attribute.
I also want do use this function to display the value in a formatted ISO date string. Something linke YYYY-MM-DD hh:mm:ss.
As I understand, the date.getMonth, getFullYear, ... functions are pretty standard for JavaScript. What do I need to do to use them in thingsboard too?
Is there a better way to convert dates?
You have to call use the new-operator to create a date-object.
See Date - JavaScript | MDN:
The only correct way to instantiate a new Date object is by using the new operator. If you simply call the Date object directly, such as now = Date(), the returned value is a string rather than a Date object.
So instead of
var date = Date(value);
it should be
var date = new Date(value);
However, there is a handy and popular javascript date-library called moment.js. Fortunately it is already bundled with Thingsboard and you can use it in widgets and those post-processing functions.
Are there any alternatives to JSON.decode (or any options to it I'm not aware of) that will parse the JSON with the date/time strings converted to DateTime objects?
ActiveSupport::JSON.decode({date_time: DateTime.now()}.to_json)
Having to
h["dt1"] = DateTime.strptime(h["dt1"])
h["dt2"] = DateTime.strptime(h["dt2"])
For each of the DateTime fields is quite annoying.
Or are there any other ways to serialize ruby objects as a string to store in redis and then get back the original object and their members as original objects?
Would Masrhal.dump and Marshal.load be preferred? Not sure why the redis-rb docs suggest JSON.
Unfortunately the JSON specification does not include a Date/Time data type.
You could use Marshal but that would limit you to only use ruby to write/read from redis. Perhaps a better option would be YAML, which does have a Date/Time type.
I am using ASP.Net Web API with JSON.Net to serialize. I had to configure the serializer to handle ISO dates properly like this:
var iso = new IsoDateTimeConverter {
DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffK"
};
GlobalConfiguration.Configuration.Formatters.JsonFormatter
.SerializerSettings.Converters.Add(iso);
This works fine when I am passing my objects down via the WebAPI. My problem, however, is that I have another place where I want to explicitly call the serialization:
#Html.Raw(JsonConvert.SerializeObject(Model));
In this case, it doesn't use the configuration I set up. I am aware that I can pass the iso converter into the SerializeObject call, but I prefer to avoid this and get a hold of a configured serialzer for obvious reasons.
Any ideas?
If you're going to do JSON serialization yourself, you have to pass the settings you want explicitly. There's no way around it. The best I can think of if you want to reuse the same serializer settings is to do something like this:
JsonConvert.SerializeObject(Model, GlobalConfiguration.Configuration.Formatters.
JsonFormatter.SerializerSettings)
I'm getting a bunch of information from a web server with SOAP and NSXMLParser, and one of the properties I'm getting is time for when a kid is being picked up...
e.g. a response would be: PT15H
In the documentation it's called: System.TimeSpan pickUpTime
Now, the other properties includes stuff like integers, strings and booleans and I just save them with e.g.
else if([elementName isEqualToString:#"a:LastName"])
{
aParent.lastName = currentElementValue;
}
What do I do with PickUpTime? I need to get the time and show it in a label in a clean manner, like 15:00
You can use NSDateFormatter and give it the proper structure of the strings you are getting. This way it can be converted into an NSDate object for your use, i.e. displaying it as "15:00" and such.
Decided to just use stringByReplacingOccurrencesOfString to remove the letters, since I know that I will always get certain letters... then I can convert it into a NSDate with with the format HH:mm
I ve a JS array, comprising multiple JS objects.
I want to convert JS array to JSON type, & pass that to controller (using AJAX - POST call).
So that I can retrieve the values from the Array of Objects, in my controller & save them in DB.
NB: I ve tried using $.stringify(myArry), but its not able to send data to controller in JSON format. Also I cant use $.toJSON(myArray), as I m not allowed to include a new plugin in our solution. :(
Plz suggest me any other idea.
Else if anyone can let me know how to deserelize the array in cotroller, that I ve sent by using $.stringify(myArry), that would also great.
Something like
var result = JavaScriptConvert.DeserializeObject(inputContent, JsonDataType);
Per this post, it looks like you'll have to add another plug-in:
JSON stringify missing from jQuery 1.4.1?
The Google solution looks good: http://code.google.com/p/jquery-json/
Or just use the JSON object from Crockford: https://github.com/douglascrockford/JSON-js