Class JSONWriter.Path

java.lang.Object
com.alibaba.fastjson2.JSONWriter.Path
Enclosing class:
JSONWriter

public static final class JSONWriter.Path extends Object
Path represents a JSON pointer path used for reference detection during serialization. It tracks the location of objects within a JSON structure to detect circular references and avoid infinite loops during serialization.

The Path class is used internally by JSONWriter to manage object references and generate JSON Pointer strings as defined in RFC 6901. Paths are hierarchical, with each Path instance containing a reference to its parent Path, forming a tree structure that mirrors the JSON structure being serialized.

Path instances are immutable once created and are used in reference detection to determine if an object has already been serialized at another location in the JSON structure.

Example paths:

  • ROOT path: "$"
  • Property path: \"$.name\"
  • Array element path: \"$.items[0]\"
  • Nested path: \"$.person.address.street\"
Since:
2.0.0
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final JSONWriter.Path
    The manager reference path instance, used for special reference handling.
    The parent path of this path segment, or null if this is the root path.
    static final JSONWriter.Path
    The root path instance, representing the top level of the JSON structure.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Path(JSONWriter.Path parent, int index)
    Creates a new Path instance representing an array index.
    Path(JSONWriter.Path parent, String name)
    Creates a new Path instance representing a named property.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Compares this Path with another object for equality.
    int
    Returns a hash code value for this Path.
    Returns a string representation of this Path in JSON Pointer format.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • ROOT

      public static final JSONWriter.Path ROOT
      The root path instance, representing the top level of the JSON structure. This is the starting point for all path calculations.
    • MANGER_REFERNCE

      public static final JSONWriter.Path MANGER_REFERNCE
      The manager reference path instance, used for special reference handling.
    • parent

      public final JSONWriter.Path parent
      The parent path of this path segment, or null if this is the root path.
  • Constructor Details

    • Path

      public Path(JSONWriter.Path parent, String name)
      Creates a new Path instance representing a named property.
      Parameters:
      parent - the parent path, or null for the root path
      name - the property name for this path segment
    • Path

      public Path(JSONWriter.Path parent, int index)
      Creates a new Path instance representing an array index.
      Parameters:
      parent - the parent path, or null for the root path
      index - the array index for this path segment
  • Method Details

    • equals

      public boolean equals(Object o)
      Compares this Path with another object for equality. Two Path instances are considered equal if they have the same parent, name, and index.
      Overrides:
      equals in class Object
      Parameters:
      o - the object to compare with
      Returns:
      true if the objects are equal, false otherwise
    • hashCode

      public int hashCode()
      Returns a hash code value for this Path.
      Overrides:
      hashCode in class Object
      Returns:
      a hash code value for this Path
    • toString

      public String toString()
      Returns a string representation of this Path in JSON Pointer format.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this Path