Statement.fromJson constructor

Statement.fromJson(
  1. Map<String, dynamic> json
)

Decode a Statement object from a given JSON object (Map).

Implementation

factory Statement.fromJson(Map<String, dynamic> json) {
  StatementType? kind;

  /// Use the 'statementType' key in the JSON Map to determine which concrete implementation of Statement to use for the decoding.
  if (json.containsKey('statementType')) {
    for (var statementType in StatementType.values) {
      if (json["statementType"] == statementType.jsonValue) {
        kind = statementType;
        break;
      }
    }
  }

  if (kind == null) {
    throw StatementSerializationException(
      json.containsKey("statementType") ? json["statementType"] : "UNKNOWN",
    );
  }

  /// Based on the determined [StatementType], call the corresponding concrete [Statement] class' `fromJson` function.
  switch (kind) {
    case StatementType.statementBlockStatement:
      return StatementBlock.fromJson(json);
    case StatementType.printStatement:
      return PrintStatement.fromJson(json);
    case StatementType.returnStatement:
      return ReturnStatement.fromJson(json);
    case StatementType.ifElseStatement:
      return IfElseStatement.fromJson(json);
    case StatementType.forLoopStatement:
      return ForLoopStatement.fromJson(json);
    case StatementType.whileLoopStatement:
      return WhileLoopStatement.fromJson(json);
    case StatementType.variableDeclarationStatement:
      return VariableDeclarationStatement.fromJson(json);
    case StatementType.variableAssignmentStatement:
      return VariableAssignmentStatement.fromJson(json);
    case StatementType.customFunctionCallStatement:
      return FunctionCallStatement.fromJson(json);
    case StatementType.breakStatement:
      return BreakStatement.fromJson(json);
    case StatementType.continueStatement:
      return ContinueStatement.fromJson(json);
  }
}