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

choosing alternative serialization field name for json at runtime #2716

Open
softstream-link opened this issue Mar 20, 2024 · 0 comments
Open

Comments

@softstream-link
Copy link

softstream-link commented Mar 20, 2024

Trying to put together a serde crate for Financial Information Exchange ( FIX ) protocol, https://fiximate.fixtrading.org, and encountered a problem that I am not sure how to solve.

Description:

So I would like to generate a bunch of message structures which I can serde both into JSON & FIX format from the same exact structure.

Example:

        #[derive(Serialize, Debug, PartialEq)]
        struct Account(String);

        #[derive(Serialize, Debug, PartialEq)]
        struct Msg {
            #[serde(rename = "1")]
            #[serde(alias = "Account")]
            account: Account,
        }

This results in a JSON {"1":"ABC"} & FIX that looks roughly like '1=ABC|' , '|' is not the actual delimiter but here for readability.

Problem:

FIX has two names for each field: a unique Numeric ID and a unique String Name. Example 1 is Same as Account but wire format is always in numeric format. Hence in the above example I can use serde's rename attribute to achieve this. However, I would like to be able to use JSON in both String Name & Numeric ID form.

Deserializing this dual form from JSON does not seem to be a problem and a alias can be used I believe. However, Serializing does not seem to have an option to choose the name, I thought that maybe a pretty_json function can be used but looking at serde_json code I don't believe this Is an option.

Question:

Is it possible at runtime to choose to ignore rename in the serde_json crait or my only option is to do a global string replacement after {"1":"ABC"} json is crated?

In other words I would like to be able to create either:

{"1":"ABC"} or {"Account":"ABC"}
as well as
'1=ABC|' or 'Account=ABC|'

Conceptually I think of them as Display vs Debug representation, where the Debug has a full name. perhaps it is a separate SerializeDebug macro, but I am open to any other simple to achieve ideas.

Also I did bump into a is_human_readable method but it seems to only enable a value variant but not the key variant. Is there something similar for the key/field name?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant