Generate valid JSON even with invalid dates (#3801)

This commit is contained in:
Dustin J. Mitchell 2025-03-06 10:59:14 -05:00 committed by GitHub
parent 022650dbff
commit 74276b400c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 2 deletions

View file

@ -798,8 +798,6 @@ std::string Task::composeJSON(bool decorate /*= false*/) const {
// If value is an empty string, do not ever output it // If value is an empty string, do not ever output it
if (i.second == "") continue; if (i.second == "") continue;
if (attributes_written) out << ',';
std::string type = Task::attributes[i.first]; std::string type = Task::attributes[i.first];
if (type == "") type = "string"; if (type == "") type = "string";
@ -808,6 +806,8 @@ std::string Task::composeJSON(bool decorate /*= false*/) const {
time_t epoch = get_date(i.first); time_t epoch = get_date(i.first);
if (epoch != 0) { if (epoch != 0) {
Datetime d(i.second); Datetime d(i.second);
if (attributes_written) out << ',';
out << '"' << (i.first == "modification" ? "modified" : i.first) out << '"' << (i.first == "modification" ? "modified" : i.first)
<< "\":\"" << "\":\""
// Date was deleted, do not export parsed empty string // Date was deleted, do not export parsed empty string
@ -824,6 +824,8 @@ std::string Task::composeJSON(bool decorate /*= false*/) const {
} }
*/ */
else if (type == "numeric") { else if (type == "numeric") {
if (attributes_written) out << ',';
out << '"' << i.first << "\":" << i.second; out << '"' << i.first << "\":" << i.second;
++attributes_written; ++attributes_written;
@ -831,6 +833,8 @@ std::string Task::composeJSON(bool decorate /*= false*/) const {
// Everything else is a quoted value. // Everything else is a quoted value.
else { else {
if (attributes_written) out << ',';
out << '"' << i.first << "\":\"" << (type == "string" ? json::encode(i.second) : i.second) out << '"' << i.first << "\":\"" << (type == "string" ? json::encode(i.second) : i.second)
<< '"'; << '"';

View file

@ -29,6 +29,7 @@ import sys
import os import os
import re import re
import time import time
import json
import unittest import unittest
# Ensure python finds the local simpletap module # Ensure python finds the local simpletap module
@ -192,6 +193,7 @@ class TestUnusualTasks(TestCase):
modified="modified", modified="modified",
) )
_, out, _ = self.t(f"{uuid} export") _, out, _ = self.t(f"{uuid} export")
json.loads(out)
if __name__ == "__main__": if __name__ == "__main__":