Interface JsonString


public interface JsonString

A JSON string in a representation that the model chooses.

JsonModel.stringValue(Object) returns a JsonString. The evaluator reads a string only from start to end, one Unicode scalar value at a time: to compare strings, to count them for length(), and to match regexps. So a model over UTF-8 bytes can give its strings without a copy.

For a Java String, use of(String). It does not copy the string:

JsonString name = JsonString.of("Wilhelm");
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    Compares two strings by their Unicode scalar values (RFC 9535, Section 2.3.5.2.2).
    static String
    Returns the value of a string as a Java String.
    static boolean
    Returns true if two strings have the same scalar values.
    static int
    hash(JsonString string)
    Returns a hash code of the scalar values of a string.
    static int
    Returns the number of Unicode scalar values in a string, as length() requires (RFC 9535, Section 2.4.4).
    static JsonString
    of(String value)
    Returns a string with the value of a Java String, without a copy.
    Returns the Unicode scalar values of the string, in order.
  • Method Details

    • scalarValues

      PrimitiveIterator.OfInt scalarValues()

      Returns the Unicode scalar values of the string, in order.

      Each call returns a new iterator that starts at the first scalar value.

      Returns:
      the scalar values
    • of

      static JsonString of(String value)
      Returns a string with the value of a Java String, without a copy.
      Parameters:
      value - the value
      Returns:
      the string
    • copyOf

      static String copyOf(JsonString string)

      Returns the value of a string as a Java String.

      For a string from of(String), this returns the same String. For other strings, it makes a copy.

      Parameters:
      string - a string
      Returns:
      the value as a String
    • compare

      static int compare(JsonString a, JsonString b)

      Compares two strings by their Unicode scalar values (RFC 9535, Section 2.3.5.2.2).

      This is not the order of String.compareTo(String), which compares UTF-16 code units.

      Parameters:
      a - a string
      b - another string
      Returns:
      a negative value, zero, or a positive value if a is less than, equal to, or greater than b
    • equal

      static boolean equal(JsonString a, JsonString b)
      Returns true if two strings have the same scalar values.
      Parameters:
      a - a string
      b - another string
      Returns:
      true if the strings are equal
    • hash

      static int hash(JsonString string)

      Returns a hash code of the scalar values of a string.

      The result is the same as String.hashCode() of copyOf(JsonString), but it makes no copy. So equal strings in different representations have the same hash code.

      Parameters:
      string - a string
      Returns:
      the hash code
    • length

      static int length(JsonString string)
      Returns the number of Unicode scalar values in a string, as length() requires (RFC 9535, Section 2.4.4).
      Parameters:
      string - a string
      Returns:
      the number of scalar values