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())); }
public void NullValueNotInOneof_FormatDefaults() { var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true)); var message = new NullValueNotInOneof(); AssertJson("{ 'nullValue': null }", formatter.Format(message)); }
public void WithFormatDefaultValues_DoesNotAffectOneofFields() { var message = new TestOneof(); var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true)); var json = formatter.Format(message); AssertJson("{ }", json); }
public void WithFormatDefaultValues_DoesNotAffectMessageFields() { var message = new TestAllTypes(); var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true)); var json = formatter.Format(message); Assert.IsFalse(json.Contains("\"singleNestedMessage\"")); Assert.IsFalse(json.Contains("\"singleForeignMessage\"")); Assert.IsFalse(json.Contains("\"singleImportMessage\"")); }
public void WithFormatDefaultValues_DoesNotAffectProto2Fields() { var message = new TestProtos.Proto2.ForeignMessage { C = 0 }; var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true)); var json = formatter.Format(message); // The specified field is formatted, but the non-specified field (d) is not. AssertJson("{ 'c': 0 }", json); }
public void WithFormatDefaultValues_DoesNotAffectProto3OptionalFields() { var message = new TestProto3Optional { OptionalInt32 = 0 }; var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true)); var json = formatter.Format(message); // The non-optional proto3 fields are formatted, as is the optional-but-specified field. AssertJson("{ 'optionalInt32': 0, 'singularInt32': 0, 'singularInt64': '0' }", json); }
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")); }
public void WrapperFormatting_FormatDefaultValuesDoesNotFormatNull() { // 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(JsonFormatter.Settings.Default.WithFormatDefaultValues(true)); var actualJson = formatter.Format(message); // This *used* to include "int64Field": null, but that was a bug. // WithDefaultValues should not affect message fields, including wrapper types. Assert.IsFalse(actualJson.Contains("\"int64Field\": null")); Assert.IsTrue(actualJson.Contains("\"int32Field\": 10")); }
public void Any_WellKnownType() { var registry = TypeRegistry.FromMessages(Timestamp.Descriptor); var formatter = new JsonFormatter(new JsonFormatter.Settings(false, registry)); var timestamp = new DateTime(1673, 6, 19, 12, 34, 56, DateTimeKind.Utc).ToTimestamp(); var original = Any.Pack(timestamp); var json = formatter.Format(original); // This is tested in JsonFormatterTest var parser = new JsonParser(new JsonParser.Settings(10, registry)); Assert.AreEqual(original, parser.Parse <Any>(json)); string valueFirstJson = "{ \"value\": \"1673-06-19T12:34:56Z\", \"@type\": \"type.googleapis.com/google.protobuf.Timestamp\" }"; Assert.AreEqual(original, parser.Parse <Any>(valueFirstJson)); }
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)); }
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)); }
public void EnumAsInt() { var message = new TestAllTypes { SingleForeignEnum = ForeignEnum.ForeignBar, RepeatedForeignEnum = { ForeignEnum.ForeignBaz, (ForeignEnum)100, ForeignEnum.ForeignFoo } }; var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatEnumsAsIntegers(true)); var actualText = formatter.Format(message); var expectedText = "{ " + "'singleForeignEnum': 5, " + "'repeatedForeignEnum': [ 6, 100, 4 ]" + " }"; AssertJson(expectedText, actualText); }
public void Any_Nested() { var registry = TypeRegistry.FromMessages(TestWellKnownTypes.Descriptor, TestAllTypes.Descriptor); var formatter = new JsonFormatter(new JsonFormatter.Settings(false, registry)); var parser = new JsonParser(new JsonParser.Settings(10, registry)); var doubleNestedMessage = new TestAllTypes { SingleInt32 = 20 }; var nestedMessage = Any.Pack(doubleNestedMessage); var message = new TestWellKnownTypes { AnyField = Any.Pack(nestedMessage) }; var json = formatter.Format(message); // Use the descriptor-based parser just for a change. Assert.AreEqual(message, parser.Parse(json, TestWellKnownTypes.Descriptor)); }
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)); }
public void Any_RegularMessage() { var registry = TypeRegistry.FromMessages(TestAllTypes.Descriptor); 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 original = Any.Pack(message); var json = formatter.Format(original); // This is tested in JsonFormatterTest var parser = new JsonParser(new JsonParser.Settings(10, registry)); Assert.AreEqual(original, parser.Parse <Any>(json)); string valueFirstJson = "{ \"singleInt32\": 10, \"singleNestedMessage\": { \"bb\": 20 }, \"@type\": \"type.googleapis.com/protobuf_unittest.TestAllTypes\" }"; Assert.AreEqual(original, parser.Parse <Any>(valueFirstJson)); }
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)); }
/// <summary> /// Converts a message to JSON for diagnostic purposes with no extra context. /// </summary> /// <remarks> /// <para> /// This differs from calling <see cref="Format(IMessage)"/> on the default JSON /// formatter in its handling of <see cref="Any"/>. As no type registry is available /// in <see cref="object.ToString"/> calls, the normal way of resolving the type of /// an <c>Any</c> message cannot be applied. Instead, a JSON property named <c>@value</c> /// is included with the base64 data from the <see cref="Any.Value"/> property of the message. /// </para> /// <para>The value returned by this method is only designed to be used for diagnostic /// purposes. It may not be parsable by <see cref="JsonParser"/>, and may not be parsable /// by other Protocol Buffer implementations.</para> /// </remarks> /// <param name="message">The message to format for diagnostic purposes.</param> /// <returns>The diagnostic-only JSON representation of the message</returns> public static string ToDiagnosticString(IMessage message) { ProtoPreconditions.CheckNotNull(message, nameof(message)); return(diagnosticFormatter.Format(message)); }
public void DefaultValues_WhenIncluded() { var formatter = new JsonFormatter(new JsonFormatter.Settings(formatDefaultValues: true)); AssertJson("{ 'c': 0 }", formatter.Format(new ForeignMessage())); }
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)); }
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)); }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { writer.WriteRawValue(_formatter.Format((IMessage)value)); }