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

TypeError on unserializedMessage. #1

Open
Samuelbella opened this issue Mar 30, 2023 · 2 comments
Open

TypeError on unserializedMessage. #1

Samuelbella opened this issue Mar 30, 2023 · 2 comments

Comments

@Samuelbella
Copy link

Hello,
I tried to use the IDMEFv2 library on python. I enter a string that I transform in Json (so dictionary) with Json.loads(),
I create the payload with SerializedMessage() as in the README. But when I use the payload with the unserialize() function, I have a
TypeError : __bytes__ returned non-bytes (type dict).

Thanks for your answer.

@Samuelbella
Copy link
Author

Samuelbella commented Mar 30, 2023

Here is the code I used to test:

import json
from idmefv2 import Message, SerializedMessage

#List of Dict
LDok = json.loads(Sok)
msg = Message()
#Dict
for Dok in LDok :

    payload = SerializedMessage('application/json', Dok)
    msg = Message.unserialize(payload)

With for example :
LDok[0] = {'Version': '2.0.3', 'ID': 'e5f9bbae-163e-42f9-a2f2-0daaf78fefb1', 'CreateTime': '2021-01-18T23:33:05.21Z', 'Analyzer': {'IP': '172.10.5.31', 'Name': 'Front door camera', 'Model': 'Netatmo 42B', 'Category': ['HAR'], 'Data': ['Images'], 'Method': ['Movement']}}

@fpoirotte
Copy link
Member

fpoirotte commented Mar 30, 2023

Hi,

Thank you for your report!

Disclaimer: I do not work on this project anymore.

The SerializedMessage class expects a serialized representation of the message as a sequence of bytes.
The call to Message.unserialize() will then unserialize the message for you (using knowledge about the serialization method in use it received as the first argument to SerializedMessage).
This makes the API more generic by decoupling it from a specific serialization method, so that new serialization methods can easily be plugged in later on (XML-based serialization, YAML-based serialization, binary serialization and so on).

Try out the following code:

import json
from idmefv2 import Message, SerializedMessage

# Notice how LDok[0] is now a byte sequence
LDok = [
  b'''
    {
      "Version": "2.0.3",
      "ID": "e5f9bbae-163e-42f9-a2f2-0daaf78fefb1",
      "CreateTime": "2021-01-18T23:33:05.21Z",
      "Analyzer": {
        "IP": "172.10.5.31",
        "Name": "Front door camera",
        "Model": "Netatmo 42B",
        "Category": ["HAR"],
        "Data": ["Images"],
        "Method": ["Movement"]
      }
    }
  '''
]

for Dok in LDok :
    payload = SerializedMessage('application/json', Dok)
    msg = Message.unserialize(payload)
    print(msg)

Regards,
François

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