mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-08-28 13:37:20 +02:00
CmdImport: JSON parse errors are assumed to imply old-style format
This commit is contained in:
parent
6e5fe0631f
commit
51cf195b99
1 changed files with 51 additions and 23 deletions
|
@ -68,7 +68,7 @@ int CmdImport::execute (std::string& output)
|
|||
std::string json;
|
||||
std::string line;
|
||||
while (std::getline (std::cin, line))
|
||||
json += line + " ";
|
||||
json += line + "\n";
|
||||
|
||||
if (nontrivial (json))
|
||||
count = import (json);
|
||||
|
@ -100,41 +100,69 @@ int CmdImport::execute (std::string& output)
|
|||
int CmdImport::import (const std::string& input)
|
||||
{
|
||||
int count = 0;
|
||||
json::value* root = json::parse (input);
|
||||
|
||||
// Single object parse. Input looks like:
|
||||
// { ... }
|
||||
if (root->type () == json::j_object)
|
||||
try
|
||||
{
|
||||
// For each object element...
|
||||
json::object* root_obj = (json::object*)root;
|
||||
if (root_obj)
|
||||
json::value* root = json::parse (input);
|
||||
if (root)
|
||||
{
|
||||
importSingleTask (root_obj);
|
||||
++count;
|
||||
// Single object parse. Input looks like:
|
||||
// { ... }
|
||||
if (root->type () == json::j_object)
|
||||
{
|
||||
// For each object element...
|
||||
json::object* root_obj = (json::object*)root;
|
||||
importSingleTask (root_obj);
|
||||
++count;
|
||||
}
|
||||
|
||||
// Multiple object array. Input looks like:
|
||||
// [ { ... } , { ... } ]
|
||||
else if (root->type () == json::j_array)
|
||||
{
|
||||
json::array* root_arr = (json::array*)root;
|
||||
|
||||
// For each object element...
|
||||
for (auto& element : root_arr->_data)
|
||||
{
|
||||
// For each object element...
|
||||
json::object* root_obj = (json::object*)element;
|
||||
importSingleTask (root_obj);
|
||||
++count;
|
||||
}
|
||||
}
|
||||
|
||||
delete root;
|
||||
}
|
||||
}
|
||||
|
||||
// Multiple object array. Input looks like:
|
||||
// [ { ... } , { ... } ]
|
||||
else if (root->type () == json::j_array)
|
||||
// If an exception is caught, then it is because the free-form JSON
|
||||
// objects/array above failed to parse. This is an indication that the input
|
||||
// is an old-style line-by-line set of JSON objects, because both an array of
|
||||
// objects, and a single object have failed to parse..
|
||||
//
|
||||
// Input looks like:
|
||||
// { ... }
|
||||
// { ... }
|
||||
catch (std::string& e)
|
||||
{
|
||||
json::array* root_arr = (json::array*)root;
|
||||
std::vector <std::string> lines;
|
||||
split (lines, input, '\n');
|
||||
|
||||
// For each object element...
|
||||
for (auto& element : root_arr->_data)
|
||||
for (auto& line : lines)
|
||||
{
|
||||
// For each object element...
|
||||
json::object* root_obj = (json::object*)element;
|
||||
if (root_obj)
|
||||
if (line.length ())
|
||||
{
|
||||
importSingleTask (root_obj);
|
||||
++count;
|
||||
json::value* root = json::parse (line);
|
||||
if (root)
|
||||
{
|
||||
importSingleTask ((json::object*) root);
|
||||
++count;
|
||||
delete root;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
delete root;
|
||||
return count;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue