Class StringNode

All Implemented Interfaces:
Serializable, Iterable<JsonNode>, TreeNode, JacksonSerializable

public class StringNode extends ValueNode
Value node that contains a String value.
See Also:
  • Field Details

    • _value

      protected final String _value
  • Constructor Details

    • StringNode

      public StringNode(String v)
  • Method Details

    • valueOf

      public static StringNode valueOf(String v)
      Factory method that should be used to construct instances. For some common cases, can reuse canonical instances: currently this is the case for empty Strings, in future possible for others as well. If null is passed, will return null.
      Returns:
      Resulting StringNode object, if v is NOT null; null if it is.
    • getNodeType

      public JsonNodeType getNodeType()
      Description copied from class: JsonNode
      Return the type of this node
      Specified by:
      getNodeType in class JsonNode
      Returns:
      the node type as a JsonNodeType enum value
    • asToken

      public JsonToken asToken()
      Description copied from class: BaseJsonNode
      Method that can be used for efficient type detection when using stream abstraction for traversing nodes. Will return the first JsonToken that equivalent stream event would produce (for most nodes there is just one token but for structured/container types multiple)
      Specified by:
      asToken in interface TreeNode
      Specified by:
      asToken in class ValueNode
    • _valueDesc

      protected String _valueDesc()
      Description copied from class: BaseJsonNode
      Method for implementation classes to return a short description of contained value, to be used in error messages.
      Specified by:
      _valueDesc in class BaseJsonNode
    • deepCopy

      public StringNode deepCopy()
      Description copied from class: ValueNode
      All current value nodes are immutable, so we can just return them as is.
      Overrides:
      deepCopy in class ValueNode
      Returns:
      Node that is either a copy of this node (and all non-leaf children); or, for immutable leaf nodes, node itself.
    • _asBoolean

      public Boolean _asBoolean()
      Description copied from class: BaseJsonNode
      Method sub-classes should override if they can produce boolean values via BaseJsonNode.asBoolean() -- if not, return null (in which case appropriate error will be thrown or default value returned).
      Overrides:
      _asBoolean in class BaseJsonNode
      Returns:
      Coerced value if possible; otherwise null to indicate this node cannot be coerced.
    • stringValue

      public String stringValue()
      Description copied from class: JsonNode
      Method that will try to access value of this node as a Java String which works if (and only if) node contains JSON String or null value: if not, a JsonNodeException will be thrown. In case of JSON null, Java null is returned.

      NOTE: for more conversions, use JsonNode.asString() instead.

      NOTE: in Jackson 2.x, this method was named textValue().

      Overrides:
      stringValue in class BaseJsonNode
      Returns:
      String value this node represents (if JSON String), null for JSON null
    • stringValue

      public String stringValue(String defaultValue)
      Description copied from class: JsonNode
      Method similar to JsonNode.stringValue(), but that will return specified defaultValue if this node does not contain a JSON String. This default value case includes JSON null.
      Overrides:
      stringValue in class BaseJsonNode
      Parameters:
      defaultValue - Value to return if this node does not contain a JSON String.
      Returns:
      Java String value this node represents (if JSON String); defaultValue otherwise -- only returns null if defaultValue is null
    • stringValueOpt

      public Optional<String> stringValueOpt()
      Description copied from class: JsonNode
      Method similar to JsonNode.stringValue(), but that will return Optional.empty() if this node does not contain a JSON String (NOTE: JSON null is not considered a String here)
      Overrides:
      stringValueOpt in class BaseJsonNode
      Returns:
      Optional<String> value (if node represents JSON String); Optional.empty() otherwise (including for JSON null)
    • _asString

      protected String _asString()
      Description copied from class: BaseJsonNode
      Method sub-classes should override if they can produce String values via BaseJsonNode.asString() -- if not, return null (in which case appropriate error will be thrown or default value returned).
      Overrides:
      _asString in class BaseJsonNode
      Returns:
      Coerced value if possible; otherwise null to indicate this node cannot be coerced.
    • asString

      public String asString()
      Description copied from class: JsonNode
      Method that will try to convert value of this node to a String. JSON Strings map naturally; other scalars map to their string representation (including Binary data as Base64 encoded String); JSON nulls map to empty String. Other values (including structured types like Objects and Arrays, and "missing" value) will result in a JsonNodeException being thrown.

      NOTE: this is NOT same as JsonNode.toString() in that result is

      NOT VALID ENCODED JSON

      for all nodes (although is for some, like NumberNodes and BooleanNodes).
      Overrides:
      asString in class BaseJsonNode
      Returns:
      String representation of this node, if coercible; exception otherwise
    • asString

      public String asString(String defaultValue)
      Description copied from class: JsonNode
      Similar to JsonNode.asString(), but instead of throwing an exception for non-coercible values, will return specified default value.
      Overrides:
      asString in class BaseJsonNode
    • asStringOpt

      public Optional<String> asStringOpt()
      Description copied from class: JsonNode
      Similar to JsonNode.asString(), but instead of throwing an exception for non-coercible values, will return Optional.empty().
      Overrides:
      asStringOpt in class BaseJsonNode
    • getBinaryValue

      public byte[] getBinaryValue(Base64Variant b64variant) throws JacksonException
      Method for accessing content String assuming they were base64 encoded; if so, content is decoded and resulting binary data is returned.
      Throws:
      JsonNodeException - if String contents are not valid Base64 encoded content
      JacksonException
    • binaryValue

      public byte[] binaryValue() throws JacksonException
      Description copied from class: JsonNode
      Method that will try to access value of this node as binary value (Java byte[]) which works if (and only if) node contains binary value (for JSON, Base64-encoded String, for other formats native binary value): if not, a JsonNodeException will be thrown. To check if this method can be used, you may call JsonNode.isBinary().

      Overrides:
      binaryValue in class BaseJsonNode
      Returns:
      Binary value this node represents (if node contains binary value)
      Throws:
      JsonNodeException - if node does not contain a Binary value (a
      JacksonException
    • asShort

      public short asShort()
      Description copied from class: JsonNode
      Method similar to JsonNode.shortValue() but in addition to coercing Number values (same as JsonNode.shortValue()), will also try to coerce a couple of additional types (or cases):
      • JSON Floating-point numbers with fractions (ones without fractions are ok for JsonNode.shortValue()) will be truncated to short (if (and only if) they fit in short range).
      • JSON Strings that represent JSON Numbers ("stringified" numbers)
      • JSON Null (converted to 0))
      • POJO nodes that contain Number values
      Overrides:
      asShort in class BaseJsonNode
      Returns:
      short value this node represents, if possible to accurately represent
    • asShort

      public short asShort(short defaultValue)
      Description copied from class: JsonNode
      Method similar to JsonNode.shortValue(), but that will return specified defaultValue if this node cannot be converted to short.
      Overrides:
      asShort in class BaseJsonNode
      Parameters:
      defaultValue - Value to return if this node cannot be converted to short
      Returns:
      short value this node represents, if possible to accurately represent; defaultValue otherwise
    • asShortOpt

      public Optional<Short> asShortOpt()
      Description copied from class: JsonNode
      Method similar to JsonNode.asShort(), but that will return (Optional.empty()) if this node cannot be coerced to short.
      Overrides:
      asShortOpt in class BaseJsonNode
      Returns:
      Optional<Short> value this node represents, if possible to accurately represent; Optional.empty() otherwise
    • asInt

      public int asInt()
      Description copied from class: JsonNode
      Method similar to JsonNode.intValue() but in addition to coercing Number values (same as JsonNode.intValue()), will also try to coerce a couple of additional types (or cases):
      • JSON Floating-point numbers with fractions (ones without fractions are ok for JsonNode.intValue()) will be truncated to int (if (and only if) they fit in int range).
      • JSON Strings that represent JSON Numbers ("stringified" numbers)
      • JSON Null (converted to 0))
      • POJO nodes that contain Number values
      Overrides:
      asInt in class BaseJsonNode
      Returns:
      int value this node represents, if possible to accurately represent
    • asInt

      public int asInt(int defaultValue)
      Description copied from class: JsonNode
      Method similar to JsonNode.intValue(), but that will return specified defaultValue if this node cannot be converted to int.
      Overrides:
      asInt in class BaseJsonNode
      Parameters:
      defaultValue - Value to return if this node cannot be converted to int
      Returns:
      int value this node represents, if possible to accurately represent; defaultValue otherwise
    • asIntOpt

      public OptionalInt asIntOpt()
      Description copied from class: JsonNode
      Method similar to JsonNode.asInt(), but that will return (OptionalInt.empty()) if this node cannot be coerced to int.
      Overrides:
      asIntOpt in class BaseJsonNode
      Returns:
      OptionalInt value this node represents, if possible to accurately represent; OptionalInt.empty() otherwise
    • asLong

      public long asLong()
      Description copied from class: JsonNode
      Method similar to JsonNode.longValue() but in addition to coercing Number values (same as JsonNode.longValue()), will also try to coerce a couple of additional types (or cases):
      • JSON Floating-point numbers with fractions (ones without fractions are ok for JsonNode.longValue()) will be truncated to long (if (and only if) they fit in long range).
      • JSON Strings that represent JSON Numbers ("stringified" numbers)
      • JSON Null (converted to 0))
      • POJO nodes that contain Number values
      Overrides:
      asLong in class BaseJsonNode
      Returns:
      long value this node represents, if possible to accurately represent
    • asLong

      public long asLong(long defaultValue)
      Description copied from class: JsonNode
      Method similar to JsonNode.asLong(), but that will return specified defaultValue if this node cannot be coerced to long (instead of throwing an exception).
      Overrides:
      asLong in class BaseJsonNode
      Parameters:
      defaultValue - Value to return if this node cannot be coerced to long
      Returns:
      long value this node represents, if possible to accurately represent; defaultValue otherwise
    • asLongOpt

      public OptionalLong asLongOpt()
      Description copied from class: JsonNode
      Method similar to JsonNode.asLong(), but that will return (OptionalLong.empty()) if this node cannot be coerced to long.
      Overrides:
      asLongOpt in class BaseJsonNode
      Returns:
      OptionalLong value this node represents (or can be coerced to), OptionalLong.empty() otherwise
    • asBigInteger

      public BigInteger asBigInteger()
      Description copied from class: JsonNode
      Method similar to JsonNode.bigIntegerValue() but in addition to coercing Number values (same as JsonNode.bigIntegerValue()), will also try to coerce a couple of additional types (or cases):
      • JSON Floating-point numbers with fractions (ones without fractions are ok for JsonNode.bigIntegerValue()) will be truncated to integer value (like with BigDecimal.toBigInteger()))
      • JSON Strings that represent JSON Numbers ("stringified" numbers)
      • JSON Null (converted to 0))
      • POJO nodes that contain Number values
      Overrides:
      asBigInteger in class BaseJsonNode
      Returns:
      BigInteger value this node represents, if possible to accurately convert; defaultValue otherwise
    • asBigInteger

      public BigInteger asBigInteger(BigInteger defaultValue)
      Description copied from class: JsonNode
      Method similar to JsonNode.asBigInteger(), but that will return specified defaultValue if this node cannot be converted to BigInteger.
      Overrides:
      asBigInteger in class BaseJsonNode
      Parameters:
      defaultValue - Value to return if this node cannot be converted to BigInteger
      Returns:
      BigInteger value this node represents, if possible to accurately convert; defaultValue otherwise
    • asBigIntegerOpt

      public Optional<BigInteger> asBigIntegerOpt()
      Description copied from class: JsonNode
      Method similar to JsonNode.bigIntegerValue(), but that will return empty (Optional.empty()) if this node cannot be converted to Java BigInteger.
      Overrides:
      asBigIntegerOpt in class BaseJsonNode
      Returns:
      BigInteger value this node represents, as Optional<BigInteger>, if possible to accurately represent; Optional.empty() otherwise.
    • asFloat

      public float asFloat()
      Description copied from class: JsonNode
      Method similar to JsonNode.floatValue() but in addition to coercing Number values will also try coerce couple of additional types:
      • JSON String that represents JSON Numbers ("stringified" numbers)
      • JSON Null (converted to 0.0f)
      • POJO nodes that contain Number values

      Overrides:
      asFloat in class BaseJsonNode
      Returns:
      float value this node represents, if possible to accurately represent
    • asFloat

      public float asFloat(float defaultValue)
      Description copied from class: JsonNode
      Method similar to JsonNode.asFloat(), but that will return defaultValue if this node cannot be coerced to float.
      Overrides:
      asFloat in class BaseJsonNode
      Returns:
      float value this node represents, if possible to accurately represent; defaultValue otherwise
    • asFloatOpt

      public Optional<Float> asFloatOpt()
      Description copied from class: JsonNode
      Method similar to JsonNode.asFloat(), but that will return (Optional.empty()) if this node cannot be coerced to float.
      Overrides:
      asFloatOpt in class BaseJsonNode
      Returns:
      Optional<Float> value this node represents, if possible to accurately represent; Optional.empty() otherwise
    • asDouble

      public double asDouble()
      Description copied from class: JsonNode
      Method similar to JsonNode.doubleValue() but in addition to coercing Number values will also try coerce couple of additional types:
      • JSON String that represents JSON Numbers ("stringified" numbers)
      • JSON Null (converted to 0.0d)
      • POJO nodes that contain Number values

      Overrides:
      asDouble in class BaseJsonNode
      Returns:
      double value this node represents, if possible to accurately represent
    • asDouble

      public double asDouble(double defaultValue)
      Description copied from class: JsonNode
      Method similar to JsonNode.asDouble(), but that will return defaultValue if this node cannot be coerced to double.
      Overrides:
      asDouble in class BaseJsonNode
      Returns:
      double value this node represents, if possible to accurately represent; defaultValue otherwise
    • asDoubleOpt

      public OptionalDouble asDoubleOpt()
      Description copied from class: JsonNode
      Method similar to JsonNode.asDouble(), but that will return (OptionalDouble.empty()) if this node cannot be coerced to double.
      Overrides:
      asDoubleOpt in class BaseJsonNode
      Returns:
      OptionalDouble value this node represents, if possible to accurately represent; OptionalDouble.empty() otherwise
    • asDecimal

      public BigDecimal asDecimal()
      Description copied from class: JsonNode
      Method that will try to access value of this node as a BigDecimal: but if node value cannot be expressed exactly as a BigDecimal, a JsonNodeException will be thrown. Access works for following cases:
      • JSON Floating-point values (but not "NaN" or "Infinity")
      • JSON Integer values
      • JSON String that represents JSON Numbers ("stringified" numbers)
      • JSON Null (converted to BigDecimal.ZERO))
      • POJO nodes that contain Number values

      Overrides:
      asDecimal in class BaseJsonNode
      Returns:
      BigDecimal value this node represents, if possible to accurately represent
    • asDecimal

      public BigDecimal asDecimal(BigDecimal defaultValue)
      Description copied from class: JsonNode
      Method similar to JsonNode.asDecimal(), but that will return defaultValue if this node cannot be coerced to Java BigDecimal.
      Overrides:
      asDecimal in class BaseJsonNode
      Returns:
      BigDecimal value this node represents, if possible to accurately represent; defaultValue otherwise
    • asDecimalOpt

      public Optional<BigDecimal> asDecimalOpt()
      Description copied from class: JsonNode
      Method similar to JsonNode.asDecimal(), but that will return empty Optional (Optional.empty()) if this node cannot be coerced to BigDecimal.
      Overrides:
      asDecimalOpt in class BaseJsonNode
      Returns:
      Java BigDecimal value this node represents, as Optional<BigDecimal>, if possible to accurately represent; Optional.empty() otherwise
    • _tryParseAsShort

      protected Short _tryParseAsShort()
    • _tryParseAsInteger

      protected Integer _tryParseAsInteger()
    • _tryParseAsLong

      protected Long _tryParseAsLong()
    • _tryParseAsBigInteger

      protected BigInteger _tryParseAsBigInteger()
    • _tryParseAsFloat

      protected Float _tryParseAsFloat()
    • _tryParseAsDouble

      protected Double _tryParseAsDouble()
    • _tryParseAsBigDecimal

      protected BigDecimal _tryParseAsBigDecimal()
    • serialize

      public final void serialize(JsonGenerator g, SerializationContext provider) throws JacksonException
      Description copied from class: BaseJsonNode
      Method called to serialize node instances using given generator.
      Specified by:
      serialize in interface JacksonSerializable
      Specified by:
      serialize in class BaseJsonNode
      Throws:
      JacksonException
    • equals

      public boolean equals(Object o)
      Description copied from class: JsonNode
      Equality for node objects is defined as full (deep) value equality. This means that it is possible to compare complete JSON trees for equality by comparing equality of root nodes.

      Note: marked as abstract to ensure all implementation classes define it properly and not rely on definition from Object.

      Specified by:
      equals in class JsonNode
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in class BaseJsonNode