Class JsonUtils


  • public class JsonUtils
    extends Object
    • Field Detail

      • DEFAULT_READER

        public static final com.fasterxml.jackson.databind.ObjectReader DEFAULT_READER
      • DEFAULT_WRITER

        public static final com.fasterxml.jackson.databind.ObjectWriter DEFAULT_WRITER
      • DEFAULT_PRETTY_WRITER

        public static final com.fasterxml.jackson.databind.ObjectWriter DEFAULT_PRETTY_WRITER
      • READER_WITH_BIG_DECIMAL

        public static final com.fasterxml.jackson.databind.ObjectReader READER_WITH_BIG_DECIMAL
      • MAP_TYPE_REFERENCE

        public static final com.fasterxml.jackson.core.type.TypeReference<HashMap<String,​Object>> MAP_TYPE_REFERENCE
    • Method Detail

      • stringToObject

        public static <T> T stringToObject​(String jsonString,
                                           Class<T> valueType)
                                    throws com.fasterxml.jackson.core.JsonProcessingException
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException
      • inputStreamToObjectAndUnrecognizedProperties

        public static <T> org.apache.commons.lang3.tuple.Pair<T,​Map<String,​Object>> inputStreamToObjectAndUnrecognizedProperties​(InputStream jsonInputStream,
                                                                                                                                             Class<T> valueType)
                                                                                                                                      throws IOException
        Throws:
        IOException
      • stringToObjectAndUnrecognizedProperties

        public static <T> org.apache.commons.lang3.tuple.Pair<T,​Map<String,​Object>> stringToObjectAndUnrecognizedProperties​(String jsonString,
                                                                                                                                        Class<T> valueType)
                                                                                                                                 throws IOException
        Throws:
        IOException
      • stringToObject

        public static <T> T stringToObject​(String jsonString,
                                           com.fasterxml.jackson.core.type.TypeReference<T> valueTypeRef)
                                    throws IOException
        Throws:
        IOException
      • stringToJsonNode

        public static com.fasterxml.jackson.databind.JsonNode stringToJsonNode​(String jsonString)
                                                                        throws IOException
        Throws:
        IOException
      • stringToJsonNodeWithBigDecimal

        public static com.fasterxml.jackson.databind.JsonNode stringToJsonNodeWithBigDecimal​(String jsonString)
                                                                                      throws IOException
        Throws:
        IOException
      • fileToJsonNode

        public static com.fasterxml.jackson.databind.JsonNode fileToJsonNode​(File jsonFile)
                                                                      throws IOException
        Throws:
        IOException
      • fileToFirstJsonNode

        @Nullable
        public static com.fasterxml.jackson.databind.JsonNode fileToFirstJsonNode​(File jsonFile)
                                                                           throws IOException
        Reads the first json object from the file that can contain multiple objects
        Throws:
        IOException
      • inputStreamToJsonNode

        public static com.fasterxml.jackson.databind.JsonNode inputStreamToJsonNode​(InputStream jsonInputStream)
                                                                             throws IOException
        Throws:
        IOException
      • bytesToJsonNode

        public static com.fasterxml.jackson.databind.JsonNode bytesToJsonNode​(byte[] jsonBytes)
                                                                       throws IOException
        Throws:
        IOException
      • jsonNodeToObject

        public static <T> T jsonNodeToObject​(com.fasterxml.jackson.databind.JsonNode jsonNode,
                                             Class<T> valueType)
                                      throws IOException
        Throws:
        IOException
      • jsonNodeToObject

        public static <T> T jsonNodeToObject​(com.fasterxml.jackson.databind.JsonNode jsonNode,
                                             com.fasterxml.jackson.core.type.TypeReference<T> valueTypeRef)
                                      throws IOException
        Throws:
        IOException
      • objectToString

        public static String objectToString​(Object object)
                                     throws com.fasterxml.jackson.core.JsonProcessingException
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException
      • objectToPrettyString

        public static String objectToPrettyString​(Object object)
                                           throws com.fasterxml.jackson.core.JsonProcessingException
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException
      • objectToBytes

        public static byte[] objectToBytes​(Object object)
                                    throws com.fasterxml.jackson.core.JsonProcessingException
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException
      • objectToJsonNode

        public static com.fasterxml.jackson.databind.JsonNode objectToJsonNode​(Object object)
      • newObjectNode

        public static com.fasterxml.jackson.databind.node.ObjectNode newObjectNode()
      • newArrayNode

        public static com.fasterxml.jackson.databind.node.ArrayNode newArrayNode()
      • extractValue

        public static Object extractValue​(@Nullable
                                          com.fasterxml.jackson.databind.JsonNode jsonValue,
                                          FieldSpec fieldSpec)
      • flatten

        public static List<Map<String,​String>> flatten​(com.fasterxml.jackson.databind.JsonNode node,
                                                             JsonIndexConfig jsonIndexConfig)
        Flattens the given json node.

        Json array will be flattened into multiple records, where each record has a special key to store the index of the element.

         E.g.
         {
           "name": "adam",
           "addresses": [
             {
               "country": "us",
               "street": "main st",
               "number": 1
             },
             {
               "country": "ca",
               "street": "second st",
               "number": 2
             }
           ]
         }
         -->
         [
           {
             ".name": "adam",
             ".addresses.$index": "0",
             ".addresses..country": "us",
             ".addresses..street": "main st",
             ".addresses..number": "1"
           },
           {
             ".name": "adam",
             ".addresses.$index": "1",
             ".addresses..country": "ca",
             ".addresses..street": "second st",
             ".addresses..number": "2"
           }
         ]
         
      • valueOf

        public static FieldSpec.DataType valueOf​(com.fasterxml.jackson.databind.JsonNode jsonNode)
        Returns the data type stored in Pinot that is associated with the given Avro type.