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
da776fbd
Commit
da776fbd
authored
Aug 03, 2015
by
Jon Skeet
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #672 from jskeet/json-struct
Formatting of Struct as JSON
parents
115e6c73
e7caf155
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
98 additions
and
3 deletions
+98
-3
JsonFormatterTest.cs
csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs
+17
-0
JsonFormatter.cs
csharp/src/Google.Protobuf/JsonFormatter.cs
+81
-3
No files found.
csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs
View file @
da776fbd
...
@@ -368,7 +368,24 @@ namespace Google.Protobuf
...
@@ -368,7 +368,24 @@ namespace Google.Protobuf
{
{
var
message
=
new
TestWellKnownTypes
{
DurationField
=
new
Duration
()
};
var
message
=
new
TestWellKnownTypes
{
DurationField
=
new
Duration
()
};
AssertJson
(
"{ 'durationField': '0s' }"
,
JsonFormatter
.
Default
.
Format
(
message
));
AssertJson
(
"{ 'durationField': '0s' }"
,
JsonFormatter
.
Default
.
Format
(
message
));
}
[
Test
]
public
void
StructSample
()
{
var
message
=
new
Struct
{
Fields
=
{
{
"a"
,
new
Value
{
NullValue
=
new
NullValue
()
}
},
{
"b"
,
new
Value
{
BoolValue
=
false
}
},
{
"c"
,
new
Value
{
NumberValue
=
10.5
}
},
{
"d"
,
new
Value
{
StringValue
=
"text"
}
},
{
"e"
,
new
Value
{
ListValue
=
new
ListValue
{
Values
=
{
new
Value
{
StringValue
=
"t1"
},
new
Value
{
NumberValue
=
5
}
}
}
}
},
{
"f"
,
new
Value
{
StructValue
=
new
Struct
{
Fields
=
{
{
"nested"
,
new
Value
{
StringValue
=
"value"
}
}
}
}
}
}
}
};
AssertJson
(
"{ 'a': null, 'b': false, 'c': 10.5, 'd': 'text', 'e': [ 't1', 5 ], 'f': { 'nested': 'value' } }"
,
message
.
ToString
());
}
}
/// <summary>
/// <summary>
...
...
csharp/src/Google.Protobuf/JsonFormatter.cs
View file @
da776fbd
...
@@ -380,23 +380,44 @@ namespace Google.Protobuf
...
@@ -380,23 +380,44 @@ namespace Google.Protobuf
/// </summary>
/// </summary>
private
void
WriteWellKnownTypeValue
(
StringBuilder
builder
,
MessageDescriptor
descriptor
,
object
value
,
bool
inField
)
private
void
WriteWellKnownTypeValue
(
StringBuilder
builder
,
MessageDescriptor
descriptor
,
object
value
,
bool
inField
)
{
{
if
(
value
==
null
)
{
WriteNull
(
builder
);
return
;
}
// For wrapper types, the value will be the (possibly boxed) "native" value,
// For wrapper types, the value will be the (possibly boxed) "native" value,
// so we can write it as if we were unconditionally writing the Value field for the wrapper type.
// so we can write it as if we were unconditionally writing the Value field for the wrapper type.
if
(
descriptor
.
File
==
Int32Value
.
Descriptor
.
File
&&
value
!=
null
)
if
(
descriptor
.
File
==
Int32Value
.
Descriptor
.
File
)
{
{
WriteSingleValue
(
builder
,
descriptor
.
FindFieldByNumber
(
1
),
value
);
WriteSingleValue
(
builder
,
descriptor
.
FindFieldByNumber
(
1
),
value
);
return
;
return
;
}
}
if
(
descriptor
.
FullName
==
Timestamp
.
Descriptor
.
FullName
&&
value
!=
null
)
if
(
descriptor
.
FullName
==
Timestamp
.
Descriptor
.
FullName
)
{
{
MaybeWrapInString
(
builder
,
value
,
WriteTimestamp
,
inField
);
MaybeWrapInString
(
builder
,
value
,
WriteTimestamp
,
inField
);
return
;
return
;
}
}
if
(
descriptor
.
FullName
==
Duration
.
Descriptor
.
FullName
&&
value
!=
null
)
if
(
descriptor
.
FullName
==
Duration
.
Descriptor
.
FullName
)
{
{
MaybeWrapInString
(
builder
,
value
,
WriteDuration
,
inField
);
MaybeWrapInString
(
builder
,
value
,
WriteDuration
,
inField
);
return
;
return
;
}
}
if
(
descriptor
.
FullName
==
Struct
.
Descriptor
.
FullName
)
{
WriteStruct
(
builder
,
(
IMessage
)
value
);
return
;
}
if
(
descriptor
.
FullName
==
ListValue
.
Descriptor
.
FullName
)
{
var
fieldAccessor
=
descriptor
.
Fields
[
ListValue
.
ValuesFieldNumber
].
Accessor
;
WriteList
(
builder
,
fieldAccessor
,
(
IList
)
fieldAccessor
.
GetValue
(
value
));
return
;
}
if
(
descriptor
.
FullName
==
Value
.
Descriptor
.
FullName
)
{
WriteStructFieldValue
(
builder
,
(
IMessage
)
value
);
return
;
}
WriteMessage
(
builder
,
(
IMessage
)
value
);
WriteMessage
(
builder
,
(
IMessage
)
value
);
}
}
...
@@ -483,6 +504,63 @@ namespace Google.Protobuf
...
@@ -483,6 +504,63 @@ namespace Google.Protobuf
}
}
}
}
private
void
WriteStruct
(
StringBuilder
builder
,
IMessage
message
)
{
builder
.
Append
(
"{ "
);
IDictionary
fields
=
(
IDictionary
)
message
.
Descriptor
.
Fields
[
Struct
.
FieldsFieldNumber
].
Accessor
.
GetValue
(
message
);
bool
first
=
true
;
foreach
(
DictionaryEntry
entry
in
fields
)
{
string
key
=
(
string
)
entry
.
Key
;
IMessage
value
=
(
IMessage
)
entry
.
Value
;
if
(
string
.
IsNullOrEmpty
(
key
)
||
value
==
null
)
{
throw
new
InvalidOperationException
(
"Struct fields cannot have an empty key or a null value."
);
}
if
(!
first
)
{
builder
.
Append
(
", "
);
}
WriteString
(
builder
,
key
);
builder
.
Append
(
": "
);
WriteStructFieldValue
(
builder
,
value
);
first
=
false
;
}
builder
.
Append
(
first
?
"}"
:
" }"
);
}
private
void
WriteStructFieldValue
(
StringBuilder
builder
,
IMessage
message
)
{
var
specifiedField
=
message
.
Descriptor
.
Oneofs
[
0
].
Accessor
.
GetCaseFieldDescriptor
(
message
);
if
(
specifiedField
==
null
)
{
throw
new
InvalidOperationException
(
"Value message must contain a value for the oneof."
);
}
object
value
=
specifiedField
.
Accessor
.
GetValue
(
message
);
switch
(
specifiedField
.
FieldNumber
)
{
case
Value
.
BoolValueFieldNumber
:
case
Value
.
StringValueFieldNumber
:
case
Value
.
NumberValueFieldNumber
:
WriteSingleValue
(
builder
,
specifiedField
,
value
);
return
;
case
Value
.
StructValueFieldNumber
:
case
Value
.
ListValueFieldNumber
:
// Structs and ListValues are nested messages, and already well-known types.
var
nestedMessage
=
(
IMessage
)
specifiedField
.
Accessor
.
GetValue
(
message
);
WriteWellKnownTypeValue
(
builder
,
nestedMessage
.
Descriptor
,
nestedMessage
,
true
);
return
;
case
Value
.
NullValueFieldNumber
:
WriteNull
(
builder
);
return
;
default
:
throw
new
InvalidOperationException
(
"Unexpected case in struct field: "
+
specifiedField
.
FieldNumber
);
}
}
private
void
WriteList
(
StringBuilder
builder
,
IFieldAccessor
accessor
,
IList
list
)
private
void
WriteList
(
StringBuilder
builder
,
IFieldAccessor
accessor
,
IList
list
)
{
{
builder
.
Append
(
"[ "
);
builder
.
Append
(
"[ "
);
...
...
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