Search in sources :

Example 26 with Gson

use of com.google.gson.Gson in project elastic-job by dangdangdotcom.

the class GsonFactoryTest method assertRegisterTypeAdapter.

@Test
public void assertRegisterTypeAdapter() {
    Gson beforeRegisterGson = GsonFactory.getGson();
    GsonFactory.registerTypeAdapter(GsonFactoryTest.class, new TypeAdapter() {

        @Override
        public Object read(final JsonReader in) throws IOException {
            return null;
        }

        @Override
        public void write(final JsonWriter out, final Object value) throws IOException {
            out.jsonValue("test");
        }
    });
    assertThat(beforeRegisterGson.toJson(new GsonFactoryTest()), is("{}"));
    assertThat(GsonFactory.getGson().toJson(new GsonFactoryTest()), is("test"));
}
Also used : TypeAdapter(com.google.gson.TypeAdapter) Gson(com.google.gson.Gson) JsonReader(com.google.gson.stream.JsonReader) IOException(java.io.IOException) JsonWriter(com.google.gson.stream.JsonWriter) Test(org.junit.Test)

Example 27 with Gson

use of com.google.gson.Gson in project MultiType by drakeet.

the class TextItem method init.

@Override
public void init(@NonNull byte[] data) {
    String json = new String(data);
    this.text = new Gson().fromJson(json, TextItem.class).text;
}
Also used : Gson(com.google.gson.Gson)

Example 28 with Gson

use of com.google.gson.Gson in project cw-omnibus by commonsguy.

the class PresoRoster method load.

void load(Context ctxt) {
    Gson gson = new Gson();
    AssetManager assets = ctxt.getAssets();
    for (String presoDir : PRESO_ASSET_DIRS) {
        PresoContents c = loadPreso(gson, assets, presoDir);
        if (c != null) {
            c.id = presos.size();
            presos.add(c);
        }
    }
}
Also used : AssetManager(android.content.res.AssetManager) Gson(com.google.gson.Gson)

Example 29 with Gson

use of com.google.gson.Gson in project sharding-jdbc by dangdangdotcom.

the class RdbTransactionLogStorage method add.

@Override
public void add(final TransactionLog transactionLog) {
    String sql = "INSERT INTO `transaction_log` (`id`, `transaction_type`, `data_source`, `sql`, `parameters`, `creation_time`) VALUES (?, ?, ?, ?, ?, ?);";
    try (Connection conn = dataSource.getConnection();
        PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
        preparedStatement.setString(1, transactionLog.getId());
        preparedStatement.setString(2, SoftTransactionType.BestEffortsDelivery.name());
        preparedStatement.setString(3, transactionLog.getDataSource());
        preparedStatement.setString(4, transactionLog.getSql());
        preparedStatement.setString(5, new Gson().toJson(transactionLog.getParameters()));
        preparedStatement.setLong(6, transactionLog.getCreationTime());
        preparedStatement.executeUpdate();
    } catch (final SQLException ex) {
        throw new TransactionLogStorageException(ex);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) Gson(com.google.gson.Gson) PreparedStatement(java.sql.PreparedStatement) TransactionLogStorageException(com.dangdang.ddframe.rdb.transaction.soft.exception.TransactionLogStorageException)

Example 30 with Gson

use of com.google.gson.Gson in project sharding-jdbc by dangdangdotcom.

the class RdbTransactionLogStorage method findEligibleTransactionLogs.

@Override
public List<TransactionLog> findEligibleTransactionLogs(final int size, final int maxDeliveryTryTimes, final long maxDeliveryTryDelayMillis) {
    List<TransactionLog> result = new ArrayList<>(size);
    String sql = "SELECT `id`, `transaction_type`, `data_source`, `sql`, `parameters`, `creation_time`, `async_delivery_try_times` " + "FROM `transaction_log` WHERE `async_delivery_try_times`<? AND `transaction_type`=? AND `creation_time`<? LIMIT ?;";
    try (Connection conn = dataSource.getConnection()) {
        try (PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
            preparedStatement.setInt(1, maxDeliveryTryTimes);
            preparedStatement.setString(2, SoftTransactionType.BestEffortsDelivery.name());
            preparedStatement.setLong(3, System.currentTimeMillis() - maxDeliveryTryDelayMillis);
            preparedStatement.setInt(4, size);
            try (ResultSet rs = preparedStatement.executeQuery()) {
                while (rs.next()) {
                    Gson gson = new Gson();
                    //TODO 对于批量执行的参数需要解析成两层列表
                    List<Object> parameters = gson.fromJson(rs.getString(5), new TypeToken<List<Object>>() {
                    }.getType());
                    result.add(new TransactionLog(rs.getString(1), "", SoftTransactionType.valueOf(rs.getString(2)), rs.getString(3), rs.getString(4), parameters, rs.getLong(6), rs.getInt(7)));
                }
            }
        }
    } catch (final SQLException ex) {
        throw new TransactionLogStorageException(ex);
    }
    return result;
}
Also used : TransactionLog(com.dangdang.ddframe.rdb.transaction.soft.storage.TransactionLog) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) Gson(com.google.gson.Gson) PreparedStatement(java.sql.PreparedStatement) TransactionLogStorageException(com.dangdang.ddframe.rdb.transaction.soft.exception.TransactionLogStorageException) TypeToken(com.google.gson.reflect.TypeToken) ResultSet(java.sql.ResultSet)

Aggregations

Gson (com.google.gson.Gson)1060 Test (org.junit.Test)236 HashMap (java.util.HashMap)200 JsonObject (com.google.gson.JsonObject)138 GsonBuilder (com.google.gson.GsonBuilder)135 CommandWrapper (ClientServerApi.CommandWrapper)123 CommandExecuter (CommandHandler.CommandExecuter)119 CriticalError (SQLDatabase.SQLDatabaseException.CriticalError)118 IOException (java.io.IOException)116 JsonSyntaxException (com.google.gson.JsonSyntaxException)100 ClientNotConnected (SQLDatabase.SQLDatabaseException.ClientNotConnected)96 ArrayList (java.util.ArrayList)94 Type (java.lang.reflect.Type)65 JsonElement (com.google.gson.JsonElement)57 ProductNotExistInCatalog (SQLDatabase.SQLDatabaseException.ProductNotExistInCatalog)53 SmartCode (BasicCommonClasses.SmartCode)50 Map (java.util.Map)45 InputStreamReader (java.io.InputStreamReader)41 BroadcastClientMessage (org.bigbluebutton.red5.client.messaging.BroadcastClientMessage)41 Location (BasicCommonClasses.Location)40