6 #if !defined(JSON_IS_AMALGAMATION)
9 #endif // if !defined(JSON_IS_AMALGAMATION)
18 #if _MSC_VER >= 1400 // VC++ 8.0
19 #pragma warning( disable : 4996 ) // disable warning about strdup being deprecated.
38 char *current = buffer +
sizeof(buffer);
39 bool isNegative = value < 0;
45 assert( current >= buffer );
53 char *current = buffer +
sizeof(buffer);
55 assert( current >= buffer );
59 #if defined(JSON_HAS_INT64)
72 #endif // # if defined(JSON_HAS_INT64)
78 #if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__) // Use secure version with visual studio 2005 to avoid warning.
79 sprintf_s(buffer,
sizeof(buffer),
"%#.16g", value);
81 sprintf(buffer,
"%#.16g", value);
83 char* ch = buffer + strlen(buffer) - 1;
84 if (*ch !=
'0')
return buffer;
85 while(ch > buffer && *ch ==
'0'){
88 char* last_nonzero = ch;
105 *(last_nonzero+2) =
'\0';
117 return value ?
"true" :
"false";
124 return std::string(
"\"") + value +
"\"";
128 std::string::size_type maxsize = strlen(value)*2 + 3;
130 result.reserve(maxsize);
132 for (
const char* c=value; *c != 0; ++c)
168 std::ostringstream oss;
169 oss <<
"\\u" << std::hex << std::uppercase << std::setfill('0') << std::setw(4) << static_cast<int>(*c);
194 : yamlCompatiblityEnabled_( false )
202 yamlCompatiblityEnabled_ =
true;
217 FastWriter::writeValue(
const Value &value )
219 switch ( value.
type() )
242 int size = value.
size();
243 for (
int index =0; index < size; ++index )
247 writeValue( value[index] );
256 for ( Value::Members::iterator it = members.begin();
260 const std::string &name = *it;
261 if ( it != members.begin() )
264 document_ += yamlCompatiblityEnabled_ ?
": "
266 writeValue( value[name] );
289 addChildValues_ =
false;
291 writeCommentBeforeValue( root );
293 writeCommentAfterValueOnSameLine( root );
300 StyledWriter::writeValue(
const Value &value )
302 switch ( value.
type() )
323 writeArrayValue( value);
328 if ( members.empty() )
332 writeWithIndent(
"{" );
334 Value::Members::iterator it = members.begin();
337 const std::string &name = *it;
338 const Value &childValue = value[name];
339 writeCommentBeforeValue( childValue );
342 writeValue( childValue );
343 if ( ++it == members.end() )
345 writeCommentAfterValueOnSameLine( childValue );
349 writeCommentAfterValueOnSameLine( childValue );
352 writeWithIndent(
"}" );
361 StyledWriter::writeArrayValue(
const Value &value )
363 unsigned size = value.size();
368 bool isArrayMultiLine = isMultineArray( value );
369 if ( isArrayMultiLine )
371 writeWithIndent(
"[" );
373 bool hasChildValue = !childValues_.empty();
377 const Value &childValue = value[index];
378 writeCommentBeforeValue( childValue );
380 writeWithIndent( childValues_[index] );
384 writeValue( childValue );
386 if ( ++index == size )
388 writeCommentAfterValueOnSameLine( childValue );
392 writeCommentAfterValueOnSameLine( childValue );
395 writeWithIndent(
"]" );
399 assert( childValues_.size() == size );
401 for (
unsigned index =0; index < size; ++index )
405 document_ += childValues_[index];
414 StyledWriter::isMultineArray(
const Value &value )
416 int size = value.size();
417 bool isMultiLine = size*3 >= rightMargin_ ;
418 childValues_.clear();
419 for (
int index =0; index < size && !isMultiLine; ++index )
421 const Value &childValue = value[index];
422 isMultiLine = isMultiLine ||
423 ( (childValue.isArray() || childValue.isObject()) &&
424 childValue.size() > 0 );
428 childValues_.reserve( size );
429 addChildValues_ =
true;
430 int lineLength = 4 + (size-1)*2;
431 for (
int index =0; index < size && !isMultiLine; ++index )
433 writeValue( value[index] );
434 lineLength += int( childValues_[index].length() );
435 isMultiLine = isMultiLine && hasCommentForValue( value[index] );
437 addChildValues_ =
false;
438 isMultiLine = isMultiLine || lineLength >= rightMargin_;
445 StyledWriter::pushValue(
const std::string &value )
447 if ( addChildValues_ )
448 childValues_.push_back( value );
455 StyledWriter::writeIndent()
457 if ( !document_.empty() )
459 char last = document_[document_.length()-1];
465 document_ += indentString_;
470 StyledWriter::writeWithIndent(
const std::string &value )
478 StyledWriter::indent()
480 indentString_ += std::string( indentSize_,
' ' );
485 StyledWriter::unindent()
487 assert(
int(indentString_.size()) >= indentSize_ );
488 indentString_.resize( indentString_.size() - indentSize_ );
493 StyledWriter::writeCommentBeforeValue(
const Value &root )
497 document_ += normalizeEOL( root.getComment(
commentBefore ) );
503 StyledWriter::writeCommentAfterValueOnSameLine(
const Value &root )
511 document_ += normalizeEOL( root.getComment(
commentAfter ) );
518 StyledWriter::hasCommentForValue(
const Value &value )
527 StyledWriter::normalizeEOL(
const std::string &text )
529 std::string normalized;
530 normalized.reserve( text.length() );
531 const char *begin = text.c_str();
532 const char *end = begin + text.length();
533 const char *current = begin;
534 while ( current != end )
539 if ( *current ==
'\n' )
556 , indentation_( indentation )
565 addChildValues_ =
false;
567 writeCommentBeforeValue( root );
569 writeCommentAfterValueOnSameLine( root );
576 StyledStreamWriter::writeValue(
const Value &value )
578 switch ( value.
type() )
599 writeArrayValue( value);
604 if ( members.empty() )
608 writeWithIndent(
"{" );
610 Value::Members::iterator it = members.begin();
613 const std::string &name = *it;
614 const Value &childValue = value[name];
615 writeCommentBeforeValue( childValue );
618 writeValue( childValue );
619 if ( ++it == members.end() )
621 writeCommentAfterValueOnSameLine( childValue );
625 writeCommentAfterValueOnSameLine( childValue );
628 writeWithIndent(
"}" );
637 StyledStreamWriter::writeArrayValue(
const Value &value )
639 unsigned size = value.size();
644 bool isArrayMultiLine = isMultineArray( value );
645 if ( isArrayMultiLine )
647 writeWithIndent(
"[" );
649 bool hasChildValue = !childValues_.empty();
653 const Value &childValue = value[index];
654 writeCommentBeforeValue( childValue );
656 writeWithIndent( childValues_[index] );
660 writeValue( childValue );
662 if ( ++index == size )
664 writeCommentAfterValueOnSameLine( childValue );
668 writeCommentAfterValueOnSameLine( childValue );
671 writeWithIndent(
"]" );
675 assert( childValues_.size() == size );
677 for (
unsigned index =0; index < size; ++index )
681 *document_ << childValues_[index];
690 StyledStreamWriter::isMultineArray(
const Value &value )
692 int size = value.size();
693 bool isMultiLine = size*3 >= rightMargin_ ;
694 childValues_.clear();
695 for (
int index =0; index < size && !isMultiLine; ++index )
697 const Value &childValue = value[index];
698 isMultiLine = isMultiLine ||
699 ( (childValue.isArray() || childValue.isObject()) &&
700 childValue.size() > 0 );
704 childValues_.reserve( size );
705 addChildValues_ =
true;
706 int lineLength = 4 + (size-1)*2;
707 for (
int index =0; index < size && !isMultiLine; ++index )
709 writeValue( value[index] );
710 lineLength += int( childValues_[index].length() );
711 isMultiLine = isMultiLine && hasCommentForValue( value[index] );
713 addChildValues_ =
false;
714 isMultiLine = isMultiLine || lineLength >= rightMargin_;
721 StyledStreamWriter::pushValue(
const std::string &value )
723 if ( addChildValues_ )
724 childValues_.push_back( value );
731 StyledStreamWriter::writeIndent()
745 *document_ <<
'\n' << indentString_;
750 StyledStreamWriter::writeWithIndent(
const std::string &value )
758 StyledStreamWriter::indent()
760 indentString_ += indentation_;
765 StyledStreamWriter::unindent()
767 assert( indentString_.size() >= indentation_.size() );
768 indentString_.resize( indentString_.size() - indentation_.size() );
773 StyledStreamWriter::writeCommentBeforeValue(
const Value &root )
777 *document_ << normalizeEOL( root.getComment(
commentBefore ) );
783 StyledStreamWriter::writeCommentAfterValueOnSameLine(
const Value &root )
791 *document_ << normalizeEOL( root.getComment(
commentAfter ) );
798 StyledStreamWriter::hasCommentForValue(
const Value &value )
807 StyledStreamWriter::normalizeEOL(
const std::string &text )
809 std::string normalized;
810 normalized.reserve( text.length() );
811 const char *begin = text.c_str();
812 const char *end = begin + text.length();
813 const char *current = begin;
814 while ( current != end )
819 if ( *current ==
'\n' )
833 writer.
write(sout, root);
static void uintToString(LargestUInt value, char *¤t)
Converts an unsigned integer to string.
std::vector< std::string > Members
array value (ordered list)
LargestUInt asLargestUInt() const
std::string valueToQuotedString(const char *value)
object value (collection of name/value pairs).
virtual std::string write(const Value &root)
void enableYAMLCompatibility()
StyledStreamWriter(std::string indentation="\t")
void write(std::ostream &out, const Value &root)
Serialize a Value in JSON format.
char UIntToStringBuffer[uintToStringBufferSize]
static bool isControlCharacter(char ch)
Returns true if ch is a control character (in range [0,32[).
std::string valueToString(Int value)
virtual std::string write(const Value &root)
Serialize a Value in JSON format.
JSON (JavaScript Object Notation).
Members getMemberNames() const
Return a list of the member names.
const char * asCString() const
ArrayIndex size() const
Number of values in array or object.
a comment on the line after a value (only make sense for root value)
LargestInt asLargestInt() const
Writes a Value in JSON format in a human friendly way, to a stream rather than to a string...
static bool containsControlCharacter(const char *str)
a comment placed on the line before a value
a comment just after a value on the same line
std::ostream & operator<<(std::ostream &, const Value &root)
Output using the StyledStreamWriter.