Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON Parsing Improvements #735

Open
atteneder opened this issue Dec 10, 2024 · 2 comments
Open

JSON Parsing Improvements #735

atteneder opened this issue Dec 10, 2024 · 2 comments
Labels
customization Make glTFast behavior more costomizable enhancement New feature or request import Import of glTF files run-time
Milestone

Comments

@atteneder
Copy link
Owner

atteneder commented Dec 10, 2024

The Problem

glTFast currently has two JSON parsing backends.

  1. JsonUtility
  2. Newtonsoft JSON (added in 6.0.0)

JsonUtility is fast, has few GC allocations and a low build-size overhead, but it requires two-pass parsing to ensure non-present object properties are null and it does not grant access to meta-data that's unknown at build-time.

Newtonsoft JSON on the other hand allows accessing arbitrary data in extras, extensions or elsewhere, but is slower and has a large build-size overhead. On top of that it was necessary to drive another abstraction layer into the de-serialization (schema) classes which is complicated and has already introduces API breaking barriers that are hard to overcome.

There's also #734 which needs to be addressed/is a hard requirement.

Solutions

The important metrics of potential solutions are:

  • Parsing Speed
  • Memory allocations
  • Build-size overhead
  • Impact on code complexity (especially de-serialization classes)

The ideal scenario is one JSON parser that wins in all categories and replaces both JsonUtility and Newtonsoft JSON.

The next best alternative would be to replace Newtonsoft JSON only.

Requirements

  • Works with currently supported Unity versions.
  • Generic, so that it works with data structures not known at build-time.
  • Data is consumable for add-on development (Access to arbitrary JSON meta-data #734).

Nice-to-haves

  • Ability to consume streams (System.IO.Stream).
  • Backwards compatible with older Unity versions.
  • Parse _n type properties into one array. (e.g. TEXCOORD_0, TEXCOORD_1...TEXCOORD_n; see spec).

Misc.

In order to avoid reflection for generic de-serialization, we should investigate into source generators.

Alternatives

There's a bunch of JSON parsers out there we should consider or maybe take a page out of their book.

@atteneder atteneder added enhancement New feature or request customization Make glTFast behavior more costomizable import Import of glTF files run-time labels Dec 10, 2024
@atteneder atteneder added this to the 7.x milestone Dec 10, 2024
@mikeskydev
Copy link

mikeskydev commented Dec 10, 2024

I can't find it now, but years ago I looked into the relative speeds of JsonUtility vs Newtonsoft vs System.Text.Json for the purpose of parsing arbitrary extensions in glTFast. System.Text.Json was much faster than Newtonsoft, but not as fast as JsonUtility, iirc.

Found it: #226 (comment)

@atteneder
Copy link
Owner Author

@mikeskydev thanks for the reminder, I've added it to the list.

SimpleJSON seems to do de-serialization into an intermediate format (JSONNode) only and not casting to a custom type (like GLTFast.Schema.Root).

IIRC, System.Text.Json is not available in Unity (leaving aside packing nuget dll into a project, which is not an option for a supported package).

@atteneder atteneder modified the milestones: 7.x, 7.0 Dec 11, 2024
@atteneder atteneder moved this from To do to Planned in glTFast development Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
customization Make glTFast behavior more costomizable enhancement New feature or request import Import of glTF files run-time
Projects
Status: Planned
Development

No branches or pull requests

2 participants