OneConfig V1
  • Welcome
  • Documentation license
  • Introduction
    • Getting started
  • Configuration
    • Creating your config
      • Option dependencies & hiding options
      • Listening for option changes
    • Available options
      • Boolean options
        • Switch option
        • Checkbox option
      • Number options
        • Slider option
        • Number option
      • Selector options
        • Dropdown option
        • Radio button option
      • Text option
      • Color option
      • Keybind option
      • Buttons
      • Accordions
      • Decorations
    • Creating your own options
  • Commands
    • Annotation-based commands
    • Tree-based commands
  • Events
    • Using the events system
      • Callback events
      • Subscriber events
  • Utilities Module
    • Hypixel utilities
    • IO utilities
    • Networking utilities
    • JSON utilities
    • Multithreading
    • Platform-specific utilities
  • Migration
    • Temporary V0 -> V1 migration guide
    • Migrating your configs from Vigilance
Powered by GitBook
On this page
  • Getting JSON elements from a string
  • Unsafe (exceptional) parsing
  • Safe parsing
  • Safe parsing via callbacks
Export as PDF
  1. Utilities Module

JSON utilities

Getting JSON elements from a string

Unsafe (exceptional) parsing

You can unsafely (exceptionally) parse JSON from a string using JsonUtils#parse(String), like so:

JsonElement jsonElement = JsonUtils.parse("{}");

If incorrect JSON syntax is passed to this method, GSON will throw a JsonSyntaxException

Safe parsing

JsonUtils#parseOrNull will automatically catch any exceptions thrown by GSON's parser and return the resulting JsonElement or null if a parsing error was thrown. It can be used like so:

JsonElement jsonElement1 = JsonUtils.parseOrNull("{}"); // non-null, JsonObject
JsonElement jsonElement2 = JsonUtils.parseOrNull("Hello, OneConfig!"); // null

Safe parsing via callbacks

An additional, third method is present for the purpose of running a callback if parsing succeeds on the string given.

JsonUtils.parse("{}", (jsonElement) -> {
    System.out.println("I'll print out if the JSON is parsed properly! " + jsonElement);
});
PreviousNetworking utilitiesNextMultithreading

Last updated 5 months ago