Skip to content

Commit beb6f35

Browse files
committed
non-const write
1 parent ceef7f5 commit beb6f35

File tree

2 files changed

+49
-54
lines changed

2 files changed

+49
-54
lines changed

include/json/writer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class JSON_API StreamWriter {
5555
/// Write Value into document as configured in sub-class.
5656
/// \return zero on success
5757
/// \throw std::exception possibly, depending on configuration
58-
virtual int write(Value const& root) const = 0;
58+
virtual int write(Value const& root) = 0;
5959

6060
/// Because this Builder is non-virtual, we can safely add
6161
/// methods without a major version bump.

src/lib_json/json_writer.cpp

Lines changed: 48 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -671,56 +671,51 @@ struct BuiltStyledStreamWriter : public StreamWriter
671671
std::ostream* sout,
672672
std::string const& indentation,
673673
StreamWriter::CommentStyle cs);
674-
virtual int write(Value const& root) const;
674+
virtual int write(Value const& root);
675675
private:
676-
void writeValue(const Value& value);
677-
void writeArrayValue(const Value& value);
678-
bool isMultineArray(const Value& value);
679-
void pushValue(const std::string& value);
676+
void writeValue(Value const& value);
677+
void writeArrayValue(Value const& value);
678+
bool isMultineArray(Value const& value);
679+
void pushValue(std::string const& value);
680680
void writeIndent();
681-
void writeWithIndent(const std::string& value);
681+
void writeWithIndent(std::string const& value);
682682
void indent();
683683
void unindent();
684-
void writeCommentBeforeValue(const Value& root);
685-
void writeCommentAfterValueOnSameLine(const Value& root);
684+
void writeCommentBeforeValue(Value const& root);
685+
void writeCommentAfterValueOnSameLine(Value const& root);
686686
bool hasCommentForValue(const Value& value);
687-
static std::string normalizeEOL(const std::string& text);
687+
static std::string normalizeEOL(std::string const& text);
688688

689689
typedef std::vector<std::string> ChildValues;
690690

691691
ChildValues childValues_;
692-
std::ostream* document_;
693692
std::string indentString_;
694693
int rightMargin_;
695694
std::string indentation_;
695+
CommentStyle cs_;
696696
bool addChildValues_;
697697
};
698698
BuiltStyledStreamWriter::BuiltStyledStreamWriter(
699699
std::ostream* sout,
700700
std::string const& indentation,
701701
StreamWriter::CommentStyle cs)
702702
: StreamWriter(sout)
703-
, indentation_(indentation)
704703
, rightMargin_(74)
704+
, indentation_(indentation)
705+
, cs_(cs)
705706
{
706707
}
707-
int BuiltStyledStreamWriter::write(Value const& root) const
708+
int BuiltStyledStreamWriter::write(Value const& root)
708709
{
709-
write(sout_, root);
710-
return 0;
711-
}
712-
void BuiltStyledStreamWriter::write(std::ostream& out, const Value& root) {
713-
document_ = &out;
714710
addChildValues_ = false;
715711
indentString_ = "";
716712
writeCommentBeforeValue(root);
717713
writeValue(root);
718714
writeCommentAfterValueOnSameLine(root);
719-
*document_ << "\n";
720-
document_ = NULL; // Forget the stream, for safety.
715+
sout_ << "\n";
716+
return 0;
721717
}
722-
723-
void BuiltStyledStreamWriter::writeValue(const Value& value) {
718+
void BuiltStyledStreamWriter::writeValue(Value const& value) {
724719
switch (value.type()) {
725720
case nullValue:
726721
pushValue("null");
@@ -752,17 +747,17 @@ void BuiltStyledStreamWriter::writeValue(const Value& value) {
752747
indent();
753748
Value::Members::iterator it = members.begin();
754749
for (;;) {
755-
const std::string& name = *it;
756-
const Value& childValue = value[name];
750+
std::string const& name = *it;
751+
Value const& childValue = value[name];
757752
writeCommentBeforeValue(childValue);
758753
writeWithIndent(valueToQuotedString(name.c_str()));
759-
*document_ << " : ";
754+
sout_ << " : ";
760755
writeValue(childValue);
761756
if (++it == members.end()) {
762757
writeCommentAfterValueOnSameLine(childValue);
763758
break;
764759
}
765-
*document_ << ",";
760+
sout_ << ",";
766761
writeCommentAfterValueOnSameLine(childValue);
767762
}
768763
unindent();
@@ -772,7 +767,7 @@ void BuiltStyledStreamWriter::writeValue(const Value& value) {
772767
}
773768
}
774769

775-
void BuiltStyledStreamWriter::writeArrayValue(const Value& value) {
770+
void BuiltStyledStreamWriter::writeArrayValue(Value const& value) {
776771
unsigned size = value.size();
777772
if (size == 0)
778773
pushValue("[]");
@@ -784,7 +779,7 @@ void BuiltStyledStreamWriter::writeArrayValue(const Value& value) {
784779
bool hasChildValue = !childValues_.empty();
785780
unsigned index = 0;
786781
for (;;) {
787-
const Value& childValue = value[index];
782+
Value const& childValue = value[index];
788783
writeCommentBeforeValue(childValue);
789784
if (hasChildValue)
790785
writeWithIndent(childValues_[index]);
@@ -796,31 +791,31 @@ void BuiltStyledStreamWriter::writeArrayValue(const Value& value) {
796791
writeCommentAfterValueOnSameLine(childValue);
797792
break;
798793
}
799-
*document_ << ",";
794+
sout_ << ",";
800795
writeCommentAfterValueOnSameLine(childValue);
801796
}
802797
unindent();
803798
writeWithIndent("]");
804799
} else // output on a single line
805800
{
806801
assert(childValues_.size() == size);
807-
*document_ << "[ ";
802+
sout_ << "[ ";
808803
for (unsigned index = 0; index < size; ++index) {
809804
if (index > 0)
810-
*document_ << ", ";
811-
*document_ << childValues_[index];
805+
sout_ << ", ";
806+
sout_ << childValues_[index];
812807
}
813-
*document_ << " ]";
808+
sout_ << " ]";
814809
}
815810
}
816811
}
817812

818-
bool BuiltStyledStreamWriter::isMultineArray(const Value& value) {
813+
bool BuiltStyledStreamWriter::isMultineArray(Value const& value) {
819814
int size = value.size();
820815
bool isMultiLine = size * 3 >= rightMargin_;
821816
childValues_.clear();
822817
for (int index = 0; index < size && !isMultiLine; ++index) {
823-
const Value& childValue = value[index];
818+
Value const& childValue = value[index];
824819
isMultiLine =
825820
isMultiLine || ((childValue.isArray() || childValue.isObject()) &&
826821
childValue.size() > 0);
@@ -840,32 +835,32 @@ bool BuiltStyledStreamWriter::isMultineArray(const Value& value) {
840835
return isMultiLine;
841836
}
842837

843-
void BuiltStyledStreamWriter::pushValue(const std::string& value) {
838+
void BuiltStyledStreamWriter::pushValue(std::string const& value) {
844839
if (addChildValues_)
845840
childValues_.push_back(value);
846841
else
847-
*document_ << value;
842+
sout_ << value;
848843
}
849844

850845
void BuiltStyledStreamWriter::writeIndent() {
851846
/*
852847
Some comments in this method would have been nice. ;-)
853848
854-
if ( !document_.empty() )
849+
if ( !sout_.empty() )
855850
{
856-
char last = document_[document_.length()-1];
851+
char last = sout_[sout_.length()-1];
857852
if ( last == ' ' ) // already indented
858853
return;
859854
if ( last != '\n' ) // Comments may add new-line
860-
*document_ << '\n';
855+
sout_ << '\n';
861856
}
862857
*/
863-
*document_ << '\n' << indentString_;
858+
sout_ << '\n' << indentString_;
864859
}
865860

866-
void BuiltStyledStreamWriter::writeWithIndent(const std::string& value) {
861+
void BuiltStyledStreamWriter::writeWithIndent(std::string const& value) {
867862
writeIndent();
868-
*document_ << value;
863+
sout_ << value;
869864
}
870865

871866
void BuiltStyledStreamWriter::indent() { indentString_ += indentation_; }
@@ -875,21 +870,21 @@ void BuiltStyledStreamWriter::unindent() {
875870
indentString_.resize(indentString_.size() - indentation_.size());
876871
}
877872

878-
void BuiltStyledStreamWriter::writeCommentBeforeValue(const Value& root) {
873+
void BuiltStyledStreamWriter::writeCommentBeforeValue(Value const& root) {
879874
if (!root.hasComment(commentBefore))
880875
return;
881-
*document_ << root.getComment(commentBefore);
882-
*document_ << "\n";
876+
sout_ << root.getComment(commentBefore);
877+
sout_ << "\n";
883878
}
884879

885-
void BuiltStyledStreamWriter::writeCommentAfterValueOnSameLine(const Value& root) {
880+
void BuiltStyledStreamWriter::writeCommentAfterValueOnSameLine(Value const& root) {
886881
if (root.hasComment(commentAfterOnSameLine))
887-
*document_ << " " + root.getComment(commentAfterOnSameLine);
882+
sout_ << " " + root.getComment(commentAfterOnSameLine);
888883

889884
if (root.hasComment(commentAfter)) {
890-
*document_ << "\n";
891-
*document_ << root.getComment(commentAfter);
892-
*document_ << "\n";
885+
sout_ << "\n";
886+
sout_ << root.getComment(commentAfter);
887+
sout_ << "\n";
893888
}
894889
}
895890

@@ -910,7 +905,7 @@ struct MyStreamWriter : public StreamWriter {
910905
public:
911906
MyStreamWriter(std::ostream* sout);
912907
virtual ~MyStreamWriter();
913-
virtual int write(Value const& root) const = 0;
908+
virtual int write(Value const& root) = 0;
914909
};
915910
MyStreamWriter::MyStreamWriter(std::ostream* sout)
916911
: StreamWriter(sout)
@@ -919,7 +914,7 @@ MyStreamWriter::MyStreamWriter(std::ostream* sout)
919914
MyStreamWriter::~MyStreamWriter()
920915
{
921916
}
922-
int MyStreamWriter::write(Value const& root) const
917+
int MyStreamWriter::write(Value const& root)
923918
{
924919
sout_ << root;
925920
return 0;
@@ -988,7 +983,7 @@ std::string writeString(Value const& root, StreamWriterBuilder const& builder) {
988983
return sout.str();
989984
}
990985

991-
std::ostream& operator<<(std::ostream& sout, const Value& root) {
986+
std::ostream& operator<<(std::ostream& sout, Value const& root) {
992987
StreamWriterBuilderFactory f;
993988
StreamWriter::Builder builder(&f);
994989
builder.setCommentStyle(StreamWriter::CommentStyle::Some);

0 commit comments

Comments
 (0)