Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize lists to comma delimited string in PaymentAccount.toJson() #1620

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
22 changes: 13 additions & 9 deletions core/src/main/java/haveno/core/payment/PaymentAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import haveno.common.proto.ProtoUtil;
import haveno.common.proto.persistable.PersistablePayload;
import haveno.common.util.Utilities;
Expand Down Expand Up @@ -341,12 +342,20 @@ public void revertChanges() {
// ---------------------------- SERIALIZATION -----------------------------

public String toJson() {
Map<String, Object> jsonMap = new HashMap<String, Object>();
if (paymentAccountPayload != null) jsonMap.putAll(gsonBuilder.create().fromJson(paymentAccountPayload.toJson(), (Type) Object.class));
Gson customGson = new GsonBuilder().registerTypeAdapter(PaymentAccountPayload.class, new PaymentAccountTypeAdapter(this.getClass())).create();
Map<String, Object> jsonMap = new HashMap<>();

if (paymentAccountPayload != null) {
String payloadJson = customGson.toJson(paymentAccountPayload);
Map<String, Object> payloadMap = customGson.fromJson(payloadJson, new TypeToken<Map<String, Object>>() {}.getType());
jsonMap.putAll(payloadMap);
}

jsonMap.put("accountName", getAccountName());
jsonMap.put("accountId", getId());
if (paymentAccountPayload != null) jsonMap.put("salt", getSaltAsHex());
return gsonBuilder.create().toJson(jsonMap);

return customGson.toJson(jsonMap);
}

/**
Expand Down Expand Up @@ -388,12 +397,7 @@ public PaymentAccountForm toForm() {
PaymentAccountForm form = new PaymentAccountForm(PaymentAccountForm.FormId.valueOf(paymentMethod.getId()));
for (PaymentAccountFormField.FieldId fieldId : getInputFieldIds()) {
PaymentAccountFormField field = getEmptyFormField(fieldId);
Object value = jsonMap.get(HavenoUtils.toCamelCase(field.getId().toString()));
if (value instanceof List) { // TODO: list should already be serialized to comma delimited string in PaymentAccount.toJson() (PaymentAccountTypeAdapter?)
field.setValue(String.join(",", (List<String>) value));
} else {
field.setValue((String) value);
}
field.setValue((String) jsonMap.get(HavenoUtils.toCamelCase(field.getId().toString())));
form.getFields().add(field);
}
return form;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ private void writeComments(JsonWriter out, PaymentAccount account) throws IOExce


private void writeInnerMutableFields(JsonWriter out, PaymentAccount account) {
if (account instanceof CountryBasedPaymentAccount) {
writeAcceptedCountryCodesField(out, account);
}

if (account.hasMultipleCurrencies()) {
writeTradeCurrenciesField(out, account);
writeSelectedTradeCurrencyField(out, account);
Expand Down Expand Up @@ -173,7 +177,16 @@ private void writeTradeCurrenciesField(JsonWriter out, PaymentAccount account) {
String fieldName = "tradeCurrencies";
log.debug("Append form with non-settable field: {}", fieldName);
out.name(fieldName);
out.value("comma delimited currency code list, e.g., gbp,eur,jpy,usd");
List<TradeCurrency> tradeCurrencies = account.getTradeCurrencies();
if (tradeCurrencies != null && !tradeCurrencies.isEmpty()) {
String tradeCurrenciesValue = tradeCurrencies.stream()
.map(TradeCurrency::getCode) // convert each currency to its code.
.reduce((c1, c2) -> c1 + "," + c2) // create a comma-delimited string.
.orElse("");
out.value(tradeCurrenciesValue);
} else {
out.value(""); // if no currencies exist, write an empty string.
}
} catch (Exception ex) {
String errMsg = format("cannot create a new %s json form",
account.getClass().getSimpleName());
Expand All @@ -198,6 +211,34 @@ private void writeSelectedTradeCurrencyField(JsonWriter out, PaymentAccount acco
throw new IllegalStateException("programmer error: " + errMsg);
}
}

private void writeAcceptedCountryCodesField(JsonWriter out, PaymentAccount account) {
try {
String fieldName = "acceptedCountryCodes";
log.debug("Append form with non-settable field: {}", fieldName);
out.name(fieldName);

if (account instanceof CountryBasedPaymentAccount) {
List<Country> acceptedCountries = ((CountryBasedPaymentAccount) account).getAcceptedCountries();
if (acceptedCountries != null && !acceptedCountries.isEmpty()) {
String countryCodesValue = acceptedCountries.stream()
.map(e -> e.code) // convert each country to its code.
.reduce((c1, c2) -> c1 + "," + c2) // create a comma-delimited string.
.orElse("");
out.value(countryCodesValue);
} else {
out.value(""); // if no countries exist, write an empty string.
}
} else {
out.value(""); // default empty value for non-country-based accounts.
}
} catch (Exception ex) {
String errMsg = format("cannot create a new %s json form",
account.getClass().getSimpleName());
log.error(capitalize(errMsg) + ".", ex);
throw new IllegalStateException("programmer error: " + errMsg);
}
}

@Override
public PaymentAccount read(JsonReader in) throws IOException {
Expand Down
Loading