Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

  • #16
    So the to/from JSON methods should not be used then?

    I am trying to figure out how to do it using the encoder, can you provide any tips or a simple example?

    Comment


    • #17
      to/fromJSON does what it says - if you want JSON, you can use them. The problem is, JSON can't automatically round-trip dates without adding some additional processing of your own.

      We plan to put a note in the docs so that people who are not familiar with this limitation of JSON don't head down this path.

      In the meantime, the setting to allow round-tripping of dates is prominently mentioned right on JSONEncoder.encode() - setDateFormat().

      Comment


      • #18
        Sorry to keep troubling you with this....

        Just want to check if this is what you had in mind:
        Code:
        public void onClick(ClickEvent event) {
            JSONEncoder encoder = new JSONEncoder();
            encoder.setDateFormat(JSONDateFormat.DATE_CONSTRUCTOR);
            String jsonCriteria = JSON.encode(fb.getCriteria().getJsObj(), encoder);
            label.setContents("Criteria: " + jsonCriteria);
            GWT.log("Criteria: " + jsonCriteria);
            JavaScriptObject jsonObj = JSON.decode(jsonCriteria);
            fb.setCriteria(new AdvancedCriteria(jsonObj));
        }
        Which still gives me the String values and not Date objects as I expected.
        Code:
        [INFO] [sandbox1] - Criteria: {
            "_constructor":"AdvancedCriteria", 
            "operator":"and", 
            "criteria":[
                {
                    "operator":"betweenInclusive", 
                    "fieldName":"adate", 
                    "start":"2014-05-31T05:00:00.000", 
                    "end":"2014-06-14T04:59:59.999"
                }
            ]
        }

        Comment


        • #19
          Just use the instance method jsonEncoder.encode() instead of using the JSON.encode() class method.

          Note that the class method should *also* work, and we'll see if we can reproduce the issue you report.

          Comment


          • #20
            When I changed it as you suggested to the encoder.encode method, I get the expected encoding:
            Code:
            [INFO] [sandbox1] - Encoded Criteria: {
                "_constructor":"AdvancedCriteria", 
                "operator":"and", 
                "criteria":[
                    {
                        "operator":"betweenInclusive", 
                        "fieldName":"adate", 
                        "start":new Date(1401512400000), 
                        "end":new Date(1404190799999)
                    }
                ]
            }

            Comment


            • #21
              OK, with that resolved (thanks!) we are back to the original issue. If the browser TimeZone is different that what is set using the setDefaultDisplayTimezone, different dates are displayed depending on the 2 TimeZones in use.

              In my particular scenario, browser is in Eastern TZ and the code is setting Central TZ ("-05:00") and when I set the 2 dates to May 31 & June 30th and click refresh, I then end up with the 2nd date showing July 1st on the input item and July 2nd on the display label (see attached image 183 and before refresh image 184). Even when setting the date via the picker, the input shows the date I selected and the label shows the next day.

              Code:
              public class Sandbox1 implements EntryPoint {
              
                  @Override
                  public void onModuleLoad() {
              
                      // set user to Central TimeZone (make sure this timezone is different than browser).
                      DateUtil.setDefaultDisplayTimezone("-05:00");
              
                      final Layout layout = new VLayout(10);
              
                      DataSource dataSource = new DataSource();
                      dataSource.setID("xxxDS");
                      DataSourceField dateField = new DataSourceField("adate", FieldType.DATE, "Date");
                      dataSource.setFields(dateField);
                      dataSource.setClientOnly(true);
              
                      final FilterBuilder fb = new FilterBuilder();
                      fb.setTopOperatorAppearance(TopOperatorAppearance.RADIO);
                      fb.setTopOperator(LogicalOperator.AND);
                      fb.setShowModeSwitcher(Boolean.TRUE);
                      fb.setDataSource(dataSource);
              
                      final Label label = new Label();
              
                      IButton clear = new IButton("Clear");
                      clear.addClickHandler(new ClickHandler() {
                          @Override
                          public void onClick(ClickEvent event) {
                              fb.clearCriteria();
                          }
                      });
              
                      IButton refresh = new IButton("Refresh");
                      refresh.addClickHandler(new ClickHandler() {
                          @Override
                          public void onClick(ClickEvent event) {
                              JSONEncoder encoder = new JSONEncoder();
                              encoder.setDateFormat(JSONDateFormat.DATE_CONSTRUCTOR);
                              String jsonCriteria = encoder.encode(fb.getCriteria().getJsObj());
                              label.setContents("Encoded Criteria: " + jsonCriteria);
                              GWT.log("Encoded Criteria: " + jsonCriteria);
                              JavaScriptObject jsonObj = JSON.decode(jsonCriteria);
                              fb.setCriteria(new AdvancedCriteria(jsonObj));
                          }
                      });
              
                      layout.addMember(label);
                      layout.addMember(fb);
                      HLayout buttonLayout = new HLayout(5);
                      buttonLayout.addMember(clear);
                      buttonLayout.addMember(refresh);
                      layout.addMember(buttonLayout);
                      layout.draw();
              
                  }
              
              }
              Where the serialized 2nd date is: 1404190799999 = Tue Jul 01 00:59:59 EDT 2014
              Attached Files

              Comment


              • #22
                Any update on this?

                Thanks.

                Comment


                • #23
                  We're looking at the best way to address it - we'll update here shortly

                  Comment


                  • #24
                    This is now fixed for tomorrow's 4.1 and 5.0 builds.

                    Comment


                    • #25
                      This also includes a fix for calling JSON.encode() statically and passing an encoder as the second argument. That now works as expected.

                      Comment


                      • #26
                        Excellent. Thank you kindly.

                        Comment

                        Working...
                        X