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

Add support for serializing to JSON #193

Open
MicahGale opened this issue Dec 11, 2023 · 2 comments
Open

Add support for serializing to JSON #193

MicahGale opened this issue Dec 11, 2023 · 2 comments
Assignees
Labels
code improvement A feature request that will improve the software and its maintainability, but be invisible to users. feature request An issue that improves the user interface.

Comments

@MicahGale
Copy link
Collaborator

Due to the time it takes to read a model into MontePy, it is helpful for users to be able to serialize the model and write it to file to make loading in the future easier. Pickling is possible, but does come with a huge security vulnerability when sharing files. Instead a way to support serialization that does not allow code execution is needed. So JSON should be supported.

Some thoughts:

  1. Most objects are not immediately writeable to JSON.
  2. The "correct" way to implement this is by implementing a default function that is passed to the load/dump function. This doesn't seem the cleanest as it would be nice for JSON to call a dunder function that an object implements so json.dump can just be called on an object.
  3. The defacto standard is to implement a to_json method, and sometimes a from_json.
  4. The syntax tree for each object is it's most important information to keep.
  5. There are two ways to save to JSON.
    1. An internal representation of the state for the MontePy object e.g., {"cell": {"number": {"value": 120, "token": "0120"} ...}
    2. A JSON version of an MCNP input file. The goal with this would be to push MCNP in this direction: e.g., {"cell": {"number": 120, "mass_density": 10.0, importance: {"n": 1.0...}}}

How to implement:

  1. Implement MCNP_Problem.export_to_json() for creating a JSON formatted Input File
  2. Implement MCNP_Problem.to_json() for serializing the object state to json.
  3. Maybe we will want to create a generic MCNP_Problem.serialize that will just generate a dictionary that can then be saved however the user wants.
  4. Need to implement montepy.from_json.
@MicahGale
Copy link
Collaborator Author

In GitLab by @tjlaboss on Dec 14, 2023, 20:22

I like serialize() as a public method so that MontePy users can select the language that serves their needs: JSON, YAML, XML, etc. Adding methods like .to_json(), as Pandas does, are also nice for UX.

@MicahGale
Copy link
Collaborator Author

to_json would be for a complete serialized dump? For MCNP input json we could do to_mcnp_json maybe?

@MicahGale MicahGale self-assigned this Feb 13, 2024
@MicahGale MicahGale removed this from the Release-1.0.0 milestone Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code improvement A feature request that will improve the software and its maintainability, but be invisible to users. feature request An issue that improves the user interface.
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

1 participant