Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » Jsonize() Date and Time serialization [patch]
Jsonize() Date and Time serialization [patch] [message #57209] Fri, 11 June 2021 19:41 Go to previous message
zsolt is currently offline  zsolt
Messages: 702
Registered: December 2005
Location: Budapest, Hungary
Contributor
I'm working on a web service and my problem was, that JavaScript handles date and time in ISO format, but U++ Jsonize() in special ways.
I attached a minimal HTML file to demonstrate, how JavaScript serializes Date and DateTime and U++ should do the same if we want to be compatible.

My HTML file shows a JSON output like this:
{
    "my_date": "2021-06-08",
    "my_datetime": "2021-06-15T19:25"
}

Jsonize() outputs dates like this:
{
    "my_date": "20210608",
    "my_datetime": "20210615T19:25"
}

Is it possible to fix Jsonize()?

I propose something like this:
@@ -328,7 +328,7 @@ template<> void Jsonize(JsonIO& io, Date& var)
 			return;
 		}
 		if(IsString(v)) {
-			String text = v;
+			String text = Filter(v.ToString(), CharFilterDigit);
 			if(text.GetCount() > 6) {
 				Date d;
 				d.year = ScanInt(text.Left(4));
@@ -346,7 +346,7 @@ template<> void Jsonize(JsonIO& io, Date& var)
 		if(IsNull(var))
 			io.Set(Null);
 		else
-			io.Set(Format("%04d%02d%02d", var.year, var.month, var.day));
+			io.Set(Format("%04d-%02d-%02d", var.year, var.month, var.day));
 }
 
 template<> void Jsonize(JsonIO& io, Time& var)
@@ -358,15 +358,15 @@ template<> void Jsonize(JsonIO& io, Time& var)
 			return;
 		}
 		if(IsString(v)) {
-			String text = v;
-			if(text.GetCount() > 15) {
+			String text = Filter(v.ToString(), CharFilterDigit);
+			if(text.GetCount() > 10) {//seconds may be missing
 				Time tm;
 				tm.year = ScanInt(text.Left(4));
 				tm.month = ScanInt(text.Mid(4, 2));
 				tm.day = ScanInt(text.Mid(6, 2));
-				tm.hour = ScanInt(text.Mid(9, 2));
-				tm.minute = ScanInt(text.Mid(12, 2));
-				tm.second = ScanInt(text.Mid(15));
+				tm.hour = ScanInt(text.Mid(8, 2));
+				tm.minute = ScanInt(text.Mid(10, 2));
+				tm.second = ScanInt(text.Mid(12, 2));
 				if(tm.IsValid()) {
 					var = tm;
 					return;
@@ -379,7 +379,7 @@ template<> void Jsonize(JsonIO& io, Time& var)
 		if(IsNull(var))
 			io.Set(Null);
 		else
-			io.Set(Format("%04d%02d%02d`T%02d:%02d:%02d",
+			io.Set(Format("%04d-%02d-%02d`T%02d:%02d:%02d",
 				          var.year, var.month, var.day, var.hour, var.minute, var.second));
 }
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: MSVS15x64: Compile Error C3256 C3259 C3250 in Fn.h findarg
Next Topic: How do I implement mouse hover in Ctrl based class
Goto Forum:
  


Current Time: Mon Aug 25 19:19:29 CEST 2025

Total time taken to generate the page: 0.06079 seconds