Skip to content

Name handler for child classes#6

Merged
Kaoticz merged 9 commits intomainfrom
classhandler
Sep 14, 2025
Merged

Name handler for child classes#6
Kaoticz merged 9 commits intomainfrom
classhandler

Conversation

@Kaoticz
Copy link
Owner

@Kaoticz Kaoticz commented Jul 27, 2025

Adds the property TypeNameHandler to BaseLanguageOptions, so library users can overwrite the type name given to custom child objects in the Json object.

// BaseLanguageOptions
public abstract Func<string, string> TypeNameHandler { get; init; }

The function takes the raw name of the JSON property and is expected to return the name of the property type in the class.
You can define your own implementation and return whatever name you want for the class. Default implementations are provided for convenience. They are language-specific and follow the naming convention for their respective languages.

Default implementation for C#:

// Json2SharpCSharpOptions : BaseLanguageOptions
public override Func<string, string> TypeNameHandler { get; init; } = static propertyName => propertyName.ToPascalCase();

Sample usage:

var options = new Json2SharpOptions()
{
    TargetLanguage = Language.CSharp,
    
    CSharpOptions = new()
    {
        TypeNameHandler = propertyType => propertyType.ToUpperInvariant() + "AYY"
    }
};

The option above would append "AYY" to the names of all child classes in upper case: blep -> BLEPAYY

@Kaoticz Kaoticz self-assigned this Jul 27, 2025
@Kaoticz Kaoticz added the feature New feature or request label Jul 27, 2025
@Kaoticz Kaoticz linked an issue Jul 27, 2025 that may be closed by this pull request
@Kaoticz Kaoticz marked this pull request as ready for review August 31, 2025 03:54
@Kaoticz Kaoticz merged commit 924fc4e into main Sep 14, 2025
5 checks passed
@Kaoticz Kaoticz deleted the classhandler branch September 14, 2025 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow specifying child class names

1 participant