|
osgEarth 2.1.1
|
Writes a Value in JSON format in a human friendly way. More...
Inheritance diagram for osgEarth::Json::StyledWriter:
Collaboration diagram for osgEarth::Json::StyledWriter:Public Member Functions | |
| StyledWriter () | |
| virtual | ~StyledWriter () |
| virtual std::string | write (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::string | document_ |
| std::string | indentString_ |
| int | rightMargin_ |
| int | indentSize_ |
| bool | addChildValues_ |
Writes a Value in JSON format in a human friendly way.
The rules for line break and indent are as follow:
If the Value have comments then they are outputed according to their CommentPlacement.
typedef std::vector<std::string> osgEarth::Json::StyledWriter::ChildValues [private] |
| StyledWriter::StyledWriter | ( | ) |
Definition at line 2959 of file JsonUtils.cpp.
: rightMargin_( 74 ) , indentSize_( 3 ) { }
| virtual osgEarth::Json::StyledWriter::~StyledWriter | ( | ) | [inline, virtual] |
| bool StyledWriter::hasCommentForValue | ( | const Value & | value | ) | [private] |
Definition at line 3199 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 StyledWriter::indent | ( | ) | [private] |
Definition at line 3159 of file JsonUtils.cpp.
{
indentString_ += std::string( indentSize_, ' ' );
}
Here is the caller graph for this function:| bool StyledWriter::isMultineArray | ( | const Value & | value | ) | [private] |
Definition at line 3095 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 StyledWriter::normalizeEOL | ( | const std::string & | text | ) | [static, private] |
Definition at line 3208 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 StyledWriter::pushValue | ( | const std::string & | value | ) | [private] |
Definition at line 3126 of file JsonUtils.cpp.
{
if ( addChildValues_ )
childValues_.push_back( value );
else
document_ += value;
}
Here is the caller graph for this function:| void StyledWriter::unindent | ( | ) | [private] |
Definition at line 3166 of file JsonUtils.cpp.
{
assert( int(indentString_.size()) >= indentSize_ );
indentString_.resize( indentString_.size() - indentSize_ );
}
Here is the caller graph for this function:| std::string StyledWriter::write | ( | const Value & | root | ) | [virtual] |
Serialize a Value in JSON format.
| root | Value to serialize. |
Implements osgEarth::Json::Writer.
Definition at line 2967 of file JsonUtils.cpp.
{
document_ = "";
addChildValues_ = false;
indentString_ = "";
writeCommentBeforeValue( root );
writeValue( root );
writeCommentAfterValueOnSameLine( root );
document_ += "\n";
return document_;
}
Here is the call graph for this function:
Here is the caller graph for this function:| void StyledWriter::writeArrayValue | ( | const Value & | value | ) | [private] |
Definition at line 3042 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 StyledWriter::writeCommentAfterValueOnSameLine | ( | const Value & | root | ) | [private] |
Definition at line 3184 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 StyledWriter::writeCommentBeforeValue | ( | const Value & | root | ) | [private] |
Definition at line 3174 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 StyledWriter::writeIndent | ( | ) | [private] |
Definition at line 3136 of file JsonUtils.cpp.
{
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_ += indentString_;
}
Here is the caller graph for this function:| void StyledWriter::writeValue | ( | const Value & | value | ) | [private] |
Definition at line 2981 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 StyledWriter::writeWithIndent | ( | const std::string & | value | ) | [private] |
Definition at line 3151 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::StyledWriter::addChildValues_ [private] |
std::string osgEarth::Json::StyledWriter::document_ [private] |
int osgEarth::Json::StyledWriter::indentSize_ [private] |
std::string osgEarth::Json::StyledWriter::indentString_ [private] |
int osgEarth::Json::StyledWriter::rightMargin_ [private] |
1.7.3