Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@tira-misu
Copy link
Contributor

Now its possible to generate json data from bfbs data type and flatbuffers data
and visa versa.

  • add deserialize functionality in parser from bfbs
  • add small usage sample

Thank you for submitting a PR!

Please make sure you include the names of the affected language(s) in your PR title.
This helps us get the correct maintainers to look at your issue.

If you make changes to any of the code generators, be sure to run
cd tests && sh generate_code.sh (or equivalent .bat) and include the generated
code changes in the PR. This allows us to better see the effect of the PR.

If your PR includes C++ code, please adhere to the Google C++ Style Guide,
and don't forget we try to support older compilers (e.g. VS2010, GCC 4.6.3),
so only some C++11 support is available.

Include other details as appropriate.

Thanks!

Now its possible to generate json data from bfbs data type and flatbuffers data
and visa versa.

* add deserialize functionality in parser from bfbs
* add small usage sample
@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here (e.g. I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

@tira-misu
Copy link
Contributor Author

I signed it!

@googlebot
Copy link

CLAs look good, thanks!

Offset<reflection::Object> Serialize(FlatBufferBuilder *builder,
const Parser &parser) const;

void Deserialize(Parser *parser, const flatbuffers::Vector<flatbuffers::Offset<reflection::Field>> *flatFields);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use snake_case, not camelCase, here and elsewhere.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, lines <80 chars, { on same line.. please read Google C++ Style Guide and apply to your code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code is now Google C++ Style Guide conform.


using namespace MyGame::Sample;

// This is an example of parsing text straight into a buffer and then
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate that you've made a sample, but really this would be better off in the tests, since these samples are not run as part of testing. Also, would be better to use the existing .bfbs file rather than generating it anew.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using existing bfbs in example now.
Used/fixed existing test of @fbenkstein. (And found/fixed some issues in deserialization)

std::string Parser::GetNameFromFullQualifiedName(std::string fullQualifiedName)
{
Namespace *ns = new Namespace();

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no empty lines needed, unless function is really long.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

@aardappel
Copy link
Collaborator

Thanks! This looks like a pretty good implementation, but how is it different from #5001 by @fbenkstein ? Could you two compare implementations and make sure we end up with the best of both somehow?

@tira-misu
Copy link
Contributor Author

I will pick up @fbenkstein implementation and merge it into mine. It seams that he has no time for it. I wasn't aware that someone had already taken on the subject.

Attribute flexbuffer has be included in bfbs. Only with this attribute test
will run. By initialization a parser by a bfbs the attribute has to be known
for this filed. monsterdata_test.golden has a flexbuffer field so parse would
fail.
src/flatc.cpp Outdated
" --raw-binary Allow binaries without file_indentifier to be read.\n"
" This may crash flatc given a mismatched schema.\n"
" --size-prefixed Input binaries are size prefixed buffers.\n"
" --schema-size-prefixed\n"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend not having this option. Having 2 kinds of binary schema files doesn't help anyone.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem is, you already have two kinds of schema files, depending whether you call flatc --binary --schema with the --size-prefixed option or not. In #5001 I added the flag so you can serialize and deserialize with or without size prefix independently of whether your schema is size prefixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compromise: removing this option and allow both file formats. Binary will be verified against both verifier and right one will be choosen. What's your opinion?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the verifier to decide doesn't sound great, better would be to use reflection::SchemaBufferHasIdentifier, and when that fails, try again assuming the first 4 bytes are a size.

I guess to me --size-prefixed is a niche feature, so --schema-size-prefixed is a niche of niche, and would prefer to not polute the options with it if we can reasonably either demand only regular FlatBuffers are used for binary schemas, or silently detect both with the above option.

const auto repack = IsInteger(e.type.base_type);
switch (e.type.base_type) {
// clang-format off
// clang-format off
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're going to indent the comment, then also the code following it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn IDE. It has changed some points of its own. I hoped that I reverted all, but somehow i missed something. I will look through all changes again an fix it.

@fbenkstein
Copy link
Contributor

fbenkstein commented Dec 11, 2018 via email

@aardappel
Copy link
Collaborator

Thanks, looking good now!

@aardappel aardappel merged commit dba962e into google:master Dec 13, 2018
@tira-misu tira-misu deleted the fix-issue4283 branch December 14, 2018 06:07
zchee pushed a commit to zchee/flatbuffers that referenced this pull request Feb 14, 2019
…e#5077)

* Enable flatbuffer to initialize Parser from bfbs (google#4283)

Now its possible to generate json data from bfbs data type and flatbuffers data
and visa versa.

* add deserialize functionality in parser from bfbs
* add small usage sample

* Fix build break

* Merge branch 'pr/1' into fix-issue4283

* Fix buildbreak

* Build monster_test.bfbs with --bfbs-builtins

Attribute flexbuffer has be included in bfbs. Only with this attribute test
will run. By initialization a parser by a bfbs the attribute has to be known
for this filed. monsterdata_test.golden has a flexbuffer field so parse would
fail.

* Fix generate_code.sh

* Revert automatic indent changes by IDE

* Auto detect size prefixed binary schema files

* Use identifier (bfbs) to detect schema files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants