fprime icon indicating copy to clipboard operation
fprime copied to clipboard

Look into Python libraries for translating JSON to Python data structures

Open jwest115 opened this issue 10 months ago • 4 comments

Look into work that Ali did last summer and see what he was using initially. Ask Andrei for recommendations.

jwest115 avatar Jun 16 '25 20:06 jwest115

A combination these should work pretty well at loading the JSON.

  • @dataclass(frozen=true).
  • https://github.com/konradhalas/dacite

It can load JSON directly into an immutable object. If we could extract the fpp-to-json JSON schema we could generate the corresponding dataclasses in python. Optimally this would be automated so that FPP changes don't break the Python package.

Kronos3 avatar Jun 16 '25 20:06 Kronos3

Thanks @Kronos3! I noticed that in some of our other tools, Pydantic is used. It looks like Pydantic is more for reading and validating JSON content - are you familiar that library or see any use case for it here?

jwest115 avatar Jun 16 '25 21:06 jwest115

Ah yes that's probably better, bigger dependency but it looks like it already has the json schema -> python class: https://docs.pydantic.dev/latest/integrations/datamodel_code_generator/#example

Kronos3 avatar Jun 16 '25 21:06 Kronos3

Pydantic is already part of our requirements https://github.com/nasa/fprime/blob/14bac5f350fef9add6c58592c690ebdabfbc83c7/requirements.txt#L59

It would be nice to see it used more! I haven't really looked into it myself... but it seemed promising

thomas-bc avatar Jun 16 '25 21:06 thomas-bc

Looked into auto-generating the Python data structures via pydandtic's code generator but the output was not something we can work with. Also tried generating a JSON schema for the fpp-to-json output before generating the Python data structures and that also did not work well. Decided to go the route of converting FPP scala AST to Python by hand. Have notes in the wiki on the way we represent FPP AST in Python here.

jwest115 avatar Aug 19 '25 16:08 jwest115