Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method to encode from JSON to base64 Protobuf #1

Closed
Benjamin-Loison opened this issue Aug 22, 2023 · 2 comments
Closed

Method to encode from JSON to base64 Protobuf #1

Benjamin-Loison opened this issue Aug 22, 2023 · 2 comments

Comments

@Benjamin-Loison
Copy link

Benjamin-Loison commented Aug 22, 2023

In this Wiki section you give a method to decode a received base64 Protobuf but as I detail in this issue I would like to write in my source code meaningful JSON parameters like:

{
    "2": {
        "2": 1
    }
}

instead of the blacker box: EgIQAQ== but still be able to generate such base64 protobuf on the fly from above JSON. Is there any way to do so, even guessing the schema or so?

@menmob
Copy link
Owner

menmob commented Aug 22, 2023

You can do this. I believe there is a python package called blackboxprotobuf I have used for it. Let me see if I can find a code example in a few hours.

@Benjamin-Loison
Copy link
Author

Benjamin-Loison commented Aug 22, 2023

Thank you for this reference, it seems to be quite what I was looking for.

import blackboxprotobuf
import base64
import json

data = base64.b64decode('EgIQAQ==', altchars = '-_')
message, typedef = blackboxprotobuf.decode_message(data)
print(json.dumps(message, indent = 4))
print(json.dumps(typedef, indent = 4))

##

message = {
    '2': {'2': 1}
}

typedef = {
    '2': {
        'type': 'message',
        'message_typedef': {
            '2': {
                'type': 'int'
            }
        },
        'field_order': [
            ('2', 0)
        ]
    }
}

data = blackboxprotobuf.encode_message(message, typedef)
print(base64.b64encode(data, altchars = b'-_').decode('ascii'))

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

No branches or pull requests

2 participants