Java Code Examples for com.google.gson.GsonBuilder#registerTypeAdapterFactory()

The following examples show how to use com.google.gson.GsonBuilder#registerTypeAdapterFactory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Feature.java    From mapbox-java with MIT License 6 votes vote down vote up
/**
 * Create a new instance of this class by passing in a formatted valid JSON String. If you are
 * creating a Feature object from scratch it is better to use one of the other provided static
 * factory methods such as {@link #fromGeometry(Geometry)}.
 *
 * @param json a formatted valid JSON string defining a GeoJson Feature
 * @return a new instance of this class defined by the values passed inside this static factory
 *   method
 * @since 1.0.0
 */
public static Feature fromJson(@NonNull String json) {

  GsonBuilder gson = new GsonBuilder();
  gson.registerTypeAdapterFactory(GeoJsonAdapterFactory.create());
  gson.registerTypeAdapterFactory(GeometryAdapterFactory.create());

  Feature feature = gson.create().fromJson(json, Feature.class);

  // Even thought properties are Nullable,
  // Feature object will be created with properties set to an empty object,
  // so that addProperties() would work
  if (feature.properties() != null) {
    return feature;
  }
  return new Feature(TYPE, feature.bbox(),
    feature.id(), feature.geometry(), new JsonObject());
}
 
Example 2
Source File: BlogHelper.java    From cognitivej with Apache License 2.0 6 votes vote down vote up
public static List<ImageHolder> loadImagesFrom(String pathname) {
    List<ImageHolder> imageHolders = new ArrayList<>();
    try {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapterFactory(new ClassTypeAdapterFactory());
        gsonBuilder.registerTypeAdapter(Class.class, new ClassTypeAdapter());
        Gson gson = gsonBuilder.create();
        imageHolders.addAll(gson.fromJson(FileUtils.readFileToString(new File(pathname), Charset.defaultCharset()), new TypeToken<List<ImageHolder>>() {
                }.getType())
        );
    } catch (IOException e) {
        e.printStackTrace();
    }

    return imageHolders;
}
 
Example 3
Source File: GraphAdapterBuilder.java    From gson with Apache License 2.0 5 votes vote down vote up
public void registerOn(GsonBuilder gsonBuilder) {
  Factory factory = new Factory(instanceCreators);
  gsonBuilder.registerTypeAdapterFactory(factory);
  for (Map.Entry<Type, InstanceCreator<?>> entry : instanceCreators.entrySet()) {
    gsonBuilder.registerTypeAdapter(entry.getKey(), factory);
  }
}
 
Example 4
Source File: IdentifyMultipleFacesExample.java    From cognitivej with Apache License 2.0 5 votes vote down vote up
private static List<ImageHolder> candidates() throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapterFactory(new ClassTypeAdapterFactory());
    gsonBuilder.registerTypeAdapter(Class.class, new ClassTypeAdapter());
    Gson gson = gsonBuilder.create();
    return gson.fromJson(FileUtils.readFileToString(new File("src/test/resources/blog/sets/love_hate_candidates.json"), Charset.defaultCharset()), new TypeToken<List<ImageHolder>>() {
    }.getType());
}
 
Example 5
Source File: SuggestionServiceImpl.java    From intellij-spring-assistant with MIT License 5 votes vote down vote up
private void processContainers(Module module, List<MetadataContainerInfo> containersToProcess,
    List<MetadataContainerInfo> containersToRemove,
    Map<String, MetadataContainerInfo> seenContainerPathToContainerInfo,
    Trie<String, MetadataSuggestionNode> rootSearchIndex) {
  // Lets remove references to files that are no longer present in classpath
  containersToRemove.forEach(
      container -> removeReferences(seenContainerPathToContainerInfo, rootSearchIndex,
          container));

  for (MetadataContainerInfo metadataContainerInfo : containersToProcess) {
    // lets remove existing references from search index, as these files are modified, so that we can rebuild index
    if (seenContainerPathToContainerInfo
        .containsKey(metadataContainerInfo.getContainerArchiveOrFileRef())) {
      removeReferences(seenContainerPathToContainerInfo, rootSearchIndex, metadataContainerInfo);
    }

    String metadataFilePath = metadataContainerInfo.getFileUrl();
    try (InputStream inputStream = metadataContainerInfo.getMetadataFile().getInputStream()) {
      GsonBuilder gsonBuilder = new GsonBuilder();
      // register custom mapper adapters
      gsonBuilder.registerTypeAdapter(SpringConfigurationMetadataValueProviderType.class,
          new SpringConfigurationMetadataValueProviderTypeDeserializer());
      gsonBuilder.registerTypeAdapterFactory(new GsonPostProcessEnablingTypeFactory());
      SpringConfigurationMetadata springConfigurationMetadata = gsonBuilder.create()
          .fromJson(new BufferedReader(new InputStreamReader(inputStream)),
              SpringConfigurationMetadata.class);
      buildMetadataHierarchy(module, rootSearchIndex, metadataContainerInfo,
          springConfigurationMetadata);

      seenContainerPathToContainerInfo
          .put(metadataContainerInfo.getContainerArchiveOrFileRef(), metadataContainerInfo);
    } catch (IOException e) {
      log.error("Exception encountered while processing metadata file: " + metadataFilePath, e);
      removeReferences(seenContainerPathToContainerInfo, rootSearchIndex, metadataContainerInfo);
    }
  }
}
 
Example 6
Source File: ModuleConfigJsonFile.java    From sensorhub with Mozilla Public License 2.0 5 votes vote down vote up
public ModuleConfigJsonFile(String moduleConfigPath)
{
    configFile = new File(moduleConfigPath);
    configMap = new LinkedHashMap<String, ModuleConfig>();
    
    // init json serializer/deserializer
    final GsonBuilder builder = new GsonBuilder();
    builder.setPrettyPrinting();
    builder.disableHtmlEscaping();
    builder.registerTypeAdapterFactory(new RuntimeTypeAdapterFactory<Object>(Object.class, OBJ_CLASS_FIELD));
    
    gson = builder.create();
    readJSON();
}
 
Example 7
Source File: FeatureCollection.java    From mapbox-java with MIT License 5 votes vote down vote up
/**
 * This takes the currently defined values found inside this instance and converts it to a GeoJson
 * string.
 *
 * @return a JSON string which represents this Feature Collection
 * @since 1.0.0
 */
@Override
public String toJson() {

  GsonBuilder gson = new GsonBuilder();
  gson.registerTypeAdapterFactory(GeoJsonAdapterFactory.create());
  gson.registerTypeAdapterFactory(GeometryAdapterFactory.create());
  return gson.create().toJson(this);
}
 
Example 8
Source File: ExampleGsonReladomoSerializerTest.java    From reladomo with Apache License 2.0 5 votes vote down vote up
private Gson register()
{
    final GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapterFactory(new GsonWrappedTypeAdaptorFactory());
    gsonBuilder.setPrettyPrinting();
    gsonBuilder.serializeNulls();
    return gsonBuilder.create();
}
 
Example 9
Source File: GeometryCollection.java    From mapbox-java with MIT License 5 votes vote down vote up
/**
 * This takes the currently defined values found inside this instance and converts it to a GeoJson
 * string.
 *
 * @return a JSON string which represents this GeometryCollection
 * @since 1.0.0
 */
@Override
public String toJson() {
  GsonBuilder gson = new GsonBuilder();
  gson.registerTypeAdapterFactory(GeoJsonAdapterFactory.create());
  gson.registerTypeAdapterFactory(GeometryAdapterFactory.create());
  return gson.create().toJson(this);
}
 
Example 10
Source File: GsonJsonEngine.java    From lastaflute with Apache License 2.0 4 votes vote down vote up
protected void registerJava8TimeAdapter(GsonBuilder builder) { // until supported by Gson
    builder.registerTypeAdapterFactory(createDateTimeTypeAdapterFactory());
}
 
Example 11
Source File: PersistanceController.java    From slide with MIT License 4 votes vote down vote up
public PersistanceController(Context c) {
    mPreferences = c.getSharedPreferences("data", 0);
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapterFactory(new GsonAdaptersState());
    mGson = gsonBuilder.create();
}
 
Example 12
Source File: GsonJsonEngine.java    From lastaflute with Apache License 2.0 4 votes vote down vote up
protected void registerBooleanAdapter(GsonBuilder builder) { // to adjust boolean expression flexibly
    builder.registerTypeAdapterFactory(createBooleanTypeAdapterFactory());
}
 
Example 13
Source File: GsonJsonEngine.java    From lastaflute with Apache License 2.0 4 votes vote down vote up
protected void registerStringAdapter(GsonBuilder builder) {
    builder.registerTypeAdapterFactory(createStringTypeAdapterFactory());
}
 
Example 14
Source File: PersistanceController.java    From talalarmo with MIT License 4 votes vote down vote up
public PersistanceController(Context context) {
    mPreferences = context.getSharedPreferences("data", 0);
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapterFactory(new GsonAdaptersState());
    mGson = gsonBuilder.create();
}
 
Example 15
Source File: PersistanceController.java    From todo-jedux with MIT License 4 votes vote down vote up
public PersistanceController(Context context) {
    mPreferences = context.getSharedPreferences("data", 0);
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapterFactory(new GsonAdaptersState());
    mGson = gsonBuilder.create();
}
 
Example 16
Source File: MultiPoint.java    From mapbox-java with MIT License 3 votes vote down vote up
/**
 * This takes the currently defined values found inside this instance and converts it to a GeoJson
 * string.
 *
 * @return a JSON string which represents this MultiPoint geometry
 * @since 1.0.0
 */
@Override
public String toJson() {
  GsonBuilder gson = new GsonBuilder();
  gson.registerTypeAdapterFactory(GeoJsonAdapterFactory.create());
  return gson.create().toJson(this);
}
 
Example 17
Source File: LineString.java    From mapbox-java with MIT License 2 votes vote down vote up
/**
 * Create a new instance of this class by passing in a formatted valid JSON String. If you are
 * creating a LineString object from scratch it is better to use one of the other provided static
 * factory methods such as {@link #fromLngLats(List)}. For a valid lineString to exist, it must
 * have at least 2 coordinate entries. The LineString should also have non-zero distance and zero
 * area.
 *
 * @param json a formatted valid JSON string defining a GeoJson LineString
 * @return a new instance of this class defined by the values passed inside this static factory
 *   method
 * @since 1.0.0
 */
public static LineString fromJson(String json) {
  GsonBuilder gson = new GsonBuilder();
  gson.registerTypeAdapterFactory(GeoJsonAdapterFactory.create());
  return gson.create().fromJson(json, LineString.class);
}
 
Example 18
Source File: MaxSpeed.java    From mapbox-java with MIT License 2 votes vote down vote up
/**
 * Create a new instance of this class by passing in a formatted valid JSON String.
 *
 * @param json a formatted valid JSON string defining a MaxSpeed
 * @return a new instance of this class defined by the values passed inside this static factory
 *   method
 * @since 3.4.0
 */
public static MaxSpeed fromJson(String json) {
  GsonBuilder gson = new GsonBuilder();
  gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
  return gson.create().fromJson(json, MaxSpeed.class);
}
 
Example 19
Source File: Polygon.java    From mapbox-java with MIT License 2 votes vote down vote up
/**
 * Create a new instance of this class by passing in a formatted valid JSON String. If you are
 * creating a Polygon object from scratch it is better to use one of the other provided static
 * factory methods such as {@link #fromOuterInner(LineString, LineString...)}. For a valid
 * For a valid Polygon to exist, it must follow the linear ring rules and the first list of
 * coordinates are considered the outer ring by default.
 *
 * @param json a formatted valid JSON string defining a GeoJson Polygon
 * @return a new instance of this class defined by the values passed inside this static factory
 *   method
 * @since 1.0.0
 */
public static Polygon fromJson(@NonNull String json) {
  GsonBuilder gson = new GsonBuilder();
  gson.registerTypeAdapterFactory(GeoJsonAdapterFactory.create());
  return gson.create().fromJson(json, Polygon.class);
}
 
Example 20
Source File: IntersectionLanes.java    From mapbox-java with MIT License 2 votes vote down vote up
/**
 * Create a new instance of this class by passing in a formatted valid JSON String.
 *
 * @param json a formatted valid JSON string defining an IntersectionLanes
 * @return a new instance of this class defined by the values passed inside this static factory
 *   method
 * @since 3.4.0
 */
public static IntersectionLanes fromJson(String json) {
  GsonBuilder gson = new GsonBuilder();
  gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
  return gson.create().fromJson(json, IntersectionLanes.class);
}