Reflection-based converter from messages to JSON.

Instances of this class are thread-safe, with no mutable state.

This is a simple start to get JSON formatting working. As it's reflection-based, it's not as quick as baking calls into generated messages - but is a simpler implementation. (This code is generally not heavily optimized.)

Пример #1
0
        public void DefaultValues_WhenOmitted()
        {
            var formatter = new JsonFormatter(new JsonFormatter.Settings(formatDefaultValues: false));

            AssertJson("{ }", formatter.Format(new ForeignMessage()));
            AssertJson("{ }", formatter.Format(new TestAllTypes()));
            AssertJson("{ }", formatter.Format(new TestMap()));
        }
Пример #2
0
 public void DefaultValues_WhenIncluded()
 {
     var formatter = new JsonFormatter(new JsonFormatter.Settings(formatDefaultValues: true));
     AssertJson("{ 'c': 0 }", formatter.Format(new ForeignMessage()));
 }
Пример #3
0
 public void AnyMessageType()
 {
     var formatter = new JsonFormatter(new JsonFormatter.Settings(false, TypeRegistry.FromMessages(TestAllTypes.Descriptor)));
     var message = new TestAllTypes { SingleInt32 = 10, SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 20 } };
     var any = Any.Pack(message);
     AssertJson("{ '@type': 'type.googleapis.com/protobuf_unittest.TestAllTypes', 'singleInt32': 10, 'singleNestedMessage': { 'bb': 20 } }", formatter.Format(any));
 }
Пример #4
0
        public void AnyNested()
        {
            var registry = TypeRegistry.FromMessages(TestWellKnownTypes.Descriptor, TestAllTypes.Descriptor);
            var formatter = new JsonFormatter(new JsonFormatter.Settings(false, registry));

            // Nest an Any as the value of an Any.
            var doubleNestedMessage = new TestAllTypes { SingleInt32 = 20 };
            var nestedMessage = Any.Pack(doubleNestedMessage);
            var message = new TestWellKnownTypes { AnyField = Any.Pack(nestedMessage) };
            AssertJson("{ 'anyField': { '@type': 'type.googleapis.com/google.protobuf.Any', 'value': { '@type': 'type.googleapis.com/protobuf_unittest.TestAllTypes', 'singleInt32': 20 } } }",
                formatter.Format(message));
        }
Пример #5
0
 public void AnyWellKnownType()
 {
     var formatter = new JsonFormatter(new JsonFormatter.Settings(false, TypeRegistry.FromMessages(Timestamp.Descriptor)));
     var timestamp = new DateTime(1673, 6, 19, 12, 34, 56, DateTimeKind.Utc).ToTimestamp();
     var any = Any.Pack(timestamp);
     AssertJson("{ '@type': 'type.googleapis.com/google.protobuf.Timestamp', 'value': '1673-06-19T12:34:56Z' }", formatter.Format(any));
 }
Пример #6
0
 public void OutputIsInNumericFieldOrder_WithDefaults()
 {
     var formatter = new JsonFormatter(new JsonFormatter.Settings(true));
     var message = new TestJsonFieldOrdering();
     AssertJson("{ 'plainString': '', 'plainInt32': 0 }", formatter.Format(message));
     message = new TestJsonFieldOrdering { O1Int32 = 5, O2String = "o2", PlainInt32 = 10, PlainString = "plain" };
     AssertJson("{ 'plainString': 'plain', 'o2String': 'o2', 'plainInt32': 10, 'o1Int32': 5 }", formatter.Format(message));
     message = new TestJsonFieldOrdering { O1String = "", O2Int32 = 0, PlainInt32 = 10, PlainString = "plain" };
     AssertJson("{ 'plainString': 'plain', 'o1String': '', 'plainInt32': 10, 'o2Int32': 0 }", formatter.Format(message));
 }
Пример #7
0
 public void WrapperFormatting_IncludeNull()
 {
     // The actual JSON here is very large because there are lots of fields. Just test a couple of them.
     var message = new TestWellKnownTypes { Int32Field = 10 };
     var formatter = new JsonFormatter(new JsonFormatter.Settings(true));
     var actualJson = formatter.Format(message);
     Assert.IsTrue(actualJson.Contains("\"int64Field\": null"));
     Assert.IsFalse(actualJson.Contains("\"int32Field\": null"));
 }
Пример #8
0
        public void Oneof(string fooStringValue, string expectedJson)
        {
            var message = new TestOneof();
            if (fooStringValue != null)
            {
                message.FooString = fooStringValue;
            }

            // We should get the same result both with and without "format default values".
            var formatter = new JsonFormatter(new JsonFormatter.Settings(false));
            AssertJson(expectedJson, formatter.Format(message));
            formatter = new JsonFormatter(new JsonFormatter.Settings(true));
            AssertJson(expectedJson, formatter.Format(message));
        }
Пример #9
0
 public void AnyMessageType_CustomPrefix()
 {
     var formatter = new JsonFormatter(new JsonFormatter.Settings(false, TypeRegistry.FromMessages(TestAllTypes.Descriptor)));
     var message = new TestAllTypes { SingleInt32 = 10 };
     var any = Any.Pack(message, "foo.bar/baz");
     AssertJson("{ '@type': 'foo.bar/baz/protobuf_unittest.TestAllTypes', 'singleInt32': 10 }", formatter.Format(any));
 }