congrats Icon
New! Announcing Tabnine Chat Beta
Learn More
Tabnine Logo
GsonBuilder.registerTypeHierarchyAdapter
Code IndexAdd Tabnine to your IDE (free)

How to use
registerTypeHierarchyAdapter
method
in
com.google.gson.GsonBuilder

Best Java code snippets using com.google.gson.GsonBuilder.registerTypeHierarchyAdapter (Showing top 20 results out of 513)

origin: spring-projects/spring-framework

/**
 * Obtain a {@link GsonBuilder} which Base64-encodes {@code byte[]}
 * properties when reading and writing JSON.
 * <p>A custom {@link com.google.gson.TypeAdapter} will be registered via
 * {@link GsonBuilder#registerTypeHierarchyAdapter(Class, Object)} which
 * serializes a {@code byte[]} property to and from a Base64-encoded String
 * instead of a JSON array.
 */
public static GsonBuilder gsonBuilderWithBase64EncodedByteArrays() {
  GsonBuilder builder = new GsonBuilder();
  builder.registerTypeHierarchyAdapter(byte[].class, new Base64TypeAdapter());
  return builder;
}
origin: org.springframework/spring-web

/**
 * Obtain a {@link GsonBuilder} which Base64-encodes {@code byte[]}
 * properties when reading and writing JSON.
 * <p>A custom {@link com.google.gson.TypeAdapter} will be registered via
 * {@link GsonBuilder#registerTypeHierarchyAdapter(Class, Object)} which
 * serializes a {@code byte[]} property to and from a Base64-encoded String
 * instead of a JSON array.
 */
public static GsonBuilder gsonBuilderWithBase64EncodedByteArrays() {
  GsonBuilder builder = new GsonBuilder();
  builder.registerTypeHierarchyAdapter(byte[].class, new Base64TypeAdapter());
  return builder;
}
origin: MovingBlocks/Terasology

private void initGson() {
  if (gson == null) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeHierarchyAdapter(BehaviorNode.class, this);
    gsonBuilder.registerTypeAdapterFactory(new UriTypeAdapterFactory());
    gsonBuilder.registerTypeAdapter(BehaviorTree.class, new TypeAdapter<BehaviorTree>() {
      @Override
      public void write(JsonWriter out, BehaviorTree value) throws IOException {
        if (value != null) {
          // TODO doublecheck URN
          out.value(value.getUrn().toString());
        } else {
          out.value("");
        }
      }
      @Override
      public BehaviorTree read(JsonReader in) throws IOException {
        String uri = in.nextString();
        AssetManager assetManager = CoreRegistry.get(AssetManager.class);
        return assetManager.getAsset(new ResourceUrn(uri), BehaviorTree.class)
            .orElse(assetManager.getAsset(new ResourceUrn("Behaviors:fallback"), BehaviorTree.class).get());
      }
    });
    gson = gsonBuilder.create();
  }
}
origin: gocd/gocd

  private static Gson gsonBuilder(final GoRequestContext requestContext) {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(JsonUrl.class, (JsonSerializer<JsonUrl>) (src, typeOfSrc, context) -> {
      if (requestContext == null) {
        return new JsonPrimitive(src.getUrl());
      } else {
        return new JsonPrimitive(requestContext.getFullRequestPath() + src.getUrl());
      }
    });

    builder.registerTypeHierarchyAdapter(MessageSourceResolvable.class, (JsonSerializer<MessageSourceResolvable>) (src, typeOfSrc, context) -> {
      if (requestContext == null) {
        return new JsonPrimitive(src.getDefaultMessage());
      } else {
        return new JsonPrimitive(requestContext.getMessage(src));
      }
    });

    builder.serializeNulls();
    return builder.create();
  }
}
origin: MovingBlocks/Terasology

public UIData load(JsonElement element, Locale otherLocale) throws IOException {
  NUIManager nuiManager = CoreRegistry.get(NUIManager.class);
  TranslationSystem translationSystem = CoreRegistry.get(TranslationSystem.class);
  TypeSerializationLibrary library = new TypeSerializationLibrary(CoreRegistry.get(TypeSerializationLibrary.class));
  library.add(UISkin.class, new AssetTypeHandler<>(UISkin.class));
  library.add(Border.class, new BorderTypeHandler());
  GsonBuilder gsonBuilder = new GsonBuilder()
      .registerTypeAdapterFactory(new GsonTypeSerializationLibraryAdapterFactory(library))
      .registerTypeAdapterFactory(new CaseInsensitiveEnumTypeAdapterFactory())
      .registerTypeAdapter(UIData.class, new UIDataTypeAdapter())
      .registerTypeHierarchyAdapter(UIWidget.class, new UIWidgetTypeAdapter(nuiManager));
  // override the String TypeAdapter from the serialization library
  gsonBuilder.registerTypeAdapter(String.class, new I18nStringTypeAdapter(translationSystem, otherLocale));
  Gson gson = gsonBuilder.create();
  return gson.fromJson(element, UIData.class);
}
origin: apache/tika

static Gson prettyInit() {
  GsonBuilder builder = new GsonBuilder();
  builder.registerTypeHierarchyAdapter(Metadata.class, new SortedJsonMetadataSerializer());
  builder.registerTypeHierarchyAdapter(Metadata.class, new JsonMetadataDeserializer());
  builder.setPrettyPrinting();
  return builder.create();
}
origin: apache/tika

static Gson defaultInit() {
  GsonBuilder builder = new GsonBuilder();
  builder.registerTypeHierarchyAdapter(Metadata.class, new JsonMetadataSerializer());
  builder.registerTypeHierarchyAdapter(Metadata.class, new JsonMetadataDeserializer());
  return builder.create();
}
origin: apache/accumulo

static Gson createGson() {
 return new GsonBuilder()
   .registerTypeHierarchyAdapter(byte[].class, new ByteArrayToBase64TypeAdapter()).create();
}
origin: apache/tika

public static AnalyzerManager newInstance(int maxTokens) throws IOException {
  InputStream is = AnalyzerManager.class.getClassLoader().getResourceAsStream("lucene-analyzers.json");
  Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8);
  GsonBuilder builder = new GsonBuilder();
  builder.registerTypeHierarchyAdapter(Map.class, new AnalyzerDeserializer(maxTokens));
  Gson gson = builder.create();
  Map<String, Analyzer> map = gson.fromJson(reader, Map.class);
  Analyzer general = map.get(GENERAL);
  Analyzer alphaIdeo = map.get(ALPHA_IDEOGRAPH);
  Analyzer common = map.get(COMMON_TOKENS);
  if (general == null) {
    throw new JsonParseException("Must specify "+GENERAL + " analyzer");
  }
  if (common == null) {
    throw new JsonParseException("Must specify "+ COMMON_TOKENS + " analyzer");
  }
  return new AnalyzerManager(general, common);
}
origin: Dromara/soul

  /**
   * To object map map.
   *
   * @param json the json
   * @return the map
   */
  public Map<String, Object> toObjectMap(final String json) {
    TypeToken typeToken = new TypeToken<Map<String, Object>>() {
    };
    Gson gson = new GsonBuilder().serializeNulls().registerTypeHierarchyAdapter(typeToken.getRawType(), new MapDeserializer<String, Object>()).create();
    return gson.fromJson(json, typeToken.getType());
  }
}
origin: javers/javers

/**
 * @since 3.1
 * @see JsonSerializer
 */
public JsonConverterBuilder registerNativeGsonHierarchySerializer(Class targetType, JsonSerializer<?> jsonSerializer) {
  Validate.argumentsAreNotNull(targetType, jsonSerializer);
  gsonBuilder.registerTypeHierarchyAdapter(targetType, jsonSerializer);
  return this;
}
origin: caelum/vraptor

protected Gson getGson() {
  GsonBuilder builder = new GsonBuilder();
  for (JsonDeserializer<?> adapter : adapters) {
    builder.registerTypeHierarchyAdapter(getAdapterType(adapter), adapter);
  }
  return builder.create();
}
origin: com.haulmont.cuba/cuba-global

protected Gson createGsonForDeserialization(@Nullable MetaClass metaClass, EntitySerializationOption... options) {
  return new GsonBuilder()
      .registerTypeHierarchyAdapter(Entity.class, new EntityDeserializer(metaClass, options))
      .registerTypeHierarchyAdapter(Date.class, new DateDeserializer())
      .create();
}
origin: com.wavefront/proxy

private MapSettings loadSettings(File file) throws IOException {
 Gson gson = new GsonBuilder().
   registerTypeHierarchyAdapter(Class.class, new MapSettings.ClassNameSerializer()).create();
 Reader br = new BufferedReader(new FileReader(file));
 return gson.fromJson(br, MapSettings.class);
}
origin: com.wavefront/proxy

private void saveSettings(MapSettings settings, File file) throws IOException {
 Gson gson = new GsonBuilder().
   registerTypeHierarchyAdapter(Class.class, new MapSettings.ClassNameSerializer()).create();
 Writer writer = new FileWriter(file);
 gson.toJson(settings, writer);
 writer.close();
}
origin: com.musala.atmosphere/atmosphere-commons

public GsonUtil() {
  gsonBuilder = new GsonBuilder().enableComplexMapKeySerialization();
  gsonBuilder.registerTypeHierarchyAdapter(Pack.class, new PackDeserializer());
  gsonBuilder.registerTypeHierarchyAdapter(UiElementSelector.class, new UiElementSelectorDeserializer());
  gsonBuilder.registerTypeHierarchyAdapter(RequestMessage.class, new RequestMessageSerializer());
  gsonBuilder.registerTypeHierarchyAdapter(RequestMessage.class, new RequestMessageDeserializer());
  gsonBuilder.registerTypeHierarchyAdapter(ResponseMessage.class, new ResponseMessageSerializer());
  gsonBuilder.registerTypeHierarchyAdapter(ResponseMessage.class, new ResponseMessageDeserializer());
  g = gsonBuilder.create();
}
origin: tomahawk-player/tomahawk-android

public static com.google.gson.Gson get() {
  if (mGson == null) {
    mGson = new GsonBuilder()
        .registerTypeHierarchyAdapter(Collection.class, new CollectionAdapter())
        .registerTypeAdapter(Date.class, new ISO8601DateFormat())
        .create();
  }
  return mGson;
}
origin: net.serenity-bdd/serenity-model

public FileSystemRequirementsStore(File outputDirectory, String storeName) {
  this.outputDirectory = outputDirectory;
  this.storeName = storeName;
  this.gson = new GsonBuilder()
      .registerTypeAdapterFactory(OptionalTypeAdapter.FACTORY)
      .registerTypeHierarchyAdapter(Collection.class, new CollectionAdapter()).create();
}
origin: caelum/vraptor

public Gson create() {
  for (JsonSerializer<?> adapter : serializers) {
    builder.registerTypeHierarchyAdapter(getAdapterType(adapter), adapter);
  }
  for (ExclusionStrategy exclusion : exclusions) {
    builder.addSerializationExclusionStrategy(exclusion);
  }
  return builder.create();
}
origin: caelum/vraptor4

protected void registerAdapter(Class<?> adapterType, Object adapter) {
  RegisterStrategy registerStrategy = adapter.getClass().getAnnotation(RegisterStrategy.class);
  if ((registerStrategy != null) && (registerStrategy.value().equals(RegisterType.SINGLE))) {
    getGsonBuilder().registerTypeAdapter(adapterType, adapter);
  } else {
    getGsonBuilder().registerTypeHierarchyAdapter(adapterType, adapter);
  }    
}

com.google.gsonGsonBuilderregisterTypeHierarchyAdapter

Javadoc

Configures Gson for custom serialization or deserialization for an inheritance type hierarchy. This method combines the registration of a TypeAdapter, JsonSerializer and a JsonDeserializer. If a type adapter was previously registered for the specified type hierarchy, it is overridden. If a type adapter is registered for a specific type in the type hierarchy, it will be invoked instead of the one registered for the type hierarchy.

Popular methods of GsonBuilder

  • create
    Creates a Gson instance based on the current configuration. This method is free of side-effects to t
  • <init>
    Constructs a GsonBuilder instance from a Gson instance. The newly constructed GsonBuilder has the sa
  • registerTypeAdapter
  • setPrettyPrinting
    Configures Gson to output Json that fits in a page for pretty printing. This option only affects Jso
  • serializeNulls
    Configure Gson to serialize null fields. By default, Gson omits all fields that are null during seri
  • disableHtmlEscaping
    By default, Gson escapes HTML characters such as < > etc. Use this option to configure Gson to pass-
  • setDateFormat
    Configures Gson to serialize Date objects according to the pattern provided. You can call this metho
  • registerTypeAdapterFactory
    Register a factory for type adapters. Registering a factory is useful when the type adapter needs to
  • setFieldNamingPolicy
    Configures Gson to apply a specific naming policy to an object's field during serialization and dese
  • excludeFieldsWithoutExposeAnnotation
    Configures Gson to exclude all fields from consideration for serialization or deserialization that d
  • setExclusionStrategies
    Configures Gson to apply a set of exclusion strategies during both serialization and deserialization
  • enableComplexMapKeySerialization
    Enabling this feature will only change the serialized form if the map key is a complex type (i.e. no
  • setExclusionStrategies,
  • enableComplexMapKeySerialization,
  • setLenient,
  • addSerializationExclusionStrategy,
  • excludeFieldsWithModifiers,
  • serializeSpecialFloatingPointValues,
  • setVersion,
  • setFieldNamingStrategy,
  • addTypeAdaptersForDate

Popular in Java

  • Making http requests using okhttp
  • onCreateOptionsMenu (Activity)
  • getSystemService (Context)
  • getContentResolver (Context)
  • Color (java.awt)
    The Color class is used to encapsulate colors in the default sRGB color space or colors in arbitrary
  • BufferedWriter (java.io)
    Wraps an existing Writer and buffers the output. Expensive interaction with the underlying reader is
  • Stack (java.util)
    Stack is a Last-In/First-Out(LIFO) data structure which represents a stack of objects. It enables u
  • Executor (java.util.concurrent)
    An object that executes submitted Runnable tasks. This interface provides a way of decoupling task s
  • AtomicInteger (java.util.concurrent.atomic)
    An int value that may be updated atomically. See the java.util.concurrent.atomic package specificati
  • JFrame (javax.swing)
  • Best IntelliJ plugins
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now