aboutsummaryrefslogtreecommitdiff
path: root/csharp/src/Google.Protobuf/JsonFormatter.cs
diff options
context:
space:
mode:
authorJon Skeet <skeet@pobox.com>2015-11-03 22:39:08 -0800
committerJon Skeet <skeet@pobox.com>2015-11-03 22:39:08 -0800
commitb6a32e909b1f58f157c19276af233e44627093f4 (patch)
treeba07d19ca93d4aa01bb4ce3131984447f53cdb4d /csharp/src/Google.Protobuf/JsonFormatter.cs
parent55ad57a235c009d0414aed1781072adda0c89137 (diff)
parentfb2488225fbd239f7880e3b493cbfd2f19da755b (diff)
downloadprotobuf-b6a32e909b1f58f157c19276af233e44627093f4.tar.gz
protobuf-b6a32e909b1f58f157c19276af233e44627093f4.tar.bz2
protobuf-b6a32e909b1f58f157c19276af233e44627093f4.zip
Merge pull request #923 from jskeet/json-parsing
Implement JSON parsing in C#.
Diffstat (limited to 'csharp/src/Google.Protobuf/JsonFormatter.cs')
-rw-r--r--csharp/src/Google.Protobuf/JsonFormatter.cs15
1 files changed, 12 insertions, 3 deletions
diff --git a/csharp/src/Google.Protobuf/JsonFormatter.cs b/csharp/src/Google.Protobuf/JsonFormatter.cs
index 3f9bd478..2070f62b 100644
--- a/csharp/src/Google.Protobuf/JsonFormatter.cs
+++ b/csharp/src/Google.Protobuf/JsonFormatter.cs
@@ -189,6 +189,7 @@ namespace Google.Protobuf
}
// Converted from src/google/protobuf/util/internal/utility.cc ToCamelCase
+ // TODO: Use the new field in FieldDescriptor.
internal static string ToCamelCase(string input)
{
bool capitalizeNext = false;
@@ -382,10 +383,19 @@ namespace Google.Protobuf
WriteNull(builder);
return;
}
- // For wrapper types, the value will be the (possibly boxed) "native" value,
- // so we can write it as if we were unconditionally writing the Value field for the wrapper type.
+ // For wrapper types, the value will either be the (possibly boxed) "native" value,
+ // or the message itself if we're formatting it at the top level (e.g. just calling ToString on the object itself).
+ // If it's the message form, we can extract the value first, which *will* be the (possibly boxed) native value,
+ // and then proceed, writing it as if we were definitely in a field. (We never need to wrap it in an extra string...
+ // WriteValue will do the right thing.)
+ // TODO: Detect this differently when we have dynamic messages.
if (descriptor.File == Int32Value.Descriptor.File)
{
+ if (value is IMessage)
+ {
+ var message = (IMessage) value;
+ value = message.Descriptor.Fields[Wrappers.WrapperValueFieldNumber].Accessor.GetValue(message);
+ }
WriteValue(builder, value);
return;
}
@@ -750,7 +760,6 @@ namespace Google.Protobuf
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).