Skip to content

Extended the functionality, and increased the performance of the builtin json library for Pyjinn - #79

Open
SmartBoty wants to merge 2 commits into
maxuser0:mainfrom
SmartBoty:main
Open

Extended the functionality, and increased the performance of the builtin json library for Pyjinn#79
SmartBoty wants to merge 2 commits into
maxuser0:mainfrom
SmartBoty:main

Conversation

@SmartBoty

Copy link
Copy Markdown

Upgraded json.py to use Gson instead of JsonParser: Significant performance improvements for deserialization and exposing dumps

Built in json.py:
1 280 307.60 ns / call (~1.28 ms)

This json.py:
852 362.57 ns / call (~0.85 ms)

Notes:

  • I tried to match the header to the builtin json.py-s header, but may still require adjustments

Code used to determine these values:

System = JavaClass("java.lang.System")

data = (
r"""
{
  "version": "1.2.0",
  "generated": "2026-07-21T14:30:00Z",
  "success": true,
  "metadata": {
    "creator": "Test Generator",
    "description": "JSON parser stress test",
    "tags": [
      "json",
      "parser",
      "test",
      "nested",
      "unicode"
    ],
    "unicode": "こんにちは 🌍 Привет مرحبا",
    "escaped": "Line1\nLine2\tTabbed\\Backslash\"Quote\""
  },
  "numbers": {
    "zero": 0,
    "negative": -42,
    "integer": 123456789,
    "float": 3.1415926535,
    "negativeFloat": -0.000123,
    "scientific1": 6.022e23,
    "scientific2": -1.6e-19,
    "large": 999999999999999,
    "small": 0.000000000001
  },
  "emptyObject": {},
  "emptyArray": [],
  "nullValue": null,
  "users": [
    {
      "id": 1,
      "name": "Alice",
      "active": true,
      "roles": [
        "admin",
        "developer"
      ],
      "address": {
        "street": "123 Main St",
        "city": "Exampleville",
        "zip": "12345",
        "coords": {
          "lat": 41.40338,
          "lon": 2.17403
        }
      },
      "scores": [
        95,
        88,
        76,
        100
      ]
    },
    {
      "id": 2,
      "name": "Bob",
      "active": false,
      "roles": [
        "user"
      ],
      "address": {
        "street": "456 Side Rd",
        "city": "Somewhere",
        "zip": null,
        "coords": {
          "lat": -33.865143,
          "lon": 151.2099
        }
      },
      "scores": []
    },
    {
      "id": 3,
      "name": "Charlie",
      "active": true,
      "roles": [],
      "address": {},
      "scores": [
        50,
        75.5,
        -12,
        1.2e4
      ]
    }
  ],
  "matrix": [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
  ],
  "mixedArray": [
    null,
    true,
    false,
    123,
    -45.67,
    "hello",
    {
      "nested": [
        {
          "a": 1
        },
        {
          "b": 2
        }
      ]
    },
    [],
    {}
  ],
  "config": {
    "graphics": {
      "width": 1920,
      "height": 1080,
      "fullscreen": false,
      "vsync": true
    },
    "audio": {
      "master": 0.8,
      "music": 0.5,
      "effects": 0.9
    },
    "network": {
      "host": "localhost",
      "port": 8080,
      "timeout": 15.5
    }
  },
  "deep": {
    "level1": {
      "level2": {
        "level3": {
          "level4": {
            "level5": {
              "message": "Reached the bottom!"
            }
          }
        }
      }
    }
  },
  "inventory": [
    {
      "id": "itm001",
      "name": "Sword",
      "stats": {
        "damage": 25,
        "durability": 98
      },
      "enchantments": [
        "Fire",
        "Sharpness"
      ]
    },
    {
      "id": "itm002",
      "name": "Shield",
      "stats": {
        "defense": 40,
        "durability": 100
      },
      "enchantments": []
    },
    {
      "id": "itm003",
      "name": "Potion",
      "stats": null,
      "stack": 15
    }
  ],
  "objectArray": [
    {
      "k": "a",
      "v": 1
    },
    {
      "k": "b",
      "v": 2
    },
    {
      "k": "c",
      "v": {
        "nested": true
      }
    }
  ]
}
""")

start = System.nanoTime()
for _ in range(10000):
  loads(data)
print(f"Average: {(System.nanoTime()-start)/10000}")

…tin json library for Pyjinn

Upgraded `json.py` to use `Gson` instead of `JsonParser`: Significant performance improvements for deserialization and exposing `dumps`

Built in `json.py`:
1 280 307.60 ns / call (~1.28 ms)

This `json.py`:
852 362.57 ns / call (~0.85 ms)

Notes:
- I tried to match the header to the builtin json.py-s header, but may still require adjustments

## Code used to determine these values:
```py
System = JavaClass("java.lang.System")

data = (
r"""
{
  "version": "1.2.0",
  "generated": "2026-07-21T14:30:00Z",
  "success": true,
  "metadata": {
    "creator": "Test Generator",
    "description": "JSON parser stress test",
    "tags": [
      "json",
      "parser",
      "test",
      "nested",
      "unicode"
    ],
    "unicode": "こんにちは 🌍 Привет مرحبا",
    "escaped": "Line1\nLine2\tTabbed\\Backslash\"Quote\""
  },
  "numbers": {
    "zero": 0,
    "negative": -42,
    "integer": 123456789,
    "float": 3.1415926535,
    "negativeFloat": -0.000123,
    "scientific1": 6.022e23,
    "scientific2": -1.6e-19,
    "large": 999999999999999,
    "small": 0.000000000001
  },
  "emptyObject": {},
  "emptyArray": [],
  "nullValue": null,
  "users": [
    {
      "id": 1,
      "name": "Alice",
      "active": true,
      "roles": [
        "admin",
        "developer"
      ],
      "address": {
        "street": "123 Main St",
        "city": "Exampleville",
        "zip": "12345",
        "coords": {
          "lat": 41.40338,
          "lon": 2.17403
        }
      },
      "scores": [
        95,
        88,
        76,
        100
      ]
    },
    {
      "id": 2,
      "name": "Bob",
      "active": false,
      "roles": [
        "user"
      ],
      "address": {
        "street": "456 Side Rd",
        "city": "Somewhere",
        "zip": null,
        "coords": {
          "lat": -33.865143,
          "lon": 151.2099
        }
      },
      "scores": []
    },
    {
      "id": 3,
      "name": "Charlie",
      "active": true,
      "roles": [],
      "address": {},
      "scores": [
        50,
        75.5,
        -12,
        1.2e4
      ]
    }
  ],
  "matrix": [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
  ],
  "mixedArray": [
    null,
    true,
    false,
    123,
    -45.67,
    "hello",
    {
      "nested": [
        {
          "a": 1
        },
        {
          "b": 2
        }
      ]
    },
    [],
    {}
  ],
  "config": {
    "graphics": {
      "width": 1920,
      "height": 1080,
      "fullscreen": false,
      "vsync": true
    },
    "audio": {
      "master": 0.8,
      "music": 0.5,
      "effects": 0.9
    },
    "network": {
      "host": "localhost",
      "port": 8080,
      "timeout": 15.5
    }
  },
  "deep": {
    "level1": {
      "level2": {
        "level3": {
          "level4": {
            "level5": {
              "message": "Reached the bottom!"
            }
          }
        }
      }
    }
  },
  "inventory": [
    {
      "id": "itm001",
      "name": "Sword",
      "stats": {
        "damage": 25,
        "durability": 98
      },
      "enchantments": [
        "Fire",
        "Sharpness"
      ]
    },
    {
      "id": "itm002",
      "name": "Shield",
      "stats": {
        "defense": 40,
        "durability": 100
      },
      "enchantments": []
    },
    {
      "id": "itm003",
      "name": "Potion",
      "stats": null,
      "stack": 15
    }
  ],
  "objectArray": [
    {
      "k": "a",
      "v": 1
    },
    {
      "k": "b",
      "v": 2
    },
    {
      "k": "c",
      "v": {
        "nested": true
      }
    }
  ]
}
""")

start = System.nanoTime()
for _ in range(10000):
  loads(data)
print(f"Average: {(System.nanoTime()-start)/10000}")
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant