Example usage for com.google.gson GsonBuilder registerTypeAdapter

List of usage examples for com.google.gson GsonBuilder registerTypeAdapter

Introduction

In this page you can find the example usage for com.google.gson GsonBuilder registerTypeAdapter.

Prototype

@SuppressWarnings({ "unchecked", "rawtypes" })
public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) 

Source Link

Document

Configures Gson for custom serialization or deserialization.

Usage

From source file:com.sk89q.worldedit.world.registry.LegacyMapper.java

License:Open Source License

/**
 * Attempt to load the data from file./*  ww w  .  j  a  v  a2s .c  o  m*/
 *
 * @throws IOException thrown on I/O error
 */
private void loadFromResource() throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Vector3.class, new VectorAdapter());
    Gson gson = gsonBuilder.disableHtmlEscaping().create();
    URL url = LegacyMapper.class.getResource("legacy.json");
    if (url == null) {
        throw new IOException("Could not find legacy.json");
    }
    String data = Resources.toString(url, Charset.defaultCharset());
    LegacyDataFile dataFile = gson.fromJson(data, new TypeToken<LegacyDataFile>() {
    }.getType());

    ParserContext parserContext = new ParserContext();
    parserContext.setPreferringWildcard(false);
    parserContext.setRestricted(false);
    parserContext.setTryLegacy(false); // This is legacy. Don't match itself.

    for (Map.Entry<String, String> blockEntry : dataFile.blocks.entrySet()) {
        try {
            String id = blockEntry.getKey();
            BlockState state = WorldEdit.getInstance().getBlockFactory()
                    .parseFromInput(blockEntry.getValue(), parserContext).toImmutableState();
            blockToStringMap.put(state, id);
            stringToBlockMap.put(id, state);
        } catch (Exception e) {
            log.warning("Unknown block: " + blockEntry.getValue());
        }
    }

    for (Map.Entry<String, String> itemEntry : dataFile.items.entrySet()) {
        try {
            String id = itemEntry.getKey();
            ItemType type = ItemTypes.get(itemEntry.getValue());
            itemToStringMap.put(type, id);
            stringToItemMap.put(id, type);
        } catch (Exception e) {
            log.warning("Unknown item: " + itemEntry.getValue());
        }
    }
}

From source file:com.skburgart.rwr.servlet.GetStats.java

License:Open Source License

private String playersToJson(List<Player> reports) {

    GsonBuilder gsonBuilder = new GsonBuilder();
    Gson gson = gsonBuilder.registerTypeAdapter(Player.class, new PlayerAdapter()).setPrettyPrinting().create();
    return gson.toJson(reports);
}

From source file:com.skysql.manager.api.APIrestful.java

License:Open Source License

/**
 * Gets the gson class.//from   w  ww  .j av a 2  s  . c  o  m
 *
 * @return the gson
 */
public static Gson getGson() {
    if (gson == null) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapter(APIrestful.class, new APIrestfulDeserializer());
        gsonBuilder.registerTypeAdapter(Backups.class, new BackupsDeserializer());
        gsonBuilder.registerTypeAdapter(BackupStates.class, new BackupStatesDeserializer());
        gsonBuilder.registerTypeAdapter(Commands.class, new CommandsDeserializer());
        gsonBuilder.registerTypeAdapter(CommandStates.class, new CommandStatesDeserializer());
        gsonBuilder.registerTypeAdapter(Monitors.class, new MonitorsDeserializer());
        gsonBuilder.registerTypeAdapter(MonitorData.class, new MonitorDataDeserializer());
        gsonBuilder.registerTypeAdapter(MonitorDataRaw.class, new MonitorDataRawDeserializer());
        gsonBuilder.registerTypeAdapter(MonitorLatest.class, new ObjectDeserializer());
        gsonBuilder.registerTypeAdapter(NodeInfo.class, new NodeInfoDeserializer());
        gsonBuilder.registerTypeAdapter(NodeStates.class, new NodeStatesDeserializer());
        gsonBuilder.registerTypeAdapter(Schedule.class, new ScheduleDeserializer());
        gsonBuilder.registerTypeAdapter(SettingsValues.class, new SettingsValuesDeserializer());
        gsonBuilder.registerTypeAdapter(Steps.class, new StepsDeserializer());
        gsonBuilder.registerTypeAdapter(SystemInfo.class, new SystemInfoDeserializer());
        gsonBuilder.registerTypeAdapter(SystemTypes.class, new SystemTypesDeserializer());
        gsonBuilder.registerTypeAdapter(TaskInfo.class, new TaskInfoDeserializer());
        gsonBuilder.registerTypeAdapter(UserInfo.class, new UserInfoDeserializer());
        gsonBuilder.registerTypeAdapter(UserObject.class, new UserObjectDeserializer());
        gsonBuilder.registerTypeAdapter(Versions.class, new VersionsDeserializer());
        gsonBuilder.registerTypeAdapter(WriteResponse.class, new ResponseDeserializer());

        //
        gsonBuilder.registerTypeAdapter(ChartProperties.class, new ChartPropertiesDeserializer());

        gson = gsonBuilder.create();
    }
    return gson;
}

From source file:com.smart.taxi.entities.CabProvider.java

@Override
public void deserializeFromJSON(JsonElement jsonElement) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(CabProvider.class, new CabProviderDeserialize());
    Gson gson = gsonBuilder.create();/* www  . j ava  2 s .com*/
    CabProvider cabProvider = gson.fromJson(jsonElement, CabProvider.class);
    copy(cabProvider);
}

From source file:com.smart.taxi.entities.Driver.java

@Override
public void deserializeFromJSON(JsonElement jsonElement) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Driver.class, new DriverDeserializer());
    Gson gson = gsonBuilder.create();/*from  ww w  .  j  a v  a  2 s . c  om*/
    Driver driver = gson.fromJson(jsonElement, Driver.class);
    copy(driver);

}

From source file:com.smart.taxi.entities.Journey.java

@Override
public void deserializeFromJSON(JsonElement jsonElement) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Journey.class, new UserDeserializer());
    Gson gson = gsonBuilder.create();/*from   w ww . j ava2 s.  co  m*/
    Journey journey = gson.fromJson(jsonElement, Journey.class);
    copy(journey);

}

From source file:com.smart.taxi.entities.User.java

@Override
public void deserializeFromJSON(JsonElement jsonElement) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(User.class, new UserDeserializer());
    Gson gson = gsonBuilder.create();//from w ww. ja  v  a 2 s. co m
    User user = gson.fromJson(jsonElement, User.class);
    copy(user);
}

From source file:com.smartling.api.sdk.BaseApiClientAdapter.java

License:Apache License

protected <T extends Data> ApiResponse<T> parseApiResponse(final String response,
        final TypeToken<ApiResponseWrapper<T>> responseType) {
    final GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Date.class, new DateTypeAdapter());

    final Gson gson = builder.create();
    final ApiResponseWrapper<T> responseWrapper = gson.fromJson(response, responseType.getType());

    return responseWrapper.getResponse();
}

From source file:com.smarttaxi.driver.entities.Driver.java

@Override
public void deserializeFromJSON(JsonElement jsonElement) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Driver.class, new DriverDeserializer());
    Gson gson = gsonBuilder.create();/*  w ww . j ava2  s . c  om*/
    Driver driver = gson.fromJson(jsonElement, Driver.class);
    copy(driver);

    /*CurrentDriverStatus status = gson.fromJson(json, CurrentDriverStatus.class);
    copy(status);*/
}

From source file:com.softwaremagico.tm.json.CharacterJsonManager.java

License:Open Source License

public static String toJson(CharacterPlayer characterPlayer) {
    if (characterPlayer != null) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setPrettyPrinting();
        gsonBuilder.setExclusionStrategies(new AnnotationExclusionStrategy()).create();
        gsonBuilder.registerTypeAdapter(IValue.class, new IValueSerializer<IValue>());
        gsonBuilder.registerTypeAdapter(Faction.class, new FactionAdapter());
        gsonBuilder.registerTypeAdapter(Blessing.class, new BlessingAdapter());
        gsonBuilder.registerTypeAdapter(AvailableBenefice.class, new AvailableBeneficeAdapter());
        gsonBuilder.registerTypeAdapter(AvailableSkill.class, new AvailableSkillAdapter());
        gsonBuilder.registerTypeAdapter(CharacteristicDefinition.class, new CharacteristicDefinitionAdapter());
        gsonBuilder.registerTypeAdapter(Race.class, new RaceAdapter());
        gsonBuilder.registerTypeAdapter(Weapon.class, new WeaponAdapter());
        gsonBuilder.registerTypeAdapter(Armour.class, new ArmourAdapter());
        gsonBuilder.registerTypeAdapter(Shield.class, new ShieldAdapter());
        gsonBuilder.registerTypeAdapter(CyberneticDevice.class, new CyberneticDeviceAdapter());
        // final Gson gson = new
        // GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create();
        Gson gson = gsonBuilder.create();
        String jsonText = gson.toJson(characterPlayer);
        return jsonText;
    }/*from  ww  w.j  av a 2  s .c  o  m*/
    return null;
}