///////////////////////////////////////////////////////////////////////////
| 778 | |
| 779 | //////////////////////////////////////////////////////////////////////////////// |
| 780 | std::string Task::composeJSON(bool decorate /*= false*/) { |
| 781 | std::stringstream out; |
| 782 | out << '{'; |
| 783 | |
| 784 | // ID inclusion is optional, but not a good idea, because it remains correct |
| 785 | // only until the next gc. |
| 786 | if (decorate) out << "\"id\":" << id << ','; |
| 787 | |
| 788 | // First the non-annotations. |
| 789 | int attributes_written = 0; |
| 790 | for (auto& i : data) { |
| 791 | // Annotations are not written out here. |
| 792 | if (!i.first.compare(0, 11, "annotation_", 11)) continue; |
| 793 | |
| 794 | // Tags and dependencies are handled below |
| 795 | if (i.first == "tags" || isTagAttr(i.first)) continue; |
| 796 | if (i.first == "depends" || isDepAttr(i.first)) continue; |
| 797 | |
| 798 | // If value is an empty string, do not ever output it |
| 799 | if (i.second == "") continue; |
| 800 | |
| 801 | std::string type = Task::attributes[i.first]; |
| 802 | if (type == "") type = "string"; |
| 803 | |
| 804 | // Date fields are written as ISO 8601. |
| 805 | if (type == "date") { |
| 806 | time_t epoch = get_date(i.first); |
| 807 | if (epoch != 0) { |
| 808 | Datetime d(i.second); |
| 809 | if (attributes_written) out << ','; |
| 810 | |
| 811 | out << '"' << (i.first == "modification" ? "modified" : i.first) |
| 812 | << "\":\"" |
| 813 | // Date was deleted, do not export parsed empty string |
| 814 | << (i.second == "" ? "" : d.toISO()) << '"'; |
| 815 | |
| 816 | ++attributes_written; |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | /* |
| 821 | else if (type == "duration") |
| 822 | { |
| 823 | // TODO Emit Datetime |
| 824 | } |
| 825 | */ |
| 826 | else if (type == "numeric") { |
| 827 | if (attributes_written) out << ','; |
| 828 | |
| 829 | out << '"' << i.first << "\":" << i.second; |
| 830 | |
| 831 | ++attributes_written; |
| 832 | } |
| 833 | |
| 834 | // Everything else is a quoted value. |
| 835 | else { |
| 836 | if (attributes_written) out << ','; |
| 837 |