Coming soon: Your brand new Help Center & Community! Get a sneak-peek here

Date and DateTime not included when converting Struct to JSON

Hi,

we have a generic method we use on structs for converting a struct to JSON. However when called it excludes Date and DateTime properties.

#setLanguageLevel 3;

struct Foo {
  Integer int;
  String str;
  DateTime dt;
  Date d;
  Bool b;
  Float f;
  
  String toJson() {
    JSONBuilder jb;
    jb.setPrettyPrint(2);
    this.toJson(jb);
    return jb.getString();
  }
  
};

Foo f;
f.int = 1;
f.str = "foo";
f.dt = getCurrentDateTime();     // THIS IS NOT INCLUDED IN JSON
f.d = getCurrentDate();          // THIS IS NOT INCLUDED IN JSON
f.b = true;
f.f = 1.23;

print(f.toJson());

Is there are reason for that? Or just a bug?

RE: Date and DateTime not included when converting Struct to JSON

Hi,

Unfortunately toJson method only supports (Bool, Float, Integer, String, struct), Date/DateTime is not supported.
You can read more here.

Von: Michel Krohn-Dale 22. Jun 2020

RE: Date and DateTime not included when converting Struct to JSON

Is there a reason why?

Von: Frode Lillerud 22. Jun 2020

RE: Date and DateTime not included when converting Struct to JSON

Hi,

Probably because this is not supported data type in Json. Should be possible to fix, will add a wish for this.

Von: Michel Krohn-Dale 22. Jun 2020