Skip to content

Allow specifying child class names #5

@theit8514

Description

@theit8514

Is your feature request related to a problem?

When generating classes from a JSON that has nested data, the root object gets generated with my defined class name, but any classes generated from properties of the root object are generated with just the property name.

Describe the feature you'd like

I would like to be able to provide a handler that can rewrite or prefix the generated class names.

What alternatives have you considered?

No response

Additional context

Input code:

var options = new Json2SharpOptions
{
    TargetLanguage = Language.CSharp,
    CSharpOptions = new Json2SharpCSharpOptions
    {
        IsSealed = false,
        TargetType = CSharpObjectType.Class,
        SerializationAttribute = CSharpSerializationAttribute.NewtonsoftJson
    }
};
var rawJson = """{"users":[{"id":"be37a82a5e052a0482e021183f68dd8c","username":"Test 1","time":1751653380834},{"id":"c474b6fe22761d8c640b657c399e5aad","username":"Test 2","time":1751653386295}]}""";
var code = Json2Sharp.Parse("MessagePresenceList", rawJson, options);
Console.WriteLine(code);

Output code:

      using Newtonsoft.Json;

      public class MessagePresenceList
      {
          [JsonProperty("users")]
          public required Users[] Users { get; init; }
      }

      public class Users
      {
          [JsonProperty("id")]
          public required string Id { get; init; }

          [JsonProperty("username")]
          public required string Username { get; init; }

          [JsonProperty("time")]
          public required long Time { get; init; }
      }

If the sub-classes are prefixed, expected output would be:

      using Newtonsoft.Json;

      public class MessagePresenceList
      {
          [JsonProperty("users")]
          public required MessagePresenceListUsers[] Users { get; init; }
      }

      public class MessagePresenceListUsers
      {
          [JsonProperty("id")]
          public required string Id { get; init; }

          [JsonProperty("username")]
          public required string Username { get; init; }

          [JsonProperty("time")]
          public required long Time { get; init; }
      }

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions