Commit afe844bc authored by csharptest's avatar csharptest Committed by rogerk

Added the JsonFormatWriter/Reader

parent 2b868846
......@@ -39,6 +39,7 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using Google.ProtocolBuffers.Serialization;
using Google.ProtocolBuffers.TestProtos;
namespace Google.ProtocolBuffers.ProtoBench
......@@ -127,12 +128,25 @@ namespace Google.ProtocolBuffers.ProtoBench
inputData = inputData ?? File.ReadAllBytes(file);
MemoryStream inputStream = new MemoryStream(inputData);
ByteString inputString = ByteString.CopyFrom(inputData);
IMessage sampleMessage =
defaultMessage.WeakCreateBuilderForType().WeakMergeFrom(inputString, registry).WeakBuild();
IMessage sampleMessage = defaultMessage.WeakCreateBuilderForType().WeakMergeFrom(inputString, registry).WeakBuild();
StringWriter temp = new StringWriter();
new XmlFormatWriter(temp).WriteMessage(sampleMessage);
string xmlMessageText = temp.ToString();
temp = new StringWriter();
new JsonFormatWriter(temp).WriteMessage(sampleMessage);
string jsonMessageText = temp.ToString();
//Serializers
if(!FastTest) RunBenchmark("Serialize to byte string", inputData.Length, () => sampleMessage.ToByteString());
RunBenchmark("Serialize to byte array", inputData.Length, () => sampleMessage.ToByteArray());
if (!FastTest) RunBenchmark("Serialize to memory stream", inputData.Length,
() => sampleMessage.WriteTo(new MemoryStream()));
RunBenchmark("Serialize to xml", xmlMessageText.Length, () => new XmlFormatWriter(new StringWriter()).WriteMessage(sampleMessage));
RunBenchmark("Serialize to json", jsonMessageText.Length, () => new JsonFormatWriter(new StringWriter()).WriteMessage(sampleMessage));
//Deserializers
if (!FastTest) RunBenchmark("Deserialize from byte string", inputData.Length,
() => defaultMessage.WeakCreateBuilderForType()
.WeakMergeFrom(inputString, registry)
......@@ -151,6 +165,10 @@ namespace Google.ProtocolBuffers.ProtoBench
CodedInputStream.CreateInstance(inputStream), registry)
.WeakBuild();
});
RunBenchmark("Deserialize from xml", xmlMessageText.Length, () => new XmlFormatReader(xmlMessageText).Merge(defaultMessage.WeakCreateBuilderForType()).WeakBuild());
RunBenchmark("Deserialize from json", jsonMessageText.Length, () => new JsonFormatReader(jsonMessageText).Merge(defaultMessage.WeakCreateBuilderForType()).WeakBuild());
Console.WriteLine();
return true;
}
......
using System.IO;
using System.Text;
using Google.ProtocolBuffers.Serialization;
using NUnit.Framework;
namespace Google.ProtocolBuffers.CompatTests
{
[TestFixture]
public class JsonCompatibilityTests : CompatibilityTests
{
protected override object SerializeMessage<TMessage, TBuilder>(TMessage message)
{
StringWriter sw = new StringWriter();
new JsonFormatWriter(sw)
.Formatted()
.WriteMessage(message);
return sw.ToString();
}
protected override TBuilder DeerializeMessage<TMessage, TBuilder>(object message, TBuilder builder, ExtensionRegistry registry)
{
new JsonFormatReader((string)message).Merge(builder);
return builder;
}
}
}
\ No newline at end of file
......@@ -77,6 +77,7 @@
<Compile Include="Collections\PopsicleListTest.cs" />
<Compile Include="CompatTests\BinaryCompatibilityTests.cs" />
<Compile Include="CompatTests\CompatibilityTests.cs" />
<Compile Include="CompatTests\JsonCompatibilityTests.cs" />
<Compile Include="CompatTests\TestResources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
......@@ -115,6 +116,7 @@
<Compile Include="TestProtos\UnitTestXmlSerializerTestProtoFile.cs" />
<Compile Include="TestRpcGenerator.cs" />
<Compile Include="TestUtil.cs" />
<Compile Include="TestWriterFormatJson.cs" />
<Compile Include="TestWriterFormatXml.cs" />
<Compile Include="TextFormatTest.cs" />
<Compile Include="UnknownFieldSetTest.cs" />
......
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectView>ProjectFiles</ProjectView>
</PropertyGroup>
</Project>
\ No newline at end of file
This diff is collapsed.
......@@ -184,6 +184,9 @@
<Compile Include="Serialization\AbstractTextReader.cs" />
<Compile Include="Serialization\AbstractTextWriter.cs" />
<Compile Include="Serialization\AbstractWriter.cs" />
<Compile Include="Serialization\JsonFormatReader.cs" />
<Compile Include="Serialization\JsonFormatWriter.cs" />
<Compile Include="Serialization\JsonTextCursor.cs" />
<Compile Include="Serialization\XmlFormatReader.cs" />
<Compile Include="Serialization\XmlFormatWriter.cs" />
<Compile Include="Serialization\XmlReaderOptions.cs" />
......
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
namespace Google.ProtocolBuffers.Serialization
{
/// <summary>
/// JsonFormatReader is used to parse Json into a message or an array of messages
/// </summary>
public class JsonFormatReader : AbstractTextReader
{
private readonly JsonTextCursor _input;
private readonly Stack<int> _stopChar;
enum ReaderState { Start, BeginValue, EndValue, BeginObject, BeginArray }
string _current;
ReaderState _state;
/// <summary>
/// Constructs a JsonFormatReader to parse Json into a message
/// </summary>
public JsonFormatReader(string jsonText)
{
_input = new JsonTextCursor(jsonText.ToCharArray());
_stopChar = new Stack<int>();
_stopChar.Push(-1);
_state = ReaderState.Start;
}
/// <summary>
/// Constructs a JsonFormatReader to parse Json into a message
/// </summary>
public JsonFormatReader(TextReader input)
{
_input = new JsonTextCursor(input);
_stopChar = new Stack<int>();
_stopChar.Push(-1);
_state = ReaderState.Start;
}
/// <summary>
/// Returns true if the reader is currently on an array element
/// </summary>
public bool IsArrayMessage { get { return _input.NextChar == '['; } }
/// <summary>
/// Returns an enumerator that is used to cursor over an array of messages
/// </summary>
/// <remarks>
/// This is generally used when receiving an array of messages rather than a single root message
/// </remarks>
public IEnumerable<JsonFormatReader> EnumerateArray()
{
foreach (string ignored in ForeachArrayItem(_current))
yield return this;
}
/// <summary>
/// Merges the contents of stream into the provided message builder
/// </summary>
public override TBuilder Merge<TBuilder>(TBuilder builder, ExtensionRegistry registry)
{
_input.Consume('{');
_stopChar.Push('}');
_state = ReaderState.BeginObject;
builder.WeakMergeFrom(this, registry);
_input.Consume((char)_stopChar.Pop());
_state = ReaderState.EndValue;
return builder;
}
/// <summary>
/// Causes the reader to skip past this field
/// </summary>
protected override void Skip()
{
object temp;
_input.ReadVariant(out temp);
_state = ReaderState.EndValue;
}
/// <summary>
/// Peeks at the next field in the input stream and returns what information is available.
/// </summary>
/// <remarks>
/// This may be called multiple times without actually reading the field. Only after the field
/// is either read, or skipped, should PeekNext return a different value.
/// </remarks>
protected override bool PeekNext(out string field)
{
field = _current;
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(':');
_state = ReaderState.BeginValue;
return true;
}
/// <summary>
/// Returns true if it was able to read a String from the input
/// </summary>
protected override bool ReadAsText(ref string value, Type typeInfo)
{
object temp;
JsonTextCursor.JsType type = _input.ReadVariant(out temp);
_state = ReaderState.EndValue;
_input.Assert(type != JsonTextCursor.JsType.Array && type != JsonTextCursor.JsType.Object, "Encountered {0} while expecting {1}", type, typeInfo);
if (type == JsonTextCursor.JsType.Null)
return false;
if (type == JsonTextCursor.JsType.True) value = "1";
else if (type == JsonTextCursor.JsType.False) value = "0";
else value = temp as string;
//exponent representation of integer number:
if (value != null && type == JsonTextCursor.JsType.Number &&
(typeInfo != typeof(double) && typeInfo != typeof(float)) &&
value.IndexOf("e", StringComparison.OrdinalIgnoreCase) > 0)
{
value = XmlConvert.ToString((long)Math.Round(XmlConvert.ToDouble(value), 0));
}
return value != null;
}
/// <summary>
/// Returns true if it was able to read a ByteString from the input
/// </summary>
protected override bool Read(ref ByteString value)
{
string bytes = null;
if (Read(ref bytes))
{
value = ByteString.FromBase64(bytes);
return true;
}
return false;
}
/// <summary>
/// Cursors through the array elements and stops at the end of the array
/// </summary>
protected override IEnumerable<string> ForeachArrayItem(string field)
{
_input.Consume('[');
_stopChar.Push(']');
_state = ReaderState.BeginArray;
while (_input.NextChar != ']')
{
_current = field;
yield return field;
if(!_input.TryConsume(','))
break;
}
_input.Consume((char)_stopChar.Pop());
_state = ReaderState.EndValue;
}
/// <summary>
/// Merges the input stream into the provided IBuilderLite
/// </summary>
protected override bool ReadMessage(IBuilderLite builder, ExtensionRegistry registry)
{
Merge(builder, registry);
return true;
}
}
}
This diff is collapsed.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
namespace Google.ProtocolBuffers.Serialization
{
/// <summary>
/// JSon Tokenizer used by JsonFormatReader
/// </summary>
class JsonTextCursor
{
public enum JsType { String, Number, Object, Array, True, False, Null }
private readonly char[] _buffer;
private int _bufferPos;
private readonly TextReader _input;
private int _lineNo, _linePos;
public JsonTextCursor(char[] input)
{
_input = null;
_buffer = input;
_bufferPos = 0;
_next = Peek();
_lineNo = 1;
}
public JsonTextCursor(TextReader input)
{
_input = input;
_next = Peek();
_lineNo = 1;
}
private int Peek()
{
if (_input != null)
return _input.Peek();
else if (_bufferPos < _buffer.Length)
return _buffer[_bufferPos];
else
return -1;
}
private int Read()
{
if (_input != null)
return _input.Read();
else if (_bufferPos < _buffer.Length)
return _buffer[_bufferPos++];
else
return -1;
}
int _next;
public Char NextChar { get { SkipWhitespace(); return (char)_next; } }
#region Assert(...)
[System.Diagnostics.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);
}
[System.Diagnostics.DebuggerNonUserCode]
private void Assert(bool cond, char expected)
{
if (!cond)
{
throw new FormatException(
String.Format(CultureInfo.InvariantCulture,
"({0}:{1}) error: Unexpected token {2}, expected: {3}.",
_lineNo, _linePos,
CharDisplay(_next),
CharDisplay(expected)
));
}
}
[System.Diagnostics.DebuggerNonUserCode]
public void Assert(bool cond, string message)
{
if (!cond)
{
throw new FormatException(
String.Format(CultureInfo.InvariantCulture,
"({0},{1}) error: {2}", _lineNo, _linePos, message));
}
}
[System.Diagnostics.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()
{
int ch = Read();
Assert(ch != -1, "Unexpected end of file.");
if (ch == '\n')
{
_lineNo++;
_linePos = 0;
}
else if (ch != '\r')
{
_linePos++;
}
_next = Peek();
return (char)ch;
}
public void Consume(char ch) { Assert(TryConsume(ch), ch); }
public bool TryConsume(char ch)
{
SkipWhitespace();
if (_next == ch)
{
ReadChar();
return true;
}
return false;
}
public void Consume(string sequence)
{
SkipWhitespace();
foreach (char ch in sequence)
Assert(ch == ReadChar(), "Expected token '{0}'.", sequence);
}
public void SkipWhitespace()
{
int chnext = _next;
while (chnext != -1)
{
if (!Char.IsWhiteSpace((char)chnext))
break;
ReadChar();
chnext = _next;
}
}
public string ReadString()
{
SkipWhitespace();
Consume('"');
StringBuilder sb = new StringBuilder();
while (_next != '"')
{
if (_next == '\\')
{
Consume('\\');//skip the escape
char ch = ReadChar();
switch (ch)
{
case 'b': sb.Append('\b'); break;
case 'f': sb.Append('\f'); break;
case 'n': sb.Append('\n'); break;
case 'r': sb.Append('\r'); break;
case 't': sb.Append('\t'); break;
case 'u':
{
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.Append((char)result);
break;
}
default:
sb.Append(ch); break;
}
}
else
{
Assert(_next != '\n' && _next != '\r' && _next != '\f' && _next != -1, '"');
sb.Append(ReadChar());
}
}
Consume('"');
return sb.ToString();
}
public string ReadNumber()
{
SkipWhitespace();
StringBuilder sb = new StringBuilder();
if (_next == '-')
sb.Append(ReadChar());
Assert(_next >= '0' && _next <= '9', "Expected a numeric type.");
while ((_next >= '0' && _next <= '9') || _next == '.')
sb.Append(ReadChar());
if (_next == 'e' || _next == 'E')
{
sb.Append(ReadChar());
if (_next == '-' || _next == '+')
sb.Append(ReadChar());
Assert(_next >= '0' && _next <= '9', "Expected a numeric type.");
while (_next >= '0' && _next <= '9')
sb.Append(ReadChar());
}
return sb.ToString();
}
public JsType ReadVariant(out object value)
{
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 '{':
{
Consume('{');
while (NextChar != '}')
{
ReadString();
Consume(':');
object tmp;
ReadVariant(out tmp);
if (!TryConsume(','))
break;
}
Consume('}');
value = null;
return JsType.Object;
}
case '[':
{
Consume('[');
List<object> values = new List<object>();
while (NextChar != ']')
{
object tmp;
ReadVariant(out tmp);
values.Add(tmp);
if (!TryConsume(','))
break;
}
Consume(']');
value = values.ToArray();
return JsType.Array;
}
default:
if ((_next >= '0' && _next <= '9') || _next == '-')
{
value = ReadNumber();
return JsType.Number;
}
Assert(false, "Expected a value.");
throw new FormatException();
}
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment