Skip to content

Commit

Permalink
Created public Json Serializer [ECR-2708]
Browse files Browse the repository at this point in the history
  • Loading branch information
bullet-tooth committed Dec 10, 2018
1 parent 5c5104d commit b6c060b
Show file tree
Hide file tree
Showing 23 changed files with 101 additions and 309 deletions.
Expand Up @@ -34,8 +34,7 @@
*
* <p>All method arguments are non-null by default.
*/
public final class HashCodeJsonSerializer
implements JsonSerializer<HashCode>, JsonDeserializer<HashCode> {
final class HashCodeJsonSerializer implements JsonSerializer<HashCode>, JsonDeserializer<HashCode> {

/**
* Serialize HashCode to JsonElement.
Expand Down
@@ -0,0 +1,52 @@
/*
* Copyright 2018 The Exonum Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.exonum.binding.common.serialization.json;

import com.exonum.binding.common.crypto.PublicKey;
import com.exonum.binding.common.hash.HashCode;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.LongSerializationPolicy;

/**
* Provides configured {@link Gson} serializer for converting java objects to Json and vice versa.
*/
public final class JsonSerializer {

/**
* Returns preconfigured {@link Gson} builder instance. Can be useful in cases when
* some customization is required. For example, type adapters should be extended or replaced.
*/
public static GsonBuilder builder() {
return new GsonBuilder()
.registerTypeHierarchyAdapter(HashCode.class, new HashCodeJsonSerializer())
.registerTypeAdapter(PublicKey.class, new PublicKeyJsonSerializer())
.registerTypeAdapterFactory(StoredConfigurationAdapterFactory.create())
.setLongSerializationPolicy(LongSerializationPolicy.STRING);
}

/**
* Returns preconfigured {@link Gson} instance.
*/
public static Gson json() {
return builder().create();
}

private JsonSerializer() {
}
}
Expand Up @@ -34,7 +34,7 @@
*
* <p>All method arguments are non-null by default.
*/
public final class PublicKeyJsonSerializer
final class PublicKeyJsonSerializer
implements JsonSerializer<PublicKey>, JsonDeserializer<PublicKey> {

/**
Expand Down

This file was deleted.

This file was deleted.

Expand Up @@ -17,12 +17,12 @@

package com.exonum.binding.blockchain;

import static com.exonum.binding.common.serialization.json.JsonSerializer.json;
import static com.google.common.base.Preconditions.checkArgument;

import com.exonum.binding.common.configuration.StoredConfiguration;
import com.exonum.binding.common.hash.HashCode;
import com.exonum.binding.common.serialization.StandardSerializers;
import com.exonum.binding.common.serialization.json.StoredConfigurationGsonSerializer;
import com.exonum.binding.proxy.Cleaner;
import com.exonum.binding.proxy.NativeHandle;
import com.exonum.binding.proxy.ProxyDestructor;
Expand Down Expand Up @@ -98,7 +98,7 @@ ProofListIndexProxy<HashCode> getBlockTransactions(long height) {
StoredConfiguration getActualConfiguration() {
String rawConfiguration = nativeGetActualConfiguration(nativeHandle.get());

return StoredConfigurationGsonSerializer.fromJson(rawConfiguration);
return json().fromJson(rawConfiguration, StoredConfiguration.class);
}

private static native long nativeCreate(long viewNativeHandle);
Expand Down
Expand Up @@ -16,6 +16,7 @@

package com.exonum.binding.cryptocurrency;

import static com.exonum.binding.common.serialization.json.JsonSerializer.json;
import static com.google.common.base.Preconditions.checkArgument;
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
Expand All @@ -24,13 +25,11 @@
import com.exonum.binding.common.crypto.PublicKey;
import com.exonum.binding.common.hash.HashCode;
import com.exonum.binding.common.message.BinaryMessage;
import com.exonum.binding.cryptocurrency.transactions.CryptocurrencyTransactionGson;
import com.exonum.binding.service.InvalidTransactionException;
import com.exonum.binding.transaction.Transaction;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
import com.google.inject.Inject;
import io.vertx.core.Handler;
import io.vertx.core.MultiMap;
Expand Down Expand Up @@ -104,10 +103,9 @@ private void getWallet(RoutingContext rc) {
Optional<Wallet> wallet = service.getWallet(walletId);

if (wallet.isPresent()) {
Gson gson = CryptocurrencyTransactionGson.instance();
rc.response()
.putHeader("Content-Type", "application/json")
.end(gson.toJson(wallet.get()));
.end(json().toJson(wallet.get()));
} else {
rc.response()
.setStatusCode(HTTP_NOT_FOUND)
Expand All @@ -122,7 +120,7 @@ private void getWalletHistory(RoutingContext rc) {

rc.response()
.putHeader("Content-Type", "application/json")
.end(CryptocurrencyTransactionGson.instance().toJson(walletHistory));
.end(json().toJson(walletHistory));
}

private static <T> T getRequiredParameter(HttpServerRequest request, String key,
Expand Down
Expand Up @@ -17,6 +17,7 @@
package com.exonum.binding.cryptocurrency.transactions;

import static com.exonum.binding.common.crypto.CryptoFunctions.Ed25519.PUBLIC_KEY_BYTES;
import static com.exonum.binding.common.serialization.json.JsonSerializer.json;
import static com.exonum.binding.cryptocurrency.CryptocurrencyServiceImpl.CRYPTO_FUNCTION;
import static com.exonum.binding.cryptocurrency.transactions.TransactionPreconditions.checkTransaction;
import static com.google.common.base.Preconditions.checkArgument;
Expand Down Expand Up @@ -72,7 +73,7 @@ public static CreateWalletTx fromMessage(BinaryMessage message) {
return new CreateWalletTx(message, ownerPublicKey, initialBalance);
} catch (InvalidProtocolBufferException e) {
throw new IllegalArgumentException(
"Unable to instantiate TxMessageProtos.CreateWalletTx instance from provided"
"Unable to instantiate TxMessageProtos.CreateWalletTx json from provided"
+ " binary data", e);
}
}
Expand All @@ -98,7 +99,7 @@ public void execute(Fork view) {

@Override
public String info() {
return CryptocurrencyTransactionGson.instance().toJson(this);
return json().toJson(this);
}

@Override
Expand Down

This file was deleted.

0 comments on commit b6c060b

Please sign in to comment.