Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2014 Spotify AB.
   *
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
  *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 
 package com.spotify.helios.common;
 
 
 
 import java.io.File;
 import java.util.Map;
 
 import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
 import static com.fasterxml.jackson.databind.MapperFeature.SORT_PROPERTIES_ALPHABETICALLY;
 import static com.fasterxml.jackson.databind.SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS;
 import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS;
 
 public class Json {
 
   private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
       .configure(true)
       .configure(false)
       .configure(false);
 
   private static final ObjectWriter NORMALIZING_OBJECT_WRITER = new ObjectMapper()
       .configure(true)
       .configure(false)
       .writer();
 
   private static final ObjectWriter PRETTY_OBJECT_WRITER = new ObjectMapper()
       .configure(true)
       .configure(false)
 
   private static final TypeReference<Map<StringObject>> MAP_TYPE =
       new TypeReference<Map<StringObject>>() {};

  
Serialize an object to json. Use when it is not know whether an object can be json serializable.

Parameters:
value The object to serialize.
Returns:
The byte array for the given object.
Throws:
com.fasterxml.jackson.core.JsonProcessingException If the json cannot be generated.
See also:
asBytesUnchecked(java.lang.Object)
 
   public static byte[] asBytes(final Object valuethrows JsonProcessingException {
     return .writeValueAsBytes(value);
   }

  
Serialize an object to json. Use when object is expected to be json serializable.

Parameters:
value The object to serialize.
Returns:
The byte array for the given object.
See also:
asBytes(java.lang.Object)
 
   public static byte[] asBytesUnchecked(final Object value) {
     try {
       return .writeValueAsBytes(value);
     } catch (JsonProcessingException e) {
       throw Throwables.propagate(e);
     }
   }

  
Serialize an object to a json string. Use when it is not know whether an object can be json serializable.

Parameters:
value The object to serialize.
Returns:
The serialized object.
Throws:
com.fasterxml.jackson.core.JsonProcessingException If the json cannot be generated.
See also:
asStringUnchecked(java.lang.Object)
  public static String asString(final Object valuethrows JsonProcessingException {
    return .writeValueAsString(value);
  }

  
Serialize an object to a json string. Use when object is expected to be json serializable.

Parameters:
value The object to serialize.
Returns:
The serialized object.
See also:
asString(java.lang.Object)
  public static String asStringUnchecked(final Object value) {
    try {
      return asString(value);
    } catch (JsonProcessingException e) {
      throw Throwables.propagate(e);
    }
  }

  
Serialize an object to a json string. Use when it is not know whether an object can be json serializable.

Parameters:
value The object to serialize.
Returns:
The serialized object.
Throws:
com.fasterxml.jackson.core.JsonProcessingException If the json cannot be generated.
See also:
asPrettyStringUnchecked(java.lang.Object)
  public static String asPrettyString(final Object valuethrows JsonProcessingException {
  }

  
Serialize an object to a json string. Use when object is expected to be json serializable.

Parameters:
value The object to serialize.
Returns:
The serialized object.
See also:
asPrettyString(java.lang.Object)
  public static String asPrettyStringUnchecked(final Object value) {
    try {
      return asPrettyString(value);
    } catch (JsonProcessingException e) {
      throw Throwables.propagate(e);
    }
  }

  
Serialize an object to a json string, ordering fields and omitting null and empty fields. Use when it is not know whether an object can be json serializable.

Parameters:
value The object to serialize.
Returns:
The serialized object.
Throws:
com.fasterxml.jackson.core.JsonProcessingException If the json cannot be generated.
See also:
asPrettyStringUnchecked(java.lang.Object)
  public static String asNormalizedString(final Object valuethrows JsonProcessingException {
  }

  
Serialize an object to a json string, ordering fields and omitting null and empty fields. Use when object is expected to be json serializable.

Parameters:
value The object to serialize.
Returns:
The serialized object.
See also:
asPrettyString(java.lang.Object)
  public static String asNormalizedStringUnchecked(final Object value) {
    try {
      return asNormalizedString(value);
    } catch (JsonProcessingException e) {
      throw Throwables.propagate(e);
    }
  }
  public static <T> T read(final String contentfinal Class<T> clazzthrows IOException {
    return .readValue(contentclazz);
  }
  public static <T> T read(final String contentfinal TypeReference<?> typeReference)
      throws IOException {
    return .readValue(contenttypeReference);
  }
  public static <T> T read(final String contentfinal JavaType javaType)
      throws IOException {
    return .readValue(contentjavaType);
  }
  public static <T> T read(final byte[] bytesfinal Class<T> clazzthrows IOException {
    return .readValue(bytesclazz);
  }
  public static <T> T read(final byte[] bytesfinal TypeReference<?> typeReference)
      throws IOException {
    return .readValue(bytestypeReference);
  }
  public static <T> T read(final byte[] bytesfinal JavaType javaType)
      throws IOException {
    return .readValue(bytesjavaType);
  }
  public static <T> T readUnchecked(final String contentfinal Class<T> clazz) {
    try {
      return .readValue(contentclazz);
    } catch (IOException e) {
      throw Throwables.propagate(e);
    }
  }
  public static <T> T readUnchecked(final String contentfinal TypeReference<?> typeReference) {
    try {
      return .readValue(contenttypeReference);
    } catch (IOException e) {
      throw Throwables.propagate(e);
    }
  }
  public static <T> T readUnchecked(final String contentfinal JavaType javaType) {
    try {
      return .readValue(contentjavaType);
    } catch (IOException e) {
      throw Throwables.propagate(e);
    }
  }
  public static <T> T readUnchecked(final byte[] bytesfinal Class<T> clazz) {
    try {
      return .readValue(bytesclazz);
    } catch (IOException e) {
      throw Throwables.propagate(e);
    }
  }
  public static <T> T readUnchecked(final byte[] bytesfinal TypeReference<?> typeReference) {
    try {
      return .readValue(bytestypeReference);
    } catch (IOException e) {
      throw Throwables.propagate(e);
    }
  }
  public static <T> T readUnchecked(final byte[] bytesfinal JavaType javaType) {
    try {
      return .readValue(bytesjavaType);
    } catch (IOException e) {
      throw Throwables.propagate(e);
    }
  }
  public static MappingIterator<Map<StringObject>> readValues(
      final InputStream streamfinal TypeReference<Map<StringObject>> typeReference)
      throws IOException {
    final JsonParser parser = .getFactory().createParser(stream);
    return .readValues(parsertypeReference);
  }
  public static MappingIterator<JsonNodereadValues(
      final InputStream stream)
      throws IOException {
    final JsonParser parser = .getFactory().createParser(stream);
    return .readValues(parserJsonNode.class);
  }
  public static JsonNode readTree(final byte[] bytesthrows IOException {
    return .readTree(bytes);
  }
  public static JsonNode readTreeUnchecked(final byte[] bytes) {
    try {
      return readTree(bytes);
    } catch (IOException e) {
      throw Throwables.propagate(e);
    }
  }
  public static JsonNode readTree(final String contentthrows IOException {
    return .readTree(content);
  }
  public static JsonNode readTreeUnchecked(final String content) {
    try {
      return readTree(content);
    } catch (IOException e) {
      throw Throwables.propagate(e);
    }
  }
  public static JsonNode readTree(final File filethrows IOException {
    return .readTree(file);
  }
  public static JsonNode readTreeUnchecked(final File file) {
    try {
      return readTree(file);
    } catch (IOException e) {
      throw Throwables.propagate(e);
    }
  }
  public static JavaType type(Type t) {
    return .constructType(t);
  }
  public static JavaType type(final TypeReference<?> typeReference) {
    return .getTypeFactory().constructType(typeReference);
  }
  public static TypeFactory typeFactory() {
    return .getTypeFactory();
  }
  public static ObjectReader reader() {
    return .reader();
  }
  public static ObjectWriter writer() {
    return .writer();
  }
  public static byte[] sha1digest(final Object othrows IOException {
    final Map<StringObjectmap = .readValue(json);
    return sha1digest(map);
  }
  public static byte[] sha1digest(final Map<String, ?> othrows IOException {
    final byte[] bytes = .writeValueAsBytes(o);
    return Hash.sha1digest(bytes);
  }
New to GrepCode? Check out our FAQ X