Skip to content

feature: Add case class support to ObjectWeaver #135

@xerial

Description

@xerial

Summary

Add support for case class serialization/deserialization in ObjectWeaver to enable automatic weaving of user-defined data structures.

Background

ObjectWeaver currently supports primitive types and collections (List, Map, etc.) but lacks support for case classes, which are fundamental data structures in Scala. This feature would enable automatic serialization/deserialization of case classes to/from MessagePack and JSON formats.

Proposed Implementation

Architecture Overview

  • Leverage existing Surface system for compile-time case class introspection
  • Serialize case classes as MessagePack maps (field name → value)
  • Use constructor reflection for deserialization
  • Integrate with existing type conversion and error handling patterns

Implementation Plan

1. Design Architecture (High Priority)

  • Create CaseClassWeaver using Surface.of[A] for field metadata
  • Follow existing collection weaver patterns for type class derivation
  • Ensure compatibility with nested case classes and existing types

2. Serialization Implementation (High Priority)

  • Pack case class fields as MessagePack map entries
  • Use RecordSurface to iterate over case class fields
  • Handle field name mapping and value extraction
  • Support nested case classes recursively

3. Deserialization Implementation (High Priority)

  • Unpack MessagePack map to case class instance
  • Use Surface parameter information for constructor invocation
  • Implement field-by-field unpacking with type conversion
  • Handle missing fields and default values appropriately

4. Testing & Edge Cases (Medium Priority)

  • Comprehensive test suite covering basic case classes
  • Nested case class scenarios
  • Optional fields (Option[T]) and default parameter values
  • Error handling for malformed data and missing required fields
  • JSON round-trip testing

Technical Details

Current Foundation

  • Surface system provides compile-time type introspection
  • RecordSurface and Parameter abstractions available
  • MessagePack infrastructure with error handling
  • Type class derivation patterns established

Key Considerations

  • Type Safety: Use compile-time Surface generation for field introspection
  • Performance: Minimize reflection overhead during serialization/deserialization
  • Compatibility: Ensure seamless integration with existing collection and primitive weavers
  • Error Handling: Follow existing WeaverContext patterns for error propagation

Expected Usage

case class Person(name: String, age: Int, email: Option[String] = None)

given ObjectWeaver[Person] = ObjectWeaver.caseClassWeaver[Person]

val person = Person("Alice", 30, Some("[email protected]"))
val json = ObjectWeaver.toJson(person)
val restored = ObjectWeaver.fromJson[Person](json)

Benefits

  • Enables automatic serialization of user-defined data structures
  • Maintains type safety through compile-time Surface generation
  • Consistent with existing ObjectWeaver patterns and error handling
  • Supports complex nested structures and optional fields

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions