CmdImport: JSON parse errors are assumed to imply old-style format

This commit is contained in:
Paul Beckingham
2015-07-28 23:22:04 -04:00
parent 6e5fe0631f
commit 51cf195b99

View File

@@ -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,20 +100,20 @@ int CmdImport::execute (std::string& output)
int CmdImport::import (const std::string& input)
{
int count = 0;
try
{
json::value* root = json::parse (input);
if (root)
{
// Single object parse. Input looks like:
// { ... }
if (root->type () == json::j_object)
{
// For each object element...
json::object* root_obj = (json::object*)root;
if (root_obj)
{
importSingleTask (root_obj);
++count;
}
}
// Multiple object array. Input looks like:
// [ { ... } , { ... } ]
@@ -126,15 +126,43 @@ int CmdImport::import (const std::string& input)
{
// For each object element...
json::object* root_obj = (json::object*)element;
if (root_obj)
{
importSingleTask (root_obj);
++count;
}
}
}
delete root;
}
}
// 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)
{
std::vector <std::string> lines;
split (lines, input, '\n');
for (auto& line : lines)
{
if (line.length ())
{
json::value* root = json::parse (line);
if (root)
{
importSingleTask ((json::object*) root);
++count;
delete root;
}
}
}
}
return count;
}