Class JsonUtil

java.lang.Object
dev.metaschema.core.model.util.JsonUtil

public final class JsonUtil extends Object
Provides utility functions to support reading and writing JSON, and for producing error and warning messages.
  • Method Summary

    Modifier and Type
    Method
    Description
    static com.fasterxml.jackson.core.JsonToken
    advanceAndAssert(com.fasterxml.jackson.core.JsonParser parser, URI resource, com.fasterxml.jackson.core.JsonToken expectedToken)
    Advance the token stream, then ensure that the current token is the one expected.
    static com.fasterxml.jackson.core.JsonToken
    advanceTo(com.fasterxml.jackson.core.JsonParser parser, URI resource, com.fasterxml.jackson.core.JsonToken token)
    Advance the parser to the next location matching the provided token.
    static com.fasterxml.jackson.core.JsonToken
    assertAndAdvance(com.fasterxml.jackson.core.JsonParser parser, URI resource, com.fasterxml.jackson.core.JsonToken expectedToken)
    Ensure that the current token is the one expected and then advance the token stream.
    static void
    assertCurrent(com.fasterxml.jackson.core.JsonParser parser, URI resource, com.fasterxml.jackson.core.JsonToken... expectedTokens)
    Ensure that the current token is one of the provided tokens.
    generateLocationMessage(com.fasterxml.jackson.core.JsonLocation location, URI resource)
    Generate a location string for the current location in the JSON token stream.
    generateLocationMessage(com.fasterxml.jackson.core.JsonParser parser, URI resource)
    Generate a location string for the current location in the JSON token stream.
    static com.fasterxml.jackson.core.JsonToken
    skipNextValue(com.fasterxml.jackson.core.JsonParser parser, URI resource)
    Skip the next JSON value in the stream.
    static org.json.JSONObject
    Parse the input stream into a JSON object.
    static org.json.JSONObject
    Parse the reader into a JSON object.
    static String
    toString(com.fasterxml.jackson.core.JsonLocation location)
    Generate an informational string describing the provided location.
    static String
    toString(com.fasterxml.jackson.core.JsonParser parser, URI resource)
    Generate an informational string describing the token at the current location of the provided parser.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • toJsonObject

      @NonNull public static org.json.JSONObject toJsonObject(@NonNull InputStream is)
      Parse the input stream into a JSON object.
      Parameters:
      is - the input stream to parse
      Returns:
      the JSON object
    • toJsonObject

      @NonNull public static org.json.JSONObject toJsonObject(@NonNull Reader reader)
      Parse the reader into a JSON object.
      Parameters:
      reader - the reader to parse
      Returns:
      the JSON object
    • toString

      @NonNull public static String toString(@NonNull com.fasterxml.jackson.core.JsonParser parser, @NonNull URI resource) throws IOException
      Generate an informational string describing the token at the current location of the provided parser.
      Parameters:
      parser - the JSON parser
      resource - the resource being parsed
      Returns:
      the informational string
      Throws:
      IOException - if an error occurred while getting the information from the parser
    • toString

      @NonNull public static String toString(@NonNull com.fasterxml.jackson.core.JsonLocation location)
      Generate an informational string describing the provided location.
      Parameters:
      location - a JSON parser location
      Returns:
      the informational string
    • advanceTo

      @Nullable public static com.fasterxml.jackson.core.JsonToken advanceTo(@NonNull com.fasterxml.jackson.core.JsonParser parser, @NonNull URI resource, @NonNull com.fasterxml.jackson.core.JsonToken token) throws IOException
      Advance the parser to the next location matching the provided token.
      Parameters:
      parser - the JSON parser
      resource - the resource being parsed
      token - the expected token
      Returns:
      the current token or null if no tokens remain in the stream
      Throws:
      IOException - if an error occurred while parsing the JSON
    • skipNextValue

      @Nullable public static com.fasterxml.jackson.core.JsonToken skipNextValue(@NonNull com.fasterxml.jackson.core.JsonParser parser, @NonNull URI resource) throws IOException
      Skip the next JSON value in the stream.
      Parameters:
      parser - the JSON parser
      resource - the resource being parsed
      Returns:
      the current token or null if no tokens remain in the stream
      Throws:
      IOException - if an error occurred while parsing the JSON
    • assertCurrent

      public static void assertCurrent(@NonNull com.fasterxml.jackson.core.JsonParser parser, @NonNull URI resource, @NonNull com.fasterxml.jackson.core.JsonToken... expectedTokens)
      Ensure that the current token is one of the provided tokens.

      Note: This uses a Java assertion to support debugging in a whay that doesn't impact parser performance during production operation.

      Parameters:
      parser - the JSON parser
      resource - the resource being parsed
      expectedTokens - the tokens for which one is expected to match against the current token
    • assertAndAdvance

      @Nullable public static com.fasterxml.jackson.core.JsonToken assertAndAdvance(@NonNull com.fasterxml.jackson.core.JsonParser parser, @NonNull URI resource, @NonNull com.fasterxml.jackson.core.JsonToken expectedToken) throws IOException
      Ensure that the current token is the one expected and then advance the token stream.
      Parameters:
      parser - the JSON parser
      resource - the resource being parsed
      expectedToken - the expected token
      Returns:
      the next token
      Throws:
      IOException - if an error occurred while reading the token stream
    • advanceAndAssert

      @Nullable public static com.fasterxml.jackson.core.JsonToken advanceAndAssert(@NonNull com.fasterxml.jackson.core.JsonParser parser, @NonNull URI resource, @NonNull com.fasterxml.jackson.core.JsonToken expectedToken) throws IOException
      Advance the token stream, then ensure that the current token is the one expected.
      Parameters:
      parser - the JSON parser
      resource - the resource being parsed
      expectedToken - the expected token
      Returns:
      the next token
      Throws:
      IOException - if an error occurred while reading the token stream
    • generateLocationMessage

      @NonNull public static CharSequence generateLocationMessage(@NonNull com.fasterxml.jackson.core.JsonParser parser, @NonNull URI resource)
      Generate a location string for the current location in the JSON token stream.
      Parameters:
      parser - the JSON parser
      resource - the resource being parsed
      Returns:
      the location string
    • generateLocationMessage

      @NonNull public static CharSequence generateLocationMessage(@NonNull com.fasterxml.jackson.core.JsonLocation location, @NonNull URI resource)
      Generate a location string for the current location in the JSON token stream.
      Parameters:
      location - a JSON token stream location
      resource - the resource being parsed
      Returns:
      the location string