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

Writing/Reading a human-readable format (probably JSON) #340

Open
MaxSavenkov opened this issue Oct 15, 2024 · 0 comments
Open

Writing/Reading a human-readable format (probably JSON) #340

MaxSavenkov opened this issue Oct 15, 2024 · 0 comments

Comments

@MaxSavenkov
Copy link

I'd really like to use MemoryPack for the purpose of serializing/deserializing game state to/from a save, but I need an option to also save/read a human-readable format. While MessagePack would get me both the binary and the JSON option, MessagePack cannot serialize private fields, which is very unfortunate, as I'm trying to replace the existing Json.NET serialization without rewriting a large portion of codebase.

TypeScript code generation would probably cover my use case, as I could deserialize a binary save in JavaScript and then easily dump it into JSON, but unfortunately, TypeScript generation is missing a lot of things, including struct serialization, and, as far as I can tell, Unity objects serialization.

I'm thinking about modifying MemoryPack generator to also (optionally?) generate classes that read and write JSON. Would you be willing to accept a PR adding such feature, and if you do, do you have any advice how to best approach this task? Currently, I'm thinking about:

  1. Creating MemoryPackJsonWriter/MemoryPackJsonReader classes to handle low-level tasks as MemoryPackWriter/Reader does. If it was just for my own project, I'd probably use Newtonsoft's JsonWriter/Reader for actual writing, but I guess such dependency is a bit too much for MessagePack proper, so I'd have to write my own Writer/Reader class, or maybe pilfer one from MessagePack.

  2. Adding SerializeJson/DeserializeJson methods to IMemoryPackFormatter and its implementations (we'll probably be unable to serialize SOME fields well - like various raw memory representations, but I guess we can fake it by serializing a Base64-encoded string, and most users won't need this anyway, including me). This is quite a bit of work, because each case should be handled with care.

  3. Adding a set of methods for generating JSON serializers to MemoryPack.Generator, in a separate source file (MemoryPackGenerator.EmitterJson), with entry point GenerateJson. This is the main body of work, I think.

  4. Calling GenerateJson at the same point where Generate is called, if option to generate JSON serializers is specified (I think it should be enabled by default for Unity, because Unity cannot pass options into generators)

Other than that, I think MemberMeta has all the necessary information to make writing and reading JSON a possibility (mostly, member names and types).

So, what do you think?

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

1 participant