|
osgEarth 2.1.1
|
Writes a Value in JSON format in a human friendly way, to a stream rather than to a string. More...
Public Member Functions | |
| StyledStreamWriter (std::string indentation="\t") | |
| ~StyledStreamWriter () | |
| void | write (std::ostream &out, const Value &root) |
| Serialize a Value in JSON format. | |
Private Types | |
| typedef std::vector< std::string > | ChildValues |
Private Member Functions | |
| void | writeValue (const Value &value) |
| void | writeArrayValue (const Value &value) |
| bool | isMultineArray (const Value &value) |
| void | pushValue (const std::string &value) |
| void | writeIndent () |
| void | writeWithIndent (const std::string &value) |
| void | indent () |
| void | unindent () |
| void | writeCommentBeforeValue (const Value &root) |
| void | writeCommentAfterValueOnSameLine (const Value &root) |
| bool | hasCommentForValue (const Value &value) |
Static Private Member Functions | |
| static std::string | normalizeEOL (const std::string &text) |
Private Attributes | |
| ChildValues | childValues_ |
| std::ostream * | document_ |
| std::string | indentString_ |
| int | rightMargin_ |
| std::string | indentation_ |
| bool | addChildValues_ |
Writes a Value in JSON format in a human friendly way, to a stream rather than to a string.
The rules for line break and indent are as follow:
If the Value have comments then they are outputed according to their CommentPlacement.
| indentation | Each level will be indented by this amount extra. |
typedef std::vector<std::string> osgEarth::Json::StyledStreamWriter::ChildValues [private] |
| StyledStreamWriter::StyledStreamWriter | ( | std::string | indentation = "\t" | ) |
Definition at line 3234 of file JsonUtils.cpp.
: document_(NULL) , rightMargin_( 74 ) , indentation_( indentation ) { }
| osgEarth::Json::StyledStreamWriter::~StyledStreamWriter | ( | ) | [inline] |
| bool StyledStreamWriter::hasCommentForValue | ( | const Value & | value | ) | [private] |
Definition at line 3479 of file JsonUtils.cpp.
{
return value.hasComment( commentBefore )
|| value.hasComment( commentAfterOnSameLine )
|| value.hasComment( commentAfter );
}
Here is the call graph for this function:
Here is the caller graph for this function:| void StyledStreamWriter::indent | ( | ) | [private] |
Definition at line 3439 of file JsonUtils.cpp.
{
indentString_ += indentation_;
}
Here is the caller graph for this function:| bool StyledStreamWriter::isMultineArray | ( | const Value & | value | ) | [private] |
Definition at line 3371 of file JsonUtils.cpp.
{
int size = value.size();
bool isMultiLine = size*3 >= rightMargin_ ;
childValues_.clear();
for ( int index =0; index < size && !isMultiLine; ++index )
{
const Value &childValue = value[index];
isMultiLine = isMultiLine ||
( (childValue.isArray() || childValue.isObject()) &&
childValue.size() > 0 );
}
if ( !isMultiLine ) // check if line length > max line length
{
childValues_.reserve( size );
addChildValues_ = true;
int lineLength = 4 + (size-1)*2; // '[ ' + ', '*n + ' ]'
for ( int index =0; index < size && !isMultiLine; ++index )
{
writeValue( value[index] );
lineLength += int( childValues_[index].length() );
isMultiLine = isMultiLine && hasCommentForValue( value[index] );
}
addChildValues_ = false;
isMultiLine = isMultiLine || lineLength >= rightMargin_;
}
return isMultiLine;
}
Here is the call graph for this function:
Here is the caller graph for this function:| std::string StyledStreamWriter::normalizeEOL | ( | const std::string & | text | ) | [static, private] |
Definition at line 3488 of file JsonUtils.cpp.
{
std::string normalized;
normalized.reserve( text.length() );
const char *begin = text.c_str();
const char *end = begin + text.length();
const char *current = begin;
while ( current != end )
{
char c = *current++;
if ( c == '\r' ) // mac or dos EOL
{
if ( *current == '\n' ) // convert dos EOL
++current;
normalized += '\n';
}
else // handle unix EOL & other char
normalized += c;
}
return normalized;
}
Here is the caller graph for this function:| void StyledStreamWriter::pushValue | ( | const std::string & | value | ) | [private] |
Definition at line 3402 of file JsonUtils.cpp.
{
if ( addChildValues_ )
childValues_.push_back( value );
else
*document_ << value;
}
Here is the caller graph for this function:| void StyledStreamWriter::unindent | ( | ) | [private] |
Definition at line 3446 of file JsonUtils.cpp.
{
assert( indentString_.size() >= indentation_.size() );
indentString_.resize( indentString_.size() - indentation_.size() );
}
Here is the caller graph for this function:| void StyledStreamWriter::write | ( | std::ostream & | out, |
| const Value & | root | ||
| ) |
Serialize a Value in JSON format.
| out | Stream to write to. (Can be ostringstream, e.g.) |
| root | Value to serialize. |
Definition at line 3243 of file JsonUtils.cpp.
{
document_ = &out;
addChildValues_ = false;
indentString_ = "";
writeCommentBeforeValue( root );
writeValue( root );
writeCommentAfterValueOnSameLine( root );
*document_ << "\n";
document_ = NULL; // Forget the stream, for safety.
}
Here is the call graph for this function:
Here is the caller graph for this function:| void StyledStreamWriter::writeArrayValue | ( | const Value & | value | ) | [private] |
Definition at line 3318 of file JsonUtils.cpp.
{
unsigned size = value.size();
if ( size == 0 )
pushValue( "[]" );
else
{
bool isArrayMultiLine = isMultineArray( value );
if ( isArrayMultiLine )
{
writeWithIndent( "[" );
indent();
bool hasChildValue = !childValues_.empty();
unsigned index =0;
while ( true )
{
const Value &childValue = value[index];
writeCommentBeforeValue( childValue );
if ( hasChildValue )
writeWithIndent( childValues_[index] );
else
{
writeIndent();
writeValue( childValue );
}
if ( ++index == size )
{
writeCommentAfterValueOnSameLine( childValue );
break;
}
*document_ << ",";
writeCommentAfterValueOnSameLine( childValue );
}
unindent();
writeWithIndent( "]" );
}
else // output on a single line
{
assert( childValues_.size() == size );
*document_ << "[ ";
for ( unsigned index =0; index < size; ++index )
{
if ( index > 0 )
*document_ << ", ";
*document_ << childValues_[index];
}
*document_ << " ]";
}
}
}
Here is the call graph for this function:
Here is the caller graph for this function:| void StyledStreamWriter::writeCommentAfterValueOnSameLine | ( | const Value & | root | ) | [private] |
Definition at line 3464 of file JsonUtils.cpp.
{
if ( root.hasComment( commentAfterOnSameLine ) )
*document_ << std::string(" ") + normalizeEOL( root.getComment( commentAfterOnSameLine ) );
if ( root.hasComment( commentAfter ) )
{
*document_ << "\n";
*document_ << normalizeEOL( root.getComment( commentAfter ) );
*document_ << "\n";
}
}
Here is the call graph for this function:
Here is the caller graph for this function:| void StyledStreamWriter::writeCommentBeforeValue | ( | const Value & | root | ) | [private] |
Definition at line 3454 of file JsonUtils.cpp.
{
if ( !root.hasComment( commentBefore ) )
return;
*document_ << normalizeEOL( root.getComment( commentBefore ) );
*document_ << "\n";
}
Here is the call graph for this function:
Here is the caller graph for this function:| void StyledStreamWriter::writeIndent | ( | ) | [private] |
Definition at line 3412 of file JsonUtils.cpp.
{
/*
Some comments in this method would have been nice. ;-)
if ( !document_.empty() )
{
char last = document_[document_.length()-1];
if ( last == ' ' ) // already indented
return;
if ( last != '\n' ) // Comments may add new-line
*document_ << '\n';
}
*/
*document_ << '\n' << indentString_;
}
Here is the caller graph for this function:| void StyledStreamWriter::writeValue | ( | const Value & | value | ) | [private] |
Definition at line 3257 of file JsonUtils.cpp.
{
switch ( value.type() )
{
case nullValue:
pushValue( "null" );
break;
case intValue:
pushValue( valueToString( value.asInt() ) );
break;
case uintValue:
pushValue( valueToString( value.asUInt() ) );
break;
case realValue:
pushValue( valueToString( value.asDouble() ) );
break;
case stringValue:
pushValue( valueToQuotedString( value.asCString() ) );
break;
case booleanValue:
pushValue( valueToString( value.asBool() ) );
break;
case arrayValue:
writeArrayValue( value);
break;
case objectValue:
{
Value::Members members( value.getMemberNames() );
if ( members.empty() )
pushValue( "{}" );
else
{
writeWithIndent( "{" );
indent();
Value::Members::iterator it = members.begin();
while ( true )
{
const std::string &name = *it;
const Value &childValue = value[name];
writeCommentBeforeValue( childValue );
writeWithIndent( valueToQuotedString( name.c_str() ) );
*document_ << " : ";
writeValue( childValue );
if ( ++it == members.end() )
{
writeCommentAfterValueOnSameLine( childValue );
break;
}
*document_ << ",";
writeCommentAfterValueOnSameLine( childValue );
}
unindent();
writeWithIndent( "}" );
}
}
break;
}
}
Here is the call graph for this function:
Here is the caller graph for this function:| void StyledStreamWriter::writeWithIndent | ( | const std::string & | value | ) | [private] |
Definition at line 3431 of file JsonUtils.cpp.
{
writeIndent();
*document_ << value;
}
Here is the call graph for this function:
Here is the caller graph for this function:bool osgEarth::Json::StyledStreamWriter::addChildValues_ [private] |
std::ostream* osgEarth::Json::StyledStreamWriter::document_ [private] |
std::string osgEarth::Json::StyledStreamWriter::indentation_ [private] |
std::string osgEarth::Json::StyledStreamWriter::indentString_ [private] |
int osgEarth::Json::StyledStreamWriter::rightMargin_ [private] |
1.7.3