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

The following examples show how to use com.google.gson.GsonBuilder#registerTypeAdapter() . 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: XGsonBuilder.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
public static Gson compactInstance() {
	if (null == COMPACTINSTANCE) {
		synchronized (XGsonBuilder.class) {
			if (null == COMPACTINSTANCE) {
				GsonBuilder gson = new GsonBuilder();
				gson.setDateFormat(DateTools.format_yyyyMMddHHmmss);
				gson.registerTypeAdapter(Integer.class, new IntegerDeserializer());
				gson.registerTypeAdapter(Double.class, new DoubleDeserializer());
				gson.registerTypeAdapter(Float.class, new FloatDeserializer());
				gson.registerTypeAdapter(Long.class, new LongDeserializer());
				gson.registerTypeAdapter(Date.class, new DateDeserializer());
				gson.registerTypeAdapter(Date.class, new DateSerializer());
				COMPACTINSTANCE = gson.create();
			}
		}
	}
	return COMPACTINSTANCE;
}
 
Example 2
Source File: HeosJsonParser.java    From org.openhab.binding.heos with Eclipse Public License 1.0 5 votes vote down vote up
public HeosJsonParser(HeosResponse response) {
    this.response = response;
    this.eventResponse = response.getEvent();
    this.payloadResponse = response.getPayload();

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(HeosResponseEvent.class, new HeosDeserializerEvent());
    gsonBuilder.registerTypeAdapter(HeosResponsePayload.class, new HeosDeserializerPayload());
    this.gson = gsonBuilder.create();
}
 
Example 3
Source File: Converters.java    From gson-javatime-serialisers with MIT License 5 votes vote down vote up
/**
 * Registers the {@link LocalDateTimeConverter} converter.
 * @param builder The GSON builder to register the converter with.
 * @return A reference to {@code builder}.
 */
public static GsonBuilder registerLocalDateTime(GsonBuilder builder)
{
  builder.registerTypeAdapter(LOCAL_DATE_TIME_TYPE, new LocalDateTimeConverter());

  return builder;
}
 
Example 4
Source File: RenderJson.java    From restcommander with Apache License 2.0 5 votes vote down vote up
public RenderJson(Object o, JsonSerializer<?>... adapters) {
    GsonBuilder gson = new GsonBuilder();
    for (Object adapter : adapters) {
        Type t = getMethod(adapter.getClass(), "serialize").getParameterTypes()[0];
        gson.registerTypeAdapter(t, adapter);
    }
    json = gson.create().toJson(o);
}
 
Example 5
Source File: PersistenceGsonProvider.java    From greenbeans with Apache License 2.0 5 votes vote down vote up
public Gson get() {
	GsonBuilder gsonBuilder = builder();
	gsonBuilder.registerTypeAdapter(Date.class, new DateTypeAdapter());
	gsonBuilder.registerTypeAdapterFactory(TransactionsTypeAdapter.factory(encryptorProviderService));
	gsonBuilder.registerTypeAdapterFactory(CategoriesTypeAdapter.factory(encryptorProviderService));
	return gsonBuilder.create();
}
 
Example 6
Source File: LogProvider.java    From restcommander with Apache License 2.0 5 votes vote down vote up
/**
 * Read JSON log to the hashmap. From file back to the memory.
 * 
 * @param nodeGroupType
 * @param agentCommandType
 * @param timeStamp
 * @return
 */
public static Map<String, NodeData> readJsonLogToNodeDataMap(
		String nodeGroupType, String agentCommandType, String timeStamp) {

	String logContent = readJsonLog(nodeGroupType, agentCommandType,
			timeStamp);
	HashMap<String, NodeData> nodeDataMapValid = null;
	if (logContent == null) {
		 models.utils.LogUtils.printLogError
				 ("Error logContent is null in readJsonLogToNodeDataMap ");

		return nodeDataMapValid;
	}

	try {

		// Great solution: very challenging part: 20130523
		// http://stackoverflow.com/questions/2779251/convert-json-to-hashmap-using-gson-in-java
		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder.registerTypeAdapter(Object.class,
				new NaturalDeserializer());
		Gson gson = gsonBuilder.create();
		Object mapObject = gson.fromJson(logContent, Object.class);
		nodeDataMapValid = (HashMap<String, NodeData>) (mapObject);

		if (VarUtils.IN_DETAIL_DEBUG) {
			models.utils.LogUtils.printLogNormal(nodeDataMapValid.size()+"");
		}

	} catch (Throwable e) {
		e.printStackTrace();
	}

	return nodeDataMapValid;
}
 
Example 7
Source File: GGraphGsonConfigurator.java    From graphical-lsp with Eclipse Public License 2.0 5 votes vote down vote up
protected void configureClassesOfPackages(GsonBuilder gsonBuilder) {
	for (EPackage pkg : ePackages) {
		for (EClassifier classifier : pkg.getEClassifiers()) {
			if (classifier instanceof EClass && !((EClass) classifier).isAbstract()) {
				Class<? extends EObject> implClass = getImplementationClass((EClass) classifier, pkg);
				gsonBuilder.registerTypeAdapter(classifier.getInstanceClass(),
						new ClassBasedDeserializer(implClass));
			}
		}
	}
}
 
Example 8
Source File: Converters.java    From gson-jodatime-serialisers with MIT License 5 votes vote down vote up
/**
 * Registers the {@link LocalDate} converter.
 * @param builder The GSON builder to register the converter with.
 * @return A reference to {@code builder}.
 */
public static GsonBuilder registerLocalDate(GsonBuilder builder)
{
  if (builder == null) { throw new NullPointerException("builder cannot be null"); }

  builder.registerTypeAdapter(LOCAL_DATE_TYPE, new LocalDateConverter());

  return builder;
}
 
Example 9
Source File: IotJobsClient.java    From aws-iot-device-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private Gson getGson() {
    GsonBuilder gson = new GsonBuilder();
    gson.disableHtmlEscaping();
    gson.registerTypeAdapter(Timestamp.class, new Timestamp.Serializer());
    gson.registerTypeAdapter(Timestamp.class, new Timestamp.Deserializer());
    addTypeAdapters(gson);
    return gson.create();
}
 
Example 10
Source File: MessageJsonSerializer.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
public static void registerTypeAdapters(GsonBuilder builder) {
    builder.registerTypeAdapter(SourceFile.class, new SourceFileJsonTypeAdapter());
    builder.registerTypeAdapter(SourcePosition.class, new SourcePositionJsonTypeAdapter());
    builder.registerTypeAdapter(SourceFilePosition.class,
            new SourceFilePositionJsonSerializer());
    builder.registerTypeAdapter(Message.class, new MessageJsonSerializer());
}
 
Example 11
Source File: BundledBlockData.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
public void add(URL url, boolean overwrite) throws IOException {
    if (url == null) {
        throw new IOException("Could not find " + url);
    }
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Vector.class, new FaweVectorAdapter());
    Gson gson = gsonBuilder.create();
    String data = Resources.toString(url, Charset.defaultCharset());
    List<BlockEntry> entries = gson.fromJson(data, new TypeToken<List<BlockEntry>>() {
    }.getType());
    for (BlockEntry entry : entries) {
        add(entry, overwrite);
    }
}
 
Example 12
Source File: SteamEconomy.java    From async-gamequery-lib with MIT License 4 votes vote down vote up
@Override
protected void configureBuilder(GsonBuilder builder) {
    builder.registerTypeAdapter(new TypeToken<Map<String, SteamAssetClassInfo>>() {
    }.getType(), new SteamAssetClassInfoMapDeserializer());
    builder.registerTypeAdapter(SteamAssetDescription.class, new SteamAssetDescDeserializer());
}
 
Example 13
Source File: ThreeTenGsonAdapter.java    From ThreeTen-Backport-Gson-Adapter with Apache License 2.0 4 votes vote down vote up
public static GsonBuilder registerLocalDate(GsonBuilder gsonBuilder) {
    return gsonBuilder.registerTypeAdapter(LocalDate.class, new LocalDateConverter());
}
 
Example 14
Source File: Dota2Match.java    From async-gamequery-lib with MIT License 4 votes vote down vote up
@Override
protected void configureBuilder(GsonBuilder builder) {
    log.info("Registering Type Adapter");
    builder.registerTypeAdapter(Dota2MatchTeamInfo.class, new Dota2TeamInfoAdapter());
}
 
Example 15
Source File: WebGsonProvider.java    From greenbeans with Apache License 2.0 4 votes vote down vote up
@Override
public Gson get() {
	GsonBuilder gsonBuilder = builder();
	gsonBuilder.registerTypeAdapter(Date.class, new DateTypeAdapter());
	return gsonBuilder.create();
}
 
Example 16
Source File: JsonConfigSerializer.java    From mr4c with Apache License 2.0 4 votes vote down vote up
private Gson buildGson() {
	GsonBuilder builder = new GsonBuilder();
	builder.setPrettyPrinting();
	builder.registerTypeAdapter( Document.class, new DocumentSerializer());
	return builder.create();
}
 
Example 17
Source File: VoxelGamesLibModule.java    From VoxelGamesLibv2 with MIT License 4 votes vote down vote up
private void addTypeAdapters(@Nonnull GsonBuilder builder, @Nonnull Injector injector) {
    builder.registerTypeAdapter(Phase.class, injector.getInstance(PhaseTypeAdapter.class));
    builder.registerTypeAdapter(Feature.class, injector.getInstance(FeatureTypeAdapter.class));
    builder.registerTypeAdapter(Game.class, injector.getInstance(GameTypeAdapter.class));
    builder.registerTypeAdapter(PlayerProfile.class, injector.getInstance(PlayerProfileTypeAdapter.class));
}
 
Example 18
Source File: ThreeTenGsonAdapter.java    From ThreeTen-Backport-Gson-Adapter with Apache License 2.0 4 votes vote down vote up
public static GsonBuilder registerOffsetTime(GsonBuilder gsonBuilder) {
    return gsonBuilder.registerTypeAdapter(OffsetTime.class, new OffsetTimeConverter());
}
 
Example 19
Source File: PathSerializerTest.java    From PeerWasp with MIT License 4 votes vote down vote up
private Gson createGsonInstance() {
	GsonBuilder gsonBuilder = new GsonBuilder();
	gsonBuilder.registerTypeAdapter(Path.class, new PathSerializer());
	return gsonBuilder.create();
}
 
Example 20
Source File: DirectionsResponse.java    From mapbox-java with MIT License 3 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 GeoJson Directions Response
 * @return a new instance of this class defined by the values passed inside this static factory
 *   method
 * @since 3.0.0
 */
public static DirectionsResponse fromJson(String json) {
  GsonBuilder gson = new GsonBuilder();
  gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
  gson.registerTypeAdapter(Point.class, new PointAsCoordinatesTypeAdapter());
  return gson.create().fromJson(json, DirectionsResponse.class);
}