Commit 637102cf authored by Jan Tattermusch's avatar Jan Tattermusch

Merge pull request #1026 from jskeet/any-conformance

Add JSON parsing to conformance tests.
parents dc633398 a4dc5968
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#endregion #endregion
using Conformance; using Conformance;
using Google.Protobuf.Reflection;
using System; using System;
using System.IO; using System.IO;
...@@ -47,16 +48,17 @@ namespace Google.Protobuf.Conformance ...@@ -47,16 +48,17 @@ namespace Google.Protobuf.Conformance
// This way we get the binary streams instead of readers/writers. // This way we get the binary streams instead of readers/writers.
var input = new BinaryReader(Console.OpenStandardInput()); var input = new BinaryReader(Console.OpenStandardInput());
var output = new BinaryWriter(Console.OpenStandardOutput()); var output = new BinaryWriter(Console.OpenStandardOutput());
var typeRegistry = TypeRegistry.FromMessages(TestAllTypes.Descriptor);
int count = 0; int count = 0;
while (RunTest(input, output)) while (RunTest(input, output, typeRegistry))
{ {
count++; count++;
} }
Console.Error.WriteLine("Received EOF after {0} tests", count); Console.Error.WriteLine("Received EOF after {0} tests", count);
} }
private static bool RunTest(BinaryReader input, BinaryWriter output) private static bool RunTest(BinaryReader input, BinaryWriter output, TypeRegistry typeRegistry)
{ {
int? size = ReadInt32(input); int? size = ReadInt32(input);
if (size == null) if (size == null)
...@@ -69,7 +71,7 @@ namespace Google.Protobuf.Conformance ...@@ -69,7 +71,7 @@ namespace Google.Protobuf.Conformance
throw new EndOfStreamException("Read " + inputData.Length + " bytes of data when expecting " + size); throw new EndOfStreamException("Read " + inputData.Length + " bytes of data when expecting " + size);
} }
ConformanceRequest request = ConformanceRequest.Parser.ParseFrom(inputData); ConformanceRequest request = ConformanceRequest.Parser.ParseFrom(inputData);
ConformanceResponse response = PerformRequest(request); ConformanceResponse response = PerformRequest(request, typeRegistry);
byte[] outputData = response.ToByteArray(); byte[] outputData = response.ToByteArray();
output.Write(outputData.Length); output.Write(outputData.Length);
output.Write(outputData); output.Write(outputData);
...@@ -77,30 +79,33 @@ namespace Google.Protobuf.Conformance ...@@ -77,30 +79,33 @@ namespace Google.Protobuf.Conformance
return true; return true;
} }
private static ConformanceResponse PerformRequest(ConformanceRequest request) private static ConformanceResponse PerformRequest(ConformanceRequest request, TypeRegistry typeRegistry)
{ {
TestAllTypes message; TestAllTypes message;
switch (request.PayloadCase) try
{ {
case ConformanceRequest.PayloadOneofCase.JsonPayload: switch (request.PayloadCase)
return new ConformanceResponse { Skipped = "JSON parsing not implemented in C# yet" }; {
case ConformanceRequest.PayloadOneofCase.ProtobufPayload: case ConformanceRequest.PayloadOneofCase.JsonPayload:
try var parser = new JsonParser(new JsonParser.Settings(20, typeRegistry));
{ message = parser.Parse<TestAllTypes>(request.JsonPayload);
break;
case ConformanceRequest.PayloadOneofCase.ProtobufPayload:
message = TestAllTypes.Parser.ParseFrom(request.ProtobufPayload); message = TestAllTypes.Parser.ParseFrom(request.ProtobufPayload);
} break;
catch (InvalidProtocolBufferException e) default:
{ throw new Exception("Unsupported request payload: " + request.PayloadCase);
return new ConformanceResponse { ParseError = e.Message }; }
} }
break; catch (InvalidProtocolBufferException e)
default: {
throw new Exception("Unsupported request payload: " + request.PayloadCase); return new ConformanceResponse { ParseError = e.Message };
} }
switch (request.RequestedOutputFormat) switch (request.RequestedOutputFormat)
{ {
case global::Conformance.WireFormat.JSON: case global::Conformance.WireFormat.JSON:
return new ConformanceResponse { JsonPayload = JsonFormatter.Default.Format(message) }; var formatter = new JsonFormatter(new JsonFormatter.Settings(false, typeRegistry));
return new ConformanceResponse { JsonPayload = formatter.Format(message) };
case global::Conformance.WireFormat.PROTOBUF: case global::Conformance.WireFormat.PROTOBUF:
return new ConformanceResponse { ProtobufPayload = message.ToByteString() }; return new ConformanceResponse { ProtobufPayload = message.ToByteString() };
default: default:
......
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