Skip to content

A new JsonConverterOptions attribute for new converter management #97

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Erifirin
Copy link
Contributor

Hello again.

Sometimes when developing a shared library I need to create a public json converter that does a specific job. This converter is not intended for general use and must be used explicitly by manually marking fields/properties with the attribute [JsonConverter(<type>)] (or by using custom JsonSerializerSettings). The problem is that I can't set such a limitation on my side. And without it, I can only hope that in each project my converter will be manually disabled in converters settings. But this approach is dangerous and inconvenient because it requires constant monitoring and maintenance of this list.

As an alternative, I propose to introduce a new attribute JsonConverterOptions with one optional property for now - Hidden (more properties can be added in future). If it is true, the converter is ignored in UnityConverterInitializer, is not displayed in converters settings and is not used globally even if Auto Sync Converters option is enabled.

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class JsonConverterOptionsAttribute : Attribute
{
    public bool Hidden { get; set; }
}

And use case:

[JsonConverterOptions(Hidden = true)]
public class SpecificConverter : JsonConverter
{
    ...
}

What do you think?

…operty Hidden to hide a converter from converters settings
@applejag
Copy link
Owner

This is a fine idea, @Erifirin. I like it!

I have not looked at the implementation yet, but I would say it would be better with a more explicit [HideInJsonConverterSettings] to mimic Unity's [HideInInspector] attribute.

Adding this feature inside a generic options/settings container like this makes it a little ambiguous, and I wouldn't bet on us supporting arbitrary compile-time settings to converters using attributes. That would be a weird way to control things when attributes anyways already require you to have code access.

@Erifirin
Copy link
Contributor Author

Erifirin commented Mar 20, 2025

Splendid. Glad to hear you liked the idea.

That would be a weird way to control things when attributes anyways already require you to have code access.

That's the point. Sometimes control should be in the hands of the converter's author.

I would say it would be better with a more explicit [HideInJsonConverterSettings] to mimic Unity's [HideInInspector] attribute.

Hmm.. Ok. Sounds good and looks clearer. I would prefer HideInJsonConverterSettings if you don't mind. Already made changes. Please review them when you have time.

Copy link
Owner

@applejag applejag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! Just some small comments. And now I have to figure out how to get my CI/CD to work again 🤔

Comment on lines +5 to +6
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class HideInJsonConverterSettings : Attribute { }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a doc comment on this type to explain how it works and why you would use it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try. But I'm not good at English))) Maybe you'll want to do it yourself later.

Comment on lines 12 to 13
var hiddenConverterRegistered = UnityConverterInitializer.defaultUnityConvertersSettings.Converters
.Any(x => x.GetType() == typeof(HiddenConverter));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make sure this works, could you also add a converter like VisibleConverter that doesn't have the attribute?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Easy. Just give me a couple of minutes)

@Erifirin
Copy link
Contributor Author

@applejag I've done. So what about problems with CI/CD? Is it because of my HiddenConverterTests?

@Erifirin Erifirin requested a review from applejag March 20, 2025 08:31
@applejag
Copy link
Owner

@applejag I've done. So what about problems with CI/CD? Is it because of my HiddenConverterTests?

I don't actively develop this repo so I always forget how to work with the CI/CD pipelines. And I don't use Unity anymore so every time I do stuff here I have to reinstall a bunch of stuff. But I'll look into it later as I'm working and I need to fully sit down with this and dedicate an hour before I feel comfortable merging and releasing anything

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

Successfully merging this pull request may close these issues.

2 participants