Skip to content
# Class: JsonMappingConverter

Defined in: packages/core/src/transformers/JsonMappingConverter.ts:225

Unified JSON mapping converter that handles all supported formats using the Strategy pattern.

This converter automatically detects the input format and applies the appropriate conversion strategy to transform any supported JSON mapping format into a standardized result.

Supported Formats:

  • Enhanced: Rich format with metadata, type protection, and advanced column configurations
  • Model-Driven: TypeScript interface-based mapping with structured field definitions
  • Legacy: Simple format compatible with PostgresJsonQueryBuilder

Usage:

typescript
const converter = new JsonMappingConverter();
const result = converter.convert(someMapping);
const legacyMapping = converter.toLegacyMapping(someMapping);

Constructors

Constructor

new JsonMappingConverter(): JsonMappingConverter

Defined in: packages/core/src/transformers/JsonMappingConverter.ts:237

Creates a new JsonMappingConverter with all supported strategies.

Strategies are checked in order of specificity:

  1. Enhanced format (most feature-rich)
  2. Model-driven format (TypeScript-based)
  3. Legacy format (fallback)

Returns

JsonMappingConverter

Methods

detectFormat()

detectFormat(input): MappingFormat

Defined in: packages/core/src/transformers/JsonMappingConverter.ts:262

Detects the format of the input mapping without performing conversion.

This method uses the same strategy pattern as conversion but only returns the detected format type for inspection purposes.

Parameters

input

JsonMappingInput

The JSON mapping to analyze

Returns

MappingFormat

The detected mapping format type

Throws

When input format is not supported by any strategy

Example

typescript
const format = converter.detectFormat(myMapping);
console.log(`Detected format: ${format}`); // "enhanced", "model-driven", or "legacy"

convert()

convert(input): ConversionResult

Defined in: packages/core/src/transformers/JsonMappingConverter.ts:297

Converts any supported JSON mapping format to a comprehensive result with metadata.

This is the primary conversion method that performs format detection and transformation in a single operation. The result includes the legacy mapping, type protection configuration, and metadata about the conversion process.

Parameters

input

JsonMappingInput

The JSON mapping in any supported format (Enhanced, Model-Driven, or Legacy)

Returns

ConversionResult

Complete conversion result with mapping, metadata, and type protection

Throws

When the input format is not recognized by any strategy

Example

typescript
const result = converter.convert(enhancedMapping);
console.log(`Format: ${result.format}`);
console.log(`Type protection: ${result.typeProtection.protectedStringFields.length} fields`);

// Use the converted mapping
const queryBuilder = new PostgresJsonQueryBuilder(result.mapping);

See


toLegacyMapping()

toLegacyMapping(input): JsonMapping

Defined in: packages/core/src/transformers/JsonMappingConverter.ts:327

Extracts only the legacy JsonMapping for direct use with PostgresJsonQueryBuilder.

This convenience method performs the full conversion but returns only the mapping portion, discarding metadata and type protection information. Use this when you only need the mapping for query building and don't require additional metadata.

Parameters

input

JsonMappingInput

The JSON mapping in any supported format

Returns

JsonMapping

Legacy-format JsonMapping ready for PostgresJsonQueryBuilder

Throws

When the input format is not supported

Example

typescript
const legacyMapping = converter.toLegacyMapping(modelDrivenMapping);
const queryBuilder = new PostgresJsonQueryBuilder(legacyMapping);
const query = queryBuilder.build(selectQuery);

See

convert For full conversion with metadata


getTypeProtection()

getTypeProtection(input): TypeProtectionConfig

Defined in: packages/core/src/transformers/JsonMappingConverter.ts:355

Extracts type protection configuration for runtime type checking.

Type protection helps identify fields that should be treated as strings to prevent injection attacks or type coercion issues. This is particularly useful when working with user input or external data sources.

Parameters

input

JsonMappingInput

The JSON mapping in any supported format

Returns

TypeProtectionConfig

Type protection configuration with protected field definitions

Throws

When the input format is not supported

Example

typescript
const typeProtection = converter.getTypeProtection(enhancedMapping);

// Apply type protection during data processing
for (const field of typeProtection.protectedStringFields) {
    if (typeof data[field] !== 'string') {
        data[field] = String(data[field]);
    }
}

validate()

validate(input): string[]

Defined in: packages/core/src/transformers/JsonMappingConverter.ts:389

Validates that the input mapping is well-formed and can be successfully converted.

This method performs comprehensive validation without attempting conversion, returning an array of error messages for any issues found. An empty array indicates the mapping is valid and ready for conversion.

Validation Checks:

  • Basic structure validation (object type, required fields)
  • Format-specific validation (Enhanced, Model-Driven, Legacy)
  • Column configuration validation
  • Type protection configuration validation

Parameters

input

JsonMappingInput

The JSON mapping to validate

Returns

string[]

Array of validation error messages (empty if valid)

Example

typescript
const errors = converter.validate(suspiciousMapping);
if (errors.length > 0) {
    console.error('Validation failed:', errors);
    throw new Error(`Invalid mapping: ${errors.join(', ')}`);
}

// Safe to convert
const result = converter.convert(suspiciousMapping);

See

convert Performs conversion after implicit validation


upgradeToEnhanced()

upgradeToEnhanced(legacy, typeInfo?): EnhancedJsonMapping

Defined in: packages/core/src/transformers/JsonMappingConverter.ts:450

Creates a new enhanced mapping from legacy mapping.

Parameters

legacy

LegacyJsonMapping

typeInfo?
interface

string

importPath

string

Returns

EnhancedJsonMapping

Released under the MIT License.