Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
f86edcbb
Commit
f86edcbb
authored
Aug 13, 2013
by
Jon Skeet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
When we write a single character to a JSON output, make sure we actually have
an output before flushing. Fixes issue 64.
parent
8c8a82a2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
0 deletions
+41
-0
JsonFormatWriter.cs
src/ProtocolBuffers.Serialization/JsonFormatWriter.cs
+4
-0
TestWriterFormatJson.cs
src/ProtocolBuffers.Test/TestWriterFormatJson.cs
+37
-0
No files found.
src/ProtocolBuffers.Serialization/JsonFormatWriter.cs
View file @
f86edcbb
...
@@ -82,6 +82,10 @@ namespace Google.ProtocolBuffers.Serialization
...
@@ -82,6 +82,10 @@ namespace Google.ProtocolBuffers.Serialization
{
{
if
(
_bufferPos
>=
_buffer
.
Length
)
if
(
_bufferPos
>=
_buffer
.
Length
)
{
{
if
(
_output
==
null
)
{
_output
=
new
StringWriter
(
new
StringBuilder
(
_buffer
.
Length
*
2
));
}
Flush
();
Flush
();
}
}
_buffer
[
_bufferPos
++]
=
ch
;
_buffer
[
_bufferPos
++]
=
ch
;
...
...
src/ProtocolBuffers.Test/TestWriterFormatJson.cs
View file @
f86edcbb
using
System
;
using
System
;
using
System.IO
;
using
System.IO
;
using
System.Text
;
using
System.Text
;
using
Google.ProtocolBuffers.DescriptorProtos
;
using
Google.ProtocolBuffers.Serialization
;
using
Google.ProtocolBuffers.Serialization
;
using
Microsoft.VisualStudio.TestTools.UnitTesting
;
using
Microsoft.VisualStudio.TestTools.UnitTesting
;
using
Google.ProtocolBuffers.TestProtos
;
using
Google.ProtocolBuffers.TestProtos
;
using
EnumOptions
=
Google
.
ProtocolBuffers
.
TestProtos
.
EnumOptions
;
namespace
Google.ProtocolBuffers
namespace
Google.ProtocolBuffers
{
{
...
@@ -152,6 +154,7 @@ namespace Google.ProtocolBuffers
...
@@ -152,6 +154,7 @@ namespace Google.ProtocolBuffers
@"{}"
@"{}"
);
);
}
}
[
TestMethod
]
[
TestMethod
]
public
void
TestRepeatedField
()
public
void
TestRepeatedField
()
{
{
...
@@ -163,6 +166,7 @@ namespace Google.ProtocolBuffers
...
@@ -163,6 +166,7 @@ namespace Google.ProtocolBuffers
@"{""options"":[""ONE"",""TWO""]}"
@"{""options"":[""ONE"",""TWO""]}"
);
);
}
}
[
TestMethod
]
[
TestMethod
]
public
void
TestNestedEmptyMessage
()
public
void
TestNestedEmptyMessage
()
{
{
...
@@ -173,6 +177,7 @@ namespace Google.ProtocolBuffers
...
@@ -173,6 +177,7 @@ namespace Google.ProtocolBuffers
@"{""child"":{}}"
@"{""child"":{}}"
);
);
}
}
[
TestMethod
]
[
TestMethod
]
public
void
TestNestedMessage
()
public
void
TestNestedMessage
()
{
{
...
@@ -183,6 +188,7 @@ namespace Google.ProtocolBuffers
...
@@ -183,6 +188,7 @@ namespace Google.ProtocolBuffers
@"{""child"":{""options"":[""TWO""]}}"
@"{""child"":{""options"":[""TWO""]}}"
);
);
}
}
[
TestMethod
]
[
TestMethod
]
public
void
TestBooleanTypes
()
public
void
TestBooleanTypes
()
{
{
...
@@ -193,6 +199,7 @@ namespace Google.ProtocolBuffers
...
@@ -193,6 +199,7 @@ namespace Google.ProtocolBuffers
@"{""valid"":true}"
@"{""valid"":true}"
);
);
}
}
[
TestMethod
]
[
TestMethod
]
public
void
TestFullMessage
()
public
void
TestFullMessage
()
{
{
...
@@ -222,6 +229,7 @@ namespace Google.ProtocolBuffers
...
@@ -222,6 +229,7 @@ namespace Google.ProtocolBuffers
0x1010101010L
.
ToString
()
0x1010101010L
.
ToString
()
);
);
}
}
[
TestMethod
]
[
TestMethod
]
public
void
TestMessageWithXmlText
()
public
void
TestMessageWithXmlText
()
{
{
...
@@ -232,6 +240,7 @@ namespace Google.ProtocolBuffers
...
@@ -232,6 +240,7 @@ namespace Google.ProtocolBuffers
@"{""text"":""<text><\/text>""}"
@"{""text"":""<text><\/text>""}"
);
);
}
}
[
TestMethod
]
[
TestMethod
]
public
void
TestWithEscapeChars
()
public
void
TestWithEscapeChars
()
{
{
...
@@ -242,6 +251,7 @@ namespace Google.ProtocolBuffers
...
@@ -242,6 +251,7 @@ namespace Google.ProtocolBuffers
"{\"text\":\" \\t <- \\\"leading space and trailing\\\" -> \\\\ \\uef54 \\u0000 \\u00ff \\uffff \\b \\f \\r \\n \\t \"}"
"{\"text\":\" \\t <- \\\"leading space and trailing\\\" -> \\\\ \\uef54 \\u0000 \\u00ff \\uffff \\b \\f \\r \\n \\t \"}"
);
);
}
}
[
TestMethod
]
[
TestMethod
]
public
void
TestWithExtensionText
()
public
void
TestWithExtensionText
()
{
{
...
@@ -253,6 +263,7 @@ namespace Google.ProtocolBuffers
...
@@ -253,6 +263,7 @@ namespace Google.ProtocolBuffers
@"{""valid"":false,""extension_text"":"" extension text value ! ""}"
@"{""valid"":false,""extension_text"":"" extension text value ! ""}"
);
);
}
}
[
TestMethod
]
[
TestMethod
]
public
void
TestWithExtensionNumber
()
public
void
TestWithExtensionNumber
()
{
{
...
@@ -264,6 +275,7 @@ namespace Google.ProtocolBuffers
...
@@ -264,6 +275,7 @@ namespace Google.ProtocolBuffers
@"{""number"":42}"
@"{""number"":42}"
);
);
}
}
[
TestMethod
]
[
TestMethod
]
public
void
TestWithExtensionArray
()
public
void
TestWithExtensionArray
()
{
{
...
@@ -276,6 +288,7 @@ namespace Google.ProtocolBuffers
...
@@ -276,6 +288,7 @@ namespace Google.ProtocolBuffers
@"{""extension_number"":[100,101,102]}"
@"{""extension_number"":[100,101,102]}"
);
);
}
}
[
TestMethod
]
[
TestMethod
]
public
void
TestWithExtensionEnum
()
public
void
TestWithExtensionEnum
()
{
{
...
@@ -286,6 +299,7 @@ namespace Google.ProtocolBuffers
...
@@ -286,6 +299,7 @@ namespace Google.ProtocolBuffers
@"{""extension_enum"":""ONE""}"
@"{""extension_enum"":""ONE""}"
);
);
}
}
[
TestMethod
]
[
TestMethod
]
public
void
TestMessageWithExtensions
()
public
void
TestMessageWithExtensions
()
{
{
...
@@ -308,6 +322,7 @@ namespace Google.ProtocolBuffers
...
@@ -308,6 +322,7 @@ namespace Google.ProtocolBuffers
@"""extension_message"":{""number"":42}"
@"""extension_message"":{""number"":42}"
);
);
}
}
[
TestMethod
]
[
TestMethod
]
public
void
TestMessageMissingExtensions
()
public
void
TestMessageMissingExtensions
()
{
{
...
@@ -340,6 +355,7 @@ namespace Google.ProtocolBuffers
...
@@ -340,6 +355,7 @@ namespace Google.ProtocolBuffers
Assert
.
AreNotEqual
(
original
,
copy
);
Assert
.
AreNotEqual
(
original
,
copy
);
Assert
.
AreEqual
(
message
,
copy
);
Assert
.
AreEqual
(
message
,
copy
);
}
}
[
TestMethod
]
[
TestMethod
]
public
void
TestMergeFields
()
public
void
TestMergeFields
()
{
{
...
@@ -350,6 +366,7 @@ namespace Google.ProtocolBuffers
...
@@ -350,6 +366,7 @@ namespace Google.ProtocolBuffers
Assert
.
AreEqual
(
"text"
,
builder
.
Text
);
Assert
.
AreEqual
(
"text"
,
builder
.
Text
);
Assert
.
AreEqual
(
411
,
builder
.
Number
);
Assert
.
AreEqual
(
411
,
builder
.
Number
);
}
}
[
TestMethod
]
[
TestMethod
]
public
void
TestMessageArray
()
public
void
TestMessageArray
()
{
{
...
@@ -374,6 +391,7 @@ namespace Google.ProtocolBuffers
...
@@ -374,6 +391,7 @@ namespace Google.ProtocolBuffers
Assert
.
AreEqual
(
3
,
ordinal
);
Assert
.
AreEqual
(
3
,
ordinal
);
Assert
.
AreEqual
(
3
,
builder
.
TextlinesCount
);
Assert
.
AreEqual
(
3
,
builder
.
TextlinesCount
);
}
}
[
TestMethod
]
[
TestMethod
]
public
void
TestNestedMessageArray
()
public
void
TestNestedMessageArray
()
{
{
...
@@ -403,6 +421,7 @@ namespace Google.ProtocolBuffers
...
@@ -403,6 +421,7 @@ namespace Google.ProtocolBuffers
Assert
.
AreEqual
(
3
,
ordinal
);
Assert
.
AreEqual
(
3
,
ordinal
);
Assert
.
AreEqual
(
3
,
builder
.
TextlinesCount
);
Assert
.
AreEqual
(
3
,
builder
.
TextlinesCount
);
}
}
[
TestMethod
]
[
TestMethod
]
public
void
TestReadWriteJsonWithoutRoot
()
public
void
TestReadWriteJsonWithoutRoot
()
{
{
...
@@ -425,6 +444,7 @@ namespace Google.ProtocolBuffers
...
@@ -425,6 +444,7 @@ namespace Google.ProtocolBuffers
Assert
.
AreEqual
(
message
,
copy
);
Assert
.
AreEqual
(
message
,
copy
);
}
}
[
TestMethod
,
ExpectedException
(
typeof
(
RecursionLimitExceededException
))]
[
TestMethod
,
ExpectedException
(
typeof
(
RecursionLimitExceededException
))]
public
void
TestRecursiveLimit
()
public
void
TestRecursiveLimit
()
{
{
...
@@ -433,29 +453,46 @@ namespace Google.ProtocolBuffers
...
@@ -433,29 +453,46 @@ namespace Google.ProtocolBuffers
sb
.
Append
(
"{\"child\":"
);
sb
.
Append
(
"{\"child\":"
);
TestXmlRescursive
msg
=
Extensions
.
MergeFromJson
(
new
TestXmlRescursive
.
Builder
(),
sb
.
ToString
()).
Build
();
TestXmlRescursive
msg
=
Extensions
.
MergeFromJson
(
new
TestXmlRescursive
.
Builder
(),
sb
.
ToString
()).
Build
();
}
}
[
TestMethod
,
ExpectedException
(
typeof
(
FormatException
))]
[
TestMethod
,
ExpectedException
(
typeof
(
FormatException
))]
public
void
FailWithEmptyText
()
public
void
FailWithEmptyText
()
{
{
JsonFormatReader
.
CreateInstance
(
""
)
JsonFormatReader
.
CreateInstance
(
""
)
.
Merge
(
TestXmlMessage
.
CreateBuilder
());
.
Merge
(
TestXmlMessage
.
CreateBuilder
());
}
}
[
TestMethod
,
ExpectedException
(
typeof
(
FormatException
))]
[
TestMethod
,
ExpectedException
(
typeof
(
FormatException
))]
public
void
FailWithUnexpectedValue
()
public
void
FailWithUnexpectedValue
()
{
{
JsonFormatReader
.
CreateInstance
(
"{{}}"
)
JsonFormatReader
.
CreateInstance
(
"{{}}"
)
.
Merge
(
TestXmlMessage
.
CreateBuilder
());
.
Merge
(
TestXmlMessage
.
CreateBuilder
());
}
}
[
TestMethod
,
ExpectedException
(
typeof
(
FormatException
))]
[
TestMethod
,
ExpectedException
(
typeof
(
FormatException
))]
public
void
FailWithUnQuotedName
()
public
void
FailWithUnQuotedName
()
{
{
JsonFormatReader
.
CreateInstance
(
"{name:{}}"
)
JsonFormatReader
.
CreateInstance
(
"{name:{}}"
)
.
Merge
(
TestXmlMessage
.
CreateBuilder
());
.
Merge
(
TestXmlMessage
.
CreateBuilder
());
}
}
[
TestMethod
,
ExpectedException
(
typeof
(
FormatException
))]
[
TestMethod
,
ExpectedException
(
typeof
(
FormatException
))]
public
void
FailWithUnexpectedType
()
public
void
FailWithUnexpectedType
()
{
{
JsonFormatReader
.
CreateInstance
(
"{\"valid\":{}}"
)
JsonFormatReader
.
CreateInstance
(
"{\"valid\":{}}"
)
.
Merge
(
TestXmlMessage
.
CreateBuilder
());
.
Merge
(
TestXmlMessage
.
CreateBuilder
());
}
}
// See issue 64 for background.
[
TestMethod
]
public
void
ToJsonRequiringBufferExpansion
()
{
string
s
=
new
string
(
'.'
,
4086
);
var
opts
=
FileDescriptorProto
.
CreateBuilder
()
.
SetName
(
s
)
.
SetPackage
(
"package"
)
.
BuildPartial
();
Assert
.
NotNull
(
opts
.
ToJson
());
}
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment