Skip to content

Commit

Permalink
- Solved Issue 2: Negative values are not handled
Browse files Browse the repository at this point in the history
- Value now throws an exception if the wrong As*type*() is used
  • Loading branch information
Zguy committed May 13, 2011
1 parent 39204fe commit 79a45fc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
33 changes: 23 additions & 10 deletions Jzon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,15 @@ namespace Jzon
return 0;
else
{
std::stringstream sstr(valueStr);
int val;
sstr >> val;
return val;
if (GetValueType() == VT_INT)
{
std::stringstream sstr(valueStr);
int val;
sstr >> val;
return val;
}
else
throw ValueException();
}
}
double Value::AsDouble() const
Expand All @@ -248,18 +253,26 @@ namespace Jzon
return 0.0;
else
{
std::stringstream sstr(valueStr);
double val;
sstr >> val;
return val;
if (GetValueType() == VT_DOUBLE)
{
std::stringstream sstr(valueStr);
double val;
sstr >> val;
return val;
}
else
throw ValueException();
}
}
bool Value::AsBool() const
{
if (IsNull())
return false;
else
return (valueStr == "true");
if (GetValueType() == VT_BOOL)
return (valueStr == "true");
else
throw ValueException();
}

void Value::SetNull()
Expand Down Expand Up @@ -383,7 +396,7 @@ namespace Jzon
{
char c = (*it);

if (c != '0' && c != '1' && c != '2' && c != '3' && c != '4' && c != '5' && c != '6' && c != '7' && c != '8' && c != '9' && c != '.')
if (c != '0' && c != '1' && c != '2' && c != '3' && c != '4' && c != '5' && c != '6' && c != '7' && c != '8' && c != '9' && c != '.' && c != '-')
{
onlyNumbers = false;
}
Expand Down
6 changes: 6 additions & 0 deletions Jzon.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ namespace Jzon
TypeException() : std::exception("A Node was used as the wrong type")
{}
};
class ValueException : public std::exception
{
public:
ValueException() : std::exception("A Value was used as the wrong type")
{}
};

struct Format
{
Expand Down

0 comments on commit 79a45fc

Please sign in to comment.