diff options
author | Jan Tattermusch <jtattermusch@users.noreply.github.com> | 2015-12-16 09:50:49 -0800 |
---|---|---|
committer | Jan Tattermusch <jtattermusch@users.noreply.github.com> | 2015-12-16 09:50:49 -0800 |
commit | 637102cf38193bf62676b4141b297479032d92a7 (patch) | |
tree | 5a9a21bee702f2a2b716438b5ddd2ad1435bde62 /csharp | |
parent | dc633398af5c87e1fe1ffcedaebd697c4775c918 (diff) | |
parent | a4dc5968b187ae034d3734131c66bbf783af62c2 (diff) | |
download | protobuf-637102cf38193bf62676b4141b297479032d92a7.tar.gz protobuf-637102cf38193bf62676b4141b297479032d92a7.tar.bz2 protobuf-637102cf38193bf62676b4141b297479032d92a7.zip |
Merge pull request #1026 from jskeet/any-conformance
Add JSON parsing to conformance tests.
Diffstat (limited to 'csharp')
-rw-r--r-- | csharp/src/Google.Protobuf.Conformance/Program.cs | 43 |
1 files changed, 24 insertions, 19 deletions
diff --git a/csharp/src/Google.Protobuf.Conformance/Program.cs b/csharp/src/Google.Protobuf.Conformance/Program.cs index af92da9f..8f72c8f9 100644 --- a/csharp/src/Google.Protobuf.Conformance/Program.cs +++ b/csharp/src/Google.Protobuf.Conformance/Program.cs @@ -31,6 +31,7 @@ #endregion using Conformance; +using Google.Protobuf.Reflection; using System; using System.IO; @@ -47,16 +48,17 @@ namespace Google.Protobuf.Conformance // This way we get the binary streams instead of readers/writers. var input = new BinaryReader(Console.OpenStandardInput()); var output = new BinaryWriter(Console.OpenStandardOutput()); + var typeRegistry = TypeRegistry.FromMessages(TestAllTypes.Descriptor); int count = 0; - while (RunTest(input, output)) + while (RunTest(input, output, typeRegistry)) { 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); if (size == null) @@ -69,7 +71,7 @@ namespace Google.Protobuf.Conformance throw new EndOfStreamException("Read " + inputData.Length + " bytes of data when expecting " + size); } ConformanceRequest request = ConformanceRequest.Parser.ParseFrom(inputData); - ConformanceResponse response = PerformRequest(request); + ConformanceResponse response = PerformRequest(request, typeRegistry); byte[] outputData = response.ToByteArray(); output.Write(outputData.Length); output.Write(outputData); @@ -77,30 +79,33 @@ namespace Google.Protobuf.Conformance return true; } - private static ConformanceResponse PerformRequest(ConformanceRequest request) + private static ConformanceResponse PerformRequest(ConformanceRequest request, TypeRegistry typeRegistry) { TestAllTypes message; - switch (request.PayloadCase) + try { - case ConformanceRequest.PayloadOneofCase.JsonPayload: - return new ConformanceResponse { Skipped = "JSON parsing not implemented in C# yet" }; - case ConformanceRequest.PayloadOneofCase.ProtobufPayload: - try - { + switch (request.PayloadCase) + { + case ConformanceRequest.PayloadOneofCase.JsonPayload: + 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); - } - catch (InvalidProtocolBufferException e) - { - return new ConformanceResponse { ParseError = e.Message }; - } - break; - default: - throw new Exception("Unsupported request payload: " + request.PayloadCase); + break; + default: + throw new Exception("Unsupported request payload: " + request.PayloadCase); + } + } + catch (InvalidProtocolBufferException e) + { + return new ConformanceResponse { ParseError = e.Message }; } switch (request.RequestedOutputFormat) { 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: return new ConformanceResponse { ProtobufPayload = message.ToByteString() }; default: |