json_events 1.1.0 copy "json_events: ^1.1.0" to clipboard
json_events: ^1.1.0 copied to clipboard

A package for parsing large json files/objects. The package processes the json in a forward-only way and emits events based on the tokens it encounters.

json_events #

A package for parsing large json files/objects. The package processes the json in a forward-only way and emits events based on the tokens it encounters.

Parsing manually #

From a Stream

Stream<List<int>> s = ...;

s.transform(const Utf8Decoder())
  .transform(const JsonEventDecoder())
  .flatten();

await for (JsonEvent je in s) {
  print("Event Type: ${je.type.name} Value: ${je.value}");
}

From a String

Stream<String> s = Stream.value(...);

s.transform(const JsonEventDecoder())
  .flatten();

await for (JsonEvent je in s) {
  print("Event Type: ${je.type.name} Value: ${je.value}");
}

Using mixin #

For objects #

class MyClass with JsonObjectTraverser {
  late int x;
  late List<MyClass> arr;
  late List<int> pArr;
  late List<List<int>> pNestedArray;
  String? text;

  @override
  Future<void> readJson(String key) async {
    switch (key) {
      case "x":
        x = await this.readPropertyJsonContinue<int>();
        break;
      case "text":
        y = await this.readPropertyJsonContinue<String?>();
        break;
      case "arr":
        arr = await this.readPropertyJsonContinue<MyClass>(creator: MyClass.new);
        break;
      case "pArr":
        pArr = await this.readArrayJsonContinue<int>().toList();
        break;
      case "pNestedArray":
        pNestedArray = await this.readNestedArrayJsonContinue<int>().toList();
        break;
    }
  }
}

Then call appropriate loader

MyClass mc = MyClass();
await mc.loadJson(streamIterator);

For arrays that are represented as objects #

class MyArrayClass with JsonArrayTraverser<MyClass> {
  @override
  FutureOr<MyClass> Function()? creator = MyClass.new; // or in constructor

  MyArrayClass() : super([]) {
    // creator = MyClass.new;
  }
}

Then call appropriate loader

MyArrayClass mc = MyArrayClass();
await mc.loadJsonFromStream(stream);

Disclaimer #

8
likes
0
points
554
downloads

Publisher

verified publisheraeb-dev.me

Weekly Downloads

A package for parsing large json files/objects. The package processes the json in a forward-only way and emits events based on the tokens it encounters.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

meta

More

Packages that depend on json_events