Class ToJsonGenerator

java.lang.Object
org.ek9lang.compiler.phase7.synthesis.AbstractSyntheticGenerator
org.ek9lang.compiler.phase7.synthesis.ToJsonGenerator

final class ToJsonGenerator extends AbstractSyntheticGenerator
Generates synthetic IR for the _json ($$) operator.

The generated code follows this pattern:

JSON _json():
  // Guard: if object is unset, return UNSET JSON
  if !this._isSet() -> return unset JSON

  // has_data: at least one field is set
  jsonTemp = JSON()
  result = jsonTemp.object()

  // For each field:
  fieldValue = LOAD this.field
  fieldJson = fieldValue._json()         // Recursive JSON conversion
  nameString = "fieldName" (literal)
  pair = JSON(nameString, fieldJson)     // Create name/value pair
  result._merge(pair)                    // Merge into result

  return result

  return_unset:
    return new JSON()  // Unset JSON

Key semantic requirements:

  • Returns UNSET JSON if this._isSet() is false (empty objects serialize to null)
  • Returns set JSON if any field is set
  • Format: {"field1": value1, "field2": value2}
  • Each field value is converted via $$ (_json) recursively
  • Unset field values become JSON null (handled by JSON constructor)
  • Lists produce JSON arrays, Dicts produce nested objects

The ? guard ensures consistency: operations on empty objects propagate uncertainty.

  • Constructor Details

  • Method Details

    • generate

      List<IRInstr> generate(MethodSymbol operatorSymbol, AggregateSymbol aggregateSymbol)
      Generate the _json operator IR for the given aggregate.
      Parameters:
      operatorSymbol - The _json operator symbol
      aggregateSymbol - The aggregate containing the operator
      Returns:
      List of IR instructions implementing the operator