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
b59bfcb3
Commit
b59bfcb3
authored
Aug 03, 2015
by
Jon Skeet
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #681 from jskeet/json-fieldmask
JSON formatting for FieldMask
parents
8136ad5c
0e30de3d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
1 deletion
+39
-1
JsonFormatterTest.cs
csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs
+18
-0
JsonFormatter.cs
csharp/src/Google.Protobuf/JsonFormatter.cs
+21
-1
No files found.
csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs
View file @
b59bfcb3
...
@@ -388,6 +388,24 @@ namespace Google.Protobuf
...
@@ -388,6 +388,24 @@ namespace Google.Protobuf
AssertJson
(
"{ 'a': null, 'b': false, 'c': 10.5, 'd': 'text', 'e': [ 't1', 5 ], 'f': { 'nested': 'value' } }"
,
message
.
ToString
());
AssertJson
(
"{ 'a': null, 'b': false, 'c': 10.5, 'd': 'text', 'e': [ 't1', 5 ], 'f': { 'nested': 'value' } }"
,
message
.
ToString
());
}
}
[
Test
]
public
void
FieldMaskStandalone
()
{
var
fieldMask
=
new
FieldMask
{
Paths
=
{
""
,
"single"
,
"with_underscore"
,
"nested.field.name"
,
"nested..double_dot"
}
};
Assert
.
AreEqual
(
",single,withUnderscore,nested.field.name,nested..doubleDot"
,
fieldMask
.
ToString
());
// Invalid, but we shouldn't create broken JSON...
fieldMask
=
new
FieldMask
{
Paths
=
{
"x\\y"
}
};
Assert
.
AreEqual
(
@"x\\y"
,
fieldMask
.
ToString
());
}
[
Test
]
public
void
FieldMaskField
()
{
var
message
=
new
TestWellKnownTypes
{
FieldMaskField
=
new
FieldMask
{
Paths
=
{
"user.display_name"
,
"photo"
}
}
};
AssertJson
(
"{ 'fieldMaskField': 'user.displayName,photo' }"
,
JsonFormatter
.
Default
.
Format
(
message
));
}
/// <summary>
/// <summary>
/// Checks that the actual JSON is the same as the expected JSON - but after replacing
/// Checks that the actual JSON is the same as the expected JSON - but after replacing
/// all apostrophes in the expected JSON with double quotes. This basically makes the tests easier
/// all apostrophes in the expected JSON with double quotes. This basically makes the tests easier
...
...
csharp/src/Google.Protobuf/JsonFormatter.cs
View file @
b59bfcb3
...
@@ -36,6 +36,7 @@ using System.Globalization;
...
@@ -36,6 +36,7 @@ using System.Globalization;
using
System.Text
;
using
System.Text
;
using
Google.Protobuf.Reflection
;
using
Google.Protobuf.Reflection
;
using
Google.Protobuf.WellKnownTypes
;
using
Google.Protobuf.WellKnownTypes
;
using
System.Linq
;
namespace
Google.Protobuf
namespace
Google.Protobuf
{
{
...
@@ -402,6 +403,11 @@ namespace Google.Protobuf
...
@@ -402,6 +403,11 @@ namespace Google.Protobuf
MaybeWrapInString
(
builder
,
value
,
WriteDuration
,
inField
);
MaybeWrapInString
(
builder
,
value
,
WriteDuration
,
inField
);
return
;
return
;
}
}
if
(
descriptor
.
FullName
==
FieldMask
.
Descriptor
.
FullName
)
{
MaybeWrapInString
(
builder
,
value
,
WriteFieldMask
,
inField
);
return
;
}
if
(
descriptor
.
FullName
==
Struct
.
Descriptor
.
FullName
)
if
(
descriptor
.
FullName
==
Struct
.
Descriptor
.
FullName
)
{
{
WriteStruct
(
builder
,
(
IMessage
)
value
);
WriteStruct
(
builder
,
(
IMessage
)
value
);
...
@@ -479,6 +485,12 @@ namespace Google.Protobuf
...
@@ -479,6 +485,12 @@ namespace Google.Protobuf
builder
.
Append
(
's'
);
builder
.
Append
(
's'
);
}
}
private
void
WriteFieldMask
(
StringBuilder
builder
,
IMessage
value
)
{
IList
paths
=
(
IList
)
value
.
Descriptor
.
Fields
[
FieldMask
.
PathsFieldNumber
].
Accessor
.
GetValue
(
value
);
AppendEscapedString
(
builder
,
string
.
Join
(
","
,
paths
.
Cast
<
string
>().
Select
(
ToCamelCase
)));
}
/// <summary>
/// <summary>
/// Appends a number of nanoseconds to a StringBuilder. Either 0 digits are added (in which
/// Appends a number of nanoseconds to a StringBuilder. Either 0 digits are added (in which
/// case no "." is appended), or 3 6 or 9 digits.
/// case no "." is appended), or 3 6 or 9 digits.
...
@@ -654,6 +666,15 @@ namespace Google.Protobuf
...
@@ -654,6 +666,15 @@ namespace Google.Protobuf
private
void
WriteString
(
StringBuilder
builder
,
string
text
)
private
void
WriteString
(
StringBuilder
builder
,
string
text
)
{
{
builder
.
Append
(
'"'
);
builder
.
Append
(
'"'
);
AppendEscapedString
(
builder
,
text
);
builder
.
Append
(
'"'
);
}
/// <summary>
/// Appends the given text to the string builder, escaping as required.
/// </summary>
private
void
AppendEscapedString
(
StringBuilder
builder
,
string
text
)
{
for
(
int
i
=
0
;
i
<
text
.
Length
;
i
++)
for
(
int
i
=
0
;
i
<
text
.
Length
;
i
++)
{
{
char
c
=
text
[
i
];
char
c
=
text
[
i
];
...
@@ -713,7 +734,6 @@ namespace Google.Protobuf
...
@@ -713,7 +734,6 @@ namespace Google.Protobuf
break
;
break
;
}
}
}
}
builder
.
Append
(
'"'
);
}
}
private
const
string
Hex
=
"0123456789abcdef"
;
private
const
string
Hex
=
"0123456789abcdef"
;
...
...
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