Class JsonSupport

java.lang.Object
com.alibaba.dashscope.transport.JsonSupport

public final class JsonSupport extends Object
Shared Jackson mapper and wire-envelope helpers. Single mapper instance per JVM.
  • Method Summary

    Modifier and Type
    Method
    Description
    static Iterable<com.fasterxml.jackson.databind.JsonNode>
    arrayElements(com.fasterxml.jackson.databind.JsonNode node)
    Shallow iterable over array elements; empty when the node is not an array.
    static String
    errorCode(com.fasterxml.jackson.databind.JsonNode body)
    Wire error code: top-level "code", else nested error.code / error.type.
    static com.fasterxml.jackson.databind.node.ObjectNode
    errorFrame(String code, String message, String requestId)
    Synthetic error frame for failures whose wire body carried no usable error code (e.g. a gateway HTML error page): guarantees raiseOnError(false) responses still report isError() true.
    static String
    errorMessage(com.fasterxml.jackson.databind.JsonNode body)
    Wire error message: top-level "message", else nested error.message.
    static String
    findRequestId(Map<String,?> headers)
    First non-empty value of x-request-id, x-dashscope-requestid or x-dashscope-request-id.
    static com.fasterxml.jackson.databind.node.ObjectNode
    flattenEnvelope(com.fasterxml.jackson.databind.JsonNode body)
    Lift body.output to the top level so public types stay flat (Python v2 flatten_envelope parity).
    static boolean
    isErrorFrame(com.fasterxml.jackson.databind.JsonNode body)
    A frame is an error frame when it carries a top-level "code" or a nested "error" object with code/type/message.
    static com.fasterxml.jackson.databind.ObjectMapper
     
    static com.fasterxml.jackson.databind.JsonNode
     
    static com.fasterxml.jackson.databind.JsonNode
    Parse an error response body, preferring a plain JSON object; falls back to extracting the error frame from SSE-shaped text (older gateways answer streaming requests with SSE frames even on failure).
    static com.fasterxml.jackson.databind.JsonNode
     
    static com.fasterxml.jackson.databind.JsonNode
    readTree(String text, Map<String,?> headers)
     
    static String
    requestId(com.fasterxml.jackson.databind.JsonNode body, Map<String,?> headers)
    request_id from the body, else the first known request id header.
    static String
     
    static Object
    toPlainValue(com.fasterxml.jackson.databind.JsonNode node)
    Convert a JsonNode to plain Java values (Map/List/scalars) for exception bodies.

    Methods inherited from class java.lang.Object

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

    • mapper

      public static com.fasterxml.jackson.databind.ObjectMapper mapper()
    • readTree

      public static com.fasterxml.jackson.databind.JsonNode readTree(String text)
    • readTree

      public static com.fasterxml.jackson.databind.JsonNode readTree(String text, Map<String,?> headers)
    • parseErrorBody

      public static com.fasterxml.jackson.databind.JsonNode parseErrorBody(String text)
      Parse an error response body, preferring a plain JSON object; falls back to extracting the error frame from SSE-shaped text (older gateways answer streaming requests with SSE frames even on failure). Never throws: unparseable bodies yield null.
    • isErrorFrame

      public static boolean isErrorFrame(com.fasterxml.jackson.databind.JsonNode body)
      A frame is an error frame when it carries a top-level "code" or a nested "error" object with code/type/message.
    • errorCode

      public static String errorCode(com.fasterxml.jackson.databind.JsonNode body)
      Wire error code: top-level "code", else nested error.code / error.type.
    • errorMessage

      public static String errorMessage(com.fasterxml.jackson.databind.JsonNode body)
      Wire error message: top-level "message", else nested error.message.
    • requestId

      public static String requestId(com.fasterxml.jackson.databind.JsonNode body, Map<String,?> headers)
      request_id from the body, else the first known request id header.
    • findRequestId

      public static String findRequestId(Map<String,?> headers)
      First non-empty value of x-request-id, x-dashscope-requestid or x-dashscope-request-id.
    • requestIdOrUnknown

      public static String requestIdOrUnknown(String requestId)
    • toPlainValue

      public static Object toPlainValue(com.fasterxml.jackson.databind.JsonNode node)
      Convert a JsonNode to plain Java values (Map/List/scalars) for exception bodies.
    • flattenEnvelope

      public static com.fasterxml.jackson.databind.node.ObjectNode flattenEnvelope(com.fasterxml.jackson.databind.JsonNode body)
      Lift body.output to the top level so public types stay flat (Python v2 flatten_envelope parity). Envelope keys win — how request_id/code/ message reach the flattened node; null envelope values are dropped so an absent field cannot overwrite a real payload value.
    • errorFrame

      public static com.fasterxml.jackson.databind.node.ObjectNode errorFrame(String code, String message, String requestId)
      Synthetic error frame for failures whose wire body carried no usable error code (e.g. a gateway HTML error page): guarantees raiseOnError(false) responses still report isError() true.
    • missingNode

      public static com.fasterxml.jackson.databind.JsonNode missingNode()
    • arrayElements

      public static Iterable<com.fasterxml.jackson.databind.JsonNode> arrayElements(com.fasterxml.jackson.databind.JsonNode node)
      Shallow iterable over array elements; empty when the node is not an array.