aboutsummaryrefslogtreecommitdiff
path: root/csharp/src/ProtocolBuffers
diff options
context:
space:
mode:
authorJon Skeet <skeet@pobox.com>2015-07-14 09:50:57 +0100
committerJon Skeet <skeet@pobox.com>2015-07-14 09:50:57 +0100
commit9440a2abe3e3520fd6a1288f794f8bad0b26cb31 (patch)
treea74d26d051a53210682b76b0cc66b87eec3ecd65 /csharp/src/ProtocolBuffers
parentb918dc1b17162465a1708cbf9f556b5a193196b2 (diff)
parent0f34daad07153a66b492ab938e85f17e82b91706 (diff)
downloadprotobuf-9440a2abe3e3520fd6a1288f794f8bad0b26cb31.tar.gz
protobuf-9440a2abe3e3520fd6a1288f794f8bad0b26cb31.tar.bz2
protobuf-9440a2abe3e3520fd6a1288f794f8bad0b26cb31.zip
Merge pull request #582 from jskeet/csharp-json
JSON formatting in C#
Diffstat (limited to 'csharp/src/ProtocolBuffers')
-rw-r--r--csharp/src/ProtocolBuffers/DescriptorProtos/DescriptorProtoFile.cs132
-rw-r--r--csharp/src/ProtocolBuffers/Descriptors/EnumDescriptor.cs1
-rw-r--r--csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs27
-rw-r--r--csharp/src/ProtocolBuffers/FieldAccess/IFieldAccessor.cs2
-rw-r--r--csharp/src/ProtocolBuffers/FieldAccess/OneofAccessor.cs57
-rw-r--r--csharp/src/ProtocolBuffers/FieldAccess/ReflectionUtil.cs15
-rw-r--r--csharp/src/ProtocolBuffers/IMessage.cs6
-rw-r--r--csharp/src/ProtocolBuffers/JsonFormatter.cs579
-rw-r--r--csharp/src/ProtocolBuffers/ProtocolBuffers.csproj1
9 files changed, 749 insertions, 71 deletions
diff --git a/csharp/src/ProtocolBuffers/DescriptorProtos/DescriptorProtoFile.cs b/csharp/src/ProtocolBuffers/DescriptorProtos/DescriptorProtoFile.cs
index c87b367a..52f0c4b2 100644
--- a/csharp/src/ProtocolBuffers/DescriptorProtos/DescriptorProtoFile.cs
+++ b/csharp/src/ProtocolBuffers/DescriptorProtos/DescriptorProtoFile.cs
@@ -161,70 +161,70 @@ namespace Google.Protobuf.DescriptorProtos {
});
internal__static_google_protobuf_FileDescriptorSet__FieldAccessorTable =
new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.FileDescriptorSet), descriptor.MessageTypes[0],
- new string[] { "File", });
+ new string[] { "File", }, new string[] { });
internal__static_google_protobuf_FileDescriptorProto__FieldAccessorTable =
new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.FileDescriptorProto), descriptor.MessageTypes[1],
- new string[] { "Name", "Package", "Dependency", "PublicDependency", "WeakDependency", "MessageType", "EnumType", "Service", "Extension", "Options", "SourceCodeInfo", "Syntax", });
+ new string[] { "Name", "Package", "Dependency", "PublicDependency", "WeakDependency", "MessageType", "EnumType", "Service", "Extension", "Options", "SourceCodeInfo", "Syntax", }, new string[] { });
internal__static_google_protobuf_DescriptorProto__FieldAccessorTable =
new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.DescriptorProto), descriptor.MessageTypes[2],
- new string[] { "Name", "Field", "Extension", "NestedType", "EnumType", "ExtensionRange", "OneofDecl", "Options", "ReservedRange", "ReservedName", });
+ new string[] { "Name", "Field", "Extension", "NestedType", "EnumType", "ExtensionRange", "OneofDecl", "Options", "ReservedRange", "ReservedName", }, new string[] { });
internal__static_google_protobuf_DescriptorProto_ExtensionRange__FieldAccessorTable =
new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.DescriptorProto.Types.ExtensionRange), descriptor.MessageTypes[2].NestedTypes[0],
- new string[] { "Start", "End", });
+ new string[] { "Start", "End", }, new string[] { });
internal__static_google_protobuf_DescriptorProto_ReservedRange__FieldAccessorTable =
new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.DescriptorProto.Types.ReservedRange), descriptor.MessageTypes[2].NestedTypes[1],
- new string[] { "Start", "End", });
+ new string[] { "Start", "End", }, new string[] { });
internal__static_google_protobuf_FieldDescriptorProto__FieldAccessorTable =
new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.FieldDescriptorProto), descriptor.MessageTypes[3],
- new string[] { "Name", "Number", "Label", "Type", "TypeName", "Extendee", "DefaultValue", "OneofIndex", "Options", });
+ new string[] { "Name", "Number", "Label", "Type", "TypeName", "Extendee", "DefaultValue", "OneofIndex", "Options", }, new string[] { });
internal__static_google_protobuf_OneofDescriptorProto__FieldAccessorTable =
new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.OneofDescriptorProto), descriptor.MessageTypes[4],
- new string[] { "Name", });
+ new string[] { "Name", }, new string[] { });
internal__static_google_protobuf_EnumDescriptorProto__FieldAccessorTable =
new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.EnumDescriptorProto), descriptor.MessageTypes[5],
- new string[] { "Name", "Value", "Options", });
+ new string[] { "Name", "Value", "Options", }, new string[] { });
internal__static_google_protobuf_EnumValueDescriptorProto__FieldAccessorTable =
new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.EnumValueDescriptorProto), descriptor.MessageTypes[6],
- new string[] { "Name", "Number", "Options", });
+ new string[] { "Name", "Number", "Options", }, new string[] { });
internal__static_google_protobuf_ServiceDescriptorProto__FieldAccessorTable =
new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.ServiceDescriptorProto), descriptor.MessageTypes[7],
- new string[] { "Name", "Method", "Options", });
+ new string[] { "Name", "Method", "Options", }, new string[] { });
internal__static_google_protobuf_MethodDescriptorProto__FieldAccessorTable =
new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.MethodDescriptorProto), descriptor.MessageTypes[8],
- new string[] { "Name", "InputType", "OutputType", "Options", "ClientStreaming", "ServerStreaming", });
+ new string[] { "Name", "InputType", "OutputType", "Options", "ClientStreaming", "ServerStreaming", }, new string[] { });
internal__static_google_protobuf_FileOptions__FieldAccessorTable =
new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.FileOptions), descriptor.MessageTypes[9],
- new string[] { "JavaPackage", "JavaOuterClassname", "JavaMultipleFiles", "JavaGenerateEqualsAndHash", "JavaStringCheckUtf8", "OptimizeFor", "GoPackage", "CcGenericServices", "JavaGenericServices", "PyGenericServices", "Deprecated", "CcEnableArenas", "ObjcClassPrefix", "CsharpNamespace", "UninterpretedOption", });
+ new string[] { "JavaPackage", "JavaOuterClassname", "JavaMultipleFiles", "JavaGenerateEqualsAndHash", "JavaStringCheckUtf8", "OptimizeFor", "GoPackage", "CcGenericServices", "JavaGenericServices", "PyGenericServices", "Deprecated", "CcEnableArenas", "ObjcClassPrefix", "CsharpNamespace", "UninterpretedOption", }, new string[] { });
internal__static_google_protobuf_MessageOptions__FieldAccessorTable =
new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.MessageOptions), descriptor.MessageTypes[10],
- new string[] { "MessageSetWireFormat", "NoStandardDescriptorAccessor", "Deprecated", "MapEntry", "UninterpretedOption", });
+ new string[] { "MessageSetWireFormat", "NoStandardDescriptorAccessor", "Deprecated", "MapEntry", "UninterpretedOption", }, new string[] { });
internal__static_google_protobuf_FieldOptions__FieldAccessorTable =
new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.FieldOptions), descriptor.MessageTypes[11],
- new string[] { "Ctype", "Packed", "Jstype", "Lazy", "Deprecated", "Weak", "UninterpretedOption", });
+ new string[] { "Ctype", "Packed", "Jstype", "Lazy", "Deprecated", "Weak", "UninterpretedOption", }, new string[] { });
internal__static_google_protobuf_EnumOptions__FieldAccessorTable =
new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.EnumOptions), descriptor.MessageTypes[12],
- new string[] { "AllowAlias", "Deprecated", "UninterpretedOption", });
+ new string[] { "AllowAlias", "Deprecated", "UninterpretedOption", }, new string[] { });
internal__static_google_protobuf_EnumValueOptions__FieldAccessorTable =
new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.EnumValueOptions), descriptor.MessageTypes[13],
- new string[] { "Deprecated", "UninterpretedOption", });
+ new string[] { "Deprecated", "UninterpretedOption", }, new string[] { });
internal__static_google_protobuf_ServiceOptions__FieldAccessorTable =
new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.ServiceOptions), descriptor.MessageTypes[14],
- new string[] { "Deprecated", "UninterpretedOption", });
+ new string[] { "Deprecated", "UninterpretedOption", }, new string[] { });
internal__static_google_protobuf_MethodOptions__FieldAccessorTable =
new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.MethodOptions), descriptor.MessageTypes[15],
- new string[] { "Deprecated", "UninterpretedOption", });
+ new string[] { "Deprecated", "UninterpretedOption", }, new string[] { });
internal__static_google_protobuf_UninterpretedOption__FieldAccessorTable =
new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.UninterpretedOption), descriptor.MessageTypes[16],
- new string[] { "Name", "IdentifierValue", "PositiveIntValue", "NegativeIntValue", "DoubleValue", "StringValue", "AggregateValue", });
+ new string[] { "Name", "IdentifierValue", "PositiveIntValue", "NegativeIntValue", "DoubleValue", "StringValue", "AggregateValue", }, new string[] { });
internal__static_google_protobuf_UninterpretedOption_NamePart__FieldAccessorTable =
new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.UninterpretedOption.Types.NamePart), descriptor.MessageTypes[16].NestedTypes[0],
- new string[] { "NamePart_", "IsExtension", });
+ new string[] { "NamePart_", "IsExtension", }, new string[] { });
internal__static_google_protobuf_SourceCodeInfo__FieldAccessorTable =
new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.SourceCodeInfo), descriptor.MessageTypes[17],
- new string[] { "Location", });
+ new string[] { "Location", }, new string[] { });
internal__static_google_protobuf_SourceCodeInfo_Location__FieldAccessorTable =
new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.SourceCodeInfo.Types.Location), descriptor.MessageTypes[17].NestedTypes[0],
- new string[] { "Path", "Span", "LeadingComments", "TrailingComments", "LeadingDetachedComments", });
+ new string[] { "Path", "Span", "LeadingComments", "TrailingComments", "LeadingDetachedComments", }, new string[] { });
}
#endregion
@@ -299,6 +299,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash;
}
+ public override string ToString() {
+ return pb::JsonFormatter.Default.Format(this);
+ }
+
public void WriteTo(pb::CodedOutputStream output) {
file_.WriteTo(output, _repeated_file_codec);
}
@@ -545,6 +549,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash;
}
+ public override string ToString() {
+ return pb::JsonFormatter.Default.Format(this);
+ }
+
public void WriteTo(pb::CodedOutputStream output) {
if (Name.Length != 0) {
output.WriteRawTag(10);
@@ -889,6 +897,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash;
}
+ public override string ToString() {
+ return pb::JsonFormatter.Default.Format(this);
+ }
+
public void WriteTo(pb::CodedOutputStream output) {
if (Name.Length != 0) {
output.WriteRawTag(10);
@@ -1094,6 +1106,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash;
}
+ public override string ToString() {
+ return pb::JsonFormatter.Default.Format(this);
+ }
+
public void WriteTo(pb::CodedOutputStream output) {
if (Start != 0) {
output.WriteRawTag(8);
@@ -1236,6 +1252,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash;
}
+ public override string ToString() {
+ return pb::JsonFormatter.Default.Format(this);
+ }
+
public void WriteTo(pb::CodedOutputStream output) {
if (Start != 0) {
output.WriteRawTag(8);
@@ -1475,6 +1495,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash;
}
+ public override string ToString() {
+ return pb::JsonFormatter.Default.Format(this);
+ }
+
public void WriteTo(pb::CodedOutputStream output) {
if (Name.Length != 0) {
output.WriteRawTag(10);
@@ -1741,6 +1765,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash;
}
+ public override string ToString() {
+ return pb::JsonFormatter.Default.Format(this);
+ }
+
public void WriteTo(pb::CodedOutputStream output) {
if (Name.Length != 0) {
output.WriteRawTag(10);
@@ -1882,6 +1910,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash;
}
+ public override string ToString() {
+ return pb::JsonFormatter.Default.Format(this);
+ }
+
public void WriteTo(pb::CodedOutputStream output) {
if (Name.Length != 0) {
output.WriteRawTag(10);
@@ -2051,6 +2083,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash;
}
+ public override string ToString() {
+ return pb::JsonFormatter.Default.Format(this);
+ }
+
public void WriteTo(pb::CodedOutputStream output) {
if (Name.Length != 0) {
output.WriteRawTag(10);
@@ -2226,6 +2262,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash;
}
+ public override string ToString() {
+ return pb::JsonFormatter.Default.Format(this);
+ }
+
public void WriteTo(pb::CodedOutputStream output) {
if (Name.Length != 0) {
output.WriteRawTag(10);
@@ -2434,6 +2474,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash;
}
+ public override string ToString() {
+ return pb::JsonFormatter.Default.Format(this);
+ }
+
public void WriteTo(pb::CodedOutputStream output) {
if (Name.Length != 0) {
output.WriteRawTag(10);
@@ -2806,6 +2850,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash;
}
+ public override string ToString() {
+ return pb::JsonFormatter.Default.Format(this);
+ }
+
public void WriteTo(pb::CodedOutputStream output) {
if (JavaPackage.Length != 0) {
output.WriteRawTag(10);
@@ -3173,6 +3221,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash;
}
+ public override string ToString() {
+ return pb::JsonFormatter.Default.Format(this);
+ }
+
public void WriteTo(pb::CodedOutputStream output) {
if (MessageSetWireFormat != false) {
output.WriteRawTag(8);
@@ -3414,6 +3466,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash;
}
+ public override string ToString() {
+ return pb::JsonFormatter.Default.Format(this);
+ }
+
public void WriteTo(pb::CodedOutputStream output) {
if (Ctype != global::Google.Protobuf.DescriptorProtos.FieldOptions.Types.CType.STRING) {
output.WriteRawTag(8);
@@ -3649,6 +3705,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash;
}
+ public override string ToString() {
+ return pb::JsonFormatter.Default.Format(this);
+ }
+
public void WriteTo(pb::CodedOutputStream output) {
if (AllowAlias != false) {
output.WriteRawTag(16);
@@ -3797,6 +3857,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash;
}
+ public override string ToString() {
+ return pb::JsonFormatter.Default.Format(this);
+ }
+
public void WriteTo(pb::CodedOutputStream output) {
if (Deprecated != false) {
output.WriteRawTag(8);
@@ -3931,6 +3995,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash;
}
+ public override string ToString() {
+ return pb::JsonFormatter.Default.Format(this);
+ }
+
public void WriteTo(pb::CodedOutputStream output) {
if (Deprecated != false) {
output.WriteRawTag(136, 2);
@@ -4065,6 +4133,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash;
}
+ public override string ToString() {
+ return pb::JsonFormatter.Default.Format(this);
+ }
+
public void WriteTo(pb::CodedOutputStream output) {
if (Deprecated != false) {
output.WriteRawTag(136, 2);
@@ -4264,6 +4336,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash;
}
+ public override string ToString() {
+ return pb::JsonFormatter.Default.Format(this);
+ }
+
public void WriteTo(pb::CodedOutputStream output) {
name_.WriteTo(output, _repeated_name_codec);
if (IdentifierValue.Length != 0) {
@@ -4470,6 +4546,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash;
}
+ public override string ToString() {
+ return pb::JsonFormatter.Default.Format(this);
+ }
+
public void WriteTo(pb::CodedOutputStream output) {
if (NamePart_.Length != 0) {
output.WriteRawTag(10);
@@ -4603,6 +4683,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash;
}
+ public override string ToString() {
+ return pb::JsonFormatter.Default.Format(this);
+ }
+
public void WriteTo(pb::CodedOutputStream output) {
location_.WriteTo(output, _repeated_location_codec);
}
@@ -4761,6 +4845,10 @@ namespace Google.Protobuf.DescriptorProtos {
return hash;
}
+ public override string ToString() {
+ return pb::JsonFormatter.Default.Format(this);
+ }
+
public void WriteTo(pb::CodedOutputStream output) {
path_.WriteTo(output, _repeated_path_codec);
span_.WriteTo(output, _repeated_span_codec);
diff --git a/csharp/src/ProtocolBuffers/Descriptors/EnumDescriptor.cs b/csharp/src/ProtocolBuffers/Descriptors/EnumDescriptor.cs
index a6db5268..57860e61 100644
--- a/csharp/src/ProtocolBuffers/Descriptors/EnumDescriptor.cs
+++ b/csharp/src/ProtocolBuffers/Descriptors/EnumDescriptor.cs
@@ -89,6 +89,7 @@ namespace Google.Protobuf.Descriptors
/// <summary>
/// Finds an enum value by number. If multiple enum values have the
/// same number, this returns the first defined value with that number.
+ /// If there is no value for the given number, this returns <c>null</c>.
/// </summary>
public EnumValueDescriptor FindValueByNumber(int number)
{
diff --git a/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs b/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs
index 57ea9c87..80be93f5 100644
--- a/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs
+++ b/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs
@@ -42,6 +42,7 @@ namespace Google.Protobuf.FieldAccess
public sealed class FieldAccessorTable
{
private readonly ReadOnlyCollection<IFieldAccessor> accessors;
+ private readonly ReadOnlyCollection<OneofAccessor> oneofs;
private readonly MessageDescriptor descriptor;
/// <summary>
@@ -51,7 +52,7 @@ namespace Google.Protobuf.FieldAccess
/// <param name="type">The CLR type for the message.</param>
/// <param name="descriptor">The type's descriptor</param>
/// <param name="propertyNames">The Pascal-case names of all the field-based properties in the message.</param>
- public FieldAccessorTable(Type type, MessageDescriptor descriptor, string[] propertyNames)
+ public FieldAccessorTable(Type type, MessageDescriptor descriptor, string[] propertyNames, string[] oneofPropertyNames)
{
this.descriptor = descriptor;
var accessorsArray = new IFieldAccessor[descriptor.Fields.Count];
@@ -65,7 +66,13 @@ namespace Google.Protobuf.FieldAccess
: (IFieldAccessor) new SingleFieldAccessor(type, name, field);
}
accessors = new ReadOnlyCollection<IFieldAccessor>(accessorsArray);
- // TODO(jonskeet): Oneof support
+ var oneofsArray = new OneofAccessor[descriptor.Oneofs.Count];
+ for (int i = 0; i < oneofsArray.Length; i++)
+ {
+ var oneof = descriptor.Oneofs[i];
+ oneofsArray[i] = new OneofAccessor(type, oneofPropertyNames[i], oneof);
+ }
+ oneofs = new ReadOnlyCollection<OneofAccessor>(oneofsArray);
}
// TODO: Validate the name here... should possibly make this type a more "general reflection access" type,
@@ -75,6 +82,10 @@ namespace Google.Protobuf.FieldAccess
/// </summary>
public ReadOnlyCollection<IFieldAccessor> Accessors { get { return accessors; } }
+ public ReadOnlyCollection<OneofAccessor> Oneofs { get { return oneofs; } }
+
+ // TODO: Review this, as it's easy to get confused between FieldNumber and Index.
+ // Currently only used to get an accessor related to a oneof... maybe just make that simpler?
public IFieldAccessor this[int fieldNumber]
{
get
@@ -83,17 +94,5 @@ namespace Google.Protobuf.FieldAccess
return accessors[field.Index];
}
}
-
- internal IFieldAccessor this[FieldDescriptor field]
- {
- get
- {
- if (field.ContainingType != descriptor)
- {
- throw new ArgumentException("FieldDescriptor does not match message type.");
- }
- return accessors[field.Index];
- }
- }
}
} \ No newline at end of file
diff --git a/csharp/src/ProtocolBuffers/FieldAccess/IFieldAccessor.cs b/csharp/src/ProtocolBuffers/FieldAccess/IFieldAccessor.cs
index 77e7146d..d1727cb4 100644
--- a/csharp/src/ProtocolBuffers/FieldAccess/IFieldAccessor.cs
+++ b/csharp/src/ProtocolBuffers/FieldAccess/IFieldAccessor.cs
@@ -44,6 +44,8 @@ namespace Google.Protobuf.FieldAccess
/// </summary>
FieldDescriptor Descriptor { get; }
+ // TODO: Should the argument type for these messages by IReflectedMessage?
+
/// <summary>
/// Clears the field in the specified message. (For repeated fields,
/// this clears the list.)
diff --git a/csharp/src/ProtocolBuffers/FieldAccess/OneofAccessor.cs b/csharp/src/ProtocolBuffers/FieldAccess/OneofAccessor.cs
index feaa6232..590b6309 100644
--- a/csharp/src/ProtocolBuffers/FieldAccess/OneofAccessor.cs
+++ b/csharp/src/ProtocolBuffers/FieldAccess/OneofAccessor.cs
@@ -30,62 +30,57 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion
+using Google.Protobuf.Descriptors;
+using System;
+using System.Reflection;
+
namespace Google.Protobuf.FieldAccess
{
- // TODO(jonskeet): Add "new" oneof API support
-
/// <summary>
- /// Access for an oneof
+ /// Reflection access for a oneof, allowing clear and "get case" actions.
/// </summary>
- internal class OneofAccessor<TMessage> where TMessage : IMessage<TMessage>
+ public sealed class OneofAccessor
{
- /*
- private readonly Func<TMessage, object> caseDelegate;
- private readonly Func<TBuilder, IBuilder> clearDelegate;
- private MessageDescriptor descriptor;
+ private readonly Func<object, int> caseDelegate;
+ private readonly Action<object> clearDelegate;
+ private OneofDescriptor descriptor;
- internal OneofAccessor(MessageDescriptor descriptor, string name)
+ internal OneofAccessor(Type type, string propertyName, OneofDescriptor descriptor)
{
- this.descriptor = descriptor;
- MethodInfo clearMethod = typeof(TBuilder).GetMethod("Clear" + name);
- PropertyInfo caseProperty = typeof(TMessage).GetProperty(name + "Case");
- if (clearMethod == null || caseProperty == null)
+ PropertyInfo property = type.GetProperty(propertyName + "Case");
+ if (property == null || !property.CanRead)
{
- throw new ArgumentException("Not all required properties/methods available for oneof");
+ throw new ArgumentException("Not all required properties/methods available");
}
-
+ this.descriptor = descriptor;
+ caseDelegate = ReflectionUtil.CreateFuncObjectT<int>(property.GetGetMethod());
- clearDelegate = ReflectionUtil.CreateDelegateFunc<TBuilder, IBuilder>(clearMethod);
- caseDelegate = ReflectionUtil.CreateUpcastDelegate<TMessage>(caseProperty.GetGetMethod());
+ this.descriptor = descriptor;
+ MethodInfo clearMethod = type.GetMethod("Clear" + propertyName);
+ clearDelegate = ReflectionUtil.CreateActionObject(clearMethod);
}
- /// <summary>
- /// Indicates whether the specified message has set any field in the oneof.
- /// </summary>
- public bool Has(TMessage message)
- {
- return ((int) caseDelegate(message) != 0);
- }
+ public OneofDescriptor Descriptor { get { return descriptor; } }
/// <summary>
- /// Clears the oneof in the specified builder.
+ /// Clears the oneof in the specified message.
/// </summary>
- public void Clear(TBuilder builder)
+ public void Clear(object message)
{
- clearDelegate(builder);
+ clearDelegate(message);
}
/// <summary>
/// Indicates which field in the oneof is set for specified message
/// </summary>
- public virtual FieldDescriptor GetOneofFieldDescriptor(TMessage message)
+ public FieldDescriptor GetCaseFieldDescriptor(object message)
{
- int fieldNumber = (int) caseDelegate(message);
+ int fieldNumber = caseDelegate(message);
if (fieldNumber > 0)
{
- return descriptor.FindFieldByNumber(fieldNumber);
+ return descriptor.ContainingType.FindFieldByNumber(fieldNumber);
}
return null;
- }*/
+ }
}
}
diff --git a/csharp/src/ProtocolBuffers/FieldAccess/ReflectionUtil.cs b/csharp/src/ProtocolBuffers/FieldAccess/ReflectionUtil.cs
index d3053920..08ef6c0c 100644
--- a/csharp/src/ProtocolBuffers/FieldAccess/ReflectionUtil.cs
+++ b/csharp/src/ProtocolBuffers/FieldAccess/ReflectionUtil.cs
@@ -63,7 +63,20 @@ namespace Google.Protobuf.FieldAccess
Expression upcast = Expression.Convert(call, typeof(object));
return Expression.Lambda<Func<object, object>>(upcast, parameter).Compile();
}
-
+
+ /// <summary>
+ /// Creates a delegate which will cast the argument to the appropriate method target type,
+ /// call the method on it, then convert the result to the specified type.
+ /// </summary>
+ internal static Func<object, T> CreateFuncObjectT<T>(MethodInfo method)
+ {
+ ParameterExpression parameter = Expression.Parameter(typeof(object), "p");
+ Expression downcast = Expression.Convert(parameter, method.DeclaringType);
+ Expression call = Expression.Call(downcast, method);
+ Expression upcast = Expression.Convert(call, typeof(T));
+ return Expression.Lambda<Func<object, T>>(upcast, parameter).Compile();
+ }
+
/// <summary>
/// Creates a delegate which will execute the given method after casting the first argument to
/// the target type of the method, and the second argument to the first parameter type of the method.
diff --git a/csharp/src/ProtocolBuffers/IMessage.cs b/csharp/src/ProtocolBuffers/IMessage.cs
index ad44668c..3324e9ae 100644
--- a/csharp/src/ProtocolBuffers/IMessage.cs
+++ b/csharp/src/ProtocolBuffers/IMessage.cs
@@ -40,9 +40,9 @@ namespace Google.Protobuf
// TODO(jonskeet): Split these interfaces into separate files when we're happy with them.
/// <summary>
- /// Reflection support for a specific message type.
+ /// Reflection support for accessing field values.
/// </summary>
- public interface IReflectedMessage
+ public interface IReflectedMessage : IMessage
{
FieldAccessorTable Fields { get; }
// TODO(jonskeet): Descriptor? Or a single property which has "all you need for reflection"?
@@ -81,7 +81,7 @@ namespace Google.Protobuf
/// the implementation class.
/// </summary>
/// <typeparam name="T">The message type.</typeparam>
- public interface IMessage<T> : IMessage, IEquatable<T>, IDeepCloneable<T>, IFreezable where T : IMessage<T>
+ public interface IMessage<T> : IReflectedMessage, IEquatable<T>, IDeepCloneable<T>, IFreezable where T : IMessage<T>
{
/// <summary>
/// Merges the given message into this one.
diff --git a/csharp/src/ProtocolBuffers/JsonFormatter.cs b/csharp/src/ProtocolBuffers/JsonFormatter.cs
new file mode 100644
index 00000000..9bd628eb
--- /dev/null
+++ b/csharp/src/ProtocolBuffers/JsonFormatter.cs
@@ -0,0 +1,579 @@
+#region Copyright notice and license
+// Protocol Buffers - Google's data interchange format
+// Copyright 2015 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#endregion
+
+using System;
+using System.Collections;
+using System.Globalization;
+using System.Text;
+using Google.Protobuf.Descriptors;
+using Google.Protobuf.FieldAccess;
+
+namespace Google.Protobuf
+{
+ /// <summary>
+ /// Reflection-based converter from messages to JSON.
+ /// </summary>
+ /// <remarks>
+ /// <para>
+ /// Instances of this class are thread-safe, with no mutable state.
+ /// </para>
+ /// <para>
+ /// 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.)
+ /// </para>
+ /// </remarks>
+ public sealed class JsonFormatter
+ {
+ private static JsonFormatter defaultInstance = new JsonFormatter(Settings.Default);
+
+ /// <summary>
+ /// Returns a formatter using the default settings.
+ /// </summary>
+ public static JsonFormatter Default { get { return defaultInstance; } }
+
+ /// <summary>
+ /// The JSON representation of the first 160 characters of Unicode.
+ /// Empty strings are replaced by the static constructor.
+ /// </summary>
+ private static readonly string[] CommonRepresentations = {
+ // C0 (ASCII and derivatives) control characters
+ "\\u0000", "\\u0001", "\\u0002", "\\u0003", // 0x00
+ "\\u0004", "\\u0005", "\\u0006", "\\u0007",
+ "\\b", "\\t", "\\n", "\\u000b",
+ "\\f", "\\r", "\\u000e", "\\u000f",
+ "\\u0010", "\\u0011", "\\u0012", "\\u0013", // 0x10
+ "\\u0014", "\\u0015", "\\u0016", "\\u0017",
+ "\\u0018", "\\u0019", "\\u001a", "\\u001b",
+ "\\u001c", "\\u001d", "\\u001e", "\\u001f",
+ // Escaping of " and \ are required by www.json.org string definition.
+ // Escaping of < and > are required for HTML security.
+ "", "", "\\\"", "", "", "", "", "", // 0x20
+ "", "", "", "", "", "", "", "",
+ "", "", "", "", "", "", "", "", // 0x30
+ "", "", "", "", "\\u003c", "", "\\u003e", "",
+ "", "", "", "", "", "", "", "", // 0x40
+ "", "", "", "", "", "", "", "",
+ "", "", "", "", "", "", "", "", // 0x50
+ "", "", "", "", "\\\\", "", "", "",
+ "", "", "", "", "", "", "", "", // 0x60
+ "", "", "", "", "", "", "", "",
+ "", "", "", "", "", "", "", "", // 0x70
+ "", "", "", "", "", "", "", "\\u007f",
+ // C1 (ISO 8859 and Unicode) extended control characters
+ "\\u0080", "\\u0081", "\\u0082", "\\u0083", // 0x80
+ "\\u0084", "\\u0085", "\\u0086", "\\u0087",
+ "\\u0088", "\\u0089", "\\u008a", "\\u008b",
+ "\\u008c", "\\u008d", "\\u008e", "\\u008f",
+ "\\u0090", "\\u0091", "\\u0092", "\\u0093", // 0x90
+ "\\u0094", "\\u0095", "\\u0096", "\\u0097",
+ "\\u0098", "\\u0099", "\\u009a", "\\u009b",
+ "\\u009c", "\\u009d", "\\u009e", "\\u009f"
+ };
+
+ static JsonFormatter()
+ {
+ for (int i = 0; i < CommonRepresentations.Length; i++)
+ {
+ if (CommonRepresentations[i] == "")
+ {
+ CommonRepresentations[i] = ((char) i).ToString();
+ }
+ }
+ }
+
+ private readonly Settings settings;
+
+ public JsonFormatter(Settings settings)
+ {
+ this.settings = settings;
+ }
+
+ public string Format(IReflectedMessage message)
+ {
+ ThrowHelper.ThrowIfNull(message, "message");
+ StringBuilder builder = new StringBuilder();
+ WriteMessage(builder, message);
+ return builder.ToString();
+ }
+
+ private void WriteMessage(StringBuilder builder, IReflectedMessage message)
+ {
+ if (message == null)
+ {
+ WriteNull(builder);
+ return;
+ }
+ builder.Append("{ ");
+ var fields = message.Fields;
+ bool first = true;
+ // First non-oneof fields
+ foreach (var accessor in fields.Accessors)
+ {
+ var descriptor = accessor.Descriptor;
+ // Oneofs are written later
+ // TODO: Change to write out fields in order, interleaving oneofs appropriately (as per binary format)
+ if (descriptor.ContainingOneof != null)
+ {
+ continue;
+ }
+ // Omit default values unless we're asked to format them
+ object value = accessor.GetValue(message);
+ if (!settings.FormatDefaultValues && IsDefaultValue(accessor, value))
+ {
+ continue;
+ }
+ // Omit awkward (single) values such as unknown enum values
+ if (!descriptor.IsRepeated && !descriptor.IsMap && !CanWriteSingleValue(accessor.Descriptor, value))
+ {
+ continue;
+ }
+
+ // Okay, all tests complete: let's write the field value...
+ if (!first)
+ {
+ builder.Append(", ");
+ }
+ WriteString(builder, ToCamelCase(accessor.Descriptor.Name));
+ builder.Append(": ");
+ WriteValue(builder, accessor, value);
+ first = false;
+ }
+
+ // Now oneofs
+ foreach (var accessor in fields.Oneofs)
+ {
+ var fieldDescriptor = accessor.GetCaseFieldDescriptor(message);
+ if (fieldDescriptor == null)
+ {
+ continue;
+ }
+ var fieldAccessor = fields[fieldDescriptor.FieldNumber];
+ object value = fieldAccessor.GetValue(message);
+ // Omit awkward (single) values such as unknown enum values
+ if (!fieldDescriptor.IsRepeated && !fieldDescriptor.IsMap && !CanWriteSingleValue(fieldDescriptor, value))
+ {
+ continue;
+ }
+
+ if (!first)
+ {
+ builder.Append(", ");
+ }
+ WriteString(builder, ToCamelCase(fieldDescriptor.Name));
+ builder.Append(": ");
+ WriteValue(builder, fieldAccessor, value);
+ first = false;
+ }
+ builder.Append(first ? "}" : " }");
+ }
+
+ // Converted from src/google/protobuf/util/internal/utility.cc ToCamelCase
+ internal static string ToCamelCase(string input)
+ {
+ bool capitalizeNext = false;
+ bool wasCap = true;
+ bool isCap = false;
+ bool firstWord = true;
+ StringBuilder result = new StringBuilder(input.Length);
+
+ for (int i = 0; i < input.Length; i++, wasCap = isCap)
+ {
+ isCap = char.IsUpper(input[i]);
+ if (input[i] == '_')
+ {
+ capitalizeNext = true;
+ if (result.Length != 0)
+ {
+ firstWord = false;
+ }
+ continue;
+ }
+ else if (firstWord)
+ {
+ // Consider when the current character B is capitalized,
+ // first word ends when:
+ // 1) following a lowercase: "...aB..."
+ // 2) followed by a lowercase: "...ABc..."
+ if (result.Length != 0 && isCap &&
+ (!wasCap || (i + 1 < input.Length && char.IsLower(input[i + 1]))))
+ {
+ firstWord = false;
+ }
+ else
+ {
+ result.Append(char.ToLowerInvariant(input[i]));
+ continue;
+ }
+ }
+ else if (capitalizeNext)
+ {
+ capitalizeNext = false;
+ if (char.IsLower(input[i]))
+ {
+ result.Append(char.ToUpperInvariant(input[i]));
+ continue;
+ }
+ }
+ result.Append(input[i]);
+ }
+ return result.ToString();
+ }
+
+ private static void WriteNull(StringBuilder builder)
+ {
+ builder.Append("null");
+ }
+
+ private static bool IsDefaultValue(IFieldAccessor accessor, object value)
+ {
+ if (accessor.Descriptor.IsMap)
+ {
+ IDictionary dictionary = (IDictionary) value;
+ return dictionary.Count == 0;
+ }
+ if (accessor.Descriptor.IsRepeated)
+ {
+ IList list = (IList) value;
+ return list.Count == 0;
+ }
+ switch (accessor.Descriptor.FieldType)
+ {
+ case FieldType.Bool:
+ return (bool) value == false;
+ case FieldType.Bytes:
+ return (ByteString) value == ByteString.Empty;
+ case FieldType.String:
+ return (string) value == "";
+ case FieldType.Double:
+ return (double) value == 0.0;
+ case FieldType.SInt32:
+ case FieldType.Int32:
+ case FieldType.SFixed32:
+ case FieldType.Enum:
+ return (int) value == 0;
+ case FieldType.Fixed32:
+ case FieldType.UInt32:
+ return (uint) value == 0;
+ case FieldType.Fixed64:
+ case FieldType.UInt64:
+ return (ulong) value == 0;
+ case FieldType.SFixed64:
+ case FieldType.Int64:
+ case FieldType.SInt64:
+ return (long) value == 0;
+ case FieldType.Float:
+ return (float) value == 0f;
+ case FieldType.Message:
+ case FieldType.Group: // Never expect to get this, but...
+ return value == null;
+ default:
+ throw new ArgumentException("Invalid field type");
+ }
+ }
+
+ private void WriteValue(StringBuilder builder, IFieldAccessor accessor, object value)
+ {
+ if (accessor.Descriptor.IsMap)
+ {
+ WriteDictionary(builder, accessor, (IDictionary) value);
+ }
+ else if (accessor.Descriptor.IsRepeated)
+ {
+ WriteList(builder, accessor, (IList) value);
+ }
+ else
+ {
+ WriteSingleValue(builder, accessor.Descriptor, value);
+ }
+ }
+
+ private void WriteSingleValue(StringBuilder builder, FieldDescriptor descriptor, object value)
+ {
+ switch (descriptor.FieldType)
+ {
+ case FieldType.Bool:
+ builder.Append((bool) value ? "true" : "false");
+ break;
+ case FieldType.Bytes:
+ // Nothing in Base64 needs escaping
+ builder.Append('"');
+ builder.Append(((ByteString) value).ToBase64());
+ builder.Append('"');
+ break;
+ case FieldType.String:
+ WriteString(builder, (string) value);
+ break;
+ case FieldType.Fixed32:
+ case FieldType.UInt32:
+ case FieldType.SInt32:
+ case FieldType.Int32:
+ case FieldType.SFixed32:
+ {
+ IFormattable formattable = (IFormattable) value;
+ builder.Append(formattable.ToString("d", CultureInfo.InvariantCulture));
+ break;
+ }
+ case FieldType.Enum:
+ EnumValueDescriptor enumValue = descriptor.EnumType.FindValueByNumber((int) value);
+ // We will already have validated that this is a known value.
+ WriteString(builder, enumValue.Name);
+ break;
+ case FieldType.Fixed64:
+ case FieldType.UInt64:
+ case FieldType.SFixed64:
+ case FieldType.Int64:
+ case FieldType.SInt64:
+ {
+ builder.Append('"');
+ IFormattable formattable = (IFormattable) value;
+ builder.Append(formattable.ToString("d", CultureInfo.InvariantCulture));
+ builder.Append('"');
+ break;
+ }
+ case FieldType.Double:
+ case FieldType.Float:
+ string text = ((IFormattable) value).ToString("r", CultureInfo.InvariantCulture);
+ if (text == "NaN" || text == "Infinity" || text == "-Infinity")
+ {
+ builder.Append('"');
+ builder.Append(text);
+ builder.Append('"');
+ }
+ else
+ {
+ builder.Append(text);
+ }
+ break;
+ case FieldType.Message:
+ case FieldType.Group: // Never expect to get this, but...
+ WriteMessage(builder, (IReflectedMessage) value);
+ break;
+ default:
+ throw new ArgumentException("Invalid field type: " + descriptor.FieldType);
+ }
+ }
+
+ private void WriteList(StringBuilder builder, IFieldAccessor accessor, IList list)
+ {
+ builder.Append("[ ");
+ bool first = true;
+ foreach (var value in list)
+ {
+ if (!CanWriteSingleValue(accessor.Descriptor, value))
+ {
+ continue;
+ }
+ if (!first)
+ {
+ builder.Append(", ");
+ }
+ WriteSingleValue(builder, accessor.Descriptor, value);
+ first = false;
+ }
+ builder.Append(first ? "]" : " ]");
+ }
+
+ private void WriteDictionary(StringBuilder builder, IFieldAccessor accessor, IDictionary dictionary)
+ {
+ builder.Append("{ ");
+ bool first = true;
+ FieldDescriptor keyType = accessor.Descriptor.MessageType.FindFieldByNumber(1);
+ FieldDescriptor valueType = accessor.Descriptor.MessageType.FindFieldByNumber(2);
+ // This will box each pair. Could use IDictionaryEnumerator, but that's ugly in terms of disposal.
+ foreach (DictionaryEntry pair in dictionary)
+ {
+ if (!CanWriteSingleValue(valueType, pair.Value))
+ {
+ continue;
+ }
+ if (!first)
+ {
+ builder.Append(", ");
+ }
+ string keyText;
+ switch (keyType.FieldType)
+ {
+ case FieldType.String:
+ keyText = (string) pair.Key;
+ break;
+ case FieldType.Bool:
+ keyText = (bool) pair.Key ? "true" : "false";
+ break;
+ case FieldType.Fixed32:
+ case FieldType.Fixed64:
+ case FieldType.SFixed32:
+ case FieldType.SFixed64:
+ case FieldType.Int32:
+ case FieldType.Int64:
+ case FieldType.SInt32:
+ case FieldType.SInt64:
+ case FieldType.UInt32:
+ case FieldType.UInt64:
+ keyText = ((IFormattable) pair.Key).ToString("d", CultureInfo.InvariantCulture);
+ break;
+ default:
+ throw new ArgumentException("Invalid key type: " + keyType.FieldType);
+ }
+ WriteString(builder, keyText);
+ builder.Append(": ");
+ WriteSingleValue(builder, valueType, pair.Value);
+ first = false;
+ }
+ builder.Append(first ? "}" : " }");
+ }
+
+ /// <summary>
+ /// Returns whether or not a singular value can be represented in JSON.
+ /// Currently only relevant for enums, where unknown values can't be represented.
+ /// For repeated/map fields, this always returns true.
+ /// </summary>
+ private bool CanWriteSingleValue(FieldDescriptor descriptor, object value)
+ {
+ if (descriptor.FieldType == FieldType.Enum)
+ {
+ EnumValueDescriptor enumValue = descriptor.EnumType.FindValueByNumber((int) value);
+ return enumValue != null;
+ }
+ return true;
+ }
+
+ /// <summary>
+ /// Writes a string (including leading and trailing double quotes) to a builder, escaping as required.
+ /// </summary>
+ /// <remarks>
+ /// Other than surrogate pair handling, this code is mostly taken from src/google/protobuf/util/internal/json_escaping.cc.
+ /// </remarks>
+ private void WriteString(StringBuilder builder, string text)
+ {
+ builder.Append('"');
+ for (int i = 0; i < text.Length; i++)
+ {
+ char c = text[i];
+ if (c < 0xa0)
+ {
+ builder.Append(CommonRepresentations[c]);
+ continue;
+ }
+ if (char.IsHighSurrogate(c))
+ {
+ // Encountered first part of a surrogate pair.
+ // Check that we have the whole pair, and encode both parts as hex.
+ i++;
+ if (i == text.Length || !char.IsLowSurrogate(text[i]))
+ {
+ throw new ArgumentException("String contains low surrogate not followed by high surrogate");
+ }
+ HexEncodeUtf16CodeUnit(builder, c);
+ HexEncodeUtf16CodeUnit(builder, text[i]);
+ continue;
+ }
+ else if (char.IsLowSurrogate(c))
+ {
+ throw new ArgumentException("String contains high surrogate not preceded by low surrogate");
+ }
+ switch ((uint) c)
+ {
+ // These are not required by json spec
+ // but used to prevent security bugs in javascript.
+ case 0xfeff: // Zero width no-break space
+ case 0xfff9: // Interlinear annotation anchor
+ case 0xfffa: // Interlinear annotation separator
+ case 0xfffb: // Interlinear annotation terminator
+
+ case 0x00ad: // Soft-hyphen
+ case 0x06dd: // Arabic end of ayah
+ case 0x070f: // Syriac abbreviation mark
+ case 0x17b4: // Khmer vowel inherent Aq
+ case 0x17b5: // Khmer vowel inherent Aa
+ HexEncodeUtf16CodeUnit(builder, c);
+ break;
+
+ default:
+ if ((c >= 0x0600 && c <= 0x0603) || // Arabic signs
+ (c >= 0x200b && c <= 0x200f) || // Zero width etc.
+ (c >= 0x2028 && c <= 0x202e) || // Separators etc.
+ (c >= 0x2060 && c <= 0x2064) || // Invisible etc.
+ (c >= 0x206a && c <= 0x206f))
+ {
+ HexEncodeUtf16CodeUnit(builder, c);
+ }
+ else
+ {
+ // No handling of surrogates here - that's done earlier
+ builder.Append(c);
+ }
+ break;
+ }
+ }
+ builder.Append('"');
+ }
+
+ private const string Hex = "0123456789abcdef";
+ private static void HexEncodeUtf16CodeUnit(StringBuilder builder, char c)
+ {
+ uint utf16 = c;
+ builder.Append("\\u");
+ builder.Append(Hex[(c >> 12) & 0xf]);
+ builder.Append(Hex[(c >> 8) & 0xf]);
+ builder.Append(Hex[(c >> 4) & 0xf]);
+ builder.Append(Hex[(c >> 0) & 0xf]);
+ }
+
+ /// <summary>
+ /// Settings controlling JSON formatting.
+ /// </summary>
+ public sealed class Settings
+ {
+ private static readonly Settings defaultInstance = new Settings(false);
+
+ /// <summary>
+ /// Default settings, as used by <see cref="JsonFormatter.Default"/>
+ /// </summary>
+ public static Settings Default { get { return defaultInstance; } }
+
+ private readonly bool formatDefaultValues;
+
+
+ /// <summary>
+ /// Whether fields whose values are the default for the field type (e.g. 0 for integers)
+ /// should be formatted (true) or omitted (false).
+ /// </summary>
+ public bool FormatDefaultValues { get { return formatDefaultValues; } }
+
+ public Settings(bool formatDefaultValues)
+ {
+ this.formatDefaultValues = formatDefaultValues;
+ }
+ }
+ }
+}
diff --git a/csharp/src/ProtocolBuffers/ProtocolBuffers.csproj b/csharp/src/ProtocolBuffers/ProtocolBuffers.csproj
index acfade59..f5dd6d5a 100644
--- a/csharp/src/ProtocolBuffers/ProtocolBuffers.csproj
+++ b/csharp/src/ProtocolBuffers/ProtocolBuffers.csproj
@@ -81,6 +81,7 @@
<Compile Include="FieldCodec.cs" />
<Compile Include="FrameworkPortability.cs" />
<Compile Include="Freezable.cs" />
+ <Compile Include="JsonFormatter.cs" />
<Compile Include="MessageExtensions.cs" />
<Compile Include="FieldAccess\FieldAccessorBase.cs" />
<Compile Include="FieldAccess\ReflectionUtil.cs" />