Subscribe for automatic updates: RSS icon RSS

Login icon Sign in for full access | Help icon Help
Advanced search

Pages: [1]
  Reply  |  Print  
Author Topic: JSONObject methods and DATE  (Read 11461 times)
Snorri B.
Posts: 104


« on: November 28, 2019, 02:09:15 pm »

Hello everyone.

I've already raised this issue with support and they say it's the expected behavior. I'd like to hear what the community has to say :)

When using the method util.JSONObject.fromFGL(rec) all DATE fields in rec (if any) are converted to the format "YYYY-MM-DD" regardless of the setting in DBDATE. This is also mentioned in the documentation. Consider this code:
Code
  1. IMPORT util
  2. MAIN
  3. DEFINE js util.JSONObject
  4. DEFINE r record
  5. date_element DATE
  6. END RECORD
  7. DEFINE date_variable DATE
  8. LET r.date_element  = TODAY
  9. DISPLAY "r.date_element=",r.date_element
  10. LET js = util.JSONObject.fromFGL(r)
  11. DISPLAY "JSON 2 string:", js.toString()
  12. LET date_variable = js.get("date_element")
  13. DISPLAY "date_variable=",date_variable
  14. DISPLAY "JSON type=",js.getType("date_element")
  15. END MAIN
  16.  

Displays:

r.date_element=28.11.2019
JSON 2 string:{"date_element":"2019-11-28"}
date_variable=     
JSON type=STRING

The simple .get() method returns the date_element as a string and  NULL is assigned to the date_variable, because the date is incorrect according to DBDATE setting.

As JSON itself knows nothing about dates the question arises: Why does FGL enforce DATE in JSON to be at a specific, hard-wired format rather than just DBDATE?

Best regards,
-Snorri
Rene S.
Four Js
Posts: 112


« Reply #1 on: December 02, 2019, 09:37:06 pm »

Hello Snorri,
your using the phrase "hard-wired format". I would argue differently: any util.JSON* method converting date to string (or vice versa) assumes dates are formatted according to the ISO 8601 date format. This fit's the real live.

Try this:
{code}
    LET date_variable = util.Date.parse(js.get("date_element"), "YYYY-MM-DD")
{code}

We should eventually add specialized get methods,
{code}
    LET date_variable = js.getDate("date_element")
{code}

Rene


Rene S.
Four Js
Posts: 112


« Reply #2 on: December 02, 2019, 09:39:50 pm »

Hello Snorri,
your using the phrase "hard-wired format". I would argue differently: any util.JSON* method converting date to string (or vice versa) assumes dates are formatted according to the ISO 8601 date format. This fit's the real live.

Try this:
Code
  1. LET date_variable = util.Date.parse(js.get("date_element"), "YYYY-MM-DD")


We should eventually add specialized get methods,
Code
  1. LET date_variable = js.getDate("date_element")

Rene


Snorri B.
Posts: 104


« Reply #3 on: December 02, 2019, 11:31:11 pm »

Hi Rene and thanks.

My problem with this is the fact that I was trying to use the JSONObject as a way to pass data to and from "generic" functions. As "DATE" is not a known JSON datatype, I have no way of knowing if the STRING I get using the .get() method is a DATE or not.  So in my "generic" routine this will fail:

Code
  1. call mydialog.setFieldValue("datefield", js.get("datefield"))

It will silently assign null to "datefield".

This works fine with all numeric and string  datatypes but I guess I will need to apply some conversions to boolean and datetime as well :(

I have a workaround where I use base.typeinfo.create() rather than util.JSONObject.fromFGL() and then I store the datatypes in a different structure. Then I covert the DATEs to ISO format before putting them back into the JSONObject structure (so the calling program can use .toFGL()). I could also probably get the datatype from the form, but all this is a lot of hassle.

Best regards,
-Snorri
Reuben B.
Four Js
Posts: 1119


« Reply #4 on: December 03, 2019, 10:05:37 pm »

Snorri,

In researching I came across this thread https://stackoverflow.com/questions/10286204/the-right-json-date-format which includes the answer because XKCD says so https://xkcd.com/1179/ :-)

By using ISO-8601 we should be good with using JSON to pass arguments to WebComponents

Quote
As "DATE" is not a known JSON datatype, I have no way of knowing if the STRING I get using the .get() method is a DATE or not.
therein lies the problem, we don't know either

Quote
This works fine with all numeric and string  datatypes but I guess I will need to apply some conversions to boolean and datetime as well :(

Boolean is a JSON data-type so shouldn't be a problem.  Datetime I'd expect us to use ISO-8601 as well but a quick check I don't see any T or Z in our output.  I am not 100% sure on the standard if Z,T mandatory or optional but I can imagine if we output "2019-12-04T09:00:00Z" instead of "2019-12-04 09:00:00" then that would cause a similar issue to what you are encountering with DATE, but for now (fortunately as it maybe we don't put T or Z into the datetime string) seems to be ok.  Also interestingly with numbers being a JSON data-type, I experimented with DBFORMAT and that does handle case of being able to assign appropriately (I tried with DBFORMAT using , as decimal point)

That point with datetime reminded me of the options we have with XML Serializer http://4js.com/online_documentation/fjs-fgl-manual-html/#fgl-topics/r_gws_XmlSerializer_option_flags.html.   It maybe that what you want is a similar option with JSON serialization ie use DBDATE or ISO-8601 with JSON serialization

Finally I also want to make sure that it is understood that with

LET date_variable = js.get("date_element")

js.get is returning a string in that instance.  So you have

LET date_variable = string

and the normal string to date conversion is taking place.  And as you discover the date in ISO-8601 format is not converted.

With util.JSONObject.toFGL as in

CALL js.toFGL(r)

http://4js.com/online_documentation/fjs-fgl-manual-html/#fgl-topics/c_fgl_ext_util_JSONObject_toFGL.html

that is effectively passing the data-type across and so has that information so as to parse the information.

Reuben
 



Product Consultant (Asia Pacific)
Developer Relations Manager (Worldwide)
Author of https://4js.com/ask-reuben
Contributor to https://github.com/FourjsGenero
Snorri B.
Posts: 104


« Reply #5 on: December 03, 2019, 10:56:28 pm »

Thanks Reuben.

Quote
That point with datetime reminded me of the options we have with XML Serializer http://4js.com/online_documentation/fjs-fgl-manual-html/#fgl-topics/r_gws_XmlSerializer_option_flags.html.   It maybe that what you want is a similar option with JSON serialization ie use DBDATE or ISO-8601 with JSON serialization

I think this is a great idea. Then you could toggle the behavior depending on what you are trying to achieve. Can you create a feature request? :)

BTW: I did not play with DBFORMAT, but js.get("integer_field") actually added ".0" at the the end. So with this JSON record:

{"i":123,"c":"Some data","d":789190.02}

DISPLAY js.get("i")

will display "123.0"

Best regards,
-Snorri

 
Pages: [1]
  Reply  |  Print  
 
Jump to:  

Powered by SMF 1.1.21 | SMF © 2015, Simple Machines