aboutsummaryrefslogtreecommitdiff
path: root/src/ProtocolBuffers/Serialization
diff options
context:
space:
mode:
authorcsharptest <roger@csharptest.net>2011-07-14 13:06:22 -0500
committerrogerk <devnull@localhost>2011-07-14 13:06:22 -0500
commit74c5e0c3762939fbf5988daf6f97c5d9c97ef4f5 (patch)
tree642a1a002907d3291c88e7933cfda87a6704beee /src/ProtocolBuffers/Serialization
parentafff2c655c1d39b8f6cd7756c190a559d15134d1 (diff)
downloadprotobuf-74c5e0c3762939fbf5988daf6f97c5d9c97ef4f5.tar.gz
protobuf-74c5e0c3762939fbf5988daf6f97c5d9c97ef4f5.tar.bz2
protobuf-74c5e0c3762939fbf5988daf6f97c5d9c97ef4f5.zip
Reformatted to include braces
Diffstat (limited to 'src/ProtocolBuffers/Serialization')
-rw-r--r--src/ProtocolBuffers/Serialization/AbstractReader.cs278
-rw-r--r--src/ProtocolBuffers/Serialization/AbstractTextReader.cs25
-rw-r--r--src/ProtocolBuffers/Serialization/AbstractTextWriter.cs55
-rw-r--r--src/ProtocolBuffers/Serialization/AbstractWriter.cs384
-rw-r--r--src/ProtocolBuffers/Serialization/DictionaryReader.cs51
-rw-r--r--src/ProtocolBuffers/Serialization/DictionaryWriter.cs29
-rw-r--r--src/ProtocolBuffers/Serialization/JsonFormatReader.cs90
-rw-r--r--src/ProtocolBuffers/Serialization/JsonFormatWriter.cs176
-rw-r--r--src/ProtocolBuffers/Serialization/JsonTextCursor.cs184
-rw-r--r--src/ProtocolBuffers/Serialization/XmlFormatReader.cs95
-rw-r--r--src/ProtocolBuffers/Serialization/XmlFormatWriter.cs106
-rw-r--r--src/ProtocolBuffers/Serialization/XmlReaderOptions.cs1
-rw-r--r--src/ProtocolBuffers/Serialization/XmlWriterOptions.cs3
13 files changed, 1120 insertions, 357 deletions
diff --git a/src/ProtocolBuffers/Serialization/AbstractReader.cs b/src/ProtocolBuffers/Serialization/AbstractReader.cs
index 3aa54e8f..bc1fa6cd 100644
--- a/src/ProtocolBuffers/Serialization/AbstractReader.cs
+++ b/src/ProtocolBuffers/Serialization/AbstractReader.cs
@@ -12,19 +12,22 @@ namespace Google.ProtocolBuffers.Serialization
/// </summary>
public abstract class AbstractReader : ICodedInputStream
{
- const int MaxDepth = CodedInputStream.DefaultRecursionLimit;
+ private const int MaxDepth = CodedInputStream.DefaultRecursionLimit;
protected int Depth;
/// <summary>
/// Merges the contents of stream into the provided message builder
/// </summary>
public TBuilder Merge<TBuilder>(TBuilder builder) where TBuilder : IBuilderLite
- { return Merge(builder, ExtensionRegistry.Empty); }
+ {
+ return Merge(builder, ExtensionRegistry.Empty);
+ }
/// <summary>
/// Merges the contents of stream into the provided message builder
/// </summary>
- public abstract TBuilder Merge<TBuilder>(TBuilder builder, ExtensionRegistry registry) where TBuilder : IBuilderLite;
+ public abstract TBuilder Merge<TBuilder>(TBuilder builder, ExtensionRegistry registry)
+ where TBuilder : IBuilderLite;
/// <summary>
/// Peeks at the next field in the input stream and returns what information is available.
@@ -117,14 +120,17 @@ namespace Google.ProtocolBuffers.Serialization
yield return next;
if (!PeekNext(out next) || next != field)
+ {
break;
+ }
}
}
/// <summary>
/// Reads an array of T messages
/// </summary>
- public virtual bool ReadMessageArray<T>(string field, ICollection<T> items, IMessageLite messageType, ExtensionRegistry registry)
+ public virtual bool ReadMessageArray<T>(string field, ICollection<T> items, IMessageLite messageType,
+ ExtensionRegistry registry)
{
bool success = false;
foreach (string next in ForeachArrayItem(field))
@@ -132,7 +138,7 @@ namespace Google.ProtocolBuffers.Serialization
IBuilderLite builder = messageType.WeakCreateBuilderForType();
if (ReadMessage(builder, registry))
{
- items.Add((T)builder.WeakBuild());
+ items.Add((T) builder.WeakBuild());
success |= true;
}
}
@@ -142,7 +148,8 @@ namespace Google.ProtocolBuffers.Serialization
/// <summary>
/// Reads an array of T messages as a proto-buffer group
/// </summary>
- public virtual bool ReadGroupArray<T>(string field, ICollection<T> items, IMessageLite messageType, ExtensionRegistry registry)
+ public virtual bool ReadGroupArray<T>(string field, ICollection<T> items, IMessageLite messageType,
+ ExtensionRegistry registry)
{
bool success = false;
foreach (string next in ForeachArrayItem(field))
@@ -150,7 +157,7 @@ namespace Google.ProtocolBuffers.Serialization
IBuilderLite builder = messageType.WeakCreateBuilderForType();
if (ReadGroup(builder, registry))
{
- items.Add((T)builder.WeakBuild());
+ items.Add((T) builder.WeakBuild());
success |= true;
}
}
@@ -186,7 +193,7 @@ namespace Google.ProtocolBuffers.Serialization
object temp = null;
if (ReadField(type, ref temp))
{
- items.Add((T)temp);
+ items.Add((T) temp);
success |= true;
}
}
@@ -204,9 +211,13 @@ namespace Google.ProtocolBuffers.Serialization
{
bool temp = false;
if (Read(ref temp))
+ {
value = temp;
+ }
else
+ {
return false;
+ }
break;
}
case FieldType.Int64:
@@ -215,9 +226,13 @@ namespace Google.ProtocolBuffers.Serialization
{
long temp = 0;
if (Read(ref temp))
+ {
value = temp;
+ }
else
+ {
return false;
+ }
break;
}
case FieldType.UInt64:
@@ -225,9 +240,13 @@ namespace Google.ProtocolBuffers.Serialization
{
ulong temp = 0;
if (Read(ref temp))
+ {
value = temp;
+ }
else
+ {
return false;
+ }
break;
}
case FieldType.Int32:
@@ -236,9 +255,13 @@ namespace Google.ProtocolBuffers.Serialization
{
int temp = 0;
if (Read(ref temp))
+ {
value = temp;
+ }
else
+ {
return false;
+ }
break;
}
case FieldType.UInt32:
@@ -246,45 +269,65 @@ namespace Google.ProtocolBuffers.Serialization
{
uint temp = 0;
if (Read(ref temp))
+ {
value = temp;
+ }
else
+ {
return false;
+ }
break;
}
case FieldType.Float:
{
float temp = float.NaN;
if (Read(ref temp))
+ {
value = temp;
+ }
else
+ {
return false;
+ }
break;
}
case FieldType.Double:
{
double temp = float.NaN;
if (Read(ref temp))
+ {
value = temp;
+ }
else
+ {
return false;
+ }
break;
}
case FieldType.String:
{
string temp = null;
if (Read(ref temp))
+ {
value = temp;
+ }
else
+ {
return false;
+ }
break;
}
case FieldType.Bytes:
{
ByteString temp = null;
if (Read(ref temp))
+ {
value = temp;
+ }
else
+ {
return false;
+ }
break;
}
default:
@@ -306,65 +349,99 @@ namespace Google.ProtocolBuffers.Serialization
}
bool ICodedInputStream.ReadDouble(ref double value)
- { return Read(ref value); }
+ {
+ return Read(ref value);
+ }
bool ICodedInputStream.ReadFloat(ref float value)
- { return Read(ref value); }
+ {
+ return Read(ref value);
+ }
bool ICodedInputStream.ReadUInt64(ref ulong value)
- { return Read(ref value); }
+ {
+ return Read(ref value);
+ }
bool ICodedInputStream.ReadInt64(ref long value)
- { return Read(ref value); }
+ {
+ return Read(ref value);
+ }
bool ICodedInputStream.ReadInt32(ref int value)
- { return Read(ref value); }
+ {
+ return Read(ref value);
+ }
bool ICodedInputStream.ReadFixed64(ref ulong value)
- { return Read(ref value); }
+ {
+ return Read(ref value);
+ }
bool ICodedInputStream.ReadFixed32(ref uint value)
- { return Read(ref value); }
+ {
+ return Read(ref value);
+ }
bool ICodedInputStream.ReadBool(ref bool value)
- { return Read(ref value); }
+ {
+ return Read(ref value);
+ }
bool ICodedInputStream.ReadString(ref string value)
- { return Read(ref value); }
+ {
+ return Read(ref value);
+ }
void ICodedInputStream.ReadGroup(int fieldNumber, IBuilderLite builder, ExtensionRegistry extensionRegistry)
{
if (Depth++ > MaxDepth)
+ {
throw InvalidProtocolBufferException.RecursionLimitExceeded();
+ }
ReadGroup(builder, extensionRegistry);
Depth--;
}
void ICodedInputStream.ReadUnknownGroup(int fieldNumber, IBuilderLite builder)
- { throw new NotSupportedException(); }
+ {
+ throw new NotSupportedException();
+ }
void ICodedInputStream.ReadMessage(IBuilderLite builder, ExtensionRegistry extensionRegistry)
{
if (Depth++ > MaxDepth)
- throw InvalidProtocolBufferException.RecursionLimitExceeded();
+ {
+ throw InvalidProtocolBufferException.RecursionLimitExceeded();
+ }
ReadMessage(builder, extensionRegistry);
Depth--;
}
bool ICodedInputStream.ReadBytes(ref ByteString value)
- { return Read(ref value); }
+ {
+ return Read(ref value);
+ }
bool ICodedInputStream.ReadUInt32(ref uint value)
- { return Read(ref value); }
+ {
+ return Read(ref value);
+ }
bool ICodedInputStream.ReadEnum(ref IEnumLite value, out object unknown, IEnumLiteMap mapping)
{
value = null;
unknown = null;
- if(ReadEnum(ref unknown))
+ if (ReadEnum(ref unknown))
{
- if (unknown is int) value = mapping.FindValueByNumber((int)unknown);
- else if (unknown is string) value = mapping.FindValueByName((string)unknown);
+ if (unknown is int)
+ {
+ value = mapping.FindValueByNumber((int) unknown);
+ }
+ else if (unknown is string)
+ {
+ value = mapping.FindValueByName((string) unknown);
+ }
return value != null;
}
return false;
@@ -375,12 +452,16 @@ namespace Google.ProtocolBuffers.Serialization
rawValue = null;
if (ReadEnum(ref rawValue))
{
- if (Enum.IsDefined(typeof(T), rawValue))
+ if (Enum.IsDefined(typeof (T), rawValue))
{
if (rawValue is int)
- value = (T)rawValue;
+ {
+ value = (T) rawValue;
+ }
else if (rawValue is string)
- value = (T)Enum.Parse(typeof(T), (string)rawValue, false);
+ {
+ value = (T) Enum.Parse(typeof (T), (string) rawValue, false);
+ }
else
{
value = default(T);
@@ -393,21 +474,33 @@ namespace Google.ProtocolBuffers.Serialization
}
bool ICodedInputStream.ReadSFixed32(ref int value)
- { return Read(ref value); }
+ {
+ return Read(ref value);
+ }
bool ICodedInputStream.ReadSFixed64(ref long value)
- { return Read(ref value); }
+ {
+ return Read(ref value);
+ }
bool ICodedInputStream.ReadSInt32(ref int value)
- { return Read(ref value); }
+ {
+ return Read(ref value);
+ }
bool ICodedInputStream.ReadSInt64(ref long value)
- { return Read(ref value); }
+ {
+ return Read(ref value);
+ }
- void ICodedInputStream.ReadPrimitiveArray(FieldType fieldType, uint fieldTag, string fieldName, ICollection<object> list)
- { ReadArray(fieldType, fieldName, list); }
+ void ICodedInputStream.ReadPrimitiveArray(FieldType fieldType, uint fieldTag, string fieldName,
+ ICollection<object> list)
+ {
+ ReadArray(fieldType, fieldName, list);
+ }
- void ICodedInputStream.ReadEnumArray(uint fieldTag, string fieldName, ICollection<IEnumLite> list, out ICollection<object> unknown, IEnumLiteMap mapping)
+ void ICodedInputStream.ReadEnumArray(uint fieldTag, string fieldName, ICollection<IEnumLite> list,
+ out ICollection<object> unknown, IEnumLiteMap mapping)
{
unknown = null;
List<object> array = new List<object>();
@@ -416,21 +509,33 @@ namespace Google.ProtocolBuffers.Serialization
foreach (object rawValue in array)
{
IEnumLite item = null;
- if (rawValue is int) item = mapping.FindValueByNumber((int)rawValue);
- else if (rawValue is string) item = mapping.FindValueByName((string)rawValue);
+ if (rawValue is int)
+ {
+ item = mapping.FindValueByNumber((int) rawValue);
+ }
+ else if (rawValue is string)
+ {
+ item = mapping.FindValueByName((string) rawValue);
+ }
if (item != null)
+ {
list.Add(item);
+ }
else
{
- if (unknown == null) unknown = new List<object>();
+ if (unknown == null)
+ {
+ unknown = new List<object>();
+ }
unknown.Add(rawValue);
}
}
}
}
- void ICodedInputStream.ReadEnumArray<T>(uint fieldTag, string fieldName, ICollection<T> list, out ICollection<object> unknown)
+ void ICodedInputStream.ReadEnumArray<T>(uint fieldTag, string fieldName, ICollection<T> list,
+ out ICollection<object> unknown)
{
unknown = null;
List<object> array = new List<object>();
@@ -439,40 +544,59 @@ namespace Google.ProtocolBuffers.Serialization
foreach (object rawValue in array)
{
if (rawValue is int)
- list.Add((T)rawValue);
+ {
+ list.Add((T) rawValue);
+ }
else if (rawValue is string)
- list.Add((T)Enum.Parse(typeof(T), (string)rawValue, false));
+ {
+ list.Add((T) Enum.Parse(typeof (T), (string) rawValue, false));
+ }
else
{
- if (unknown == null) unknown = new List<object>();
+ if (unknown == null)
+ {
+ unknown = new List<object>();
+ }
unknown.Add(rawValue);
}
}
}
}
- void ICodedInputStream.ReadMessageArray<T>(uint fieldTag, string fieldName, ICollection<T> list, T messageType, ExtensionRegistry registry)
+ void ICodedInputStream.ReadMessageArray<T>(uint fieldTag, string fieldName, ICollection<T> list, T messageType,
+ ExtensionRegistry registry)
{
if (Depth++ > MaxDepth)
- throw InvalidProtocolBufferException.RecursionLimitExceeded();
+ {
+ throw InvalidProtocolBufferException.RecursionLimitExceeded();
+ }
ReadMessageArray(fieldName, list, messageType, registry);
Depth--;
}
- void ICodedInputStream.ReadGroupArray<T>(uint fieldTag, string fieldName, ICollection<T> list, T messageType, ExtensionRegistry registry)
+ void ICodedInputStream.ReadGroupArray<T>(uint fieldTag, string fieldName, ICollection<T> list, T messageType,
+ ExtensionRegistry registry)
{
if (Depth++ > MaxDepth)
+ {
throw InvalidProtocolBufferException.RecursionLimitExceeded();
+ }
ReadGroupArray(fieldName, list, messageType, registry);
Depth--;
}
bool ICodedInputStream.ReadPrimitiveField(FieldType fieldType, ref object value)
- { return ReadField(fieldType, ref value); }
+ {
+ return ReadField(fieldType, ref value);
+ }
bool ICodedInputStream.IsAtEnd
{
- get { string next; return PeekNext(out next) == false; }
+ get
+ {
+ string next;
+ return PeekNext(out next) == false;
+ }
}
bool ICodedInputStream.SkipField()
@@ -482,50 +606,80 @@ namespace Google.ProtocolBuffers.Serialization
}
void ICodedInputStream.ReadStringArray(uint fieldTag, string fieldName, ICollection<string> list)
- { ReadArray(FieldType.String, fieldName, list); }
+ {
+ ReadArray(FieldType.String, fieldName, list);
+ }
void ICodedInputStream.ReadBytesArray(uint fieldTag, string fieldName, ICollection<ByteString> list)
- { ReadArray(FieldType.Bytes, fieldName, list); }
+ {
+ ReadArray(FieldType.Bytes, fieldName, list);
+ }
void ICodedInputStream.ReadBoolArray(uint fieldTag, string fieldName, ICollection<bool> list)
- { ReadArray(FieldType.Bool, fieldName, list); }
+ {
+ ReadArray(FieldType.Bool, fieldName, list);
+ }
void ICodedInputStream.ReadInt32Array(uint fieldTag, string fieldName, ICollection<int> list)
- { ReadArray(FieldType.Int32, fieldName, list); }
+ {
+ ReadArray(FieldType.Int32, fieldName, list);
+ }
void ICodedInputStream.ReadSInt32Array(uint fieldTag, string fieldName, ICollection<int> list)
- { ReadArray(FieldType.SInt32, fieldName, list); }
+ {
+ ReadArray(FieldType.SInt32, fieldName, list);
+ }
void ICodedInputStream.ReadUInt32Array(uint fieldTag, string fieldName, ICollection<uint> list)
- { ReadArray(FieldType.UInt32, fieldName, list); }
+ {
+ ReadArray(FieldType.UInt32, fieldName, list);
+ }
void ICodedInputStream.ReadFixed32Array(uint fieldTag, string fieldName, ICollection<uint> list)
- { ReadArray(FieldType.Fixed32, fieldName, list); }
+ {
+ ReadArray(FieldType.Fixed32, fieldName, list);
+ }
void ICodedInputStream.ReadSFixed32Array(uint fieldTag, string fieldName, ICollection<int> list)
- { ReadArray(FieldType.SFixed32, fieldName, list); }
+ {
+ ReadArray(FieldType.SFixed32, fieldName, list);
+ }
void ICodedInputStream.ReadInt64Array(uint fieldTag, string fieldName, ICollection<long> list)
- { ReadArray(FieldType.Int64, fieldName, list); }
+ {
+ ReadArray(FieldType.Int64, fieldName, list);
+ }
void ICodedInputStream.ReadSInt64Array(uint fieldTag, string fieldName, ICollection<long> list)
- { ReadArray(FieldType.SInt64, fieldName, list); }
+ {
+ ReadArray(FieldType.SInt64, fieldName, list);
+ }
void ICodedInputStream.ReadUInt64Array(uint fieldTag, string fieldName, ICollection<ulong> list)
- { ReadArray(FieldType.UInt64, fieldName, list); }
+ {
+ ReadArray(FieldType.UInt64, fieldName, list);
+ }
void ICodedInputStream.ReadFixed64Array(uint fieldTag, string fieldName, ICollection<ulong> list)
- { ReadArray(FieldType.Fixed64, fieldName, list); }
+ {
+ ReadArray(FieldType.Fixed64, fieldName, list);
+ }
void ICodedInputStream.ReadSFixed64Array(uint fieldTag, string fieldName, ICollection<long> list)
- { ReadArray(FieldType.SFixed64, fieldName, list); }
+ {
+ ReadArray(FieldType.SFixed64, fieldName, list);
+ }
void ICodedInputStream.ReadDoubleArray(uint fieldTag, string fieldName, ICollection<double> list)
- { ReadArray(FieldType.Double, fieldName, list); }
+ {
+ ReadArray(FieldType.Double, fieldName, list);
+ }
void ICodedInputStream.ReadFloatArray(uint fieldTag, string fieldName, ICollection<float> list)
- { ReadArray(FieldType.Float, fieldName, list); }
+ {
+ ReadArray(FieldType.Float, fieldName, list);
+ }
#endregion
}
-}
+} \ No newline at end of file
diff --git a/src/ProtocolBuffers/Serialization/AbstractTextReader.cs b/src/ProtocolBuffers/Serialization/AbstractTextReader.cs
index 2f07ac72..83a8dca5 100644
--- a/src/ProtocolBuffers/Serialization/AbstractTextReader.cs
+++ b/src/ProtocolBuffers/Serialization/AbstractTextReader.cs
@@ -20,7 +20,7 @@ namespace Google.ProtocolBuffers.Serialization
protected override bool Read(ref string value)
{
string text = null;
- if (ReadAsText(ref text, typeof(string)))
+ if (ReadAsText(ref text, typeof (string)))
{
value = text;
return true;
@@ -34,7 +34,7 @@ namespace Google.ProtocolBuffers.Serialization
protected override bool Read(ref bool value)
{
string text = null;
- if (ReadAsText(ref text, typeof(bool)))
+ if (ReadAsText(ref text, typeof (bool)))
{
value = XmlConvert.ToBoolean(text);
return true;
@@ -48,7 +48,7 @@ namespace Google.ProtocolBuffers.Serialization
protected override bool Read(ref int value)
{
string text = null;
- if (ReadAsText(ref text, typeof(int)))
+ if (ReadAsText(ref text, typeof (int)))
{
value = XmlConvert.ToInt32(text);
return true;
@@ -63,7 +63,7 @@ namespace Google.ProtocolBuffers.Serialization
protected override bool Read(ref uint value)
{
string text = null;
- if (ReadAsText(ref text, typeof(uint)))
+ if (ReadAsText(ref text, typeof (uint)))
{
value = XmlConvert.ToUInt32(text);
return true;
@@ -77,7 +77,7 @@ namespace Google.ProtocolBuffers.Serialization
protected override bool Read(ref long value)
{
string text = null;
- if (ReadAsText(ref text, typeof(long)))
+ if (ReadAsText(ref text, typeof (long)))
{
value = XmlConvert.ToInt64(text);
return true;
@@ -92,7 +92,7 @@ namespace Google.ProtocolBuffers.Serialization
protected override bool Read(ref ulong value)
{
string text = null;
- if (ReadAsText(ref text, typeof(ulong)))
+ if (ReadAsText(ref text, typeof (ulong)))
{
value = XmlConvert.ToUInt64(text);
return true;
@@ -106,7 +106,7 @@ namespace Google.ProtocolBuffers.Serialization
protected override bool Read(ref float value)
{
string text = null;
- if (ReadAsText(ref text, typeof(float)))
+ if (ReadAsText(ref text, typeof (float)))
{
value = XmlConvert.ToSingle(text);
return true;
@@ -120,7 +120,7 @@ namespace Google.ProtocolBuffers.Serialization
protected override bool Read(ref double value)
{
string text = null;
- if (ReadAsText(ref text, typeof(double)))
+ if (ReadAsText(ref text, typeof (double)))
{
value = XmlConvert.ToDouble(text);
return true;
@@ -131,7 +131,10 @@ namespace Google.ProtocolBuffers.Serialization
/// <summary>
/// Provides decoding of bytes read from the input stream
/// </summary>
- protected virtual ByteString DecodeBytes(string bytes) { return ByteString.FromBase64(bytes); }
+ protected virtual ByteString DecodeBytes(string bytes)
+ {
+ return ByteString.FromBase64(bytes);
+ }
/// <summary>
/// Returns true if it was able to read a ByteString from the input
@@ -139,7 +142,7 @@ namespace Google.ProtocolBuffers.Serialization
protected override bool Read(ref ByteString value)
{
string text = null;
- if (ReadAsText(ref text, typeof(ByteString)))
+ if (ReadAsText(ref text, typeof (ByteString)))
{
value = DecodeBytes(text);
return true;
@@ -154,7 +157,7 @@ namespace Google.ProtocolBuffers.Serialization
protected override bool ReadEnum(ref object value)
{
string text = null;
- if (ReadAsText(ref text, typeof(Enum)))
+ if (ReadAsText(ref text, typeof (Enum)))
{
int number;
if (int.TryParse(text, NumberStyles.Integer, CultureInfo.InvariantCulture, out number))
diff --git a/src/ProtocolBuffers/Serialization/AbstractTextWriter.cs b/src/ProtocolBuffers/Serialization/AbstractTextWriter.cs
index 75acc0f4..2c778dfc 100644
--- a/src/ProtocolBuffers/Serialization/AbstractTextWriter.cs
+++ b/src/ProtocolBuffers/Serialization/AbstractTextWriter.cs
@@ -11,7 +11,10 @@ namespace Google.ProtocolBuffers.Serialization
/// <summary>
/// Encodes raw bytes to be written to the stream
/// </summary>
- protected virtual string EncodeBytes(ByteString bytes) { return bytes.ToBase64(); }
+ protected virtual string EncodeBytes(ByteString bytes)
+ {
+ return bytes.ToBase64();
+ }
/// <summary>
/// Writes a typed field as a text value
@@ -21,53 +24,83 @@ namespace Google.ProtocolBuffers.Serialization
/// <summary>
/// Writes a String value
/// </summary>
- protected override void Write(string field, string value) { WriteAsText(field, value, value); }
+ protected override void Write(string field, string value)
+ {
+ WriteAsText(field, value, value);
+ }
/// <summary>
/// Writes a Boolean value
/// </summary>
- protected override void Write(string field, bool value) { WriteAsText(field, XmlConvert.ToString(value), value); }
+ protected override void Write(string field, bool value)
+ {
+ WriteAsText(field, XmlConvert.ToString(value), value);
+ }
/// <summary>
/// Writes a Int32 value
/// </summary>
- protected override void Write(string field, int value) { WriteAsText(field, XmlConvert.ToString(value), value); }
+ protected override void Write(string field, int value)
+ {
+ WriteAsText(field, XmlConvert.ToString(value), value);
+ }
/// <summary>
/// Writes a UInt32 value
/// </summary>
[CLSCompliant(false)]
- protected override void Write(string field, uint value) { WriteAsText(field, XmlConvert.ToString(value), value); }
+ protected override void Write(string field, uint value)
+ {
+ WriteAsText(field, XmlConvert.ToString(value), value);
+ }
/// <summary>
/// Writes a Int64 value
/// </summary>
- protected override void Write(string field, long value) { WriteAsText(field, XmlConvert.ToString(value), value); }
+ protected override void Write(string field, long value)
+ {
+ WriteAsText(field, XmlConvert.ToString(value), value);
+ }
/// <summary>
/// Writes a UInt64 value
/// </summary>
[CLSCompliant(false)]
- protected override void Write(string field, ulong value) { WriteAsText(field, XmlConvert.ToString(value), value); }
+ protected override void Write(string field, ulong value)
+ {
+ WriteAsText(field, XmlConvert.ToString(value), value);
+ }
/// <summary>
/// Writes a Single value
/// </summary>
- protected override void Write(string field, float value) { WriteAsText(field, XmlConvert.ToString(value), value); }
+ protected override void Write(string field, float value)
+ {
+ WriteAsText(field, XmlConvert.ToString(value), value);
+ }
/// <summary>
/// Writes a Double value
/// </summary>
- protected override void Write(string field, double value) { WriteAsText(field, XmlConvert.ToString(value), value); }
+ protected override void Write(string field, double value)
+ {
+ WriteAsText(field, XmlConvert.ToString(value), value);
+ }
/// <summary>
/// Writes a set of bytes
/// </summary>
- protected override void Write(string field, ByteString value) { WriteAsText(field, EncodeBytes(value), value); }
+ protected override void Write(string field, ByteString value)
+ {
+ WriteAsText(field, EncodeBytes(value), value);
+ }
/// <summary>
/// Writes a System.Enum by the numeric and textual value
/// </summary>
- protected override void WriteEnum(string field, int number, string name) { WriteAsText(field, name, number); }
+ protected override void WriteEnum(string field, int number, string name)
+ {
+ WriteAsText(field, name, number);
+ }
}
} \ No newline at end of file
diff --git a/src/ProtocolBuffers/Serialization/AbstractWriter.cs b/src/ProtocolBuffers/Serialization/AbstractWriter.cs
index 96153162..6592c1dd 100644
--- a/src/ProtocolBuffers/Serialization/AbstractWriter.cs
+++ b/src/ProtocolBuffers/Serialization/AbstractWriter.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections;
+using System.Collections.Generic;
using System.Globalization;
using Google.ProtocolBuffers.Descriptors;
@@ -27,7 +28,8 @@ namespace Google.ProtocolBuffers.Serialization
/// Completes any pending write operations
/// </summary>
public virtual void Flush()
- { }
+ {
+ }
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
@@ -105,28 +107,60 @@ namespace Google.ProtocolBuffers.Serialization
{
switch (fieldType)
{
- case FieldType.Bool: Write(field, (bool)value); break;
+ case FieldType.Bool:
+ Write(field, (bool) value);
+ break;
case FieldType.Int64:
case FieldType.SInt64:
- case FieldType.SFixed64: Write(field, (long)value); break;
+ case FieldType.SFixed64:
+ Write(field, (long) value);
+ break;
case FieldType.UInt64:
- case FieldType.Fixed64: Write(field, (ulong)value); break;
+ case FieldType.Fixed64:
+ Write(field, (ulong) value);
+ break;
case FieldType.Int32:
case FieldType.SInt32:
- case FieldType.SFixed32: Write(field, (int)value); break;
+ case FieldType.SFixed32:
+ Write(field, (int) value);
+ break;
case FieldType.UInt32:
- case FieldType.Fixed32: Write(field, (uint)value); break;
- case FieldType.Float: Write(field, (float)value); break;
- case FieldType.Double: Write(field, (double)value); break;
- case FieldType.String: Write(field, (string)value); break;
- case FieldType.Bytes: Write(field, (ByteString)value); break;
- case FieldType.Group: WriteMessageOrGroup(field, (IMessageLite)value); break;
- case FieldType.Message: WriteMessageOrGroup(field, (IMessageLite)value); break;
+ case FieldType.Fixed32:
+ Write(field, (uint) value);
+ break;
+ case FieldType.Float:
+ Write(field, (float) value);
+ break;
+ case FieldType.Double:
+ Write(field, (double) value);
+ break;
+ case FieldType.String:
+ Write(field, (string) value);
+ break;
+ case FieldType.Bytes:
+ Write(field, (ByteString) value);
+ break;
+ case FieldType.Group:
+ WriteMessageOrGroup(field, (IMessageLite) value);
+ break;
+ case FieldType.Message:
+ WriteMessageOrGroup(field, (IMessageLite) value);
+ break;
case FieldType.Enum:
{
- if (value is IEnumLite) WriteEnum(field, ((IEnumLite)value).Number, ((IEnumLite)value).Name);
- else if (value is IConvertible) WriteEnum(field, ((IConvertible)value).ToInt32(CultureInfo.InvariantCulture), ((IConvertible)value).ToString(CultureInfo.InvariantCulture));
- else throw new ArgumentException("Expected an Enum type for field " + field);
+ if (value is IEnumLite)
+ {
+ WriteEnum(field, ((IEnumLite) value).Number, ((IEnumLite) value).Name);
+ }
+ else if (value is IConvertible)
+ {
+ WriteEnum(field, ((IConvertible) value).ToInt32(CultureInfo.InvariantCulture),
+ ((IConvertible) value).ToString(CultureInfo.InvariantCulture));
+ }
+ else
+ {
+ throw new ArgumentException("Expected an Enum type for field " + field);
+ }
break;
}
default:
@@ -140,7 +174,9 @@ namespace Google.ProtocolBuffers.Serialization
protected virtual void WriteArray(FieldType fieldType, string field, IEnumerable items)
{
foreach (object obj in items)
+ {
WriteField(fieldType, field, obj);
+ }
}
/// <summary>
@@ -148,191 +184,321 @@ namespace Google.ProtocolBuffers.Serialization
/// </summary>
[CLSCompliant(false)]
protected virtual void WriteUnknown(WireFormat.WireType wireType, int fieldNumber, ulong value)
- { }
+ {
+ }
/// <summary>
/// Writes an unknown field, Expect WireType of GroupStart or LengthPrefix
/// </summary>
[CLSCompliant(false)]
protected virtual void WriteUnknown(WireFormat.WireType wireType, int fieldNumber, ByteString value)
- { }
+ {
+ }
#region ICodedOutputStream Members
void ICodedOutputStream.WriteUnknownGroup(int fieldNumber, IMessageLite value)
- { }
+ {
+ }
+
void ICodedOutputStream.WriteUnknownBytes(int fieldNumber, ByteString value)
- { }
+ {
+ }
+
void ICodedOutputStream.WriteUnknownField(int fieldNumber, WireFormat.WireType type, ulong value)
- { }
+ {
+ }
void ICodedOutputStream.WriteMessageSetExtension(int fieldNumber, string fieldName, IMessageLite value)
- { }
+ {
+ }
void ICodedOutputStream.WriteMessageSetExtension(int fieldNumber, string fieldName, ByteString value)
- { }
+ {
+ }
void ICodedOutputStream.WriteField(FieldType fieldType, int fieldNumber, string fieldName, object value)
- { WriteField(fieldType, fieldName, value); }
+ {
+ WriteField(fieldType, fieldName, value);
+ }
void ICodedOutputStream.WriteDouble(int fieldNumber, string fieldName, double value)
- { Write(fieldName, value); }
+ {
+ Write(fieldName, value);
+ }
void ICodedOutputStream.WriteFloat(int fieldNumber, string fieldName, float value)
- { Write(fieldName, value); }
+ {
+ Write(fieldName, value);
+ }
void ICodedOutputStream.WriteUInt64(int fieldNumber, string fieldName, ulong value)
- { Write(fieldName, value); }
+ {
+ Write(fieldName, value);
+ }
void ICodedOutputStream.WriteInt64(int fieldNumber, string fieldName, long value)
- { Write(fieldName, value); }
+ {
+ Write(fieldName, value);
+ }
void ICodedOutputStream.WriteInt32(int fieldNumber, string fieldName, int value)
- { Write(fieldName, value); }
+ {
+ Write(fieldName, value);
+ }
void ICodedOutputStream.WriteFixed64(int fieldNumber, string fieldName, ulong value)
- { Write(fieldName, value); }
+ {
+ Write(fieldName, value);
+ }
void ICodedOutputStream.WriteFixed32(int fieldNumber, string fieldName, uint value)
- { Write(fieldName, value); }
+ {
+ Write(fieldName, value);
+ }
void ICodedOutputStream.WriteBool(int fieldNumber, string fieldName, bool value)
- { Write(fieldName, value); }
+ {
+ Write(fieldName, value);
+ }
void ICodedOutputStream.WriteString(int fieldNumber, string fieldName, string value)
- { Write(fieldName, value); }
+ {
+ Write(fieldName, value);
+ }
void ICodedOutputStream.WriteGroup(int fieldNumber, string fieldName, IMessageLite value)
- { WriteMessageOrGroup(fieldName, value); }
+ {
+ WriteMessageOrGroup(fieldName, value);
+ }
void ICodedOutputStream.WriteMessage(int fieldNumber, string fieldName, IMessageLite value)
- { WriteMessageOrGroup(fieldName, value); }
+ {
+ WriteMessageOrGroup(fieldName, value);
+ }
void ICodedOutputStream.WriteBytes(int fieldNumber, string fieldName, ByteString value)
- { Write(fieldName, value); }
+ {
+ Write(fieldName, value);
+ }
void ICodedOutputStream.WriteUInt32(int fieldNumber, string fieldName, uint value)
- { Write(fieldName, value); }
+ {
+ Write(fieldName, value);
+ }
void ICodedOutputStream.WriteEnum(int fieldNumber, string fieldName, int value, object rawValue)
- { WriteEnum(fieldName, value, rawValue.ToString()); }
+ {
+ WriteEnum(fieldName, value, rawValue.ToString());
+ }
void ICodedOutputStream.WriteSFixed32(int fieldNumber, string fieldName, int value)
- { Write(fieldName, value); }
+ {
+ Write(fieldName, value);
+ }
void ICodedOutputStream.WriteSFixed64(int fieldNumber, string fieldName, long value)
- { Write(fieldName, value); }
+ {
+ Write(fieldName, value);
+ }
void ICodedOutputStream.WriteSInt32(int fieldNumber, string fieldName, int value)
- { Write(fieldName, value); }
+ {
+ Write(fieldName, value);
+ }
void ICodedOutputStream.WriteSInt64(int fieldNumber, string fieldName, long value)
- { Write(fieldName, value); }
+ {
+ Write(fieldName, value);
+ }
void ICodedOutputStream.WriteArray(FieldType fieldType, int fieldNumber, string fieldName, IEnumerable list)
- { WriteArray(fieldType, fieldName, list); }
+ {
+ WriteArray(fieldType, fieldName, list);
+ }
- void ICodedOutputStream.WriteGroupArray<T>(int fieldNumber, string fieldName, System.Collections.Generic.IEnumerable<T> list)
- { WriteArray(FieldType.Group, fieldName, list); }
+ void ICodedOutputStream.WriteGroupArray<T>(int fieldNumber, string fieldName, IEnumerable<T> list)
+ {
+ WriteArray(FieldType.Group, fieldName, list);
+ }
- void ICodedOutputStream.WriteMessageArray<T>(int fieldNumber, string fieldName, System.Collections.Generic.IEnumerable<T> list)
- { WriteArray(FieldType.Message, fieldName, list); }
+ void ICodedOutputStream.WriteMessageArray<T>(int fieldNumber, string fieldName, IEnumerable<T> list)
+ {
+ WriteArray(FieldType.Message, fieldName, list);
+ }
- void ICodedOutputStream.WriteStringArray(int fieldNumber, string fieldName, System.Collections.Generic.IEnumerable<string> list)
- { WriteArray(FieldType.String, fieldName, list); }
+ void ICodedOutputStream.WriteStringArray(int fieldNumber, string fieldName, IEnumerable<string> list)
+ {
+ WriteArray(FieldType.String, fieldName, list);
+ }
- void ICodedOutputStream.WriteBytesArray(int fieldNumber, string fieldName, System.Collections.Generic.IEnumerable<ByteString> list)
- { WriteArray(FieldType.Bytes, fieldName, list); }
+ void ICodedOutputStream.WriteBytesArray(int fieldNumber, string fieldName, IEnumerable<ByteString> list)
+ {
+ WriteArray(FieldType.Bytes, fieldName, list);
+ }
- void ICodedOutputStream.WriteBoolArray(int fieldNumber, string fieldName, System.Collections.Generic.IEnumerable<bool> list)
- { WriteArray(FieldType.Bool, fieldName, list); }
+ void ICodedOutputStream.WriteBoolArray(int fieldNumber, string fieldName, IEnumerable<bool> list)
+ {
+ WriteArray(FieldType.Bool, fieldName, list);
+ }
- void ICodedOutputStream.WriteInt32Array(int fieldNumber, string fieldName, System.Collections.Generic.IEnumerable<int> list)
- { WriteArray(FieldType.Int32, fieldName, list); }
+ void ICodedOutputStream.WriteInt32Array(int fieldNumber, string fieldName, IEnumerable<int> list)
+ {
+ WriteArray(FieldType.Int32, fieldName, list);
+ }
- void ICodedOutputStream.WriteSInt32Array(int fieldNumber, string fieldName, System.Collections.Generic.IEnumerable<int> list)
- { WriteArray(FieldType.SInt32, fieldName, list); }
+ void ICodedOutputStream.WriteSInt32Array(int fieldNumber, string fieldName, IEnumerable<int> list)
+ {
+ WriteArray(FieldType.SInt32, fieldName, list);
+ }
- void ICodedOutputStream.WriteUInt32Array(int fieldNumber, string fieldName, System.Collections.Generic.IEnumerable<uint> list)
- { WriteArray(FieldType.UInt32, fieldName, list); }
+ void ICodedOutputStream.WriteUInt32Array(int fieldNumber, string fieldName, IEnumerable<uint> list)
+ {
+ WriteArray(FieldType.UInt32, fieldName, list);
+ }
- void ICodedOutputStream.WriteFixed32Array(int fieldNumber, string fieldName, System.Collections.Generic.IEnumerable<uint> list)
- { WriteArray(FieldType.Fixed32, fieldName, list); }
+ void ICodedOutputStream.WriteFixed32Array(int fieldNumber, string fieldName, IEnumerable<uint> list)
+ {
+ WriteArray(FieldType.Fixed32, fieldName, list);
+ }
- void ICodedOutputStream.WriteSFixed32Array(int fieldNumber, string fieldName, System.Collections.Generic.IEnumerable<int> list)
- { WriteArray(FieldType.SFixed32, fieldName, list); }
+ void ICodedOutputStream.WriteSFixed32Array(int fieldNumber, string fieldName, IEnumerable<int> list)
+ {
+ WriteArray(FieldType.SFixed32, fieldName, list);
+ }
- void ICodedOutputStream.WriteInt64Array(int fieldNumber, string fieldName, System.Collections.Generic.IEnumerable<long> list)
- { WriteArray(FieldType.Int64, fieldName, list); }
+ void ICodedOutputStream.WriteInt64Array(int fieldNumber, string fieldName, IEnumerable<long> list)
+ {
+ WriteArray(FieldType.Int64, fieldName, list);
+ }
- void ICodedOutputStream.WriteSInt64Array(int fieldNumber, string fieldName, System.Collections.Generic.IEnumerable<long> list)
- { WriteArray(FieldType.SInt64, fieldName, list); }
+ void ICodedOutputStream.WriteSInt64Array(int fieldNumber, string fieldName, IEnumerable<long> list)
+ {
+ WriteArray(FieldType.SInt64, fieldName, list);
+ }
- void ICodedOutputStream.WriteUInt64Array(int fieldNumber, string fieldName, System.Collections.Generic.IEnumerable<ulong> list)
- { WriteArray(FieldType.UInt64, fieldName, list); }
+ void ICodedOutputStream.WriteUInt64Array(int fieldNumber, string fieldName, IEnumerable<ulong> list)
+ {
+ WriteArray(FieldType.UInt64, fieldName, list);
+ }
- void ICodedOutputStream.WriteFixed64Array(int fieldNumber, string fieldName, System.Collections.Generic.IEnumerable<ulong> list)
- { WriteArray(FieldType.Fixed64, fieldName, list); }
+ void ICodedOutputStream.WriteFixed64Array(int fieldNumber, string fieldName, IEnumerable<ulong> list)
+ {
+ WriteArray(FieldType.Fixed64, fieldName, list);
+ }
- void ICodedOutputStream.WriteSFixed64Array(int fieldNumber, string fieldName, System.Collections.Generic.IEnumerable<long> list)
- { WriteArray(FieldType.SFixed64, fieldName, list); }
+ void ICodedOutputStream.WriteSFixed64Array(int fieldNumber, string fieldName, IEnumerable<long> list)
+ {
+ WriteArray(FieldType.SFixed64, fieldName, list);
+ }
- void ICodedOutputStream.WriteDoubleArray(int fieldNumber, string fieldName, System.Collections.Generic.IEnumerable<double> list)
- { WriteArray(FieldType.Double, fieldName, list); }
+ void ICodedOutputStream.WriteDoubleArray(int fieldNumber, string fieldName, IEnumerable<double> list)
+ {
+ WriteArray(FieldType.Double, fieldName, list);
+ }
- void ICodedOutputStream.WriteFloatArray(int fieldNumber, string fieldName, System.Collections.Generic.IEnumerable<float> list)
- { WriteArray(FieldType.Float, fieldName, list); }
+ void ICodedOutputStream.WriteFloatArray(int fieldNumber, string fieldName, IEnumerable<float> list)
+ {
+ WriteArray(FieldType.Float, fieldName, list);
+ }
- void ICodedOutputStream.WriteEnumArray<T>(int fieldNumber, string fieldName, System.Collections.Generic.IEnumerable<T> list)
- { WriteArray(FieldType.Enum, fieldName, list); }
+ void ICodedOutputStream.WriteEnumArray<T>(int fieldNumber, string fieldName, IEnumerable<T> list)
+ {
+ WriteArray(FieldType.Enum, fieldName, list);
+ }
- void ICodedOutputStream.WritePackedArray(FieldType fieldType, int fieldNumber, string fieldName, IEnumerable list)
- { WriteArray(fieldType, fieldName, list); }
+ void ICodedOutputStream.WritePackedArray(FieldType fieldType, int fieldNumber, string fieldName,
+ IEnumerable list)
+ {
+ WriteArray(fieldType, fieldName, list);
+ }
- void ICodedOutputStream.WritePackedBoolArray(int fieldNumber, string fieldName, int computedSize, System.Collections.Generic.IEnumerable<bool> list)
- { WriteArray(FieldType.Bool, fieldName, list); }
+ void ICodedOutputStream.WritePackedBoolArray(int fieldNumber, string fieldName, int computedSize,
+ IEnumerable<bool> list)
+ {
+ WriteArray(FieldType.Bool, fieldName, list);
+ }
- void ICodedOutputStream.WritePackedInt32Array(int fieldNumber, string fieldName, int computedSize, System.Collections.Generic.IEnumerable<int> list)
- { WriteArray(FieldType.Int32, fieldName, list); }
+ void ICodedOutputStream.WritePackedInt32Array(int fieldNumber, string fieldName, int computedSize,
+ IEnumerable<int> list)
+ {
+ WriteArray(FieldType.Int32, fieldName, list);
+ }
- void ICodedOutputStream.WritePackedSInt32Array(int fieldNumber, string fieldName, int computedSize, System.Collections.Generic.IEnumerable<int> list)
- { WriteArray(FieldType.SInt32, fieldName, list); }
+ void ICodedOutputStream.WritePackedSInt32Array(int fieldNumber, string fieldName, int computedSize,
+ IEnumerable<int> list)
+ {
+ WriteArray(FieldType.SInt32, fieldName, list);
+ }
- void ICodedOutputStream.WritePackedUInt32Array(int fieldNumber, string fieldName, int computedSize, System.Collections.Generic.IEnumerable<uint> list)
- { WriteArray(FieldType.UInt32, fieldName, list); }
+ void ICodedOutputStream.WritePackedUInt32Array(int fieldNumber, string fieldName, int computedSize,
+ IEnumerable<uint> list)
+ {
+ WriteArray(FieldType.UInt32, fieldName, list);
+ }
- void ICodedOutputStream.WritePackedFixed32Array(int fieldNumber, string fieldName, int computedSize, System.Collections.Generic.IEnumerable<uint> list)
- { WriteArray(FieldType.Fixed32, fieldName, list); }
+ void ICodedOutputStream.WritePackedFixed32Array(int fieldNumber, string fieldName, int computedSize,
+ IEnumerable<uint> list)
+ {
+ WriteArray(FieldType.Fixed32, fieldName, list);
+ }
- void ICodedOutputStream.WritePackedSFixed32Array(int fieldNumber, string fieldName, int computedSize, System.Collections.Generic.IEnumerable<int> list)
- { WriteArray(FieldType.SFixed32, fieldName, list); }
+ void ICodedOutputStream.WritePackedSFixed32Array(int fieldNumber, string fieldName, int computedSize,
+ IEnumerable<int> list)
+ {
+ WriteArray(FieldType.SFixed32, fieldName, list);
+ }
- void ICodedOutputStream.WritePackedInt64Array(int fieldNumber, string fieldName, int computedSize, System.Collections.Generic.IEnumerable<long> list)
- { WriteArray(FieldType.Int64, fieldName, list); }
+ void ICodedOutputStream.WritePackedInt64Array(int fieldNumber, string fieldName, int computedSize,
+ IEnumerable<long> list)
+ {
+ WriteArray(FieldType.Int64, fieldName, list);
+ }
- void ICodedOutputStream.WritePackedSInt64Array(int fieldNumber, string fieldName, int computedSize, System.Collections.Generic.IEnumerable<long> list)
- { WriteArray(FieldType.SInt64, fieldName, list); }
+ void ICodedOutputStream.WritePackedSInt64Array(int fieldNumber, string fieldName, int computedSize,
+ IEnumerable<long> list)
+ {
+ WriteArray(FieldType.SInt64, fieldName, list);
+ }
- void ICodedOutputStream.WritePackedUInt64Array(int fieldNumber, string fieldName, int computedSize, System.Collections.Generic.IEnumerable<ulong> list)
- { WriteArray(FieldType.UInt64, fieldName, list); }
+ void ICodedOutputStream.WritePackedUInt64Array(int fieldNumber, string fieldName, int computedSize,
+ IEnumerable<ulong> list)
+ {
+ WriteArray(FieldType.UInt64, fieldName, list);
+ }
- void ICodedOutputStream.WritePackedFixed64Array(int fieldNumber, string fieldName, int computedSize, System.Collections.Generic.IEnumerable<ulong> list)
- { WriteArray(FieldType.Fixed64, fieldName, list); }
+ void ICodedOutputStream.WritePackedFixed64Array(int fieldNumber, string fieldName, int computedSize,
+ IEnumerable<ulong> list)
+ {
+ WriteArray(FieldType.Fixed64, fieldName, list);
+ }
- void ICodedOutputStream.WritePackedSFixed64Array(int fieldNumber, string fieldName, int computedSize, System.Collections.Generic.IEnumerable<long> list)
- { WriteArray(FieldType.SFixed64, fieldName, list); }
+ void ICodedOutputStream.WritePackedSFixed64Array(int fieldNumber, string fieldName, int computedSize,
+ IEnumerable<long> list)
+ {
+ WriteArray(FieldType.SFixed64, fieldName, list);
+ }
- void ICodedOutputStream.WritePackedDoubleArray(int fieldNumber, string fieldName, int computedSize, System.Collections.Generic.IEnumerable<double> list)
- { WriteArray(FieldType.Double, fieldName, list); }
+ void ICodedOutputStream.WritePackedDoubleArray(int fieldNumber, string fieldName, int computedSize,
+ IEnumerable<double> list)
+ {
+ WriteArray(FieldType.Double, fieldName, list);
+ }
- void ICodedOutputStream.WritePackedFloatArray(int fieldNumber, string fieldName, int computedSize, System.Collections.Generic.IEnumerable<float> list)
- { WriteArray(FieldType.Float, fieldName, list); }
+ void ICodedOutputStream.WritePackedFloatArray(int fieldNumber, string fieldName, int computedSize,
+ IEnumerable<float> list)
+ {
+ WriteArray(FieldType.Float, fieldName, list);
+ }
- void ICodedOutputStream.WritePackedEnumArray<T>(int fieldNumber, string fieldName, int computedSize, System.Collections.Generic.IEnumerable<T> list)
- { WriteArray(FieldType.Enum, fieldName, list); }
+ void ICodedOutputStream.WritePackedEnumArray<T>(int fieldNumber, string fieldName, int computedSize,
+ IEnumerable<T> list)
+ {
+ WriteArray(FieldType.Enum, fieldName, list);
+ }
#endregion
}
-}
+} \ No newline at end of file
diff --git a/src/ProtocolBuffers/Serialization/DictionaryReader.cs b/src/ProtocolBuffers/Serialization/DictionaryReader.cs
index d6e5c189..70028958 100644
--- a/src/ProtocolBuffers/Serialization/DictionaryReader.cs
+++ b/src/ProtocolBuffers/Serialization/DictionaryReader.cs
@@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
-using System.Text;
+using System.Globalization;
using Google.ProtocolBuffers.Descriptors;
namespace Google.ProtocolBuffers.Serialization
@@ -54,23 +54,32 @@ namespace Google.ProtocolBuffers.Serialization
private bool GetValue<T>(ref T value)
{
- if (!_ready) return false;
+ if (!_ready)
+ {
+ return false;
+ }
object obj = _input.Current.Value;
if (obj is T)
- value = (T)obj;
+ {
+ value = (T) obj;
+ }
else
{
- try
+ try
{
if (obj is IConvertible)
- value = (T)Convert.ChangeType(obj, typeof(T), System.Globalization.CultureInfo.InvariantCulture);
+ {
+ value = (T) Convert.ChangeType(obj, typeof (T), CultureInfo.InvariantCulture);
+ }
else
- value = (T)obj;
+ {
+ value = (T) obj;
+ }
}
catch
{
- _ready = _input.MoveNext();
+ _ready = _input.MoveNext();
return false;
}
}
@@ -83,7 +92,7 @@ namespace Google.ProtocolBuffers.Serialization
/// </summary>
protected override bool Read(ref bool value)
{
- return GetValue(ref value);
+ return GetValue(ref value);
}
/// <summary>
@@ -186,35 +195,42 @@ namespace Google.ProtocolBuffers.Serialization
object[] array = null;
if (GetValue(ref array))
{
- if (typeof(T) == typeof(ByteString))
+ if (typeof (T) == typeof (ByteString))
{
- ICollection<ByteString> output = (ICollection<ByteString>)items;
+ ICollection<ByteString> output = (ICollection<ByteString>) items;
foreach (byte[] item in array)
+ {
output.Add(ByteString.CopyFrom(item));
+ }
}
else
{
foreach (T item in array)
+ {
items.Add(item);
+ }
}
return true;
}
return false;
}
-
+
public override bool ReadEnumArray(string field, ICollection<object> items)
{
object[] array = null;
if (GetValue(ref array))
{
foreach (object item in array)
+ {
items.Add(item);
+ }
return true;
}
return false;
}
- public override bool ReadMessageArray<T>(string field, ICollection<T> items, IMessageLite messageType, ExtensionRegistry registry)
+ public override bool ReadMessageArray<T>(string field, ICollection<T> items, IMessageLite messageType,
+ ExtensionRegistry registry)
{
object[] array = null;
if (GetValue(ref array))
@@ -223,14 +239,17 @@ namespace Google.ProtocolBuffers.Serialization
{
IBuilderLite builder = messageType.WeakCreateBuilderForType();
new DictionaryReader(item).Merge(builder);
- items.Add((T)builder.WeakBuild());
+ items.Add((T) builder.WeakBuild());
}
return true;
}
return false;
}
- public override bool ReadGroupArray<T>(string field, ICollection<T> items, IMessageLite messageType, ExtensionRegistry registry)
- { return ReadMessageArray(field, items, messageType, registry); }
+ public override bool ReadGroupArray<T>(string field, ICollection<T> items, IMessageLite messageType,
+ ExtensionRegistry registry)
+ {
+ return ReadMessageArray(field, items, messageType, registry);
+ }
}
-}
+} \ No newline at end of file
diff --git a/src/ProtocolBuffers/Serialization/DictionaryWriter.cs b/src/ProtocolBuffers/Serialization/DictionaryWriter.cs
index ccfa31f0..96175a7e 100644
--- a/src/ProtocolBuffers/Serialization/DictionaryWriter.cs
+++ b/src/ProtocolBuffers/Serialization/DictionaryWriter.cs
@@ -1,4 +1,5 @@
using System;
+using System.Collections;
using System.Collections.Generic;
using Google.ProtocolBuffers.Descriptors;
@@ -15,8 +16,9 @@ namespace Google.ProtocolBuffers.Serialization
/// Constructs a writer using a new dictionary
/// </summary>
public DictionaryWriter()
- : this(new Dictionary<string,object>(StringComparer.Ordinal))
- { }
+ : this(new Dictionary<string, object>(StringComparer.Ordinal))
+ {
+ }
/// <summary>
/// Constructs a writer using an existing dictionary
@@ -38,9 +40,12 @@ namespace Google.ProtocolBuffers.Serialization
/// <summary>
/// Accesses the dictionary that is backing this writer
/// </summary>
- public IDictionary<string, object> ToDictionary() { return _output; }
+ public IDictionary<string, object> ToDictionary()
+ {
+ return _output;
+ }
- /// <summary>
+ /// <summary>
/// Writes the message to the the formatted stream.
/// </summary>
public override void WriteMessage(IMessageLite message)
@@ -144,7 +149,7 @@ namespace Google.ProtocolBuffers.Serialization
/// <summary>
/// Writes an array of field values
/// </summary>
- protected override void WriteArray(FieldType fieldType, string field, System.Collections.IEnumerable items)
+ protected override void WriteArray(FieldType fieldType, string field, IEnumerable items)
{
List<object> objects = new List<object>();
foreach (object o in items)
@@ -155,18 +160,22 @@ namespace Google.ProtocolBuffers.Serialization
case FieldType.Message:
{
DictionaryWriter writer = Create();
- writer.WriteMessage((IMessageLite)o);
+ writer.WriteMessage((IMessageLite) o);
objects.Add(writer.ToDictionary());
}
break;
case FieldType.Bytes:
- objects.Add(((ByteString)o).ToByteArray());
+ objects.Add(((ByteString) o).ToByteArray());
break;
case FieldType.Enum:
if (o is IEnumLite)
- objects.Add(((IEnumLite)o).Number);
+ {
+ objects.Add(((IEnumLite) o).Number);
+ }
else
- objects.Add((int)o);
+ {
+ objects.Add((int) o);
+ }
break;
default:
objects.Add(o);
@@ -177,4 +186,4 @@ namespace Google.ProtocolBuffers.Serialization
_output[field] = objects.ToArray();
}
}
-}
+} \ No newline at end of file
diff --git a/src/ProtocolBuffers/Serialization/JsonFormatReader.cs b/src/ProtocolBuffers/Serialization/JsonFormatReader.cs
index 2808ff5c..d4505d70 100644
--- a/src/ProtocolBuffers/Serialization/JsonFormatReader.cs
+++ b/src/ProtocolBuffers/Serialization/JsonFormatReader.cs
@@ -13,28 +13,51 @@ namespace Google.ProtocolBuffers.Serialization
private readonly JsonCursor _input;
private readonly Stack<int> _stopChar;
- enum ReaderState { Start, BeginValue, EndValue, BeginObject, BeginArray }
- string _current;
- ReaderState _state;
+ private enum ReaderState
+ {
+ Start,
+ BeginValue,
+ EndValue,
+ BeginObject,
+ BeginArray
+ }
+
+ private string _current;
+ private ReaderState _state;
/// <summary>
/// Constructs a JsonFormatReader to parse Json into a message, this method does not use text encoding, all bytes MUST
/// represent ASCII character values.
/// </summary>
- public static JsonFormatReader CreateInstance(Stream stream) { return new JsonFormatReader(JsonCursor.CreateInstance(stream)); }
+ public static JsonFormatReader CreateInstance(Stream stream)
+ {
+ return new JsonFormatReader(JsonCursor.CreateInstance(stream));
+ }
+
/// <summary>
/// Constructs a JsonFormatReader to parse Json into a message, this method does not use text encoding, all bytes MUST
/// represent ASCII character values.
/// </summary>
- public static JsonFormatReader CreateInstance(byte[] bytes) { return new JsonFormatReader(JsonCursor.CreateInstance(bytes)); }
+ public static JsonFormatReader CreateInstance(byte[] bytes)
+ {
+ return new JsonFormatReader(JsonCursor.CreateInstance(bytes));
+ }
+
/// <summary>
/// Constructs a JsonFormatReader to parse Json into a message
/// </summary>
- public static JsonFormatReader CreateInstance(string jsonText) { return new JsonFormatReader(JsonCursor.CreateInstance(jsonText)); }
+ public static JsonFormatReader CreateInstance(string jsonText)
+ {
+ return new JsonFormatReader(JsonCursor.CreateInstance(jsonText));
+ }
+
/// <summary>
/// Constructs a JsonFormatReader to parse Json into a message
/// </summary>
- public static JsonFormatReader CreateInstance(TextReader input) { return new JsonFormatReader(JsonCursor.CreateInstance(input)); }
+ public static JsonFormatReader CreateInstance(TextReader input)
+ {
+ return new JsonFormatReader(JsonCursor.CreateInstance(input));
+ }
/// <summary>
/// Constructs a JsonFormatReader to parse Json into a message
@@ -52,12 +75,16 @@ namespace Google.ProtocolBuffers.Serialization
/// </summary>
protected JsonFormatReader(TextReader input)
: this(JsonCursor.CreateInstance(input))
- { }
+ {
+ }
/// <summary>
/// Returns true if the reader is currently on an array element
/// </summary>
- public bool IsArrayMessage { get { return _input.NextChar == '['; } }
+ public bool IsArrayMessage
+ {
+ get { return _input.NextChar == '['; }
+ }
/// <summary>
/// Returns an enumerator that is used to cursor over an array of messages
@@ -68,7 +95,9 @@ namespace Google.ProtocolBuffers.Serialization
public IEnumerable<JsonFormatReader> EnumerateArray()
{
foreach (string ignored in ForeachArrayItem(_current))
+ {
yield return this;
+ }
}
/// <summary>
@@ -81,7 +110,7 @@ namespace Google.ProtocolBuffers.Serialization
_state = ReaderState.BeginObject;
builder.WeakMergeFrom(this, registry);
- _input.Consume((char)_stopChar.Pop());
+ _input.Consume((char) _stopChar.Pop());
_state = ReaderState.EndValue;
return builder;
}
@@ -106,18 +135,24 @@ namespace Google.ProtocolBuffers.Serialization
protected override bool PeekNext(out string field)
{
field = _current;
- if(_state == ReaderState.BeginValue)
+ if (_state == ReaderState.BeginValue)
+ {
return true;
+ }
int next = _input.NextChar;
if (next == _stopChar.Peek())
+ {
return false;
+ }
_input.Assert(next != -1, "Unexpected end of file.");
//not sure about this yet, it will allow {, "a":true }
if (_state == ReaderState.EndValue && !_input.TryConsume(','))
+ {
return false;
+ }
field = _current = _input.ReadString();
_input.Consume(':');
@@ -134,19 +169,31 @@ namespace Google.ProtocolBuffers.Serialization
JsonCursor.JsType type = _input.ReadVariant(out temp);
_state = ReaderState.EndValue;
- _input.Assert(type != JsonCursor.JsType.Array && type != JsonCursor.JsType.Object, "Encountered {0} while expecting {1}", type, typeInfo);
+ _input.Assert(type != JsonCursor.JsType.Array && type != JsonCursor.JsType.Object,
+ "Encountered {0} while expecting {1}", type, typeInfo);
if (type == JsonCursor.JsType.Null)
+ {
return false;
- if (type == JsonCursor.JsType.True) value = "1";
- else if (type == JsonCursor.JsType.False) value = "0";
- else value = temp as string;
+ }
+ if (type == JsonCursor.JsType.True)
+ {
+ value = "1";
+ }
+ else if (type == JsonCursor.JsType.False)
+ {
+ value = "0";
+ }
+ else
+ {
+ value = temp as string;
+ }
//exponent representation of integer number:
if (value != null && type == JsonCursor.JsType.Number &&
- (typeInfo != typeof(double) && typeInfo != typeof(float)) &&
+ (typeInfo != typeof (double) && typeInfo != typeof (float)) &&
value.IndexOf("e", StringComparison.OrdinalIgnoreCase) > 0)
{
- value = XmlConvert.ToString((long)Math.Round(XmlConvert.ToDouble(value), 0));
+ value = XmlConvert.ToString((long) Math.Round(XmlConvert.ToDouble(value), 0));
}
return value != null;
}
@@ -177,10 +224,12 @@ namespace Google.ProtocolBuffers.Serialization
{
_current = field;
yield return field;
- if(!_input.TryConsume(','))
+ if (!_input.TryConsume(','))
+ {
break;
+ }
}
- _input.Consume((char)_stopChar.Pop());
+ _input.Consume((char) _stopChar.Pop());
_state = ReaderState.EndValue;
}
@@ -192,6 +241,5 @@ namespace Google.ProtocolBuffers.Serialization
Merge(builder, registry);
return true;
}
-
}
-}
+} \ No newline at end of file
diff --git a/src/ProtocolBuffers/Serialization/JsonFormatWriter.cs b/src/ProtocolBuffers/Serialization/JsonFormatWriter.cs
index b2057524..d54507cc 100644
--- a/src/ProtocolBuffers/Serialization/JsonFormatWriter.cs
+++ b/src/ProtocolBuffers/Serialization/JsonFormatWriter.cs
@@ -1,4 +1,5 @@
using System;
+using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
@@ -14,11 +15,12 @@ namespace Google.ProtocolBuffers.Serialization
public abstract class JsonFormatWriter : AbstractTextWriter
{
#region buffering implementations
+
private class JsonTextWriter : JsonFormatWriter
{
private readonly char[] _buffer;
private TextWriter _output;
- int _bufferPos;
+ private int _bufferPos;
public JsonTextWriter(TextWriter output)
{
@@ -36,7 +38,9 @@ namespace Google.ProtocolBuffers.Serialization
Flush();
if (_output != null)
+ {
return _output.ToString();
+ }
return new String(_buffer, 0, _bufferPos);
}
@@ -46,7 +50,9 @@ namespace Google.ProtocolBuffers.Serialization
if (_bufferPos + len >= _buffer.Length)
{
if (_output == null)
- _output = new StringWriter(new System.Text.StringBuilder(_buffer.Length * 2 + len));
+ {
+ _output = new StringWriter(new StringBuilder(_buffer.Length*2 + len));
+ }
Flush();
}
@@ -56,7 +62,9 @@ namespace Google.ProtocolBuffers.Serialization
{
int stop = offset + len;
for (int i = offset; i < stop; i++)
+ {
_buffer[_bufferPos++] = chars[i];
+ }
}
else
{
@@ -65,13 +73,17 @@ namespace Google.ProtocolBuffers.Serialization
}
}
else
+ {
_output.Write(chars, offset, len);
+ }
}
protected override void WriteToOutput(char ch)
{
if (_bufferPos >= _buffer.Length)
+ {
Flush();
+ }
_buffer[_bufferPos++] = ch;
}
@@ -85,16 +97,17 @@ namespace Google.ProtocolBuffers.Serialization
base.Flush();
}
}
+
private class JsonStreamWriter : JsonFormatWriter
{
#if SILVERLIGHT2 || COMPACT_FRAMEWORK_35
static readonly Encoding Encoding = Encoding.UTF8;
#else
- static readonly Encoding Encoding = Encoding.ASCII;
+ private static readonly Encoding Encoding = Encoding.ASCII;
#endif
private readonly byte[] _buffer;
private Stream _output;
- int _bufferPos;
+ private int _bufferPos;
public JsonStreamWriter(Stream output)
{
@@ -107,7 +120,9 @@ namespace Google.ProtocolBuffers.Serialization
protected override void WriteToOutput(char[] chars, int offset, int len)
{
if (_bufferPos + len >= _buffer.Length)
+ {
Flush();
+ }
if (len < _buffer.Length)
{
@@ -115,7 +130,9 @@ namespace Google.ProtocolBuffers.Serialization
{
int stop = offset + len;
for (int i = offset; i < stop; i++)
- _buffer[_bufferPos++] = (byte)chars[i];
+ {
+ _buffer[_bufferPos++] = (byte) chars[i];
+ }
}
else
{
@@ -132,8 +149,10 @@ namespace Google.ProtocolBuffers.Serialization
protected override void WriteToOutput(char ch)
{
if (_bufferPos >= _buffer.Length)
+ {
Flush();
- _buffer[_bufferPos++] = (byte)ch;
+ }
+ _buffer[_bufferPos++] = (byte) ch;
}
public override void Flush()
@@ -146,10 +165,12 @@ namespace Google.ProtocolBuffers.Serialization
base.Flush();
}
}
+
#endregion
private readonly List<int> _counter;
private bool _isArray;
+
/// <summary>
/// Constructs a JsonFormatWriter, use the ToString() member to extract the final Json on completion.
/// </summary>
@@ -161,26 +182,42 @@ namespace Google.ProtocolBuffers.Serialization
/// <summary>
/// Constructs a JsonFormatWriter, use ToString() to extract the final output
/// </summary>
- public static JsonFormatWriter CreateInstance() { return new JsonTextWriter(null); }
-
+ public static JsonFormatWriter CreateInstance()
+ {
+ return new JsonTextWriter(null);
+ }
+
/// <summary>
/// Constructs a JsonFormatWriter to output to the given text writer
/// </summary>
- public static JsonFormatWriter CreateInstance(TextWriter output) { return new JsonTextWriter(output); }
+ public static JsonFormatWriter CreateInstance(TextWriter output)
+ {
+ return new JsonTextWriter(output);
+ }
/// <summary>
/// Constructs a JsonFormatWriter to output to the given stream
/// </summary>
- public static JsonFormatWriter CreateInstance(Stream output) { return new JsonStreamWriter(output); }
+ public static JsonFormatWriter CreateInstance(Stream output)
+ {
+ return new JsonStreamWriter(output);
+ }
/// <summary> Write to the output stream </summary>
protected void WriteToOutput(string format, params object[] args)
- { WriteToOutput(String.Format(format, args)); }
+ {
+ WriteToOutput(String.Format(format, args));
+ }
+
/// <summary> Write to the output stream </summary>
protected void WriteToOutput(string text)
- { WriteToOutput(text.ToCharArray(), 0, text.Length); }
+ {
+ WriteToOutput(text.ToCharArray(), 0, text.Length);
+ }
+
/// <summary> Write to the output stream </summary>
protected abstract void WriteToOutput(char ch);
+
/// <summary> Write to the output stream </summary>
protected abstract void WriteToOutput(char[] chars, int offset, int len);
@@ -195,19 +232,25 @@ namespace Google.ProtocolBuffers.Serialization
/// <summary> Gets or sets the characters to use for the new-line, default = empty </summary>
public string NewLine { get; set; }
+
/// <summary> Gets or sets the text to use for indenting, default = empty </summary>
public string Indent { get; set; }
+
/// <summary> Gets or sets the whitespace to use to separate the text, default = empty </summary>
public string Whitespace { get; set; }
private void Seperator()
{
if (_counter.Count == 0)
+ {
throw new InvalidOperationException("Missmatched open/close in Json writer.");
+ }
int index = _counter.Count - 1;
if (_counter[index] > 0)
+ {
WriteToOutput(',');
+ }
WriteLine(String.Empty);
_counter[index] = _counter[index] + 1;
@@ -219,10 +262,14 @@ namespace Google.ProtocolBuffers.Serialization
{
WriteToOutput(NewLine);
for (int i = 1; i < _counter.Count; i++)
+ {
WriteToOutput(Indent);
+ }
}
- else if(!String.IsNullOrEmpty(Whitespace))
+ else if (!String.IsNullOrEmpty(Whitespace))
+ {
WriteToOutput(Whitespace);
+ }
WriteToOutput(content);
}
@@ -237,7 +284,9 @@ namespace Google.ProtocolBuffers.Serialization
WriteToOutput('"');
WriteToOutput(':');
if (!String.IsNullOrEmpty(Whitespace))
+ {
WriteToOutput(Whitespace);
+ }
}
}
@@ -250,23 +299,44 @@ namespace Google.ProtocolBuffers.Serialization
while (pos < len)
{
int next = pos;
- while (next < len && text[next] >= 32 && text[next] < 127 && text[next] != '\\' && text[next] != '/' && text[next] != '"')
+ while (next < len && text[next] >= 32 && text[next] < 127 && text[next] != '\\' && text[next] != '/' &&
+ text[next] != '"')
+ {
next++;
+ }
WriteToOutput(text, pos, next - pos);
if (next < len)
{
switch (text[next])
{
- case '"': WriteToOutput(@"\"""); break;
- case '\\': WriteToOutput(@"\\"); break;
- //odd at best to escape '/', most Json implementations don't, but it is defined in the rfc-4627
- case '/': WriteToOutput(@"\/"); break;
- case '\b': WriteToOutput(@"\b"); break;
- case '\f': WriteToOutput(@"\f"); break;
- case '\n': WriteToOutput(@"\n"); break;
- case '\r': WriteToOutput(@"\r"); break;
- case '\t': WriteToOutput(@"\t"); break;
- default: WriteToOutput(@"\u{0:x4}", (int)text[next]); break;
+ case '"':
+ WriteToOutput(@"\""");
+ break;
+ case '\\':
+ WriteToOutput(@"\\");
+ break;
+ //odd at best to escape '/', most Json implementations don't, but it is defined in the rfc-4627
+ case '/':
+ WriteToOutput(@"\/");
+ break;
+ case '\b':
+ WriteToOutput(@"\b");
+ break;
+ case '\f':
+ WriteToOutput(@"\f");
+ break;
+ case '\n':
+ WriteToOutput(@"\n");
+ break;
+ case '\r':
+ WriteToOutput(@"\r");
+ break;
+ case '\t':
+ WriteToOutput(@"\t");
+ break;
+ default:
+ WriteToOutput(@"\u{0:x4}", (int) text[next]);
+ break;
}
next++;
}
@@ -280,15 +350,22 @@ namespace Google.ProtocolBuffers.Serialization
protected override void WriteAsText(string field, string textValue, object typedValue)
{
WriteName(field);
- if(typedValue is bool || typedValue is int || typedValue is uint || typedValue is long || typedValue is ulong || typedValue is double || typedValue is float)
+ if (typedValue is bool || typedValue is int || typedValue is uint || typedValue is long ||
+ typedValue is ulong || typedValue is double || typedValue is float)
+ {
WriteToOutput(textValue);
+ }
else
{
WriteToOutput('"');
if (typedValue is string)
+ {
EncodeText(textValue);
+ }
else
+ {
WriteToOutput(textValue);
+ }
WriteToOutput('"');
}
}
@@ -299,7 +376,9 @@ namespace Google.ProtocolBuffers.Serialization
protected override void Write(string field, double value)
{
if (double.IsNaN(value) || double.IsNegativeInfinity(value) || double.IsPositiveInfinity(value))
+ {
throw new InvalidOperationException("This format does not support NaN, Infinity, or -Infinity");
+ }
base.Write(field, value);
}
@@ -309,7 +388,9 @@ namespace Google.ProtocolBuffers.Serialization
protected override void Write(string field, float value)
{
if (float.IsNaN(value) || float.IsNegativeInfinity(value) || float.IsPositiveInfinity(value))
+ {
throw new InvalidOperationException("This format does not support NaN, Infinity, or -Infinity");
+ }
base.Write(field, value);
}
@@ -322,11 +403,23 @@ namespace Google.ProtocolBuffers.Serialization
/// <summary>
/// Writes an array of field values
/// </summary>
- protected override void WriteArray(FieldType type, string field, System.Collections.IEnumerable items)
+ protected override void WriteArray(FieldType type, string field, IEnumerable items)
{
- System.Collections.IEnumerator enumerator = items.GetEnumerator();
- try { if (!enumerator.MoveNext()) return; }
- finally { if (enumerator is IDisposable) ((IDisposable)enumerator).Dispose(); }
+ IEnumerator enumerator = items.GetEnumerator();
+ try
+ {
+ if (!enumerator.MoveNext())
+ {
+ return;
+ }
+ }
+ finally
+ {
+ if (enumerator is IDisposable)
+ {
+ ((IDisposable) enumerator).Dispose();
+ }
+ }
WriteName(field);
WriteToOutput("[");
@@ -352,7 +445,10 @@ namespace Google.ProtocolBuffers.Serialization
/// </summary>
public override void WriteMessage(IMessageLite message)
{
- if (_isArray) Seperator();
+ if (_isArray)
+ {
+ Seperator();
+ }
WriteToOutput("{");
_counter.Add(0);
message.WriteTo(this);
@@ -371,9 +467,10 @@ namespace Google.ProtocolBuffers.Serialization
/// writer.WriteMessage(m);
/// </code>
/// </example>
- public sealed class JsonArray : IDisposable
+ public sealed class JsonArray : IDisposable
{
- JsonFormatWriter _writer;
+ private JsonFormatWriter _writer;
+
internal JsonArray(JsonFormatWriter writer)
{
_writer = writer;
@@ -384,7 +481,7 @@ namespace Google.ProtocolBuffers.Serialization
/// <summary>
/// Causes the end of the array character to be written.
/// </summary>
- void EndArray()
+ private void EndArray()
{
if (_writer != null)
{
@@ -392,9 +489,13 @@ namespace Google.ProtocolBuffers.Serialization
_writer.WriteLine("]");
_writer.Flush();
}
- _writer = null;
+ _writer = null;
+ }
+
+ void IDisposable.Dispose()
+ {
+ EndArray();
}
- void IDisposable.Dispose() { EndArray(); }
}
/// <summary>
@@ -409,7 +510,10 @@ namespace Google.ProtocolBuffers.Serialization
/// </example>
public JsonArray StartArray()
{
- if (_isArray) Seperator();
+ if (_isArray)
+ {
+ Seperator();
+ }
_isArray = true;
return new JsonArray(this);
}
diff --git a/src/ProtocolBuffers/Serialization/JsonTextCursor.cs b/src/ProtocolBuffers/Serialization/JsonTextCursor.cs
index a0b468f6..48e84ccc 100644
--- a/src/ProtocolBuffers/Serialization/JsonTextCursor.cs
+++ b/src/ProtocolBuffers/Serialization/JsonTextCursor.cs
@@ -1,20 +1,30 @@
using System;
using System.Collections.Generic;
+using System.Diagnostics;
using System.Globalization;
using System.IO;
-using System.Text;
namespace Google.ProtocolBuffers.Serialization
{
/// <summary>
/// JSon Tokenizer used by JsonFormatReader
/// </summary>
- abstract class JsonCursor
+ internal abstract class JsonCursor
{
- public enum JsType { String, Number, Object, Array, True, False, Null }
+ public enum JsType
+ {
+ String,
+ Number,
+ Object,
+ Array,
+ True,
+ False,
+ Null
+ }
#region Buffering implementations
- class JsonStreamCursor : JsonCursor
+
+ private class JsonStreamCursor : JsonCursor
{
private readonly byte[] _buffer;
private int _bufferPos;
@@ -25,6 +35,7 @@ namespace Google.ProtocolBuffers.Serialization
_input = input;
_next = _input.ReadByte();
}
+
public JsonStreamCursor(byte[] input)
{
_input = null;
@@ -35,11 +46,17 @@ namespace Google.ProtocolBuffers.Serialization
protected override int Peek()
{
if (_input != null)
+ {
return _next;
+ }
else if (_bufferPos < _buffer.Length)
+ {
return _buffer[_bufferPos];
+ }
else
+ {
return -1;
+ }
}
protected override int Read()
@@ -51,13 +68,17 @@ namespace Google.ProtocolBuffers.Serialization
return result;
}
else if (_bufferPos < _buffer.Length)
+ {
return _buffer[_bufferPos++];
+ }
else
+ {
return -1;
+ }
}
}
- class JsonTextCursor : JsonCursor
+ private class JsonTextCursor : JsonCursor
{
private readonly char[] _buffer;
private int _bufferPos;
@@ -80,55 +101,95 @@ namespace Google.ProtocolBuffers.Serialization
protected override int Peek()
{
if (_input != null)
+ {
return _input.Peek();
+ }
else if (_bufferPos < _buffer.Length)
+ {
return _buffer[_bufferPos];
+ }
else
+ {
return -1;
+ }
}
protected override int Read()
{
if (_input != null)
+ {
return _input.Read();
+ }
else if (_bufferPos < _buffer.Length)
+ {
return _buffer[_bufferPos++];
+ }
else
+ {
return -1;
+ }
}
}
+
#endregion
protected int _next;
private int _lineNo, _linePos;
- public static JsonCursor CreateInstance(byte[] input) { return new JsonStreamCursor(input); }
- public static JsonCursor CreateInstance(Stream input) { return new JsonStreamCursor(input); }
- public static JsonCursor CreateInstance(string input) { return new JsonTextCursor(input.ToCharArray()); }
- public static JsonCursor CreateInstance(TextReader input) { return new JsonTextCursor(input); }
+ public static JsonCursor CreateInstance(byte[] input)
+ {
+ return new JsonStreamCursor(input);
+ }
+
+ public static JsonCursor CreateInstance(Stream input)
+ {
+ return new JsonStreamCursor(input);
+ }
+
+ public static JsonCursor CreateInstance(string input)
+ {
+ return new JsonTextCursor(input.ToCharArray());
+ }
+
+ public static JsonCursor CreateInstance(TextReader input)
+ {
+ return new JsonTextCursor(input);
+ }
protected JsonCursor()
{
_lineNo = 1;
_linePos = 0;
}
-
+
/// <summary>Returns the next character without actually 'reading' it</summary>
protected abstract int Peek();
+
/// <summary>Reads the next character in the input</summary>
protected abstract int Read();
- public Char NextChar { get { SkipWhitespace(); return (char)_next; } }
+ public Char NextChar
+ {
+ get
+ {
+ SkipWhitespace();
+ return (char) _next;
+ }
+ }
#region Assert(...)
- [System.Diagnostics.DebuggerNonUserCode]
+
+ [DebuggerNonUserCode]
private string CharDisplay(int ch)
{
- return ch == -1 ? "EOF" :
- (ch > 32 && ch < 127) ? String.Format("'{0}'", (char)ch) :
- String.Format("'\\u{0:x4}'", ch);
+ return ch == -1
+ ? "EOF"
+ : (ch > 32 && ch < 127)
+ ? String.Format("'{0}'", (char) ch)
+ : String.Format("'\\u{0:x4}'", ch);
}
- [System.Diagnostics.DebuggerNonUserCode]
+
+ [DebuggerNonUserCode]
private void Assert(bool cond, char expected)
{
if (!cond)
@@ -136,13 +197,14 @@ namespace Google.ProtocolBuffers.Serialization
throw new FormatException(
String.Format(CultureInfo.InvariantCulture,
"({0}:{1}) error: Unexpected token {2}, expected: {3}.",
- _lineNo, _linePos,
+ _lineNo, _linePos,
CharDisplay(_next),
CharDisplay(expected)
));
}
}
- [System.Diagnostics.DebuggerNonUserCode]
+
+ [DebuggerNonUserCode]
public void Assert(bool cond, string message)
{
if (!cond)
@@ -152,18 +214,22 @@ namespace Google.ProtocolBuffers.Serialization
"({0},{1}) error: {2}", _lineNo, _linePos, message));
}
}
- [System.Diagnostics.DebuggerNonUserCode]
+
+ [DebuggerNonUserCode]
public void Assert(bool cond, string format, params object[] args)
{
if (!cond)
{
if (args != null && args.Length > 0)
+ {
format = String.Format(format, args);
+ }
throw new FormatException(
String.Format(CultureInfo.InvariantCulture,
"({0},{1}) error: {2}", _lineNo, _linePos, format));
}
}
+
#endregion
private char ReadChar()
@@ -180,10 +246,14 @@ namespace Google.ProtocolBuffers.Serialization
_linePos++;
}
_next = Peek();
- return (char)ch;
+ return (char) ch;
+ }
+
+ public void Consume(char ch)
+ {
+ Assert(TryConsume(ch), ch);
}
- public void Consume(char ch) { Assert(TryConsume(ch), ch); }
public bool TryConsume(char ch)
{
SkipWhitespace();
@@ -200,7 +270,9 @@ namespace Google.ProtocolBuffers.Serialization
SkipWhitespace();
foreach (char ch in sequence)
+ {
Assert(ch == ReadChar(), "Expected token '{0}'.", sequence);
+ }
}
public void SkipWhitespace()
@@ -208,8 +280,10 @@ namespace Google.ProtocolBuffers.Serialization
int chnext = _next;
while (chnext != -1)
{
- if (!Char.IsWhiteSpace((char)chnext))
+ if (!Char.IsWhiteSpace((char) chnext))
+ {
break;
+ }
ReadChar();
chnext = _next;
}
@@ -224,26 +298,39 @@ namespace Google.ProtocolBuffers.Serialization
{
if (_next == '\\')
{
- Consume('\\');//skip the escape
+ Consume('\\'); //skip the escape
char ch = ReadChar();
switch (ch)
{
- case 'b': sb.Add('\b'); break;
- case 'f': sb.Add('\f'); break;
- case 'n': sb.Add('\n'); break;
- case 'r': sb.Add('\r'); break;
- case 't': sb.Add('\t'); break;
+ case 'b':
+ sb.Add('\b');
+ break;
+ case 'f':
+ sb.Add('\f');
+ break;
+ case 'n':
+ sb.Add('\n');
+ break;
+ case 'r':
+ sb.Add('\r');
+ break;
+ case 't':
+ sb.Add('\t');
+ break;
case 'u':
{
- string hex = new string(new char[] { ReadChar(), ReadChar(), ReadChar(), ReadChar() });
+ string hex = new string(new char[] {ReadChar(), ReadChar(), ReadChar(), ReadChar()});
int result;
- Assert(int.TryParse(hex, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out result),
- "Expected a 4-character hex specifier.");
- sb.Add((char)result);
+ Assert(
+ int.TryParse(hex, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture,
+ out result),
+ "Expected a 4-character hex specifier.");
+ sb.Add((char) result);
break;
}
default:
- sb.Add(ch); break;
+ sb.Add(ch);
+ break;
}
}
else
@@ -261,18 +348,26 @@ namespace Google.ProtocolBuffers.Serialization
SkipWhitespace();
List<Char> sb = new List<char>(24);
if (_next == '-')
+ {
sb.Add(ReadChar());
+ }
Assert(_next >= '0' && _next <= '9', "Expected a numeric type.");
while ((_next >= '0' && _next <= '9') || _next == '.')
+ {
sb.Add(ReadChar());
+ }
if (_next == 'e' || _next == 'E')
{
sb.Add(ReadChar());
if (_next == '-' || _next == '+')
+ {
sb.Add(ReadChar());
+ }
Assert(_next >= '0' && _next <= '9', "Expected a numeric type.");
while (_next >= '0' && _next <= '9')
+ {
sb.Add(ReadChar());
+ }
}
return new String(sb.ToArray());
}
@@ -282,10 +377,21 @@ namespace Google.ProtocolBuffers.Serialization
SkipWhitespace();
switch (_next)
{
- case 'n': Consume("null"); value = null; return JsType.Null;
- case 't': Consume("true"); value = true; return JsType.True;
- case 'f': Consume("false"); value = false; return JsType.False;
- case '"': value = ReadString(); return JsType.String;
+ case 'n':
+ Consume("null");
+ value = null;
+ return JsType.Null;
+ case 't':
+ Consume("true");
+ value = true;
+ return JsType.True;
+ case 'f':
+ Consume("false");
+ value = false;
+ return JsType.False;
+ case '"':
+ value = ReadString();
+ return JsType.String;
case '{':
{
Consume('{');
@@ -296,7 +402,9 @@ namespace Google.ProtocolBuffers.Serialization
object tmp;
ReadVariant(out tmp);
if (!TryConsume(','))
+ {
break;
+ }
}
Consume('}');
value = null;
@@ -312,7 +420,9 @@ namespace Google.ProtocolBuffers.Serialization
ReadVariant(out tmp);
values.Add(tmp);
if (!TryConsume(','))
+ {
break;
+ }
}
Consume(']');
value = values.ToArray();
diff --git a/src/ProtocolBuffers/Serialization/XmlFormatReader.cs b/src/ProtocolBuffers/Serialization/XmlFormatReader.cs
index f3ca1314..112910a7 100644
--- a/src/ProtocolBuffers/Serialization/XmlFormatReader.cs
+++ b/src/ProtocolBuffers/Serialization/XmlFormatReader.cs
@@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.IO;
using System.Xml;
-using Google.ProtocolBuffers.Descriptors;
namespace Google.ProtocolBuffers.Serialization
{
@@ -17,31 +16,55 @@ namespace Google.ProtocolBuffers.Serialization
private readonly XmlReader _input;
private string _rootElementName;
- static XmlReaderSettings DefaultSettings
+ private static XmlReaderSettings DefaultSettings
{
- get { return new XmlReaderSettings() { CheckCharacters=false, IgnoreComments=true, IgnoreProcessingInstructions = true }; }
+ get
+ {
+ return new XmlReaderSettings()
+ {CheckCharacters = false, IgnoreComments = true, IgnoreProcessingInstructions = true};
+ }
}
/// <summary>
/// Constructs the XmlFormatReader using the stream provided as the xml
/// </summary>
- public static XmlFormatReader CreateInstance(byte[] input) { return new XmlFormatReader(XmlReader.Create(new MemoryStream(input, false), DefaultSettings)); }
+ public static XmlFormatReader CreateInstance(byte[] input)
+ {
+ return new XmlFormatReader(XmlReader.Create(new MemoryStream(input, false), DefaultSettings));
+ }
+
/// <summary>
/// Constructs the XmlFormatReader using the stream provided as the xml
/// </summary>
- public static XmlFormatReader CreateInstance(Stream input) { return new XmlFormatReader(XmlReader.Create(input, DefaultSettings)); }
+ public static XmlFormatReader CreateInstance(Stream input)
+ {
+ return new XmlFormatReader(XmlReader.Create(input, DefaultSettings));
+ }
+
/// <summary>
/// Constructs the XmlFormatReader using the string provided as the xml to be read
/// </summary>
- public static XmlFormatReader CreateInstance(String input) { return new XmlFormatReader(XmlReader.Create(new StringReader(input), DefaultSettings)); }
+ public static XmlFormatReader CreateInstance(String input)
+ {
+ return new XmlFormatReader(XmlReader.Create(new StringReader(input), DefaultSettings));
+ }
+
/// <summary>
/// Constructs the XmlFormatReader using the xml in the TextReader
/// </summary>
- public static XmlFormatReader CreateInstance(TextReader input) { return new XmlFormatReader(XmlReader.Create(input, DefaultSettings)); }
+ public static XmlFormatReader CreateInstance(TextReader input)
+ {
+ return new XmlFormatReader(XmlReader.Create(input, DefaultSettings));
+ }
+
/// <summary>
/// Constructs the XmlFormatReader with the XmlReader
/// </summary>
- public static XmlFormatReader CreateInstance(XmlReader input) { return new XmlFormatReader(input); }
+ public static XmlFormatReader CreateInstance(XmlReader input)
+ {
+ return new XmlFormatReader(input);
+ }
+
/// <summary>
/// Constructs the XmlFormatReader with the XmlReader and options
/// </summary>
@@ -51,23 +74,32 @@ namespace Google.ProtocolBuffers.Serialization
_rootElementName = DefaultRootElementName;
Options = XmlReaderOptions.None;
}
-
+
/// <summary>
/// Gets or sets the options to use when reading the xml
/// </summary>
public XmlReaderOptions Options { get; set; }
+
/// <summary>
/// Sets the options to use while generating the XML
/// </summary>
- public XmlFormatReader SetOptions(XmlReaderOptions options) { Options = options; return this; }
+ public XmlFormatReader SetOptions(XmlReaderOptions options)
+ {
+ Options = options;
+ return this;
+ }
/// <summary>
/// Gets or sets the default element name to use when using the Merge&lt;TBuilder>()
/// </summary>
- public string RootElementName
+ public string RootElementName
{
- get { return _rootElementName; }
- set { ThrowHelper.ThrowIfNull(value, "RootElementName"); _rootElementName = value; }
+ get { return _rootElementName; }
+ set
+ {
+ ThrowHelper.ThrowIfNull(value, "RootElementName");
+ _rootElementName = value;
+ }
}
private XmlFormatReader CloneWith(XmlReader rdr)
@@ -76,34 +108,45 @@ namespace Google.ProtocolBuffers.Serialization
copy._rootElementName = _rootElementName;
copy.Depth = Depth;
return copy;
-
}
+
private void NextElement()
{
while (!_input.IsStartElement() && _input.Read())
+ {
continue;
+ }
}
+
private static void Assert(bool cond)
{
- if (!cond) throw new FormatException();
+ if (!cond)
+ {
+ throw new FormatException();
+ }
}
/// <summary>
/// Merge the provided builder as an element named <see cref="RootElementName"/> in the current context
/// </summary>
public override TBuilder Merge<TBuilder>(TBuilder builder, ExtensionRegistry registry)
- { return Merge(_rootElementName, builder, registry); }
+ {
+ return Merge(_rootElementName, builder, registry);
+ }
/// <summary>
/// Merge the provided builder as an element of the current context
/// </summary>
public TBuilder Merge<TBuilder>(string element, TBuilder builder) where TBuilder : IBuilderLite
- { return Merge(element, builder, ExtensionRegistry.Empty); }
+ {
+ return Merge(element, builder, ExtensionRegistry.Empty);
+ }
/// <summary>
/// Merge the provided builder as an element of the current context
/// </summary>
- public TBuilder Merge<TBuilder>(string element, TBuilder builder, ExtensionRegistry registry) where TBuilder : IBuilderLite
+ public TBuilder Merge<TBuilder>(string element, TBuilder builder, ExtensionRegistry registry)
+ where TBuilder : IBuilderLite
{
string field;
Assert(PeekNext(out field) && field == element);
@@ -121,7 +164,7 @@ namespace Google.ProtocolBuffers.Serialization
protected override bool PeekNext(out string field)
{
NextElement();
- if(_input.IsStartElement())
+ if (_input.IsStartElement())
{
field = _input.LocalName;
return true;
@@ -141,7 +184,9 @@ namespace Google.ProtocolBuffers.Serialization
{
int depth = _input.Depth;
while (_input.Depth >= depth && _input.NodeType != XmlNodeType.EndElement)
+ {
Assert(_input.Read());
+ }
}
_input.Read();
}
@@ -171,7 +216,7 @@ namespace Google.ProtocolBuffers.Serialization
{
Assert(_input.NodeType == XmlNodeType.Element);
value = _input.ReadElementContentAsString();
-
+
return true;
}
@@ -187,7 +232,9 @@ namespace Google.ProtocolBuffers.Serialization
int depth = _input.Depth;
XmlReader child = _input.ReadSubtree();
while (!child.IsStartElement() && child.Read())
+ {
continue;
+ }
child.Read();
builder.WeakMergeFrom(CloneWith(child), registry);
Assert(depth == _input.Depth && _input.NodeType == XmlNodeType.EndElement);
@@ -211,7 +258,9 @@ namespace Google.ProtocolBuffers.Serialization
if (!isNested)
{
foreach (string item in NonNestedArrayItems(field))
+ {
yield return item;
+ }
yield break;
}
if (!_input.IsEmptyElement)
@@ -220,15 +269,19 @@ namespace Google.ProtocolBuffers.Serialization
XmlReader child = _input.ReadSubtree();
while (!child.IsStartElement() && child.Read())
+ {
continue;
+ }
child.Read();
foreach (string item in CloneWith(child).NonNestedArrayItems("item"))
+ {
yield return item;
+ }
Assert(depth == _input.Depth && _input.NodeType == XmlNodeType.EndElement);
}
_input.Read();
yield break;
}
}
-}
+} \ No newline at end of file
diff --git a/src/ProtocolBuffers/Serialization/XmlFormatWriter.cs b/src/ProtocolBuffers/Serialization/XmlFormatWriter.cs
index 1be2f390..fd36c1de 100644
--- a/src/ProtocolBuffers/Serialization/XmlFormatWriter.cs
+++ b/src/ProtocolBuffers/Serialization/XmlFormatWriter.cs
@@ -1,4 +1,5 @@
using System;
+using System.Collections;
using System.IO;
using System.Text;
using System.Xml;
@@ -18,33 +19,48 @@ namespace Google.ProtocolBuffers.Serialization
private readonly XmlWriter _output;
private string _rootElementName;
- static XmlWriterSettings DefaultSettings(Encoding encoding)
+ private static XmlWriterSettings DefaultSettings(Encoding encoding)
{
- return new XmlWriterSettings()
- {
- CheckCharacters = false,
- NewLineHandling = NewLineHandling.Entitize,
- OmitXmlDeclaration = true,
- Encoding = encoding,
- };
+ return new XmlWriterSettings()
+ {
+ CheckCharacters = false,
+ NewLineHandling = NewLineHandling.Entitize,
+ OmitXmlDeclaration = true,
+ Encoding = encoding,
+ };
}
/// <summary>
/// Constructs the XmlFormatWriter to write to the given TextWriter
/// </summary>
- public static XmlFormatWriter CreateInstance(TextWriter output) { return new XmlFormatWriter(XmlWriter.Create(output, DefaultSettings(output.Encoding))); }
+ public static XmlFormatWriter CreateInstance(TextWriter output)
+ {
+ return new XmlFormatWriter(XmlWriter.Create(output, DefaultSettings(output.Encoding)));
+ }
+
/// <summary>
/// Constructs the XmlFormatWriter to write to the given stream
/// </summary>
- public static XmlFormatWriter CreateInstance(Stream output) { return new XmlFormatWriter(XmlWriter.Create(output, DefaultSettings(Encoding.UTF8))); }
+ public static XmlFormatWriter CreateInstance(Stream output)
+ {
+ return new XmlFormatWriter(XmlWriter.Create(output, DefaultSettings(Encoding.UTF8)));
+ }
+
/// <summary>
/// Constructs the XmlFormatWriter to write to the given stream
/// </summary>
- public static XmlFormatWriter CreateInstance(Stream output, Encoding encoding) { return new XmlFormatWriter(XmlWriter.Create(output, DefaultSettings(encoding))); }
+ public static XmlFormatWriter CreateInstance(Stream output, Encoding encoding)
+ {
+ return new XmlFormatWriter(XmlWriter.Create(output, DefaultSettings(encoding)));
+ }
+
/// <summary>
/// Constructs the XmlFormatWriter to write to the given XmlWriter
/// </summary>
- public static XmlFormatWriter CreateInstance(XmlWriter output) { return new XmlFormatWriter(output); }
+ public static XmlFormatWriter CreateInstance(XmlWriter output)
+ {
+ return new XmlFormatWriter(output);
+ }
protected XmlFormatWriter(XmlWriter output)
{
@@ -57,8 +73,10 @@ namespace Google.ProtocolBuffers.Serialization
/// </summary>
protected override void Dispose(bool disposing)
{
- if(disposing)
+ if (disposing)
+ {
_output.Close();
+ }
}
/// <summary>
@@ -67,25 +85,39 @@ namespace Google.ProtocolBuffers.Serialization
public string RootElementName
{
get { return _rootElementName; }
- set { ThrowHelper.ThrowIfNull(value, "RootElementName"); _rootElementName = value; }
+ set
+ {
+ ThrowHelper.ThrowIfNull(value, "RootElementName");
+ _rootElementName = value;
+ }
}
/// <summary>
/// Gets or sets the options to use while generating the XML
/// </summary>
public XmlWriterOptions Options { get; set; }
+
/// <summary>
/// Sets the options to use while generating the XML
/// </summary>
- public XmlFormatWriter SetOptions(XmlWriterOptions options) { Options = options; return this; }
+ public XmlFormatWriter SetOptions(XmlWriterOptions options)
+ {
+ Options = options;
+ return this;
+ }
- private bool TestOption(XmlWriterOptions option) { return (Options & option) != 0; }
+ private bool TestOption(XmlWriterOptions option)
+ {
+ return (Options & option) != 0;
+ }
/// <summary>
/// Writes a message as an element using the name defined in <see cref="RootElementName"/>
/// </summary>
public override void WriteMessage(IMessageLite message)
- { WriteMessage(_rootElementName, message); }
+ {
+ WriteMessage(_rootElementName, message);
+ }
/// <summary>
/// Writes a message as an element with the given name
@@ -98,7 +130,9 @@ namespace Google.ProtocolBuffers.Serialization
_output.WriteAttributeString("type", "object");
}
else
+ {
_output.WriteStartElement(elementName);
+ }
message.WriteTo(this);
_output.WriteEndElement();
@@ -113,7 +147,9 @@ namespace Google.ProtocolBuffers.Serialization
_output.WriteStartElement(field);
if (TestOption(XmlWriterOptions.OutputJsonTypes))
+ {
_output.WriteAttributeString("type", "object");
+ }
message.WriteTo(this);
_output.WriteEndElement();
@@ -128,16 +164,23 @@ namespace Google.ProtocolBuffers.Serialization
if (TestOption(XmlWriterOptions.OutputJsonTypes))
{
- if (typedValue is int || typedValue is uint || typedValue is long || typedValue is ulong || typedValue is double || typedValue is float)
+ if (typedValue is int || typedValue is uint || typedValue is long || typedValue is ulong ||
+ typedValue is double || typedValue is float)
+ {
_output.WriteAttributeString("type", "number");
+ }
else if (typedValue is bool)
+ {
_output.WriteAttributeString("type", "boolean");
+ }
}
_output.WriteString(textValue);
//Empty strings should not be written as empty elements '<item/>', rather as '<item></item>'
if (_output.WriteState == WriteState.Element)
+ {
_output.WriteRaw("");
+ }
_output.WriteEndElement();
}
@@ -145,25 +188,40 @@ namespace Google.ProtocolBuffers.Serialization
/// <summary>
/// Writes an array of field values
/// </summary>
- protected override void WriteArray(FieldType fieldType, string field, System.Collections.IEnumerable items)
+ protected override void WriteArray(FieldType fieldType, string field, IEnumerable items)
{
//see if it's empty
- System.Collections.IEnumerator eitems = items.GetEnumerator();
- try { if (!eitems.MoveNext()) return; }
+ IEnumerator eitems = items.GetEnumerator();
+ try
+ {
+ if (!eitems.MoveNext())
+ {
+ return;
+ }
+ }
finally
- { if (eitems is IDisposable) ((IDisposable) eitems).Dispose(); }
+ {
+ if (eitems is IDisposable)
+ {
+ ((IDisposable) eitems).Dispose();
+ }
+ }
if (TestOption(XmlWriterOptions.OutputNestedArrays | XmlWriterOptions.OutputJsonTypes))
{
_output.WriteStartElement(field);
if (TestOption(XmlWriterOptions.OutputJsonTypes))
+ {
_output.WriteAttributeString("type", "array");
+ }
base.WriteArray(fieldType, "item", items);
_output.WriteEndElement();
}
else
+ {
base.WriteArray(fieldType, field, items);
+ }
}
/// <summary>
@@ -174,10 +232,12 @@ namespace Google.ProtocolBuffers.Serialization
_output.WriteStartElement(field);
if (!TestOption(XmlWriterOptions.OutputJsonTypes) && TestOption(XmlWriterOptions.OutputEnumValues))
+ {
_output.WriteAttributeString("value", XmlConvert.ToString(number));
+ }
_output.WriteString(name);
_output.WriteEndElement();
}
}
-}
+} \ No newline at end of file
diff --git a/src/ProtocolBuffers/Serialization/XmlReaderOptions.cs b/src/ProtocolBuffers/Serialization/XmlReaderOptions.cs
index fc75e4b5..f7eca1d7 100644
--- a/src/ProtocolBuffers/Serialization/XmlReaderOptions.cs
+++ b/src/ProtocolBuffers/Serialization/XmlReaderOptions.cs
@@ -10,6 +10,7 @@ namespace Google.ProtocolBuffers.Serialization
{
/// <summary> Simple xml formatting with no attributes </summary>
None,
+
/// <summary> Requires that arrays items are nested in an &lt;item> element </summary>
ReadNestedArrays = 1,
}
diff --git a/src/ProtocolBuffers/Serialization/XmlWriterOptions.cs b/src/ProtocolBuffers/Serialization/XmlWriterOptions.cs
index 2d91c742..7d740ee3 100644
--- a/src/ProtocolBuffers/Serialization/XmlWriterOptions.cs
+++ b/src/ProtocolBuffers/Serialization/XmlWriterOptions.cs
@@ -10,10 +10,13 @@ namespace Google.ProtocolBuffers.Serialization
{
/// <summary> Simple xml formatting with no attributes </summary>
None,
+
/// <summary> Writes the 'value' attribute on all enumerations with the numeric identifier </summary>
OutputEnumValues = 0x1,
+
/// <summary> Embeds array items into child &lt;item> elements </summary>
OutputNestedArrays = 0x4,
+
/// <summary> Outputs the 'type' attribute for compatibility with the <see cref="System.Runtime.Serialization.Json.JsonReaderWriterFactory">JsonReaderWriterFactory</see> </summary>
/// <remarks> This option must, by nessessity, also enable NestedArrayItems </remarks>
OutputJsonTypes = 0x8,