Skip to content

Event-based API #5

@d9k

Description

@d9k

It would be nice to implement event-based (SAX-style) parser API as in JSR 353 standart:

      String document = "[{\n" + "\"nom\":\"nom1\", \"prenom\": \"prenom1\", \"taille\": 175\n"
              + "},\n"
              + "{\n"
              + "\"nom\": \"nom2\", \"prenom\": \"prenom2\",\"taille\": 183\n" + "}\n"
              + "]";
      try (JsonParser parser = Json.createParser(new StringReader(document))) {
        Event event = null;
        while (parser.hasNext()) {
          event = parser.next();
          System.out.print("event=" + event);
          switch (event) {
            case KEY_NAME:
              System.out.print(" key=" + parser.getString());
              break;
            case VALUE_STRING:
              System.out.print(" value=" + parser.getString());
              break;
            case VALUE_NUMBER:
              if (parser.isIntegralNumber()) {
                System.out.println(" value=" + parser.getInt());
              } else {
                System.out.println(" value=" + parser.getBigDecimal());
              }
              break;
            case VALUE_NULL:
              System.out.print(" value=null");
              break;
          }
          System.out.println("");
        }
      } catch (Exception e) {
        e.printStackTrace();
      }

— example from https://www-jmdoudoux-fr.translate.goog/java/dej/chap-json-p.htm?_x_tr_sl=fr&_x_tr_tl=en&_x_tr_pto=wapp.

Event-based parsing will allow to lower memory usage by getting rid of intermediate tree structures and parsing directly to the application/game data structures on the fly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions