Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
F
flatbuffers
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
flatbuffers
Commits
94d5643f
Commit
94d5643f
authored
Aug 18, 2016
by
Ben Hamilton
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow GenerateText() to indicate failure to encode flatbuffer to JSON (i.e., non-UTF-8 string data)
parent
f0d91fa1
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
35 additions
and
7 deletions
+35
-7
idl.h
include/flatbuffers/idl.h
+3
-1
sample_text.cpp
samples/sample_text.cpp
+4
-1
idl_gen_text.cpp
src/idl_gen_text.cpp
+0
-0
test.cpp
tests/test.cpp
+28
-5
No files found.
include/flatbuffers/idl.h
View file @
94d5643f
...
@@ -596,7 +596,9 @@ extern void GenComment(const std::vector<std::string> &dc,
...
@@ -596,7 +596,9 @@ extern void GenComment(const std::vector<std::string> &dc,
// if it is less than 0, no linefeeds will be generated either.
// if it is less than 0, no linefeeds will be generated either.
// See idl_gen_text.cpp.
// See idl_gen_text.cpp.
// strict_json adds "quotes" around field names if true.
// strict_json adds "quotes" around field names if true.
extern
void
GenerateText
(
const
Parser
&
parser
,
// If the flatbuffer cannot be encoded in JSON (e.g., it contains non-UTF-8
// byte arrays in String values), returns false.
extern
bool
GenerateText
(
const
Parser
&
parser
,
const
void
*
flatbuffer
,
const
void
*
flatbuffer
,
std
::
string
*
text
);
std
::
string
*
text
);
extern
bool
GenerateTextFile
(
const
Parser
&
parser
,
extern
bool
GenerateTextFile
(
const
Parser
&
parser
,
...
...
samples/sample_text.cpp
View file @
94d5643f
...
@@ -46,7 +46,10 @@ int main(int /*argc*/, const char * /*argv*/[]) {
...
@@ -46,7 +46,10 @@ int main(int /*argc*/, const char * /*argv*/[]) {
// to ensure it is correct, we now generate text back from the binary,
// to ensure it is correct, we now generate text back from the binary,
// and compare the two:
// and compare the two:
std
::
string
jsongen
;
std
::
string
jsongen
;
GenerateText
(
parser
,
parser
.
builder_
.
GetBufferPointer
(),
&
jsongen
);
if
(
!
GenerateText
(
parser
,
parser
.
builder_
.
GetBufferPointer
(),
&
jsongen
))
{
printf
(
"Couldn't serialize parsed data to JSON!
\n
"
);
return
1
;
}
if
(
jsongen
!=
jsonfile
)
{
if
(
jsongen
!=
jsonfile
)
{
printf
(
"%s----------------
\n
%s"
,
jsongen
.
c_str
(),
jsonfile
.
c_str
());
printf
(
"%s----------------
\n
%s"
,
jsongen
.
c_str
(),
jsonfile
.
c_str
());
...
...
src/idl_gen_text.cpp
View file @
94d5643f
This diff is collapsed.
Click to expand it.
tests/test.cpp
View file @
94d5643f
...
@@ -410,7 +410,8 @@ void ParseAndGenerateTextTest() {
...
@@ -410,7 +410,8 @@ void ParseAndGenerateTextTest() {
// to ensure it is correct, we now generate text back from the binary,
// to ensure it is correct, we now generate text back from the binary,
// and compare the two:
// and compare the two:
std
::
string
jsongen
;
std
::
string
jsongen
;
GenerateText
(
parser
,
parser
.
builder_
.
GetBufferPointer
(),
&
jsongen
);
auto
result
=
GenerateText
(
parser
,
parser
.
builder_
.
GetBufferPointer
(),
&
jsongen
);
TEST_EQ
(
result
,
true
);
if
(
jsongen
!=
jsonfile
)
{
if
(
jsongen
!=
jsonfile
)
{
printf
(
"%s----------------
\n
%s"
,
jsongen
.
c_str
(),
jsonfile
.
c_str
());
printf
(
"%s----------------
\n
%s"
,
jsongen
.
c_str
(),
jsonfile
.
c_str
());
...
@@ -827,7 +828,8 @@ void FuzzTest2() {
...
@@ -827,7 +828,8 @@ void FuzzTest2() {
std
::
string
jsongen
;
std
::
string
jsongen
;
parser
.
opts
.
indent_step
=
0
;
parser
.
opts
.
indent_step
=
0
;
GenerateText
(
parser
,
parser
.
builder_
.
GetBufferPointer
(),
&
jsongen
);
auto
result
=
GenerateText
(
parser
,
parser
.
builder_
.
GetBufferPointer
(),
&
jsongen
);
TEST_EQ
(
result
,
true
);
if
(
jsongen
!=
json
)
{
if
(
jsongen
!=
json
)
{
// These strings are larger than a megabyte, so we show the bytes around
// These strings are larger than a megabyte, so we show the bytes around
...
@@ -987,7 +989,8 @@ void UnicodeTest() {
...
@@ -987,7 +989,8 @@ void UnicodeTest() {
true
);
true
);
std
::
string
jsongen
;
std
::
string
jsongen
;
parser
.
opts
.
indent_step
=
-
1
;
parser
.
opts
.
indent_step
=
-
1
;
GenerateText
(
parser
,
parser
.
builder_
.
GetBufferPointer
(),
&
jsongen
);
auto
result
=
GenerateText
(
parser
,
parser
.
builder_
.
GetBufferPointer
(),
&
jsongen
);
TEST_EQ
(
result
,
true
);
TEST_EQ
(
jsongen
,
TEST_EQ
(
jsongen
,
std
::
string
(
std
::
string
(
"{F:
\"\\
u20AC
\\
u00A2
\\
u30E6
\\
u30FC
\\
u30B6
\\
u30FC"
"{F:
\"\\
u20AC
\\
u00A2
\\
u30E6
\\
u30FC
\\
u30B6
\\
u30FC"
...
@@ -1003,13 +1006,31 @@ void UnicodeTestAllowNonUTF8() {
...
@@ -1003,13 +1006,31 @@ void UnicodeTestAllowNonUTF8() {
"
\\
u5225
\\
u30B5
\\
u30A4
\\
u30C8
\\
x01
\\
x80
\\
u0080
\\
uD83D
\\
uDE0E
\"
}"
),
true
);
"
\\
u5225
\\
u30B5
\\
u30A4
\\
u30C8
\\
x01
\\
x80
\\
u0080
\\
uD83D
\\
uDE0E
\"
}"
),
true
);
std
::
string
jsongen
;
std
::
string
jsongen
;
parser
.
opts
.
indent_step
=
-
1
;
parser
.
opts
.
indent_step
=
-
1
;
GenerateText
(
parser
,
parser
.
builder_
.
GetBufferPointer
(),
&
jsongen
);
auto
result
=
GenerateText
(
parser
,
parser
.
builder_
.
GetBufferPointer
(),
&
jsongen
);
TEST_EQ
(
result
,
true
);
TEST_EQ
(
jsongen
,
TEST_EQ
(
jsongen
,
std
::
string
(
std
::
string
(
"{F:
\"\\
u20AC
\\
u00A2
\\
u30E6
\\
u30FC
\\
u30B6
\\
u30FC"
"{F:
\"\\
u20AC
\\
u00A2
\\
u30E6
\\
u30FC
\\
u30B6
\\
u30FC"
"
\\
u5225
\\
u30B5
\\
u30A4
\\
u30C8
\\
u0001
\\
x80
\\
u0080
\\
uD83D
\\
uDE0E
\"
}"
));
"
\\
u5225
\\
u30B5
\\
u30A4
\\
u30C8
\\
u0001
\\
x80
\\
u0080
\\
uD83D
\\
uDE0E
\"
}"
));
}
}
void
UnicodeTestGenerateTextFailsOnNonUTF8
()
{
flatbuffers
::
Parser
parser
;
// Allow non-UTF-8 initially to model what happens when we load a binary flatbuffer from disk
// which contains non-UTF-8 strings.
parser
.
opts
.
allow_non_utf8
=
true
;
TEST_EQ
(
parser
.
Parse
(
"table T { F:string; }"
"root_type T;"
"{ F:
\"\\
u20AC
\\
u00A2
\\
u30E6
\\
u30FC
\\
u30B6
\\
u30FC"
"
\\
u5225
\\
u30B5
\\
u30A4
\\
u30C8
\\
x01
\\
x80
\\
u0080
\\
uD83D
\\
uDE0E
\"
}"
),
true
);
std
::
string
jsongen
;
parser
.
opts
.
indent_step
=
-
1
;
// Now, disallow non-UTF-8 (the default behavior) so GenerateText indicates failure.
parser
.
opts
.
allow_non_utf8
=
false
;
auto
result
=
GenerateText
(
parser
,
parser
.
builder_
.
GetBufferPointer
(),
&
jsongen
);
TEST_EQ
(
result
,
false
);
}
void
UnicodeSurrogatesTest
()
{
void
UnicodeSurrogatesTest
()
{
flatbuffers
::
Parser
parser
;
flatbuffers
::
Parser
parser
;
...
@@ -1157,7 +1178,8 @@ void UnknownFieldsTest() {
...
@@ -1157,7 +1178,8 @@ void UnknownFieldsTest() {
std
::
string
jsongen
;
std
::
string
jsongen
;
parser
.
opts
.
indent_step
=
-
1
;
parser
.
opts
.
indent_step
=
-
1
;
GenerateText
(
parser
,
parser
.
builder_
.
GetBufferPointer
(),
&
jsongen
);
auto
result
=
GenerateText
(
parser
,
parser
.
builder_
.
GetBufferPointer
(),
&
jsongen
);
TEST_EQ
(
result
,
true
);
TEST_EQ
(
jsongen
==
"{str:
\"
test
\"
,i: 10}"
,
true
);
TEST_EQ
(
jsongen
==
"{str:
\"
test
\"
,i: 10}"
,
true
);
}
}
...
@@ -1222,6 +1244,7 @@ int main(int /*argc*/, const char * /*argv*/[]) {
...
@@ -1222,6 +1244,7 @@ int main(int /*argc*/, const char * /*argv*/[]) {
IntegerOutOfRangeTest
();
IntegerOutOfRangeTest
();
UnicodeTest
();
UnicodeTest
();
UnicodeTestAllowNonUTF8
();
UnicodeTestAllowNonUTF8
();
UnicodeTestGenerateTextFailsOnNonUTF8
();
UnicodeSurrogatesTest
();
UnicodeSurrogatesTest
();
UnicodeInvalidSurrogatesTest
();
UnicodeInvalidSurrogatesTest
();
InvalidUTF8Test
();
InvalidUTF8Test
();
...
...
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