Commit 5b9288e4 authored by Jon Skeet's avatar Jon Skeet

Use the new JsonFormatter to implement ToString on generated messages.

parent f8c151f2
...@@ -160,6 +160,10 @@ namespace Google.Protobuf.Examples.AddressBook { ...@@ -160,6 +160,10 @@ namespace Google.Protobuf.Examples.AddressBook {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
if (Name.Length != 0) { if (Name.Length != 0) {
output.WriteRawTag(10); output.WriteRawTag(10);
...@@ -330,6 +334,10 @@ namespace Google.Protobuf.Examples.AddressBook { ...@@ -330,6 +334,10 @@ namespace Google.Protobuf.Examples.AddressBook {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
if (Number.Length != 0) { if (Number.Length != 0) {
output.WriteRawTag(10); output.WriteRawTag(10);
...@@ -463,6 +471,10 @@ namespace Google.Protobuf.Examples.AddressBook { ...@@ -463,6 +471,10 @@ namespace Google.Protobuf.Examples.AddressBook {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
person_.WriteTo(output, _repeated_person_codec); person_.WriteTo(output, _repeated_person_codec);
} }
......
...@@ -124,6 +124,10 @@ namespace Google.Protobuf.TestProtos { ...@@ -124,6 +124,10 @@ namespace Google.Protobuf.TestProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
if (D != 0) { if (D != 0) {
output.WriteRawTag(8); output.WriteRawTag(8);
......
...@@ -109,6 +109,10 @@ namespace Google.Protobuf.TestProtos { ...@@ -109,6 +109,10 @@ namespace Google.Protobuf.TestProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
if (E != 0) { if (E != 0) {
output.WriteRawTag(8); output.WriteRawTag(8);
......
...@@ -148,6 +148,10 @@ namespace UnitTest.Issues.TestProtos { ...@@ -148,6 +148,10 @@ namespace UnitTest.Issues.TestProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
} }
...@@ -237,6 +241,10 @@ namespace UnitTest.Issues.TestProtos { ...@@ -237,6 +241,10 @@ namespace UnitTest.Issues.TestProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
} }
...@@ -326,6 +334,10 @@ namespace UnitTest.Issues.TestProtos { ...@@ -326,6 +334,10 @@ namespace UnitTest.Issues.TestProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
} }
...@@ -459,6 +471,10 @@ namespace UnitTest.Issues.TestProtos { ...@@ -459,6 +471,10 @@ namespace UnitTest.Issues.TestProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
if (Value != global::UnitTest.Issues.TestProtos.NegativeEnum.NEGATIVE_ENUM_ZERO) { if (Value != global::UnitTest.Issues.TestProtos.NegativeEnum.NEGATIVE_ENUM_ZERO) {
output.WriteRawTag(8); output.WriteRawTag(8);
...@@ -577,6 +593,10 @@ namespace UnitTest.Issues.TestProtos { ...@@ -577,6 +593,10 @@ namespace UnitTest.Issues.TestProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
} }
...@@ -746,6 +766,10 @@ namespace UnitTest.Issues.TestProtos { ...@@ -746,6 +766,10 @@ namespace UnitTest.Issues.TestProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
if (PrimitiveValue != 0) { if (PrimitiveValue != 0) {
output.WriteRawTag(8); output.WriteRawTag(8);
...@@ -918,6 +942,10 @@ namespace UnitTest.Issues.TestProtos { ...@@ -918,6 +942,10 @@ namespace UnitTest.Issues.TestProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
if (Item != 0) { if (Item != 0) {
output.WriteRawTag(8); output.WriteRawTag(8);
......
...@@ -299,6 +299,10 @@ namespace Google.Protobuf.DescriptorProtos { ...@@ -299,6 +299,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
file_.WriteTo(output, _repeated_file_codec); file_.WriteTo(output, _repeated_file_codec);
} }
...@@ -545,6 +549,10 @@ namespace Google.Protobuf.DescriptorProtos { ...@@ -545,6 +549,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
if (Name.Length != 0) { if (Name.Length != 0) {
output.WriteRawTag(10); output.WriteRawTag(10);
...@@ -889,6 +897,10 @@ namespace Google.Protobuf.DescriptorProtos { ...@@ -889,6 +897,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
if (Name.Length != 0) { if (Name.Length != 0) {
output.WriteRawTag(10); output.WriteRawTag(10);
...@@ -1094,6 +1106,10 @@ namespace Google.Protobuf.DescriptorProtos { ...@@ -1094,6 +1106,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
if (Start != 0) { if (Start != 0) {
output.WriteRawTag(8); output.WriteRawTag(8);
...@@ -1236,6 +1252,10 @@ namespace Google.Protobuf.DescriptorProtos { ...@@ -1236,6 +1252,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
if (Start != 0) { if (Start != 0) {
output.WriteRawTag(8); output.WriteRawTag(8);
...@@ -1475,6 +1495,10 @@ namespace Google.Protobuf.DescriptorProtos { ...@@ -1475,6 +1495,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
if (Name.Length != 0) { if (Name.Length != 0) {
output.WriteRawTag(10); output.WriteRawTag(10);
...@@ -1741,6 +1765,10 @@ namespace Google.Protobuf.DescriptorProtos { ...@@ -1741,6 +1765,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
if (Name.Length != 0) { if (Name.Length != 0) {
output.WriteRawTag(10); output.WriteRawTag(10);
...@@ -1882,6 +1910,10 @@ namespace Google.Protobuf.DescriptorProtos { ...@@ -1882,6 +1910,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
if (Name.Length != 0) { if (Name.Length != 0) {
output.WriteRawTag(10); output.WriteRawTag(10);
...@@ -2051,6 +2083,10 @@ namespace Google.Protobuf.DescriptorProtos { ...@@ -2051,6 +2083,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
if (Name.Length != 0) { if (Name.Length != 0) {
output.WriteRawTag(10); output.WriteRawTag(10);
...@@ -2226,6 +2262,10 @@ namespace Google.Protobuf.DescriptorProtos { ...@@ -2226,6 +2262,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
if (Name.Length != 0) { if (Name.Length != 0) {
output.WriteRawTag(10); output.WriteRawTag(10);
...@@ -2434,6 +2474,10 @@ namespace Google.Protobuf.DescriptorProtos { ...@@ -2434,6 +2474,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
if (Name.Length != 0) { if (Name.Length != 0) {
output.WriteRawTag(10); output.WriteRawTag(10);
...@@ -2806,6 +2850,10 @@ namespace Google.Protobuf.DescriptorProtos { ...@@ -2806,6 +2850,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
if (JavaPackage.Length != 0) { if (JavaPackage.Length != 0) {
output.WriteRawTag(10); output.WriteRawTag(10);
...@@ -3173,6 +3221,10 @@ namespace Google.Protobuf.DescriptorProtos { ...@@ -3173,6 +3221,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
if (MessageSetWireFormat != false) { if (MessageSetWireFormat != false) {
output.WriteRawTag(8); output.WriteRawTag(8);
...@@ -3414,6 +3466,10 @@ namespace Google.Protobuf.DescriptorProtos { ...@@ -3414,6 +3466,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
if (Ctype != global::Google.Protobuf.DescriptorProtos.FieldOptions.Types.CType.STRING) { if (Ctype != global::Google.Protobuf.DescriptorProtos.FieldOptions.Types.CType.STRING) {
output.WriteRawTag(8); output.WriteRawTag(8);
...@@ -3649,6 +3705,10 @@ namespace Google.Protobuf.DescriptorProtos { ...@@ -3649,6 +3705,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
if (AllowAlias != false) { if (AllowAlias != false) {
output.WriteRawTag(16); output.WriteRawTag(16);
...@@ -3797,6 +3857,10 @@ namespace Google.Protobuf.DescriptorProtos { ...@@ -3797,6 +3857,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
if (Deprecated != false) { if (Deprecated != false) {
output.WriteRawTag(8); output.WriteRawTag(8);
...@@ -3931,6 +3995,10 @@ namespace Google.Protobuf.DescriptorProtos { ...@@ -3931,6 +3995,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
if (Deprecated != false) { if (Deprecated != false) {
output.WriteRawTag(136, 2); output.WriteRawTag(136, 2);
...@@ -4065,6 +4133,10 @@ namespace Google.Protobuf.DescriptorProtos { ...@@ -4065,6 +4133,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
if (Deprecated != false) { if (Deprecated != false) {
output.WriteRawTag(136, 2); output.WriteRawTag(136, 2);
...@@ -4264,6 +4336,10 @@ namespace Google.Protobuf.DescriptorProtos { ...@@ -4264,6 +4336,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
name_.WriteTo(output, _repeated_name_codec); name_.WriteTo(output, _repeated_name_codec);
if (IdentifierValue.Length != 0) { if (IdentifierValue.Length != 0) {
...@@ -4470,6 +4546,10 @@ namespace Google.Protobuf.DescriptorProtos { ...@@ -4470,6 +4546,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
if (NamePart_.Length != 0) { if (NamePart_.Length != 0) {
output.WriteRawTag(10); output.WriteRawTag(10);
...@@ -4603,6 +4683,10 @@ namespace Google.Protobuf.DescriptorProtos { ...@@ -4603,6 +4683,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
location_.WriteTo(output, _repeated_location_codec); location_.WriteTo(output, _repeated_location_codec);
} }
...@@ -4761,6 +4845,10 @@ namespace Google.Protobuf.DescriptorProtos { ...@@ -4761,6 +4845,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash; return hash;
} }
public override string ToString() {
return pb::JsonFormatter.Default.Format(this);
}
public void WriteTo(pb::CodedOutputStream output) { public void WriteTo(pb::CodedOutputStream output) {
path_.WriteTo(output, _repeated_path_codec); path_.WriteTo(output, _repeated_path_codec);
span_.WriteTo(output, _repeated_span_codec); span_.WriteTo(output, _repeated_span_codec);
......
...@@ -58,6 +58,7 @@ class FieldGeneratorBase : public SourceGeneratorBase { ...@@ -58,6 +58,7 @@ class FieldGeneratorBase : public SourceGeneratorBase {
virtual void WriteHash(io::Printer* printer) = 0; virtual void WriteHash(io::Printer* printer) = 0;
virtual void WriteEquals(io::Printer* printer) = 0; virtual void WriteEquals(io::Printer* printer) = 0;
// Currently unused, as we use reflection to generate JSON
virtual void WriteToString(io::Printer* printer) = 0; virtual void WriteToString(io::Printer* printer) = 0;
protected: protected:
......
...@@ -117,12 +117,9 @@ void MapFieldGenerator::WriteEquals(io::Printer* printer) { ...@@ -117,12 +117,9 @@ void MapFieldGenerator::WriteEquals(io::Printer* printer) {
variables_, variables_,
"if (!$property_name$.Equals(other.$property_name$)) return false;\n"); "if (!$property_name$.Equals(other.$property_name$)) return false;\n");
} }
void MapFieldGenerator::WriteToString(io::Printer* printer) { void MapFieldGenerator::WriteToString(io::Printer* printer) {
/* // TODO: If we ever actually use ToString, we'll need to impleme this...
variables_["field_name"] = GetFieldName(descriptor_);
printer->Print(
variables_,
"PrintField(\"$field_name$\", has$property_name$, $name$_, writer);\n");*/
} }
void MapFieldGenerator::GenerateCloningCode(io::Printer* printer) { void MapFieldGenerator::GenerateCloningCode(io::Printer* printer) {
......
...@@ -429,7 +429,10 @@ void MessageGenerator::GenerateFrameworkMethods(io::Printer* printer) { ...@@ -429,7 +429,10 @@ void MessageGenerator::GenerateFrameworkMethods(io::Printer* printer) {
printer->Outdent(); printer->Outdent();
printer->Print("}\n\n"); printer->Print("}\n\n");
// TODO(jonskeet): ToString. printer->Print(
"public override string ToString() {\n"
" return pb::JsonFormatter.Default.Format(this);\n"
"}\n\n");
} }
void MessageGenerator::GenerateMessageSerializationMethods(io::Printer* printer) { void MessageGenerator::GenerateMessageSerializationMethods(io::Printer* printer) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment