json property

Stream<Map<String, Object?>> json
latefinal

Try to parse JSON data from the stream. If data is a valid JSON Map, the stream will emit a Map<String, Object?>. If data is not a valid JSON Map, the stream will not emit any data. If data is a valid JSON array, the stream will not emit any data.

Implementation

late final Stream<Map<String, Object?>> json =
    transform<Map<String, Object?>>(
  StreamTransformer<Object, Map<String, Object?>>.fromHandlers(
    handleData: (data, sink) {
      try {
        final json = switch (data) {
          String text
              when text.length >= 2 &&
                  text.codeUnitAt(0) == 123 &&
                  text.codeUnitAt(text.length - 1) == 125 =>
            _$jsonTextDecoder.convert(text),
          List<int> bytes
              when bytes.length >= 2 &&
                  bytes.first == 123 &&
                  bytes.last == 125 =>
            _$jsonBytesDecoder.convert(bytes),
          _ => null,
        };
        if (json != null) sink.add(json);
      } on Object {
        /* Do nothing */
      }
    },
  ),
).asBroadcastStream();