Skip to content

[FEATURE] Add merge method to ObjectType for deep merging support Labels: enhancement #30

Open
@puneetbansal-diconium

Description

@puneetbansal-diconium

Describe the solution you'd like
I would like to propose adding a merge method to the ObjectType class in the Koson library. This method would allow two JSON-like objects to be combined into a single, deeply merged object. It would be especially helpful while dealing with nested data structures, such as configurations, dynamic responses, or incremental updates to JSON.

The expected behaviour is that when two objects have the same key:

  • If the values are also ObjectType, they should be merged recursively.
  • If they are not, the value from the second object should overwrite the one from the first. All other keys that are unique to either object should remain as-is in the final result. The method should return a new ObjectType and must not modify the originals, thus ensuring immutability.

This would simplify real-world use cases like merging default and override configurations, combining JSON data coming from multiple sources, or building up objects incrementally. At present, such merging needs to be done manually, which can get verbose and error-prone — especially with nested data.

Adding this functionality would improve Koson's utility and make it more powerful and expressive for developers working with structured JSON data in Kotlin.

Describe alternatives you've considered
The current approach requires writing custom merge logic outside of Koson. While doable, it results in repetitive code and reduces readability, particularly when working with deeply nested JSON. A native merge method would be far cleaner and more reusable.

Additional context
This feature aligns well with Koson's philosophy of keeping JSON construction elegant and concise. I'm happy to contribute a PR for this if the maintainers are open to the idea. Looking forward to your thoughts!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions