osgEarth 2.1.1
Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes

osgEarth::Json::StyledStreamWriter Class Reference

Writes a Value in JSON format in a human friendly way, to a stream rather than to a string. More...

List of all members.

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_

Detailed Description

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.

Parameters:
indentationEach level will be indented by this amount extra.
See also:
Reader, Value, Value::setComment()

Definition at line 1224 of file JsonUtils.


Member Typedef Documentation

typedef std::vector<std::string> osgEarth::Json::StyledStreamWriter::ChildValues [private]

Definition at line 1252 of file JsonUtils.


Constructor & Destructor Documentation

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]

Definition at line 1228 of file JsonUtils.

{}

Member Function Documentation

bool StyledStreamWriter::hasCommentForValue ( const Value value) [private]

Definition at line 3479 of file JsonUtils.cpp.

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.

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.

Parameters:
outStream to write to. (Can be ostringstream, e.g.)
rootValue to serialize.
Note:
There is no point in deriving from Writer, since write() should not return a value.

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:


Member Data Documentation

Definition at line 1259 of file JsonUtils.

Definition at line 1254 of file JsonUtils.

Definition at line 1255 of file JsonUtils.

Definition at line 1258 of file JsonUtils.

Definition at line 1256 of file JsonUtils.

Definition at line 1257 of file JsonUtils.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines