decodeJsonArray method

Map<String, JsonNode> decodeJsonArray({
  1. required int maxDepth,
  2. bool normalizeSpaces = false,
})

Decode JSON String containing (sub) List(s) by saving each value and storing their position as width/depth coordinates.

Example:

Given a JSON Array: [[ "hello", "goodbye" , "Sam", "Hanna"]]

Would result in the following Map: ".0.0.0.0" : "hello" ".0.0.0.1" : "goodbye" ".0.0.1.1" : "Sam" ".0.0.1.2" : "Hanna"

Implementation

Map<String, JsonNode> decodeJsonArray({
  required int maxDepth,

  /// Can be set to true for testing purposes.
  ///
  /// The squint json decode method will normalize
  /// the entire JSON content before calling any methods.
  bool normalizeSpaces = false,
}) {
  var size = <int, int>{};

  for (var i = 0; i <= maxDepth; i++) {
    size[i] = 0;
  }

  final output = <String, JsonNode>{};

  final input = normalizeSpaces ? split("").normalizeSpaces : split("");

  var currentDepth = 0;

  final keygen = ArrayDecodingKeyGenerator();

  var currentValue = "";

  _Token? previousToken;

  _Token? currentToken;

  var i = 0;

  while (i < input.length) {
    if (previousToken is ListOpeningBracketToken &&
        currentToken is ListClosingBracketToken) {
      output[keygen.currentKey] = _PlaceHolder(key: keygen.currentKey);
    }
    previousToken = currentToken;
    currentToken = _Token.fromChar(
      previousToken: previousToken,
      characters: input,
      currentSize: size,
      currentDepth: currentDepth,
      keygen: keygen,
      currentValue: currentValue,
      output: output,
      index: i,
    );

    i = currentToken.index;
    size = currentToken.size;
    currentDepth = currentToken.depth;
    currentValue = currentToken.value;
  }

  return output..removeWhere((key, value) => value is _PlaceHolder);
}