Package play.libs

Class Json

java.lang.Object
play.libs.Json

public class Json extends Object
Helper functions to handle JsonNode values.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    asciiStringify(com.fasterxml.jackson.databind.JsonNode json)
    Converts a JsonNode to its string representation, escaping non-ascii characters.
    static <A> A
    fromJson(com.fasterxml.jackson.databind.JsonNode json, Class<A> clazz)
    Converts a JsonNode to a Java value
    static com.fasterxml.jackson.databind.ObjectMapper
    Gets the ObjectMapper used to serialize and deserialize objects to and from JSON values.
    static com.fasterxml.jackson.databind.node.ArrayNode
    Creates a new empty ArrayNode.
    static com.fasterxml.jackson.databind.ObjectMapper
    Deprecated.
    Deprecated as of 2.8.0.
    static com.fasterxml.jackson.databind.node.ObjectNode
    Creates a new empty ObjectNode.
    static com.fasterxml.jackson.databind.JsonNode
    parse(byte[] src)
    Parses a byte array representing a json, and return it as a JsonNode.
    static com.fasterxml.jackson.databind.JsonNode
    Parses a InputStream representing a json, and return it as a JsonNode.
    static com.fasterxml.jackson.databind.JsonNode
    Parses a String representing a json, and return it as a JsonNode.
    static String
    prettyPrint(com.fasterxml.jackson.databind.JsonNode json)
    Converts a JsonNode to its string representation.
    static void
    setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper mapper)
    Inject the object mapper to use.
    static String
    stringify(com.fasterxml.jackson.databind.JsonNode json)
    Converts a JsonNode to its string representation.
    static com.fasterxml.jackson.databind.JsonNode
    toJson(Object data)
    Converts an object to JsonNode.

    Methods inherited from class java.lang.Object

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

    • Json

      public Json()
  • Method Details

    • newDefaultMapper

      @Deprecated public static com.fasterxml.jackson.databind.ObjectMapper newDefaultMapper()
      Deprecated.
      Deprecated as of 2.8.0. Inject an ObjectMapper instead.
      Creates an ObjectMapper with the default configuration for Play.
      Returns:
      an ObjectMapper with some modules enabled.
    • mapper

      public static com.fasterxml.jackson.databind.ObjectMapper mapper()
      Gets the ObjectMapper used to serialize and deserialize objects to and from JSON values.

      This can be set to a custom implementation using Json.setObjectMapper.

      Returns:
      the ObjectMapper currently being used
    • toJson

      public static com.fasterxml.jackson.databind.JsonNode toJson(Object data)
      Converts an object to JsonNode.
      Parameters:
      data - Value to convert in Json.
      Returns:
      the JSON node.
    • fromJson

      public static <A> A fromJson(com.fasterxml.jackson.databind.JsonNode json, Class<A> clazz)
      Converts a JsonNode to a Java value
      Type Parameters:
      A - the type of the return value.
      Parameters:
      json - Json value to convert.
      clazz - Expected Java value type.
      Returns:
      the return value.
    • newObject

      public static com.fasterxml.jackson.databind.node.ObjectNode newObject()
      Creates a new empty ObjectNode.
      Returns:
      new empty ObjectNode.
    • newArray

      public static com.fasterxml.jackson.databind.node.ArrayNode newArray()
      Creates a new empty ArrayNode.
      Returns:
      a new empty ArrayNode.
    • stringify

      public static String stringify(com.fasterxml.jackson.databind.JsonNode json)
      Converts a JsonNode to its string representation.
      Parameters:
      json - the JSON node to convert.
      Returns:
      the string representation.
    • asciiStringify

      public static String asciiStringify(com.fasterxml.jackson.databind.JsonNode json)
      Converts a JsonNode to its string representation, escaping non-ascii characters.
      Parameters:
      json - the JSON node to convert.
      Returns:
      the string representation with escaped non-ascii characters.
    • prettyPrint

      public static String prettyPrint(com.fasterxml.jackson.databind.JsonNode json)
      Converts a JsonNode to its string representation.
      Parameters:
      json - the JSON node to convert.
      Returns:
      the string representation, pretty printed.
    • parse

      public static com.fasterxml.jackson.databind.JsonNode parse(String src)
      Parses a String representing a json, and return it as a JsonNode.
      Parameters:
      src - the JSON string.
      Returns:
      the JSON node.
    • parse

      public static com.fasterxml.jackson.databind.JsonNode parse(InputStream src)
      Parses a InputStream representing a json, and return it as a JsonNode.
      Parameters:
      src - the JSON input stream.
      Returns:
      the JSON node.
    • parse

      public static com.fasterxml.jackson.databind.JsonNode parse(byte[] src)
      Parses a byte array representing a json, and return it as a JsonNode.
      Parameters:
      src - the JSON input bytes.
      Returns:
      the JSON node.
    • setObjectMapper

      public static void setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper mapper)
      Inject the object mapper to use.

      This is intended to be used when Play starts up. By default, Play will inject its own object mapper here, but this mapper can be overridden either by a custom module.

      Parameters:
      mapper - the object mapper.