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 » Community » U++ community news and announcements » Json Date/Time support
Json Date/Time support [message #40904] Sun, 06 October 2013 13:18 Go to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
JSON standard does not directly support Date/Time, which causes troubles when using it. Anyway, I have found a neat trick

http://weblogs.asp.net/bleroy/archive/2008/01/18/dates-and-j son.aspx

which seems quite reasonable, so I have added support for Date/Time in JSON in this manner.
Re: Json Date/Time support [message #40907 is a reply to message #40904] Sun, 06 October 2013 19:55 Go to previous messageGo to next message
unodgs is currently offline  unodgs
Messages: 1366
Registered: November 2005
Location: Poland
Ultimate Contributor

That's great to have it, but I had it already implemented in my private branch. But my solution checked if the json value can be converted to a date using IsDateString function:
bool IsDateString(const String& s)
{
	if(s.GetLength() != 10)
		return false;
	
	int cnt = 0;
	for(int i = 0; i < s.GetLength(); i++) {
		if(IsAlpha(s[i]) || IsSpace(s[i]))
			return false;
		if(!IsDigit(s[i]))
			 ++cnt;
	}
	return cnt == 2;
}

and then in ParseJSON I changed if(p.IsString()) section to:
if(p.IsString())
{
	String s = p.ReadString();
	if(IsDateString(s)) {
		Date d = ScanDate(s);
		if(!IsNull(d))
			return d;
	}
	return s;
}

The good thing is it does accept most of the date formats that satisfy ScanDate function.
However relaying on unofficial standard might be better.
Re: Json Date/Time support [message #40908 is a reply to message #40907] Sun, 06 October 2013 20:06 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
unodgs wrote on Sun, 06 October 2013 13:55

That's great to have it, but I had it already implemented in my private branch. But my solution checked if the json value can be converted to a date using IsDateString function:
bool IsDateString(const String& s)
{
	if(s.GetLength() != 10)
		return false;
	
	int cnt = 0;
	for(int i = 0; i < s.GetLength(); i++) {
		if(IsAlpha(s[i]) || IsSpace(s[i]))
			return false;
		if(!IsDigit(s[i]))
			 ++cnt;
	}
	return cnt == 2;
}

and then in ParseJSON I changed if(p.IsString()) section to:
if(p.IsString())
{
	String s = p.ReadString();
	if(IsDateString(s)) {
		Date d = ScanDate(s);
		if(!IsNull(d))
			return d;
	}
	return s;
}

The good thing is it does accept most of the date formats that satisfy ScanDate function.
However relaying on unofficial standard might be better.



Well, what I do like about that hack is that it quite safely excludes false positives. "\/" escape code really does not make much sense....
Previous Topic: Ide improvements
Next Topic: ArrayCtrl: Set/Add maps
Goto Forum:
  


Current Time: Thu Mar 28 10:18:32 CET 2024

Total time taken to generate the page: 0.01247 seconds