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
f292523d
Commit
f292523d
authored
Jun 11, 2011
by
csharptest
Committed by
rogerk
Jun 11, 2011
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added convenience methods for to/from xml and json
parent
7fc785c1
Show whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
130 additions
and
9 deletions
+130
-9
Program.cs
src/ProtoBench/Program.cs
+1
-1
CompatibilityTests.cs
src/ProtocolBuffers.Test/CompatTests/CompatibilityTests.cs
+3
-3
XmlCompatibilityTests.cs
...ProtocolBuffers.Test/CompatTests/XmlCompatibilityTests.cs
+1
-0
TestWriterFormatJson.cs
src/ProtocolBuffers.Test/TestWriterFormatJson.cs
+22
-0
TestWriterFormatXml.cs
src/ProtocolBuffers.Test/TestWriterFormatXml.cs
+22
-0
AbstractMessage.cs
src/ProtocolBuffers/AbstractMessage.cs
+21
-0
ExtendableBuilder.cs
src/ProtocolBuffers/ExtendableBuilder.cs
+1
-1
ExtendableMessage.cs
src/ProtocolBuffers/ExtendableMessage.cs
+1
-1
GeneratedBuilder.cs
src/ProtocolBuffers/GeneratedBuilder.cs
+1
-1
GeneratedMessage.cs
src/ProtocolBuffers/GeneratedMessage.cs
+32
-1
IMessage.cs
src/ProtocolBuffers/IMessage.cs
+18
-0
XmlFormatWriter.cs
src/ProtocolBuffers/Serialization/XmlFormatWriter.cs
+7
-1
No files found.
src/ProtoBench/Program.cs
View file @
f292523d
...
...
@@ -158,7 +158,7 @@ namespace Google.ProtocolBuffers.ProtoBench
}
);
RunBenchmark
(
"Serialize to json"
,
jsonBytes
.
Length
,
()
=>
{
JsonFormatWriter
.
CreateInstance
(
new
MemoryStream
()
).
WriteMessage
(
sampleMessage
);
JsonFormatWriter
.
CreateInstance
().
WriteMessage
(
sampleMessage
);
});
RunBenchmark
(
"Serialize to json via xml"
,
jsonBytes
.
Length
,
()
=>
...
...
src/ProtocolBuffers.Test/CompatTests/CompatibilityTests.cs
View file @
f292523d
...
...
@@ -73,7 +73,7 @@ namespace Google.ProtocolBuffers.CompatTests
#
region
Test
message
builders
pr
ivate
static
TestAllTypes
.
Builder
AddAllTypes
(
TestAllTypes
.
Builder
builder
)
pr
otected
static
TestAllTypes
.
Builder
AddAllTypes
(
TestAllTypes
.
Builder
builder
)
{
return
builder
.
SetOptionalInt32
(
1001
)
.
SetOptionalInt64
(
1001
)
...
...
@@ -96,7 +96,7 @@ namespace Google.ProtocolBuffers.CompatTests
;
}
pr
ivate
static
TestAllTypes
.
Builder
AddRepeatedTypes
(
TestAllTypes
.
Builder
builder
,
int
size
)
pr
otected
static
TestAllTypes
.
Builder
AddRepeatedTypes
(
TestAllTypes
.
Builder
builder
,
int
size
)
{
//repeated values
for
(
int
i
=
0
;
i
<
size
;
i
++)
...
...
@@ -122,7 +122,7 @@ namespace Google.ProtocolBuffers.CompatTests
return
builder
;
}
pr
ivate
static
TestPackedTypes
.
Builder
AddPackedTypes
(
TestPackedTypes
.
Builder
builder
,
int
size
)
pr
otected
static
TestPackedTypes
.
Builder
AddPackedTypes
(
TestPackedTypes
.
Builder
builder
,
int
size
)
{
for
(
int
i
=
0
;
i
<
size
;
i
++
)
builder
.
AddPackedInt32
(
1001
)
...
...
src/ProtocolBuffers.Test/CompatTests/XmlCompatibilityTests.cs
View file @
f292523d
using
System.IO
;
using
Google.ProtocolBuffers.Serialization
;
using
Google.ProtocolBuffers.TestProtos
;
using
NUnit.Framework
;
namespace
Google.ProtocolBuffers.CompatTests
...
...
src/ProtocolBuffers.Test/TestWriterFormatJson.cs
View file @
f292523d
...
...
@@ -31,6 +31,28 @@ namespace Google.ProtocolBuffers
Assert
.
IsTrue
(
Content
.
IndexOf
(
expect
)
>=
0
,
"Expected to find content '{0}' in: \r\n{1}"
,
expect
,
Content
);
}
[
Test
]
public
void
TestToJsonParseFromJson
()
{
TestAllTypes
msg
=
new
TestAllTypes
.
Builder
().
SetDefaultBool
(
true
).
Build
();
string
json
=
msg
.
ToJson
();
Assert
.
AreEqual
(
"{\"default_bool\":true}"
,
json
);
TestAllTypes
copy
=
TestAllTypes
.
ParseFromJson
(
json
);
Assert
.
IsTrue
(
copy
.
HasDefaultBool
&&
copy
.
DefaultBool
);
Assert
.
AreEqual
(
msg
,
copy
);
}
[
Test
]
public
void
TestToJsonParseFromJsonReader
()
{
TestAllTypes
msg
=
new
TestAllTypes
.
Builder
().
SetDefaultBool
(
true
).
Build
();
string
json
=
msg
.
ToJson
();
Assert
.
AreEqual
(
"{\"default_bool\":true}"
,
json
);
TestAllTypes
copy
=
TestAllTypes
.
ParseFromJson
(
new
StringReader
(
json
));
Assert
.
IsTrue
(
copy
.
HasDefaultBool
&&
copy
.
DefaultBool
);
Assert
.
AreEqual
(
msg
,
copy
);
}
[
Test
]
public
void
TestJsonFormatted
()
{
...
...
src/ProtocolBuffers.Test/TestWriterFormatXml.cs
View file @
f292523d
...
...
@@ -12,6 +12,28 @@ namespace Google.ProtocolBuffers
[
TestFixture
]
public
class
TestWriterFormatXml
{
[
Test
]
public
void
TestToXmlParseFromXml
()
{
TestAllTypes
msg
=
new
TestAllTypes
.
Builder
().
SetDefaultBool
(
true
).
Build
();
string
xml
=
msg
.
ToXml
();
Assert
.
AreEqual
(
"<root><default_bool>true</default_bool></root>"
,
xml
);
TestAllTypes
copy
=
TestAllTypes
.
ParseFromXml
(
XmlReader
.
Create
(
new
StringReader
(
xml
)));
Assert
.
IsTrue
(
copy
.
HasDefaultBool
&&
copy
.
DefaultBool
);
Assert
.
AreEqual
(
msg
,
copy
);
}
[
Test
]
public
void
TestToXmlParseFromXmlWithRootName
()
{
TestAllTypes
msg
=
new
TestAllTypes
.
Builder
().
SetDefaultBool
(
true
).
Build
();
string
xml
=
msg
.
ToXml
(
"message"
);
Assert
.
AreEqual
(
"<message><default_bool>true</default_bool></message>"
,
xml
);
TestAllTypes
copy
=
TestAllTypes
.
ParseFromXml
(
"message"
,
XmlReader
.
Create
(
new
StringReader
(
xml
)));
Assert
.
IsTrue
(
copy
.
HasDefaultBool
&&
copy
.
DefaultBool
);
Assert
.
AreEqual
(
msg
,
copy
);
}
[
Test
]
public
void
TestEmptyMessage
()
{
...
...
src/ProtocolBuffers/AbstractMessage.cs
View file @
f292523d
...
...
@@ -121,6 +121,27 @@ namespace Google.ProtocolBuffers
return
TextFormat
.
PrintToString
(
this
);
}
public
string
ToJson
()
{
Serialization
.
JsonFormatWriter
w
=
Serialization
.
JsonFormatWriter
.
CreateInstance
();
w
.
WriteMessage
(
this
);
return
w
.
ToString
();
}
public
string
ToXml
()
{
StringWriter
w
=
new
StringWriter
(
new
System
.
Text
.
StringBuilder
(
4096
));
Serialization
.
XmlFormatWriter
.
CreateInstance
(
w
).
WriteMessage
(
this
);
return
w
.
ToString
();
}
public
string
ToXml
(
string
rootElementName
)
{
StringWriter
w
=
new
StringWriter
(
new
System
.
Text
.
StringBuilder
(
4096
));
Serialization
.
XmlFormatWriter
.
CreateInstance
(
w
).
WriteMessage
(
rootElementName
,
this
);
return
w
.
ToString
();
}
public
override
sealed
void
PrintTo
(
TextWriter
writer
)
{
TextFormat
.
Print
(
this
,
writer
);
...
...
src/ProtocolBuffers/ExtendableBuilder.cs
View file @
f292523d
...
...
@@ -42,7 +42,7 @@ namespace Google.ProtocolBuffers
{
public
abstract
class
ExtendableBuilder
<
TMessage
,
TBuilder
>
:
GeneratedBuilder
<
TMessage
,
TBuilder
>
where
TMessage
:
ExtendableMessage
<
TMessage
,
TBuilder
>
where
TBuilder
:
GeneratedBuilder
<
TMessage
,
TBuilder
>
where
TBuilder
:
GeneratedBuilder
<
TMessage
,
TBuilder
>
,
new
()
{
protected
ExtendableBuilder
()
{
...
...
src/ProtocolBuffers/ExtendableMessage.cs
View file @
f292523d
...
...
@@ -43,7 +43,7 @@ namespace Google.ProtocolBuffers
{
public
abstract
class
ExtendableMessage
<
TMessage
,
TBuilder
>
:
GeneratedMessage
<
TMessage
,
TBuilder
>
where
TMessage
:
GeneratedMessage
<
TMessage
,
TBuilder
>
where
TBuilder
:
GeneratedBuilder
<
TMessage
,
TBuilder
>
where
TBuilder
:
GeneratedBuilder
<
TMessage
,
TBuilder
>
,
new
()
{
protected
ExtendableMessage
()
{
...
...
src/ProtocolBuffers/GeneratedBuilder.cs
View file @
f292523d
...
...
@@ -49,7 +49,7 @@ namespace Google.ProtocolBuffers
/// </summary>
public
abstract
class
GeneratedBuilder
<
TMessage
,
TBuilder
>
:
AbstractBuilder
<
TMessage
,
TBuilder
>
where
TMessage
:
GeneratedMessage
<
TMessage
,
TBuilder
>
where
TBuilder
:
GeneratedBuilder
<
TMessage
,
TBuilder
>
where
TBuilder
:
GeneratedBuilder
<
TMessage
,
TBuilder
>
,
new
()
{
/// <summary>
/// Returns the message being built at the moment.
...
...
src/ProtocolBuffers/GeneratedMessage.cs
View file @
f292523d
...
...
@@ -50,7 +50,7 @@ namespace Google.ProtocolBuffers
/// </summary>
public
abstract
class
GeneratedMessage
<
TMessage
,
TBuilder
>
:
AbstractMessage
<
TMessage
,
TBuilder
>
where
TMessage
:
GeneratedMessage
<
TMessage
,
TBuilder
>
where
TBuilder
:
GeneratedBuilder
<
TMessage
,
TBuilder
>
where
TBuilder
:
GeneratedBuilder
<
TMessage
,
TBuilder
>
,
new
()
{
private
UnknownFieldSet
unknownFields
=
UnknownFieldSet
.
DefaultInstance
;
...
...
@@ -175,5 +175,35 @@ namespace Google.ProtocolBuffers
{
unknownFields
=
fieldSet
;
}
public
static
TMessage
ParseFromJson
(
string
jsonText
)
{
return
Serialization
.
JsonFormatReader
.
CreateInstance
(
jsonText
)
.
Merge
(
new
TBuilder
())
.
Build
();
}
public
static
TMessage
ParseFromJson
(
System
.
IO
.
TextReader
reader
)
{
return
ParseFromJson
(
reader
,
ExtensionRegistry
.
Empty
);
}
public
static
TMessage
ParseFromJson
(
System
.
IO
.
TextReader
reader
,
ExtensionRegistry
extensionRegistry
)
{
return
Serialization
.
JsonFormatReader
.
CreateInstance
(
reader
)
.
Merge
(
new
TBuilder
(),
extensionRegistry
)
.
Build
();
}
public
static
TMessage
ParseFromXml
(
System
.
Xml
.
XmlReader
reader
)
{
return
ParseFromXml
(
Serialization
.
XmlFormatReader
.
DefaultRootElementName
,
reader
,
ExtensionRegistry
.
Empty
);
}
public
static
TMessage
ParseFromXml
(
string
rootElementName
,
System
.
Xml
.
XmlReader
reader
)
{
return
ParseFromXml
(
rootElementName
,
reader
,
ExtensionRegistry
.
Empty
);
}
public
static
TMessage
ParseFromXml
(
string
rootElementName
,
System
.
Xml
.
XmlReader
reader
,
ExtensionRegistry
extensionRegistry
)
{
return
Serialization
.
XmlFormatReader
.
CreateInstance
(
reader
)
.
Merge
(
rootElementName
,
new
TBuilder
(),
extensionRegistry
)
.
Build
();
}
}
}
\ No newline at end of file
src/ProtocolBuffers/IMessage.cs
View file @
f292523d
...
...
@@ -184,6 +184,24 @@ namespace Google.ProtocolBuffers
/// </summary>
new
byte
[]
ToByteArray
();
/// <summary>
/// Serializes the message to JSON text. This is a trivial wrapper
/// around Serialization.JsonFormatWriter.WriteMessage.
/// </summary>
string
ToJson
();
/// <summary>
/// Serializes the message to XML text. This is a trivial wrapper
/// around Serialization.XmlFormatWriter.WriteMessage.
/// </summary>
string
ToXml
();
/// <summary>
/// Serializes the message to XML text using the element name provided.
/// This is a trivial wrapper around Serialization.XmlFormatWriter.WriteMessage.
/// </summary>
string
ToXml
(
string
rootElementName
);
/// <summary>
/// Serializes the message and writes it to the given stream.
/// This is just a wrapper around WriteTo(ICodedOutputStream). This
...
...
src/ProtocolBuffers/Serialization/XmlFormatWriter.cs
View file @
f292523d
...
...
@@ -20,7 +20,13 @@ namespace Google.ProtocolBuffers.Serialization
static
XmlWriterSettings
DefaultSettings
(
Encoding
encoding
)
{
return
new
XmlWriterSettings
()
{
CheckCharacters
=
false
,
NewLineHandling
=
NewLineHandling
.
Entitize
,
Encoding
=
encoding
};
return
new
XmlWriterSettings
()
{
CheckCharacters
=
false
,
NewLineHandling
=
NewLineHandling
.
Entitize
,
OmitXmlDeclaration
=
true
,
Encoding
=
encoding
,
};
}
/// <summary>
...
...
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