robotframework-thailand / robotframework-jsonlibrary

Robotframework Test Library to manipulate JSON using JSONPath
The Unlicense
48 stars 42 forks source link

More return type for Get Value From JSON #36

Open pencherek opened 2 years ago

pencherek commented 2 years ago

I have the idea to add more return type to the keyword Get Value From JSON. Because what it return is not a JSON, this optonial parameter would let you choose if the keyword must return a json object, a string ?, a array, ...

elrandira commented 2 years ago

It is a bit complex to return an object as the data must be formatted for JSON. I would assume you would like to instantiate an object from the data but the reverse should be applied as well (serialize the object) so you can dump in a JSON file?

as #35 is merged, you can now create a new branch from the master to implement your idea.

pencherek commented 2 years ago

I don't think we can have object in robot framework like in C# or Java, so of do we instantiate a object without class ?

elrandira commented 2 years ago

You need to create some code in python that could be called by Robot after:

in somescript.py

class somescript:
  @staticmethod
  def json_to_object(self, json):
     return SomeClass(json)

  @staticmethod
  def object_to_json(object):
     return object.doJsonification()

in test.robot

Library JSONLibrary
Library somescript

*** testcases ***
my test
    ${json_obj}=  Load JSON From File  ${some_file_in_json}
    ${my_object}=  json to object  ${json_obj}

    #do something with my_object
    ${new_json}=  object to json  ${my_object}
    Dump Json To File  ${some_file_in_json}  ${new_json}
elrandira commented 2 years ago

you can call python code with Evaluate:

 ${items} =    Evaluate    [json.loads(item) for item in ('1', '"b"')]    modules=json

or use method on object

  Log to console  ${my_object.saySomething()}
pencherek commented 2 years ago

Ok, just I have no experience in python, I will need to learn

otemek commented 1 year ago

@pencherek If You could describe more precisely Your case I could try to implement that. For now, the keyword "Get Value From Json" returns a list of found values filtered by expression in a given JSON object: https://github.com/robotframework-thailand/robotframework-jsonlibrary/blob/53e1d019475430a30d9586b0fc501352bb30ac7b/JSONLibrary/jsonlibrary.py#L157

I think this library doesn't need to operate on custom objects as here we're only playing with the strings, lists, or dictionaries, where at the very end all the values are primitive ones (str, int, bool). Once again, If You could present some use case scenario then I can try to implement that. As @elrandira said it will probably require serialization/deserialization implementation for every custom object You want to operate or maybe something very different is on Your mind :)